Skip to content

Commit

Permalink
[IngestManager] Move RequiredPackage type near DefaultPackages (#82188)…
Browse files Browse the repository at this point in the history
… (#82654)

## Summary

- [x] Move the `RequiredPackage` type to the same location as `DefaultPackages` so anyone looking at one will see the other as well
- [x] New approach to defining & using enum-like values. 

## The basic outline of the approach 
  1. Export a JS object `as const` from `common/constants/*` (expose runtime value)
    https://github.com/elastic/kibana/blob/d66cf601bb0466bd36254f40339ac41f9935436c/x-pack/plugins/ingest_manager/common/constants/epm.ts#L12-L15
  1. Import that JS value and convert to a TS type as `type TSType = typeof jsValue` 
    <img width="484" alt="Screen Shot 2020-11-02 at 2 12 27 PM" src="https://user-images.githubusercontent.com/57655/97910756-2e693700-1d18-11eb-9605-e33ceb64e857.png"> 
  1. The values (right-hand side) of that type can be converted to a union type with `ValueOf<X>`
    <img width="557" alt="Screen Shot 2020-11-02 at 3 10 22 PM" src="https://user-images.githubusercontent.com/57655/97914250-99693c80-1d1d-11eb-9605-92379aa4db38.png">
  1. The values can be accessed as TS types `TSType['key']` or JS  `jsValue.key`

This way we
  - still get the readability of TS `enum`s
  - can choose to only import the types of values we need (compile time or runtime) 
  - use the TS utility types like `Pick`, `Omit`, etc

## More detail on 1
  * Value which can be access/iterated at runtime, e.g. `Object.values(requiredPackages)` or `requiredPackages.System`
  * narrows the type those exact values, not `string`
    <img width="823" alt="Screen Shot 2020-11-02 at 2 15 58 PM" src="https://user-images.githubusercontent.com/57655/97910538-e0ecca00-1d17-11eb-9153-71171e9fba75.png">
  * can't be modified (it's `readonly`) 
      <img width="443" alt="Screen Shot 2020-11-02 at 2 12 09 PM" src="https://user-images.githubusercontent.com/57655/97910753-2dd0a080-1d18-11eb-82d4-3fe635ba3071.png">
  • Loading branch information
John Schulz authored Nov 4, 2020
1 parent 4118dea commit b65c5d5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugins/ingest_manager/common/constants/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export const PACKAGES_SAVED_OBJECT_TYPE = 'epm-packages';
export const INDEX_PATTERN_SAVED_OBJECT_TYPE = 'index-pattern';
export const INDEX_PATTERN_PLACEHOLDER_SUFFIX = '-index_pattern_placeholder';
export const MAX_TIME_COMPLETE_INSTALL = 60000;

export const requiredPackages = {
System: 'system',
Endpoint: 'endpoint',
} as const;
5 changes: 5 additions & 0 deletions x-pack/plugins/ingest_manager/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ export interface IngestManagerConfigType {
// and https://github.com/Microsoft/TypeScript/pull/12253#issuecomment-263132208
// and https://github.com/Microsoft/TypeScript/issues/21826#issuecomment-479851685
export const entries = Object.entries as <T>(o: T) => Array<[keyof T, T[keyof T]]>;

/**
* Creates a Union Type for all the values of an object
*/
export type ValueOf<T> = T[keyof T];
3 changes: 3 additions & 0 deletions x-pack/plugins/ingest_manager/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Follow pattern from https://github.com/elastic/kibana/pull/52447
// TODO: Update when https://github.com/elastic/kibana/issues/53021 is closed
import { SavedObject, SavedObjectAttributes, SavedObjectReference } from 'src/core/public';
import { requiredPackages } from '../../constants';

export enum InstallationStatus {
installed = 'installed',
Expand Down Expand Up @@ -277,6 +278,8 @@ export type EsAssetReference = Pick<SavedObjectReference, 'id'> & {
type: ElasticsearchAssetType;
};

export type RequiredPackage = typeof requiredPackages;

export enum DefaultPackages {
system = 'system',
endpoint = 'endpoint',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { SavedObject } from 'src/core/server';
import { RequiredPackage, requiredPackages, ValueOf } from '../../../../common';
import {
AssetType,
Installable,
Expand Down Expand Up @@ -35,14 +36,8 @@ export {
} from './install';
export { removeInstallation } from './remove';

type RequiredPackage = 'system' | 'endpoint';
const requiredPackages: Record<RequiredPackage, boolean> = {
system: true,
endpoint: true,
};

export function isRequiredPackage(value: string): value is RequiredPackage {
return value in requiredPackages;
export function isRequiredPackage(value: string): value is ValueOf<RequiredPackage> {
return Object.values(requiredPackages).some((required) => value === required);
}

export class PackageNotInstalledError extends Error {
Expand Down

0 comments on commit b65c5d5

Please sign in to comment.