diff --git a/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts b/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts index 3892b83da0..e1a3553394 100644 --- a/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts +++ b/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts @@ -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 { + async findUser( + query: UserQuery, + options?: { token?: string }, + ): Promise { const filter: Record = { 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) { diff --git a/plugins/catalog/src/CatalogClientWrapper.ts b/plugins/catalog/src/CatalogClientWrapper.ts index 19bf77c91d..42e9d872d4 100644 --- a/plugins/catalog/src/CatalogClientWrapper.ts +++ b/plugins/catalog/src/CatalogClientWrapper.ts @@ -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 { - 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> { - 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 { - 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 { - 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 { - 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 { - return await this.client.removeEntityByUid( - uid, - await this.injectToken(context), - ); + async removeEntityByUid( + uid: string, + options?: CatalogRequestOptions, + ): Promise { + return await this.client.removeEntityByUid(uid, { + token: options?.token ?? (await this.identityApi.getIdToken()), + }); } } diff --git a/plugins/scaffolder-backend/src/lib/catalog/CatalogEntityClient.ts b/plugins/scaffolder-backend/src/lib/catalog/CatalogEntityClient.ts index 9ab9b17483..827ed0559a 100644 --- a/plugins/scaffolder-backend/src/lib/catalog/CatalogEntityClient.ts +++ b/plugins/scaffolder-backend/src/lib/catalog/CatalogEntityClient.ts @@ -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 { 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) {