Skip to content

Commit

Permalink
Switch to new 'as const' + typeof + ValueOf approach (#82499)
Browse files Browse the repository at this point in the history
## Summary

Follow pattern from #82188. Same behavior as now. Stronger type safety.

 * `installationSources` is runtime value (JS object)
 * `InstallationSource` is TS type (`typeof installationSources`)
 * `ValueOf<InstallationSource>` is TS union type `'installed' | 'not_installed'`
 * Can access values directly as `installationSources.Installed` (JS) or `InstallationSource['Installed']` (TS)
  • Loading branch information
John Schulz authored Nov 3, 2020
1 parent 0fec6cb commit 13dae5e
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 43 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 @@ -22,3 +22,8 @@ export const dataTypes = {
Logs: 'logs',
Metrics: 'metrics',
} as const;

export const installationStatuses = {
Installed: 'installed',
NotInstalled: 'not_installed',
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { PackageInfo, InstallationStatus } from '../types';
import { installationStatuses } from '../constants';
import { PackageInfo } from '../types';
import { packageToPackagePolicy, packageToPackagePolicyInputs } from './package_to_package_policy';

describe('Ingest Manager - packageToPackagePolicy', () => {
Expand All @@ -28,7 +29,7 @@ describe('Ingest Manager - packageToPackagePolicy', () => {
map: [],
},
},
status: InstallationStatus.notInstalled,
status: installationStatuses.NotInstalled,
};

describe('packageToPackagePolicyInputs', () => {
Expand Down
19 changes: 11 additions & 8 deletions x-pack/plugins/ingest_manager/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
// 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 { agentAssetTypes, dataTypes, requiredPackages } from '../../constants';
import {
agentAssetTypes,
dataTypes,
installationStatuses,
requiredPackages,
} from '../../constants';
import { ValueOf } from '../../types';

export enum InstallationStatus {
installed = 'installed',
notInstalled = 'not_installed',
}
export type InstallationStatus = typeof installationStatuses;

export enum InstallStatus {
installed = 'installed',
notInstalled = 'not_installed',
Expand Down Expand Up @@ -233,7 +236,7 @@ interface PackageAdditions {
export type PackageList = PackageListItem[];

export type PackageListItem = Installable<RegistrySearchResult>;
export type PackagesGroupedByStatus = Record<InstallationStatus, PackageList>;
export type PackagesGroupedByStatus = Record<ValueOf<InstallationStatus>, PackageList>;
export type PackageInfo = Installable<
// remove the properties we'll be altering/replacing from the base type
Omit<RegistryPackage, keyof PackageAdditions> &
Expand All @@ -256,12 +259,12 @@ export interface Installation extends SavedObjectAttributes {
export type Installable<T> = Installed<T> | NotInstalled<T>;

export type Installed<T = {}> = T & {
status: InstallationStatus.installed;
status: InstallationStatus['Installed'];
savedObject: SavedObject<Installation>;
};

export type NotInstalled<T = {}> = T & {
status: InstallationStatus.notInstalled;
status: InstallationStatus['NotInstalled'];
};

export type AssetReference = KibanaAssetReference | EsAssetReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import {
PackageInfo,
InstallationStatus,
NewPackagePolicy,
RegistryPolicyTemplate,
} from '../../../../types';
import { installationStatuses } from '../../../../../../../common/constants';
import { PackageInfo, NewPackagePolicy, RegistryPolicyTemplate } from '../../../../types';
import { validatePackagePolicy, validationHasErrors } from './validate_package_policy';

describe('Ingest Manager - validatePackagePolicy()', () => {
Expand All @@ -31,7 +27,7 @@ describe('Ingest Manager - validatePackagePolicy()', () => {
'index-pattern': [],
},
},
status: InstallationStatus.notInstalled,
status: installationStatuses.NotInstalled,
data_streams: [
{
dataset: 'foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React, { useState } from 'react';
import { useRouteMatch, Switch, Route, useLocation, useHistory } from 'react-router-dom';
import { Props as EuiTabProps } from '@elastic/eui/src/components/tabs/tab';
import { i18n } from '@kbn/i18n';
import { installationStatuses } from '../../../../../../../common/constants';
import { PAGE_ROUTING_PATHS } from '../../../../constants';
import { useLink, useGetCategories, useGetPackages, useBreadcrumbs } from '../../../../hooks';
import { WithHeaderLayout } from '../../../../layouts';
Expand Down Expand Up @@ -72,7 +73,7 @@ function InstalledPackages() {

const allInstalledPackages =
allPackages && allPackages.response
? allPackages.response.filter((pkg) => pkg.status === 'installed')
? allPackages.response.filter((pkg) => pkg.status === installationStatuses.Installed)
: [];

const updatablePackages = allInstalledPackages.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import { OverviewPanel } from './overview_panel';
import { OverviewStats } from './overview_stats';
import { useLink, useGetPackages } from '../../../hooks';
import { Loading } from '../../fleet/components';
import { InstallationStatus } from '../../../types';
import { installationStatuses } from '../../../../../../common/constants';

export const OverviewIntegrationSection: React.FC = () => {
const { getHref } = useLink();
const packagesRequest = useGetPackages();
const res = packagesRequest.data?.response;
const total = res?.length ?? 0;
const installed = res?.filter((p) => p.status === InstallationStatus.installed)?.length ?? 0;
const installed = res?.filter((p) => p.status === installationStatuses.Installed)?.length ?? 0;
const updatablePackages =
res?.filter(
(item) => 'savedObject' in item && item.version > item.savedObject.attributes.version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ import {
import * as Registry from '../../registry';
import { loadFieldsFromYaml, Fields, Field } from '../../fields/field';
import { getPackageKeysByStatus } from '../../packages/get';
import { dataTypes } from '../../../../../common/constants';
import { dataTypes, installationStatuses } from '../../../../../common/constants';
import { ValueOf } from '../../../../../common/types';
import {
InstallationStatus,
RegistryPackage,
CallESAsCurrentUser,
DataType,
} from '../../../../types';
import { RegistryPackage, CallESAsCurrentUser, DataType } from '../../../../types';
import { appContextService } from '../../../../services';

interface FieldFormatMap {
Expand Down Expand Up @@ -87,7 +82,7 @@ export async function installIndexPatterns(
// get all user installed packages
const installedPackages = await getPackageKeysByStatus(
savedObjectsClient,
InstallationStatus.installed
installationStatuses.Installed
);
// TODO: move to install package
// cache all installed packages if they don't exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/

import { SavedObjectsClientContract, SavedObjectsFindOptions } from 'src/core/server';
import { isPackageLimited } from '../../../../common';
import { isPackageLimited, installationStatuses } from '../../../../common';
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../constants';
import { ValueOf } from '../../../../common/types';
import { Installation, InstallationStatus, PackageInfo, KibanaAssetType } from '../../../types';
import * as Registry from '../registry';
import { createInstallableFrom, isRequiredPackage } from './index';
Expand Down Expand Up @@ -58,7 +59,7 @@ export async function getLimitedPackages(options: {
const { savedObjectsClient } = options;
const allPackages = await getPackages({ savedObjectsClient, experimental: true });
const installedPackages = allPackages.filter(
(pkg) => pkg.status === InstallationStatus.installed
(pkg) => pkg.status === installationStatuses.Installed
);
const installedPackagesInfo = await Promise.all(
installedPackages.map((pkgInstall) => {
Expand All @@ -84,12 +85,12 @@ export async function getPackageSavedObjects(

export async function getPackageKeysByStatus(
savedObjectsClient: SavedObjectsClientContract,
status: InstallationStatus
status: ValueOf<InstallationStatus>
) {
const allPackages = await getPackages({ savedObjectsClient, experimental: true });
return allPackages.reduce<Array<{ pkgName: string; pkgVersion: string }>>((acc, pkg) => {
if (pkg.status === status) {
if (pkg.status === InstallationStatus.installed) {
if (pkg.status === installationStatuses.Installed) {
// if we're looking for installed packages grab the version from the saved object because `getPackages` will
// return the latest package information from the registry
acc.push({ pkgName: pkg.name, pkgVersion: pkg.savedObject.attributes.version });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
*/

import { SavedObject } from 'src/core/server';
import { RequiredPackage, requiredPackages, ValueOf } from '../../../../common';
import {
AssetType,
Installable,
Installation,
InstallationStatus,
KibanaAssetType,
} from '../../../types';
RequiredPackage,
requiredPackages,
ValueOf,
installationStatuses,
} from '../../../../common';
import { AssetType, Installable, Installation, KibanaAssetType } from '../../../types';

export { bulkInstallPackages, isBulkInstallError } from './bulk_install_packages';
export {
Expand Down Expand Up @@ -56,11 +55,11 @@ export function createInstallableFrom<T>(
return savedObject
? {
...from,
status: InstallationStatus.installed,
status: installationStatuses.Installed,
savedObject,
}
: {
...from,
status: InstallationStatus.notInstalled,
status: installationStatuses.NotInstalled,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
} from '../../../ingest_manager/common/types/rest_spec';
import {
EsAssetReference,
InstallationStatus,
KibanaAssetReference,
} from '../../../ingest_manager/common/types/models';
import { agentPolicyStatuses } from '../../../ingest_manager/common/constants';
Expand Down Expand Up @@ -1267,7 +1266,7 @@ export class EndpointDocGenerator {
type: 'image/svg+xml',
},
],
status: 'installed' as InstallationStatus,
status: 'installed',
savedObject: {
type: 'epm-packages',
id: 'endpoint',
Expand Down

0 comments on commit 13dae5e

Please sign in to comment.