From b1c5b42849a400dbac414df2d8f20c813d2111a1 Mon Sep 17 00:00:00 2001 From: Alex Rybchenko Date: Wed, 15 Jun 2022 13:58:33 +0200 Subject: [PATCH] added type predicates for entities Signed-off-by: Alex Rybchenko --- .../src/components/EntitySwitch/conditions.ts | 61 ++++++++++++++++++- .../src/components/EntitySwitch/index.ts | 2 +- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.ts b/plugins/catalog/src/components/EntitySwitch/conditions.ts index 5f50fde5c9..b6837627c0 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.ts @@ -14,7 +14,17 @@ * limitations under the License. */ -import { Entity, ComponentEntity } from '@backstage/catalog-model'; +import { + ApiEntity, + ComponentEntity, + DomainEntity, + Entity, + GroupEntity, + LocationEntity, + ResourceEntity, + SystemEntity, + UserEntity, +} from '@backstage/catalog-model'; function strCmp(a: string | undefined, b: string | undefined): boolean { return Boolean( @@ -57,3 +67,52 @@ export function isComponentType(types: string | string[]) { export function isNamespace(namespaces: string | string[]) { return (entity: Entity) => strCmpAll(entity.metadata?.namespace, namespaces); } + +/** + * @public + */ +export function isApiEntity(entity: Entity): entity is ApiEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'API'; +} +/** + * @public + */ +export function isComponentEntity(entity: Entity): entity is ComponentEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'COMPONENT'; +} +/** + * @public + */ +export function isDomainEntity(entity: Entity): entity is DomainEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'DOMAIN'; +} +/** + * @public + */ +export function isGroupEntity(entity: Entity): entity is GroupEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'GROUP'; +} +/** + * @public + */ +export function isLocationEntity(entity: Entity): entity is LocationEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'LOCATION'; +} +/** + * @public + */ +export function isResourceEntity(entity: Entity): entity is ResourceEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'RESOURCE'; +} +/** + * @public + */ +export function isSystemEntity(entity: Entity): entity is SystemEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'SYSTEM'; +} +/** + * @public + */ +export function isUserEntity(entity: Entity): entity is UserEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'USER'; +} diff --git a/plugins/catalog/src/components/EntitySwitch/index.ts b/plugins/catalog/src/components/EntitySwitch/index.ts index 6cccf4be6e..5d0537bfa5 100644 --- a/plugins/catalog/src/components/EntitySwitch/index.ts +++ b/plugins/catalog/src/components/EntitySwitch/index.ts @@ -16,4 +16,4 @@ export { EntitySwitch } from './EntitySwitch'; export type { EntitySwitchProps, EntitySwitchCaseProps } from './EntitySwitch'; -export { isKind, isNamespace, isComponentType } from './conditions'; +export * from './conditions';