-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from nfroidure/feat/openAPI
feat(whook-swagger-ui): Add a `getOpenAPI` endpoint
- Loading branch information
Showing
8 changed files
with
254 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,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; |
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
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
58 changes: 58 additions & 0 deletions
58
packages/whook-swagger-ui/src/handlers/__snapshots__/getOpenAPI.test.js.snap
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,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, | ||
}, | ||
} | ||
`; |
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,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, | ||
}; | ||
} |
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,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(); | ||
}); | ||
}); |