refactor existing usage of ApiContext
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import { ConflictError, NotFoundError } from '@backstage/backend-common';
|
||||
import { ApiContext, CatalogApi } from '@backstage/catalog-client';
|
||||
import { CatalogApi } from '@backstage/catalog-client';
|
||||
import { UserEntity } from '@backstage/catalog-model';
|
||||
|
||||
type UserQuery = {
|
||||
@@ -37,7 +37,10 @@ export class CatalogIdentityClient {
|
||||
*
|
||||
* Throws a NotFoundError or ConflictError if 0 or multiple users are found.
|
||||
*/
|
||||
async findUser(query: UserQuery, context?: ApiContext): Promise<UserEntity> {
|
||||
async findUser(
|
||||
query: UserQuery,
|
||||
options?: { token?: string },
|
||||
): Promise<UserEntity> {
|
||||
const filter: Record<string, string> = {
|
||||
kind: 'user',
|
||||
};
|
||||
@@ -45,7 +48,7 @@ export class CatalogIdentityClient {
|
||||
filter[`metadata.annotations.${key}`] = value;
|
||||
}
|
||||
|
||||
const { items } = await this.catalogApi.getEntities({ filter }, context);
|
||||
const { items } = await this.catalogApi.getEntities({ filter }, options);
|
||||
|
||||
if (items.length !== 1) {
|
||||
if (items.length > 1) {
|
||||
|
||||
@@ -18,7 +18,6 @@ import { Entity, EntityName, Location } from '@backstage/catalog-model';
|
||||
import {
|
||||
AddLocationRequest,
|
||||
AddLocationResponse,
|
||||
ApiContext,
|
||||
CatalogApi,
|
||||
CatalogEntitiesRequest,
|
||||
CatalogListResponse,
|
||||
@@ -26,6 +25,10 @@ import {
|
||||
} from '@backstage/catalog-client';
|
||||
import { IdentityApi } from '@backstage/core';
|
||||
|
||||
type CatalogRequestOptions = {
|
||||
token?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* CatalogClient wrapper that injects identity token for all requests
|
||||
*/
|
||||
@@ -38,69 +41,57 @@ export class CatalogClientWrapper implements CatalogApi {
|
||||
this.identityApi = options.identityApi;
|
||||
}
|
||||
|
||||
private async injectToken(context?: ApiContext) {
|
||||
const result: ApiContext = context ?? {};
|
||||
// Inject identity token if not provided
|
||||
if (!result.token) {
|
||||
result.token = await this.identityApi.getIdToken();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async getLocationById(
|
||||
id: String,
|
||||
context?: ApiContext,
|
||||
options?: CatalogRequestOptions,
|
||||
): Promise<Location | undefined> {
|
||||
return await this.client.getLocationById(
|
||||
id,
|
||||
await this.injectToken(context),
|
||||
);
|
||||
return await this.client.getLocationById(id, {
|
||||
token: options?.token ?? (await this.identityApi.getIdToken()),
|
||||
});
|
||||
}
|
||||
|
||||
async getEntities(
|
||||
request?: CatalogEntitiesRequest,
|
||||
context?: ApiContext,
|
||||
options?: CatalogRequestOptions,
|
||||
): Promise<CatalogListResponse<Entity>> {
|
||||
return await this.client.getEntities(
|
||||
request,
|
||||
await this.injectToken(context),
|
||||
);
|
||||
return await this.client.getEntities(request, {
|
||||
token: options?.token ?? (await this.identityApi.getIdToken()),
|
||||
});
|
||||
}
|
||||
|
||||
async getEntityByName(
|
||||
compoundName: EntityName,
|
||||
context?: ApiContext,
|
||||
options?: CatalogRequestOptions,
|
||||
): Promise<Entity | undefined> {
|
||||
return await this.client.getEntityByName(
|
||||
compoundName,
|
||||
await this.injectToken(context),
|
||||
);
|
||||
return await this.client.getEntityByName(compoundName, {
|
||||
token: options?.token ?? (await this.identityApi.getIdToken()),
|
||||
});
|
||||
}
|
||||
|
||||
async addLocation(
|
||||
request: AddLocationRequest,
|
||||
context?: ApiContext,
|
||||
options?: CatalogRequestOptions,
|
||||
): Promise<AddLocationResponse> {
|
||||
return await this.client.addLocation(
|
||||
request,
|
||||
await this.injectToken(context),
|
||||
);
|
||||
return await this.client.addLocation(request, {
|
||||
token: options?.token ?? (await this.identityApi.getIdToken()),
|
||||
});
|
||||
}
|
||||
|
||||
async getLocationByEntity(
|
||||
entity: Entity,
|
||||
context?: ApiContext,
|
||||
options?: CatalogRequestOptions,
|
||||
): Promise<Location | undefined> {
|
||||
return await this.client.getLocationByEntity(
|
||||
entity,
|
||||
await this.injectToken(context),
|
||||
);
|
||||
return await this.client.getLocationByEntity(entity, {
|
||||
token: options?.token ?? (await this.identityApi.getIdToken()),
|
||||
});
|
||||
}
|
||||
|
||||
async removeEntityByUid(uid: string, context?: ApiContext): Promise<void> {
|
||||
return await this.client.removeEntityByUid(
|
||||
uid,
|
||||
await this.injectToken(context),
|
||||
);
|
||||
async removeEntityByUid(
|
||||
uid: string,
|
||||
options?: CatalogRequestOptions,
|
||||
): Promise<void> {
|
||||
return await this.client.removeEntityByUid(uid, {
|
||||
token: options?.token ?? (await this.identityApi.getIdToken()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
|
||||
import { ApiContext, CatalogClient } from '@backstage/catalog-client';
|
||||
import { CatalogClient } from '@backstage/catalog-client';
|
||||
import {
|
||||
ConflictError,
|
||||
NotFoundError,
|
||||
@@ -41,7 +41,7 @@ export class CatalogEntityClient {
|
||||
*/
|
||||
async findTemplate(
|
||||
templateName: string,
|
||||
context?: ApiContext,
|
||||
options?: { token?: string },
|
||||
): Promise<TemplateEntityV1alpha1> {
|
||||
const { items: templates } = (await this.catalogClient.getEntities(
|
||||
{
|
||||
@@ -50,7 +50,7 @@ export class CatalogEntityClient {
|
||||
'metadata.name': templateName,
|
||||
},
|
||||
},
|
||||
context,
|
||||
options,
|
||||
)) as { items: TemplateEntityV1alpha1[] };
|
||||
|
||||
if (templates.length !== 1) {
|
||||
|
||||
Reference in New Issue
Block a user