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

OpenAPI v3 support #235

Merged
merged 2 commits into from
Jan 4, 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
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"jest": "^29.3.1",
"jest-cli": "^29.2.1",
"jest-environment-jsdom": "^29.3.1",
"openapi-types": "^12.0.2",
"semantic-release": "^19.0.5",
"typedoc": "^0.23.23",
"typescript": "^4.9.4"
Expand Down
38 changes: 15 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> {
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,11 @@ export class OpenAPIService {

const json = await response.json();

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

return json as OpenAPIV2.Document;
} 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