Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use backstage fetch api instead of the default fetch for node js #180

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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