Skip to content

Commit

Permalink
feat: Add OpenAPIService
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlkoch committed Jun 21, 2022
1 parent c534377 commit f7b6e5f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/service/OpenAPIService/index.spec.ts
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();
});
});
67 changes: 67 additions & 0 deletions src/service/OpenAPIService/index.ts
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;
2 changes: 2 additions & 0 deletions src/service/SHOGunClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import SHOGunClient from './SHOGunClient';
import CacheService from './CacheService';
import FileService from './FileService';
import ImageFileService from './ImageFileService';
import OpenAPIService from './OpenAPIService';

export interface MyDefaultApplicationClientConfig extends DefaultApplicationClientConfig {
newField: number;
Expand Down Expand Up @@ -70,6 +71,7 @@ describe('SHOGunClient', () => {
expect(client.cache() instanceof CacheService).toBeTruthy();
expect(client.file() instanceof FileService).toBeTruthy();
expect(client.imagefile() instanceof ImageFileService).toBeTruthy();
expect(client.openapi() instanceof OpenAPIService).toBeTruthy();
});

it('can be extended', async () => {
Expand Down
8 changes: 8 additions & 0 deletions src/service/SHOGunClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import GraphQLService from './GraphQLService';
import GroupService from './GroupService';
import ImageFileService from './ImageFileService';
import LayerService from './LayerService';
import OpenAPIService from './OpenAPIService';
import UserService from './UserService';

export interface SHOGunClientOpts {
Expand Down Expand Up @@ -113,6 +114,13 @@ export class SHOGunClient {
});
}

openapi() {
return new OpenAPIService({
basePath: `${this.basePath}v2`,
keycloak: this.keycloak
});
}

getBasePath() {
return this.basePath;
}
Expand Down

0 comments on commit f7b6e5f

Please sign in to comment.