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';