Merge pull request #23903 from alex-mckay/addApiTypeFunction

Add api type function
This commit is contained in:
Patrik Oldsberg
2024-04-02 11:56:15 +02:00
committed by GitHub
5 changed files with 61 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog': minor
---
Add `isApiType()` to EntitySwitch routing functions.
+5
View File
@@ -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[],
@@ -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: [] });
@@ -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
@@ -22,5 +22,6 @@ export {
isNamespace,
isComponentType,
isResourceType,
isApiType,
isEntityWith,
} from './conditions';