fix: use FetchApi instead of IdentityApi

Signed-off-by: pamelin <pamelin@expediagroup.com>
This commit is contained in:
pamelin
2023-02-16 10:25:29 +00:00
parent 5b85cc1595
commit 83e8387391
3 changed files with 28 additions and 23 deletions
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { UrlPatternDiscovery } from '@backstage/core-app-api';
import { setupRequestMockHandlers } from '@backstage/test-utils';
import { MockFetchApi, setupRequestMockHandlers } from '@backstage/test-utils';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { StackstormClient } from './StackstormClient';
@@ -141,13 +141,6 @@ const actions: Action[] = [
},
];
const identityApiMock = () => ({
signOut: jest.fn(),
getProfileInfo: jest.fn(),
getBackstageIdentity: jest.fn(),
getCredentials: jest.fn().mockResolvedValue({ token: undefined }),
});
describe('StackstormClient', () => {
setupRequestMockHandlers(server);
@@ -186,7 +179,7 @@ describe('StackstormClient', () => {
setupHandlers();
client = new StackstormClient({
discoveryApi: discoveryApi,
identityApi: identityApiMock(),
fetchApi: new MockFetchApi(),
});
});
+22 -10
View File
@@ -14,36 +14,48 @@
* limitations under the License.
*/
import { Action, Execution, Pack, StackstormApi } from './types';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import { DiscoveryApi, FetchApi } from '@backstage/core-plugin-api';
export class StackstormClient implements StackstormApi {
private readonly discoveryApi: DiscoveryApi;
private readonly identityApi: IdentityApi;
private readonly fetchApi: FetchApi;
constructor({
discoveryApi,
identityApi,
fetchApi,
}: {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
fetchApi: FetchApi;
}) {
this.discoveryApi = discoveryApi;
this.identityApi = identityApi;
this.fetchApi = fetchApi;
}
private async get<T = any>(input: string): Promise<T> {
const { token: idToken } = await this.identityApi.getCredentials();
const apiUrl = `${await this.discoveryApi.getBaseUrl('proxy')}/stackstorm`;
const response = await fetch(`${apiUrl}${input}`, {
method: 'GET',
const response = await this.fetchApi.fetch(`${apiUrl}${input}`, {
headers: {
'Content-Type': 'application/json',
...(idToken && { Authorization: `Bearer ${idToken}` }),
},
});
if (!response.ok) throw new Error(`Unable to get data: ${response.status}`);
return await response.json();
return (await response.json()) as T;
}
async addProject(bazaarProject: any): Promise<any> {
const baseUrl = await this.discoveryApi.getBaseUrl('bazaar');
return await this.fetchApi
.fetch(`${baseUrl}/projects`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(bazaarProject),
})
.then(resp => resp.json());
}
async getExecutions(limit?: number, offset?: number): Promise<Execution[]> {
+4 -4
View File
@@ -19,7 +19,7 @@ import {
createPlugin,
createRoutableExtension,
discoveryApiRef,
identityApiRef,
fetchApiRef,
} from '@backstage/core-plugin-api';
import { stackstormApiRef, StackstormClient } from './api';
import { rootRouteRef } from './routes';
@@ -36,12 +36,12 @@ export const stackstormPlugin = createPlugin({
api: stackstormApiRef,
deps: {
discoveryApi: discoveryApiRef,
identityApi: identityApiRef,
fetchApi: fetchApiRef,
},
factory: ({ discoveryApi, identityApi }) =>
factory: ({ discoveryApi, fetchApi }) =>
new StackstormClient({
discoveryApi,
identityApi,
fetchApi,
}),
}),
],