Skip to content

Commit

Permalink
Merge pull request #17 from nfroidure/feat/openAPI
Browse files Browse the repository at this point in the history
feat(whook-swagger-ui): Add a `getOpenAPI` endpoint
  • Loading branch information
nfroidure authored Feb 10, 2019
2 parents 559c4ad + f2b9853 commit be41c92
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/create-whook/src/handlers/getOpenAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import getOpenAPI, {
definition,
} from 'whook-swagger-ui/dist/handlers/getOpenAPI';

// TODO: Use WHOOK_PLUGINS to get handlers from plugins
// instead of proxying here
export { definition };
export default getOpenAPI;
4 changes: 4 additions & 0 deletions packages/create-whook/src/services/API.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { name, autoService } from 'knifecycle';

import { definition as getOpenAPIDefinition } from '../handlers/getOpenAPI';
import { definition as getPingDefinition } from '../handlers/getPing';
import { definition as getTimeDefinition } from '../handlers/getTime';
import { definition as putEchoDefinition } from '../handlers/putEcho';
Expand Down Expand Up @@ -28,6 +29,9 @@ async function initAPI({ CONFIG, log }) {
description: CONFIG.description,
},
paths: {
[getOpenAPIDefinition.path]: {
[getOpenAPIDefinition.method]: getOpenAPIDefinition.operation,
},
[getPingDefinition.path]: {
[getPingDefinition.method]: getPingDefinition.operation,
},
Expand Down
57 changes: 57 additions & 0 deletions packages/create-whook/src/services/__snapshots__/API.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ Object {
},
},
"summary": "Enable OPTIONS for CORS",
"tags": Array [
"CORS",
],
"x-whook": Object {
"private": true,
"sourceOperationId": "putEcho",
"suffix": "CORS",
"type": "http",
Expand Down Expand Up @@ -82,6 +86,51 @@ Object {
],
},
},
"/openAPI": Object {
"get": Object {
"consumes": Array [],
"operationId": "getOpenAPI",
"produces": Array [
"application/json",
],
"responses": Object {
"200": Object {
"description": "Provides the private Open API documentation",
"schema": Object {
"type": "object",
},
},
},
"summary": "Get API documentation.",
"tags": Array [
"system",
],
"x-whook": Object {
"private": false,
},
},
"options": Object {
"consumes": Array [],
"operationId": "optionsWithCORS",
"parameters": Array [],
"produces": Array [],
"responses": Object {
"200": Object {
"description": "CORS sent.",
},
},
"summary": "Enable OPTIONS for CORS",
"tags": Array [
"CORS",
],
"x-whook": Object {
"private": true,
"sourceOperationId": "getOpenAPI",
"suffix": "CORS",
"type": "http",
},
},
},
"/ping": Object {
"get": Object {
"consumes": Array [],
Expand Down Expand Up @@ -122,7 +171,11 @@ Object {
},
},
"summary": "Enable OPTIONS for CORS",
"tags": Array [
"CORS",
],
"x-whook": Object {
"private": true,
"sourceOperationId": "getPing",
"suffix": "CORS",
"type": "http",
Expand Down Expand Up @@ -167,7 +220,11 @@ Object {
},
},
"summary": "Enable OPTIONS for CORS",
"tags": Array [
"CORS",
],
"x-whook": Object {
"private": true,
"sourceOperationId": "getTime",
"suffix": "CORS",
"type": "http",
Expand Down
4 changes: 4 additions & 0 deletions packages/whook-cors/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ Object {
},
},
"summary": "Enable OPTIONS for CORS",
"tags": Array [
"CORS",
],
"x-whook": Object {
"private": true,
"sourceOperationId": "ping",
"suffix": "CORS",
"type": "http",
Expand Down
2 changes: 2 additions & 0 deletions packages/whook-cors/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export async function augmentAPIWithCORS(API) {
...(operation['x-whook'] || {}),
suffix: 'CORS',
sourceOperationId: operation.operationId,
private: true,
};

if (whookConfig.type !== 'http') {
Expand All @@ -69,6 +70,7 @@ export async function augmentAPIWithCORS(API) {
newAPI.paths[operation.path].options = {
operationId: 'optionsWithCORS',
summary: 'Enable OPTIONS for CORS',
tags: ['CORS'],
'x-whook': {
...whookConfig,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getOpenAPI should show every endpoints when authenticated 1`] = `
Object {
"response": Object {
"body": Object {
"info": Object {
"version": "<already_tested>",
},
"paths": Object {
"/time": Object {
"get": Object {
"tags": Array [
"public",
],
"x-whook": Object {
"memx": 2,
"tx": 18,
},
},
"put": Object {
"tags": Array [],
"x-whook": Object {
"private": true,
},
},
},
},
"swagger": "2.0",
},
"status": 200,
},
}
`;

exports[`getOpenAPI should work 1`] = `
Object {
"response": Object {
"body": Object {
"info": Object {
"version": "<already_tested>",
},
"paths": Object {
"/time": Object {
"get": Object {
"tags": Array [
"public",
],
"x-whook": undefined,
},
},
},
"swagger": "2.0",
},
"status": 200,
},
}
`;
60 changes: 60 additions & 0 deletions packages/whook-swagger-ui/src/handlers/getOpenAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { autoHandler } from 'knifecycle';
import { getSwaggerOperations } from 'swagger-http-router/dist/utils';

export default autoHandler(getOpenAPI);

export const definition = {
path: '/openAPI',
method: 'get',
operation: {
operationId: 'getOpenAPI',
summary: 'Get API documentation.',
tags: ['system'],
'x-whook': { private: false },
consumes: [],
produces: ['application/json'],
responses: {
'200': {
description: 'Provides the private Open API documentation',
schema: {
type: 'object',
},
},
},
},
};

async function getOpenAPI({ API }, { userId }) {
const authenticatedRequest = !!userId;
if (authenticatedRequest) {
return {
status: 200,
body: API,
};
}

const operations = await getSwaggerOperations(API);
const CLEANED_API = {
...API,
paths: operations.reduce((paths, operation) => {
if (operation['x-whook'] && operation['x-whook'].private) {
return paths;
}

paths[operation.path] = {
...paths[operation.path],
[operation.method]: {
...API.paths[operation.path][operation.method],
'x-whook': {}.undef,
},
};

return paths;
}, {}),
};

return {
status: 200,
body: CLEANED_API,
};
}
61 changes: 61 additions & 0 deletions packages/whook-swagger-ui/src/handlers/getOpenAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import initGetOpenAPI from './getOpenAPI';

describe('getOpenAPI', () => {
const API = {
swagger: '2.0',
paths: {
'/time': {
get: {
tags: ['public'],
'x-whook': { memx: 2, tx: 18 },
},
put: {
tags: [],
'x-whook': { private: true },
},
},
},
};

it('should work', async () => {
const getOpenAPI = await initGetOpenAPI({
API,
});
const response = await getOpenAPI({});

expect({
response: {
...response,
body: {
...response.body,
info: {
...response.body.info,
version: '<already_tested>',
},
},
},
}).toMatchSnapshot();
});

it('should show every endpoints when authenticated', async () => {
const getOpenAPI = await initGetOpenAPI({
API,
});
const response = await getOpenAPI({
userId: 1,
});

expect({
response: {
...response,
body: {
...response.body,
info: {
...response.body.info,
version: '<already_tested>',
},
},
},
}).toMatchSnapshot();
});
});

0 comments on commit be41c92

Please sign in to comment.