Skip to content

Commit

Permalink
fix: use backstage fetch api instead of the default fetch for node js
Browse files Browse the repository at this point in the history
  • Loading branch information
gomezcapg committed Aug 9, 2023
1 parent 0c9d794 commit d10f3b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
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 d10f3b7

Please sign in to comment.