-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import fetchSpy, { | ||
failureResponse, | ||
successResponse | ||
} from '../../spec/fetchSpy'; | ||
|
||
import OpenAPIService from '.'; | ||
|
||
describe('OpenAPIService', () => { | ||
let fetchMock: jest.SpyInstance; | ||
let service: OpenAPIService; | ||
|
||
beforeEach(() => { | ||
service = new OpenAPIService(); | ||
}); | ||
|
||
afterEach(() => { | ||
if (fetchMock) { | ||
fetchMock.mockReset(); | ||
fetchMock.mockRestore(); | ||
} | ||
}); | ||
|
||
it('is defined', () => { | ||
expect(OpenAPIService).toBeDefined(); | ||
}); | ||
|
||
it('has set the correct defaults (getApiDocs)', async () => { | ||
fetchMock = fetchSpy(successResponse([])); | ||
|
||
await service.getApiDocs(); | ||
|
||
expect(fetchMock).toHaveBeenCalledWith('/v2/api-docs', { | ||
headers: {}, | ||
method: 'GET' | ||
}); | ||
}); | ||
|
||
it('returns the api docs (getApiDocs)', async () => { | ||
const response = { | ||
id: 1 | ||
}; | ||
|
||
fetchMock = fetchSpy(successResponse(response)); | ||
|
||
const resp = await service.getApiDocs(); | ||
|
||
expect(resp).toEqual(response); | ||
}); | ||
|
||
it('throws an error if the api docs couldn\'t be fetched (getApiDocs)', async () => { | ||
fetchMock = fetchSpy(failureResponse()); | ||
|
||
await expect(service.getApiDocs()).rejects.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Keycloak from 'keycloak-js'; | ||
|
||
import { getBearerTokenHeader } from '../../security/getBearerTokenHeader'; | ||
|
||
export interface SwaggerDocs { | ||
basePath: string; | ||
definitions: { | ||
[key: string]: any; | ||
}; | ||
host: string; | ||
info: any; | ||
paths: { | ||
[key: string]: any; | ||
}; | ||
securityDefinitions: { | ||
[key: string]: any; | ||
}; | ||
swagger: string; | ||
tags: { | ||
name: string; | ||
description: string; | ||
}[]; | ||
} | ||
|
||
export interface OpenAPIServiceOpts { | ||
basePath: string; | ||
keycloak?: Keycloak; | ||
}; | ||
|
||
export class OpenAPIService { | ||
|
||
private basePath: string; | ||
|
||
private keycloak?: Keycloak; | ||
|
||
constructor(opts: OpenAPIServiceOpts = { | ||
basePath: '/v2' | ||
}) { | ||
this.basePath = opts.basePath; | ||
this.keycloak = opts.keycloak; | ||
} | ||
|
||
async getApiDocs(fetchOpts?: RequestInit): Promise<SwaggerDocs> { | ||
try { | ||
const response = await fetch(`${this.basePath}/api-docs`, { | ||
method: 'GET', | ||
headers: { | ||
...getBearerTokenHeader(this.keycloak) | ||
}, | ||
...fetchOpts | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error status: ${response.status}`); | ||
} | ||
|
||
const json = await response.json(); | ||
|
||
return json; | ||
} catch (error) { | ||
throw new Error(`Error while requesting the swagger docs: ${error}`); | ||
} | ||
} | ||
|
||
} | ||
|
||
export default OpenAPIService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters