Skip to content

Commit

Permalink
Merge pull request backstage#9630 from naijith/bazaar-api
Browse files Browse the repository at this point in the history
bazaar: use token while request to backend
  • Loading branch information
freben authored Mar 24, 2022
2 parents 011c0b4 + 89f5cb7 commit b1b793d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-buses-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-bazaar': patch
---

Pass authorization header with Backstage token to backend requests.
75 changes: 36 additions & 39 deletions plugins/bazaar/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {
createApiRef,
DiscoveryApi,
FetchApi,
IdentityApi,
} from '@backstage/core-plugin-api';

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)}`,
Expand All @@ -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',
});
}
Expand Down
6 changes: 4 additions & 2 deletions plugins/bazaar/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
createRoutableExtension,
identityApiRef,
discoveryApiRef,
fetchApiRef,
} from '@backstage/core-plugin-api';
import { bazaarApiRef, BazaarClient } from './api';

Expand All @@ -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 }),
}),
],
});
Expand Down

0 comments on commit b1b793d

Please sign in to comment.