Merge pull request #9630 from naijith/bazaar-api

bazaar: use token while request to backend
This commit is contained in:
Fredrik Adelöw
2022-03-24 16:43:41 +01:00
committed by GitHub
3 changed files with 45 additions and 41 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-bazaar': patch
---
Pass authorization header with Backstage token to backend requests.
+36 -39
View File
@@ -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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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<void> {
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<void> {
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<any> {
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<void> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
await fetch(`${baseUrl}/projects/${encodeURIComponent(id)}`, {
await this.fetchApi.fetch(`${baseUrl}/projects/${encodeURIComponent(id)}`, {
method: 'DELETE',
});
}
+4 -2
View File
@@ -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 }),
}),
],
});