From ae7edbea45d95174d3c2d913495a890b48e9f486 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Tue, 15 Feb 2022 16:33:18 +0100 Subject: [PATCH 1/2] catalog: Remove CatalogClientWrapper Signed-off-by: Johan Haals --- .changeset/funny-crabs-drop.md | 5 + .../catalog/src/CatalogClientWrapper.test.ts | 153 ----------------- plugins/catalog/src/CatalogClientWrapper.ts | 159 ------------------ plugins/catalog/src/index.ts | 1 - 4 files changed, 5 insertions(+), 313 deletions(-) create mode 100644 .changeset/funny-crabs-drop.md delete mode 100644 plugins/catalog/src/CatalogClientWrapper.test.ts delete mode 100644 plugins/catalog/src/CatalogClientWrapper.ts diff --git a/.changeset/funny-crabs-drop.md b/.changeset/funny-crabs-drop.md new file mode 100644 index 0000000000..764f7c042a --- /dev/null +++ b/.changeset/funny-crabs-drop.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': minor +--- + +Removed `CatalogClientWrapper` as usage have been replaced by the `fetchApiRef`. diff --git a/plugins/catalog/src/CatalogClientWrapper.test.ts b/plugins/catalog/src/CatalogClientWrapper.test.ts deleted file mode 100644 index e1bba9e60f..0000000000 --- a/plugins/catalog/src/CatalogClientWrapper.test.ts +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright 2020 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { CatalogClient } from '@backstage/catalog-client'; -import { CatalogClientWrapper } from './CatalogClientWrapper'; -import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api'; - -jest.mock('@backstage/catalog-client'); -const MockedCatalogClient = CatalogClient as jest.Mock; - -const mockBaseUrl = 'http://backstage:9191/i-am-a-mock-base'; -const discoveryApi: DiscoveryApi = { - async getBaseUrl(_pluginId) { - return mockBaseUrl; - }, -}; -const identityApi: IdentityApi = { - signOut: jest.fn(), - getProfileInfo: jest.fn(), - getBackstageIdentity: jest.fn(), - getCredentials: jest.fn().mockResolvedValue({ token: 'fake-id-token' }), -}; -const guestIdentityApi: IdentityApi = { - signOut: jest.fn(), - getProfileInfo: jest.fn(), - getBackstageIdentity: jest.fn(), - getCredentials: jest.fn().mockResolvedValue({ token: undefined }), -}; - -describe('CatalogClientWrapper', () => { - beforeEach(() => { - MockedCatalogClient.mockClear(); - }); - - describe('getEntities', () => { - it('injects authorization token', async () => { - const client = new CatalogClientWrapper({ - client: new MockedCatalogClient({ discoveryApi }), - identityApi, - }); - await client.getEntities(); - const getEntities = MockedCatalogClient.mock.instances[0].getEntities; - expect(getEntities).toHaveBeenCalledWith(undefined, { - token: 'fake-id-token', - }); - expect(getEntities).toHaveBeenCalledTimes(1); - }); - }); - - describe('getLocationById', () => { - it('omits authorization token when guest', async () => { - const client = new CatalogClientWrapper({ - client: new MockedCatalogClient({ discoveryApi }), - identityApi: guestIdentityApi, - }); - await client.getLocationById('42'); - const getLocationById = - MockedCatalogClient.mock.instances[0].getLocationById; - expect(getLocationById).toHaveBeenCalledWith('42', {}); - expect(getLocationById).toHaveBeenCalledTimes(1); - }); - }); - - describe('getEntityByName', () => { - const name = { - kind: 'kind', - namespace: 'namespace', - name: 'name', - }; - it('injects authorization token', async () => { - const client = new CatalogClientWrapper({ - client: new MockedCatalogClient({ discoveryApi }), - identityApi, - }); - await client.getEntityByName(name); - const getEntityByName = - MockedCatalogClient.mock.instances[0].getEntityByName; - expect(getEntityByName).toHaveBeenCalledWith(name, { - token: 'fake-id-token', - }); - expect(getEntityByName).toHaveBeenCalledTimes(1); - }); - }); - - describe('addLocation', () => { - const location = { target: 'target' }; - it('injects authorization token', async () => { - const client = new CatalogClientWrapper({ - client: new MockedCatalogClient({ discoveryApi }), - identityApi, - }); - await client.addLocation(location); - const addLocation = MockedCatalogClient.mock.instances[0].addLocation; - expect(addLocation).toHaveBeenCalledWith(location, { - token: 'fake-id-token', - }); - expect(addLocation).toHaveBeenCalledTimes(1); - }); - }); - - describe('getLocationByEntity', () => { - const entity = { - apiVersion: 'apiVersion', - kind: 'kind', - metadata: { - name: 'name', - }, - }; - it('injects authorization token', async () => { - const client = new CatalogClientWrapper({ - client: new MockedCatalogClient({ discoveryApi }), - identityApi, - }); - await client.getLocationByEntity(entity); - const getLocationByEntity = - MockedCatalogClient.mock.instances[0].getLocationByEntity; - expect(getLocationByEntity).toHaveBeenCalledWith(entity, { - token: 'fake-id-token', - }); - expect(getLocationByEntity).toHaveBeenCalledTimes(1); - }); - }); - - describe('removeEntityByUid', () => { - it('injects authorization token', async () => { - const uid = 'uid'; - const client = new CatalogClientWrapper({ - client: new MockedCatalogClient({ discoveryApi }), - identityApi, - }); - await client.removeEntityByUid(uid); - const removeEntityByUid = - MockedCatalogClient.mock.instances[0].removeEntityByUid; - expect(removeEntityByUid).toHaveBeenCalledWith(uid, { - token: 'fake-id-token', - }); - expect(removeEntityByUid).toHaveBeenCalledTimes(1); - }); - }); -}); diff --git a/plugins/catalog/src/CatalogClientWrapper.ts b/plugins/catalog/src/CatalogClientWrapper.ts deleted file mode 100644 index b54ac0ed61..0000000000 --- a/plugins/catalog/src/CatalogClientWrapper.ts +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright 2020 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { Entity, EntityName } from '@backstage/catalog-model'; -import { - AddLocationRequest, - AddLocationResponse, - CatalogApi, - CatalogClient, - GetEntitiesRequest, - GetEntitiesResponse, - CatalogRequestOptions, - GetEntityAncestorsRequest, - GetEntityAncestorsResponse, - Location, -} from '@backstage/catalog-client'; -import { IdentityApi } from '@backstage/core-plugin-api'; - -/** - * CatalogClient wrapper that injects identity token for all requests - * - * @deprecated The default catalog client now uses the `fetchApiRef` - * implementation, which in turn by default issues tokens just the same as this - * class used to assist in doing. If you use a custom `fetchApiRef` - * implementation that does NOT issue tokens, or use a custom `catalogApiRef` - * implementation which does not use the default `fetchApiRef`, you can wrap - * your catalog API in this class to get back the old behavior. - */ -export class CatalogClientWrapper implements CatalogApi { - private readonly identityApi: IdentityApi; - private readonly client: CatalogClient; - - constructor(options: { client: CatalogClient; identityApi: IdentityApi }) { - this.client = options.client; - this.identityApi = options.identityApi; - } - - async getLocationById( - id: string, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.getLocationById( - id, - await this.getCredentials(options), - ); - } - - async getEntities( - request?: GetEntitiesRequest, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.getEntities( - request, - await this.getCredentials(options), - ); - } - - async getEntityByName( - compoundName: EntityName, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.getEntityByName( - compoundName, - await this.getCredentials(options), - ); - } - - async addLocation( - request: AddLocationRequest, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.addLocation( - request, - await this.getCredentials(options), - ); - } - - async getOriginLocationByEntity( - entity: Entity, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.getOriginLocationByEntity( - entity, - await this.getCredentials(options), - ); - } - - async getLocationByEntity( - entity: Entity, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.getLocationByEntity( - entity, - await this.getCredentials(options), - ); - } - - async removeLocationById( - id: string, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.removeLocationById( - id, - await this.getCredentials(options), - ); - } - - async removeEntityByUid( - uid: string, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.removeEntityByUid( - uid, - await this.getCredentials(options), - ); - } - - async refreshEntity( - entityRef: string, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.refreshEntity( - entityRef, - await this.getCredentials(options), - ); - } - - async getEntityAncestors( - request: GetEntityAncestorsRequest, - options?: CatalogRequestOptions, - ): Promise { - return await this.client.getEntityAncestors( - request, - await this.getCredentials(options), - ); - } - - private async getCredentials( - options?: CatalogRequestOptions, - ): Promise<{ token?: string }> { - if (options?.token) { - return { token: options?.token }; - } - return this.identityApi.getCredentials(); - } -} diff --git a/plugins/catalog/src/index.ts b/plugins/catalog/src/index.ts index abe34c29b9..0732d184ca 100644 --- a/plugins/catalog/src/index.ts +++ b/plugins/catalog/src/index.ts @@ -20,7 +20,6 @@ * @packageDocumentation */ -export { CatalogClientWrapper } from './CatalogClientWrapper'; export * from './components/AboutCard'; export * from './components/CatalogKindHeader'; export * from './components/CatalogResultListItem'; From af6062aa200cc2bfdbc377b86aaf26eae75e2863 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Tue, 15 Feb 2022 16:50:47 +0100 Subject: [PATCH 2/2] Update api report Signed-off-by: Johan Haals --- plugins/catalog/api-report.md | 68 ----------------------------------- 1 file changed, 68 deletions(-) diff --git a/plugins/catalog/api-report.md b/plugins/catalog/api-report.md index d526d4dee1..3df1eeba1c 100644 --- a/plugins/catalog/api-report.md +++ b/plugins/catalog/api-report.md @@ -5,24 +5,13 @@ ```ts /// -import { AddLocationRequest } from '@backstage/catalog-client'; -import { AddLocationResponse } from '@backstage/catalog-client'; import { ApiHolder } from '@backstage/core-plugin-api'; import { BackstagePlugin } from '@backstage/core-plugin-api'; -import { CatalogApi } from '@backstage/catalog-client'; -import { CatalogClient } from '@backstage/catalog-client'; -import { CatalogRequestOptions } from '@backstage/catalog-client'; import { Entity } from '@backstage/catalog-model'; import { EntityName } from '@backstage/catalog-model'; import { ExternalRouteRef } from '@backstage/core-plugin-api'; -import { GetEntitiesRequest } from '@backstage/catalog-client'; -import { GetEntitiesResponse } from '@backstage/catalog-client'; -import { GetEntityAncestorsRequest } from '@backstage/catalog-client'; -import { GetEntityAncestorsResponse } from '@backstage/catalog-client'; import { IconComponent } from '@backstage/core-plugin-api'; -import { IdentityApi } from '@backstage/core-plugin-api'; import { InfoCardVariants } from '@backstage/core-components'; -import { Location as Location_2 } from '@backstage/catalog-client'; import { Overrides } from '@material-ui/core/styles/overrides'; import { PropsWithChildren } from 'react'; import { default as React_2 } from 'react'; @@ -64,63 +53,6 @@ export type BackstageOverrides = Overrides & { >; }; -// Warning: (ae-missing-release-tag) "CatalogClientWrapper" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public @deprecated -export class CatalogClientWrapper implements CatalogApi { - constructor(options: { client: CatalogClient; identityApi: IdentityApi }); - // (undocumented) - addLocation( - request: AddLocationRequest, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - getEntities( - request?: GetEntitiesRequest, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - getEntityAncestors( - request: GetEntityAncestorsRequest, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - getEntityByName( - compoundName: EntityName, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - getLocationByEntity( - entity: Entity, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - getLocationById( - id: string, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - getOriginLocationByEntity( - entity: Entity, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - refreshEntity( - entityRef: string, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - removeEntityByUid( - uid: string, - options?: CatalogRequestOptions, - ): Promise; - // (undocumented) - removeLocationById( - id: string, - options?: CatalogRequestOptions, - ): Promise; -} - // Warning: (ae-missing-release-tag) "CatalogEntityPage" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented)