Skip to content

Commit

Permalink
feat: add support for OpenAPI version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlkoch committed Dec 22, 2022
1 parent 8528dfa commit 0446acc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
42 changes: 19 additions & 23 deletions src/service/OpenAPIService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@ 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;
}[];
}
import {
OpenAPIV2,
OpenAPIV3
} from 'openapi-types';

export type OpenApiVersion = 'v2' | 'v3';

export interface OpenAPIServiceOpts {
basePath: string;
Expand All @@ -34,15 +21,16 @@ export class OpenAPIService {
private keycloak?: Keycloak;

constructor(opts: OpenAPIServiceOpts = {
basePath: '/v2'
basePath: '/'
}) {
this.basePath = opts.basePath;
this.keycloak = opts.keycloak;
}

async getApiDocs(fetchOpts?: RequestInit): Promise<SwaggerDocs> {
async getApiDocs(version: OpenApiVersion = 'v2', fetchOpts?: RequestInit):
Promise<OpenAPIV2.Document | OpenAPIV3.Document | undefined> {
try {
const response = await fetch(`${this.basePath}/api-docs`, {
const response = await fetch(`${this.basePath}${version}/api-docs`, {
method: 'GET',
headers: {
...getBearerTokenHeader(this.keycloak)
Expand All @@ -56,7 +44,15 @@ export class OpenAPIService {

const json = await response.json();

return json;
if (version === 'v2') {
return json as OpenAPIV2.Document;
}

if (version === 'v3') {
return json as OpenAPIV3.Document;
}

return;
} catch (error) {
throw new Error(`Error while requesting the swagger docs: ${error}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/service/SHOGunAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class SHOGunAPIClient {
openapi(): OpenAPIService {
if (!this.openapiService) {
this.openapiService = new OpenAPIService({
basePath: `${this.basePath}v2`,
basePath: `${this.basePath}`,
keycloak: this.keycloak
});
}
Expand Down

0 comments on commit 0446acc

Please sign in to comment.