diff --git a/.changeset/witty-kings-smoke.md b/.changeset/witty-kings-smoke.md new file mode 100644 index 0000000000..d59716eeff --- /dev/null +++ b/.changeset/witty-kings-smoke.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': minor +--- + +Add `isApiType()` to EntitySwitch routing functions. diff --git a/plugins/catalog/api-report.md b/plugins/catalog/api-report.md index 8766b983ca..a26099d613 100644 --- a/plugins/catalog/api-report.md +++ b/plugins/catalog/api-report.md @@ -559,6 +559,11 @@ export interface HasSystemsCardProps { variant?: InfoCardVariants; } +// @public +export function isApiType( + types: string | string[], +): (entity: Entity) => boolean; + // @public export function isComponentType( types: string | string[], diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts index 66408aec18..363ce772cb 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts @@ -20,6 +20,7 @@ import { isKind, isNamespace, isResourceType, + isApiType, isEntityWith, } from './conditions'; @@ -65,6 +66,27 @@ const notComponent: Entity = { spec: { type: 'service' }, }; +const graphqlApi: Entity = { + apiVersion: '', + kind: 'api', + metadata: { name: 'aProtocol' }, + spec: { type: 'graphql' }, +}; + +const grpcApi: Entity = { + apiVersion: '', + kind: 'api', + metadata: { name: 'aProtocol' }, + spec: { type: 'grpc' }, +}; + +const notApi: Entity = { + apiVersion: '', + kind: 'not-api', + metadata: { name: 'aProtocol' }, + spec: { type: 'grpc' }, +}; + const missingSpecType: Entity = { apiVersion: '', kind: 'another-type', @@ -133,6 +155,26 @@ describe('isComponentType', () => { }); }); +describe('isApiType', () => { + it('should false on non API kinds', () => { + const checkEntity = isApiType('openapi'); + + expect(checkEntity(notApi)).not.toBeTruthy(); + }); + it('should check for the intended type', () => { + const checkEntity = isApiType('grpc'); + + expect(checkEntity(graphqlApi)).not.toBeTruthy(); + expect(checkEntity(grpcApi)).toBeTruthy(); + }); + it('should check for multiple types', () => { + const checkEntity = isApiType(['grpc', 'graphql']); + + expect(checkEntity(graphqlApi)).toBeTruthy(); + expect(checkEntity(grpcApi)).toBeTruthy(); + }); +}); + describe('isEntityWith', () => { it('allows for a kind-only check (empty type array)', () => { const checkEntity = isEntityWith({ kind: 'api', type: [] }); diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.ts b/plugins/catalog/src/components/EntitySwitch/conditions.ts index f834804141..890bcdac9c 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.ts @@ -60,6 +60,14 @@ export function isResourceType(types: string | string[]) { return isEntityWith({ kind: 'resource', type: types }); } +/** + * For use in EntitySwitch.Case. Matches if the entity is an API of a given spec.type. + * @public + */ +export function isApiType(types: string | string[]) { + return isEntityWith({ kind: 'api', type: types }); +} + /** * For use in EntitySwitch.Case. Matches if the entity is the specified kind and type (if present). * @public diff --git a/plugins/catalog/src/components/EntitySwitch/index.ts b/plugins/catalog/src/components/EntitySwitch/index.ts index 8848f98866..a4e46c0b77 100644 --- a/plugins/catalog/src/components/EntitySwitch/index.ts +++ b/plugins/catalog/src/components/EntitySwitch/index.ts @@ -22,5 +22,6 @@ export { isNamespace, isComponentType, isResourceType, + isApiType, isEntityWith, } from './conditions';