Skip to content

Commit

Permalink
feat(@whook/swagger-ui): Allow to specify a dev token
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Mar 3, 2019
1 parent 211c246 commit a2c6fcf
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/whook-authorization/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function handleWithAuthorization(
'debug',
'🔓 - Public endpoint detected, letting the call pass through!',
);
response = await handler(parameters, operation);
response = await handler({ parameters, authenticated: false }, operation);
} else {
const authorization = parameters.access_token
? `${DEFAULT_MECHANISM} ${parameters.access_token}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ Object {
],
"logInfoCalls": Array [
Array [
"💁 - Serving the API docs: http://localhost:9999/docs?url=http%3A%2F%2Flocalhost%3A9999%2Fv4%2FopenAPI",
"💁 - Serving the public API docs: http://localhost:9999/docs?url=http%3A%2F%2Flocalhost%3A9999%2Fv4%2FopenAPI",
],
Array [
"🎙️ - HTTP Server listening at \\"http://localhost:9999\\".",
Expand Down
2 changes: 2 additions & 0 deletions packages/whook-example/src/config/development/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import COMMON_CONFIG from '../common/config';

const CONFIG = {
...COMMON_CONFIG,
DEV_ACCESS_TOKEN: '1-admin',
DEFAULT_MECHANISM: 'fake',
};

export default CONFIG;
27 changes: 25 additions & 2 deletions packages/whook-example/src/handlers/getOpenAPI.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import getOpenAPI, {
definition,
definition as baseGetOpenAPIDefinition,
} from '@whook/swagger-ui/dist/handlers/getOpenAPI';

// TODO: Use WHOOK_PLUGINS to get handlers from plugins
// instead of proxying here
export { definition };

export const definition = {
...baseGetOpenAPIDefinition,
operation: {
...baseGetOpenAPIDefinition.operation,
parameters: [
...(baseGetOpenAPIDefinition.operation.parameters || []),
{
in: 'header',
name: 'authorization',
schema: {
type: 'string',
},
},
{
in: 'query',
name: 'access_token',
schema: {
type: 'string',
},
},
],
},
};
export default getOpenAPI;
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ Object {
"/openAPI": Object {
"get": Object {
"operationId": "getOpenAPI",
"parameters": Array [
Object {
"in": "header",
"name": "authorization",
"schema": Object {
"type": "string",
},
},
Object {
"in": "query",
"name": "access_token",
"schema": Object {
"type": "string",
},
},
],
"responses": Object {
"200": Object {
"content": Object {
Expand All @@ -260,7 +276,15 @@ Object {
},
"options": Object {
"operationId": "optionsWithCORS",
"parameters": Array [],
"parameters": Array [
Object {
"in": "query",
"name": "access_token",
"schema": Object {
"type": "string",
},
},
],
"responses": Object {
"200": Object {
"description": "CORS sent.",
Expand Down
10 changes: 8 additions & 2 deletions packages/whook-swagger-ui/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ Object {
],
"logInfoCalls": Array [
Array [
"💁 - Serving the API docs: http://localhost:8890/docs?url=http%3A%2F%2Flocalhost%3A8890%2Fv1%2FopenAPI",
"💁 - Serving the public API docs: http://localhost:8890/docs?url=http%3A%2F%2Flocalhost%3A8890%2Fv1%2FopenAPI",
],
Array [
"💁 - Serving the private API docs: http://localhost:8890/docs?url=http%3A%2F%2Flocalhost%3A8890%2Fv1%2FopenAPI%3Faccess_token%3Doudelali",
],
Array [
"🎙️ - HTTP Server listening at \\"http://localhost:8890\\".",
Expand Down Expand Up @@ -356,7 +359,10 @@ Object {
],
"logInfoCalls": Array [
Array [
"💁 - Serving the API docs: http://localhost:8888/docs?url=http%3A%2F%2Flocalhost%3A8888%2Fv1%2FopenAPI",
"💁 - Serving the public API docs: http://localhost:8888/docs?url=http%3A%2F%2Flocalhost%3A8888%2Fv1%2FopenAPI",
],
Array [
"💁 - Serving the private API docs: http://localhost:8888/docs?url=http%3A%2F%2Flocalhost%3A8888%2Fv1%2FopenAPI%3Faccess_token%3Doudelali",
],
Array [
"🎙️ - HTTP Server listening at \\"http://localhost:8888\\".",
Expand Down
5 changes: 2 additions & 3 deletions packages/whook-swagger-ui/src/handlers/getOpenAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ export const definition = {
},
};

async function getOpenAPI({ API }, { userId }) {
const authenticatedRequest = !!userId;
if (authenticatedRequest) {
async function getOpenAPI({ API }, { authenticated = false }) {
if (authenticated) {
return {
status: 200,
body: API,
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-swagger-ui/src/handlers/getOpenAPI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('getOpenAPI', () => {
API,
});
const response = await getOpenAPI({
userId: 1,
authenticated: true,
});

expect({
Expand Down
38 changes: 34 additions & 4 deletions packages/whook-swagger-ui/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { wrapInitializer, alsoInject } from 'knifecycle';
import swaggerDist from 'swagger-ui-dist';
import ecstatic from 'ecstatic';
import { definition as getOpenAPIDefinition } from './handlers/getOpenAPI';

/**
* Wraps the `httpRouter` initializer to also serve the
Expand All @@ -11,7 +12,15 @@ import ecstatic from 'ecstatic';
export default function wrapHTTPRouterWithSwaggerUI(initHTTPRouter) {
return wrapInitializer(
async (
{ DEBUG_NODE_ENVS, NODE_ENV, BASE_PATH, HOST, PORT, log = noop },
{
DEBUG_NODE_ENVS,
NODE_ENV,
DEV_ACCESS_TOKEN,
BASE_PATH,
HOST,
PORT,
log = noop,
},
httpRouter,
) => {
if (!DEBUG_NODE_ENVS.includes(NODE_ENV)) {
Expand All @@ -20,7 +29,9 @@ export default function wrapHTTPRouterWithSwaggerUI(initHTTPRouter) {

const localURL = `http://${HOST}:${PORT}`;
const swaggerUIURL = `${localURL}/docs`;
const publicSwaggerURL = `${localURL}${BASE_PATH || ''}/openAPI`;
const publicSwaggerURL = `${localURL}${BASE_PATH || ''}${
getOpenAPIDefinition.path
}`;
const staticRouter = ecstatic({
root: swaggerDist.absolutePath(),
showdir: false,
Expand All @@ -29,11 +40,22 @@ export default function wrapHTTPRouterWithSwaggerUI(initHTTPRouter) {

log(
'info',
`💁 - Serving the API docs: ${swaggerUIURL}?url=${encodeURIComponent(
`💁 - Serving the public API docs: ${swaggerUIURL}?url=${encodeURIComponent(
publicSwaggerURL,
)}`,
);

if (DEV_ACCESS_TOKEN) {
log(
'info',
`💁 - Serving the private API docs: ${swaggerUIURL}?url=${encodeURIComponent(
publicSwaggerURL +
'?access_token=' +
encodeURIComponent(DEV_ACCESS_TOKEN),
)}`,
);
}

return {
...httpRouter,
service: customHTTPRouter,
Expand All @@ -47,7 +69,15 @@ export default function wrapHTTPRouterWithSwaggerUI(initHTTPRouter) {
}
},
alsoInject(
['DEBUG_NODE_ENVS', 'NODE_ENV', 'BASE_PATH', 'HOST', 'PORT', '?log'],
[
'DEBUG_NODE_ENVS',
'NODE_ENV',
'?DEV_ACCESS_TOKEN',
'BASE_PATH',
'HOST',
'PORT',
'?log',
],
initHTTPRouter,
),
);
Expand Down
1 change: 1 addition & 0 deletions packages/whook-swagger-ui/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('wrapHTTPRouterWithSwaggerUI', () => {
$.register(constant('API', API));
$.register(constant('ENV', {}));
$.register(constant('NODE_ENV', 'test'));
$.register(constant('DEV_ACCESS_TOKEN', 'oudelali'));
$.register(constant('HOST', HOST));
$.register(constant('WRAPPERS', []));
$.register(
Expand Down

0 comments on commit a2c6fcf

Please sign in to comment.