diff --git a/.changeset/big-buses-clap.md b/.changeset/big-buses-clap.md new file mode 100644 index 0000000000..503aa314bc --- /dev/null +++ b/.changeset/big-buses-clap.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-bazaar': patch +--- + +Pass authorization header with Backstage token to backend requests. diff --git a/plugins/bazaar/src/api.ts b/plugins/bazaar/src/api.ts index 2d661506bc..d2486f74dc 100644 --- a/plugins/bazaar/src/api.ts +++ b/plugins/bazaar/src/api.ts @@ -17,6 +17,7 @@ import { createApiRef, DiscoveryApi, + FetchApi, IdentityApi, } from '@backstage/core-plugin-api'; @@ -47,49 +48,53 @@ export interface BazaarApi { export class BazaarClient implements BazaarApi { private readonly identityApi: IdentityApi; private readonly discoveryApi: DiscoveryApi; + private readonly fetchApi: FetchApi; constructor(options: { identityApi: IdentityApi; discoveryApi: DiscoveryApi; + fetchApi: FetchApi; }) { this.identityApi = options.identityApi; this.discoveryApi = options.discoveryApi; + this.fetchApi = options.fetchApi; } async updateProject(bazaarProject: any): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - return await fetch(`${baseUrl}/projects`, { - method: 'PUT', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify(bazaarProject), - }).then(resp => resp.json()); + return await this.fetchApi + .fetch(`${baseUrl}/projects`, { + method: 'PUT', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify(bazaarProject), + }) + .then(resp => resp.json()); } async addProject(bazaarProject: any): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - return await fetch(`${baseUrl}/projects`, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify(bazaarProject), - }).then(resp => resp.json()); + return await this.fetchApi + .fetch(`${baseUrl}/projects`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify(bazaarProject), + }) + .then(resp => resp.json()); } async getProjectById(id: number): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - const response = await fetch( + const response = await this.fetchApi.fetch( `${baseUrl}/projects/${encodeURIComponent(id)}`, - { - method: 'GET', - }, ); return response.ok ? response : null; @@ -98,11 +103,8 @@ export class BazaarClient implements BazaarApi { async getProjectByRef(entityRef: string): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - const response = await fetch( + const response = await this.fetchApi.fetch( `${baseUrl}/projects/${encodeURIComponent(entityRef)}`, - { - method: 'GET', - }, ); return response.ok ? response : null; @@ -111,19 +113,16 @@ export class BazaarClient implements BazaarApi { async getMembers(id: number): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - return await fetch( - `${baseUrl}/projects/${encodeURIComponent(id)}/members`, - { - method: 'GET', - }, - ).then(resp => resp.json()); + return await this.fetchApi + .fetch(`${baseUrl}/projects/${encodeURIComponent(id)}/members`) + .then(resp => resp.json()); } async addMember(id: number, userId: string): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); const { picture } = await this.identityApi.getProfileInfo(); - await fetch( + await this.fetchApi.fetch( `${baseUrl}/projects/${encodeURIComponent( id, )}/member/${encodeURIComponent(userId)}`, @@ -141,28 +140,26 @@ export class BazaarClient implements BazaarApi { async deleteMember(id: number, userId: string): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - await fetch( + await this.fetchApi.fetch( `${baseUrl}/projects/${encodeURIComponent( id, )}/member/${encodeURIComponent(userId)}`, - { - method: 'DELETE', - }, + { method: 'DELETE' }, ); } async getProjects(): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - return await fetch(`${baseUrl}/projects`, { - method: 'GET', - }).then(resp => resp.json()); + return await this.fetchApi + .fetch(`${baseUrl}/projects`) + .then(resp => resp.json()); } async deleteProject(id: number): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - await fetch(`${baseUrl}/projects/${encodeURIComponent(id)}`, { + await this.fetchApi.fetch(`${baseUrl}/projects/${encodeURIComponent(id)}`, { method: 'DELETE', }); } diff --git a/plugins/bazaar/src/plugin.ts b/plugins/bazaar/src/plugin.ts index 5eb5e2aacd..438208624e 100644 --- a/plugins/bazaar/src/plugin.ts +++ b/plugins/bazaar/src/plugin.ts @@ -21,6 +21,7 @@ import { createRoutableExtension, identityApiRef, discoveryApiRef, + fetchApiRef, } from '@backstage/core-plugin-api'; import { bazaarApiRef, BazaarClient } from './api'; @@ -35,9 +36,10 @@ export const bazaarPlugin = createPlugin({ deps: { identityApi: identityApiRef, discoveryApi: discoveryApiRef, + fetchApi: fetchApiRef, }, - factory: ({ identityApi, discoveryApi }) => - new BazaarClient({ identityApi, discoveryApi }), + factory: ({ identityApi, discoveryApi, fetchApi }) => + new BazaarClient({ identityApi, discoveryApi, fetchApi }), }), ], });