Skip to content

Commit

Permalink
Merge pull request #180 from gomezcapg/fix/use-backstage-fetch
Browse files Browse the repository at this point in the history
fix: use backstage fetch api instead of the default fetch for node js
  • Loading branch information
K-Phoen authored Dec 27, 2023
2 parents 26b0816 + f503221 commit cd551f5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-pens-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@k-phoen/backstage-plugin-announcements': patch
---

Use backstage fetch API
48 changes: 26 additions & 22 deletions plugins/announcements/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
IdentityApi,
} from '@backstage/core-plugin-api';
import { ResponseError } from '@backstage/errors';
import { FetchApi } from '@backstage/core-plugin-api';

const lastSeenKey = 'user_last_seen_date';

Expand Down Expand Up @@ -71,17 +72,20 @@ type Options = {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
errorApi: ErrorApi;
fetchApi: FetchApi;
};

export class DefaultAnnouncementsApi implements AnnouncementsApi {
private readonly discoveryApi: DiscoveryApi;
private readonly identityApi: IdentityApi;
private readonly webStorage: WebStorage;
private readonly fetchApi: FetchApi;

constructor(opts: Options) {
this.discoveryApi = opts.discoveryApi;
this.identityApi = opts.identityApi;
this.webStorage = new WebStorage('announcements', opts.errorApi);
this.fetchApi = opts.fetchApi;
}

private async fetch<T = any>(input: string, init?: RequestInit): Promise<T> {
Expand All @@ -93,18 +97,18 @@ export class DefaultAnnouncementsApi implements AnnouncementsApi {
headers.set('authorization', `Bearer ${token}`);
}

const request = new Request(`${baseApiUrl}${input}`, {
...init,
headers,
});

return fetch(request).then(async response => {
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}

return response.json() as Promise<T>;
});
return this.fetchApi
.fetch(`${baseApiUrl}${input}`, {
...init,
headers,
})
.then(async response => {
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}

return response.json() as Promise<T>;
});
}

private async delete(input: string, init?: RequestInit): Promise<void> {
Expand All @@ -116,16 +120,16 @@ export class DefaultAnnouncementsApi implements AnnouncementsApi {
headers.set('authorization', `Bearer ${token}`);
}

const request = new Request(`${baseApiUrl}${input}`, {
...{ method: 'DELETE' },
headers,
});

return fetch(request).then(async response => {
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
});
return this.fetchApi
.fetch(`${baseApiUrl}${input}`, {
...{ method: 'DELETE' },
headers,
})
.then(async response => {
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
});
}

async announcements({
Expand Down
5 changes: 4 additions & 1 deletion plugins/announcements/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
discoveryApiRef,
errorApiRef,
identityApiRef,
fetchApiRef,
} from '@backstage/core-plugin-api';
import {
createSearchResultListItemExtension,
Expand All @@ -27,12 +28,14 @@ export const announcementsPlugin = createPlugin({
discoveryApi: discoveryApiRef,
identityApi: identityApiRef,
errorApi: errorApiRef,
fetchApi: fetchApiRef,
},
factory: ({ discoveryApi, identityApi, errorApi }) => {
factory: ({ discoveryApi, identityApi, errorApi, fetchApi }) => {
return new DefaultAnnouncementsApi({
discoveryApi: discoveryApi,
identityApi: identityApi,
errorApi: errorApi,
fetchApi: fetchApi,
});
},
}),
Expand Down

0 comments on commit cd551f5

Please sign in to comment.