fix: use fetchapi to automatically handle authentication

Signed-off-by: Anton Ganhammar <ganhammar@gmail.com>
This commit is contained in:
Anton Ganhammar
2023-08-18 07:07:39 +02:00
parent 1d8f4f0a74
commit 850ca8eb10
3 changed files with 10 additions and 13 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
'@backstage/plugin-code-coverage': patch
---
Include authorization token (if one exists) when fetching code-coverage data
Use fetchApi to ensure authorization is used when fetching code-coverage data
+5 -8
View File
@@ -23,7 +23,7 @@ import { JsonCodeCoverage, JsonCoverageHistory } from './types';
import {
createApiRef,
DiscoveryApi,
IdentityApi,
FetchApi,
} from '@backstage/core-plugin-api';
export type CodeCoverageApi = {
@@ -46,16 +46,16 @@ export const codeCoverageApiRef = createApiRef<CodeCoverageApi>({
export class CodeCoverageRestApi implements CodeCoverageApi {
private readonly discoveryApi: DiscoveryApi;
private readonly identityApi: IdentityApi;
private readonly fetchApi: FetchApi;
private url: string = '';
public constructor(options: {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
fetchApi: FetchApi;
}) {
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
this.fetchApi = options.fetchApi;
}
private async fetch<T = unknown | string | JsonCoverageHistory>(
@@ -64,10 +64,7 @@ export class CodeCoverageRestApi implements CodeCoverageApi {
if (!this.url) {
this.url = await this.discoveryApi.getBaseUrl('code-coverage');
}
const { token } = await this.identityApi.getCredentials();
const resp = await fetch(`${this.url}${path}`, {
headers: token ? { Authorization: `Bearer ${token}` } : {},
});
const resp = await this.fetchApi.fetch(`${this.url}${path}`);
if (!resp.ok) {
throw await ResponseError.fromResponse(resp);
}
+4 -4
View File
@@ -21,7 +21,7 @@ import {
createPlugin,
createRoutableExtension,
discoveryApiRef,
identityApiRef,
fetchApiRef,
} from '@backstage/core-plugin-api';
/**
@@ -35,9 +35,9 @@ export const codeCoveragePlugin = createPlugin({
apis: [
createApiFactory({
api: codeCoverageApiRef,
deps: { discoveryApi: discoveryApiRef, identityApi: identityApiRef },
factory: ({ discoveryApi, identityApi }) =>
new CodeCoverageRestApi({ discoveryApi, identityApi }),
deps: { discoveryApi: discoveryApiRef, fetchApi: fetchApiRef },
factory: ({ discoveryApi, fetchApi }) =>
new CodeCoverageRestApi({ discoveryApi, fetchApi }),
}),
],
});