From e56bc3d2c88644e684a26ec176a74895794853bf Mon Sep 17 00:00:00 2001 From: gomezcapg Date: Wed, 9 Aug 2023 16:52:25 -0600 Subject: [PATCH 1/2] fix: use backstage fetch api instead of the default fetch for node js --- plugins/announcements/src/api.ts | 48 ++++++++++++++++------------- plugins/announcements/src/plugin.ts | 5 ++- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/plugins/announcements/src/api.ts b/plugins/announcements/src/api.ts index d654256..48c4033 100644 --- a/plugins/announcements/src/api.ts +++ b/plugins/announcements/src/api.ts @@ -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'; @@ -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(input: string, init?: RequestInit): Promise { @@ -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; - }); + return this.fetchApi + .fetch(`${baseApiUrl}${input}`, { + ...init, + headers, + }) + .then(async response => { + if (!response.ok) { + throw await ResponseError.fromResponse(response); + } + + return response.json() as Promise; + }); } private async delete(input: string, init?: RequestInit): Promise { @@ -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({ diff --git a/plugins/announcements/src/plugin.ts b/plugins/announcements/src/plugin.ts index 053b06e..2dcb51b 100644 --- a/plugins/announcements/src/plugin.ts +++ b/plugins/announcements/src/plugin.ts @@ -6,6 +6,7 @@ import { discoveryApiRef, errorApiRef, identityApiRef, + fetchApiRef, } from '@backstage/core-plugin-api'; import { createSearchResultListItemExtension, @@ -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, }); }, }), From f50322143f50b37f89d5d13d90b0754ef4617001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Wed, 27 Dec 2023 18:00:28 +0100 Subject: [PATCH 2/2] Add changeset --- .changeset/dry-pens-hope.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dry-pens-hope.md diff --git a/.changeset/dry-pens-hope.md b/.changeset/dry-pens-hope.md new file mode 100644 index 0000000..b0f38a4 --- /dev/null +++ b/.changeset/dry-pens-hope.md @@ -0,0 +1,5 @@ +--- +'@k-phoen/backstage-plugin-announcements': patch +--- + +Use backstage fetch API