catalog: Remove CatalogClientWrapper

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2022-02-15 16:33:18 +01:00
parent a52c98522a
commit ae7edbea45
4 changed files with 5 additions and 313 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog': minor
---
Removed `CatalogClientWrapper` as usage have been replaced by the `fetchApiRef`.
@@ -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<CatalogClient>;
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);
});
});
});
-159
View File
@@ -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<Location | undefined> {
return await this.client.getLocationById(
id,
await this.getCredentials(options),
);
}
async getEntities(
request?: GetEntitiesRequest,
options?: CatalogRequestOptions,
): Promise<GetEntitiesResponse> {
return await this.client.getEntities(
request,
await this.getCredentials(options),
);
}
async getEntityByName(
compoundName: EntityName,
options?: CatalogRequestOptions,
): Promise<Entity | undefined> {
return await this.client.getEntityByName(
compoundName,
await this.getCredentials(options),
);
}
async addLocation(
request: AddLocationRequest,
options?: CatalogRequestOptions,
): Promise<AddLocationResponse> {
return await this.client.addLocation(
request,
await this.getCredentials(options),
);
}
async getOriginLocationByEntity(
entity: Entity,
options?: CatalogRequestOptions,
): Promise<Location | undefined> {
return await this.client.getOriginLocationByEntity(
entity,
await this.getCredentials(options),
);
}
async getLocationByEntity(
entity: Entity,
options?: CatalogRequestOptions,
): Promise<Location | undefined> {
return await this.client.getLocationByEntity(
entity,
await this.getCredentials(options),
);
}
async removeLocationById(
id: string,
options?: CatalogRequestOptions,
): Promise<void> {
return await this.client.removeLocationById(
id,
await this.getCredentials(options),
);
}
async removeEntityByUid(
uid: string,
options?: CatalogRequestOptions,
): Promise<void> {
return await this.client.removeEntityByUid(
uid,
await this.getCredentials(options),
);
}
async refreshEntity(
entityRef: string,
options?: CatalogRequestOptions,
): Promise<void> {
return await this.client.refreshEntity(
entityRef,
await this.getCredentials(options),
);
}
async getEntityAncestors(
request: GetEntityAncestorsRequest,
options?: CatalogRequestOptions,
): Promise<GetEntityAncestorsResponse> {
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();
}
}
-1
View File
@@ -20,7 +20,6 @@
* @packageDocumentation
*/
export { CatalogClientWrapper } from './CatalogClientWrapper';
export * from './components/AboutCard';
export * from './components/CatalogKindHeader';
export * from './components/CatalogResultListItem';