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

Fix/multiple event instances #171

Merged
merged 6 commits into from
Aug 2, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/runTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run lint
- run: npm run lint -- --fix
- run: npm test
coverage:
name: coverage
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const options = {
// you can extend swagger-ui-express config. You can checkout an example of this
// in the `example/configuration/swaggerOptions.js`
swaggerUiOptions: {},
// multiple option in case you want more that one instance
multiple: true,
};

const app = express();
Expand Down
6 changes: 6 additions & 0 deletions config/swaggerEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

const swaggerEvents = options => ({
multiple: options.multiple !== undefined ? options.multiple : false,
});

module.exports = swaggerEvents;
14 changes: 12 additions & 2 deletions examples/configuration/multipleInstance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const optionsClientAPIInstance = {
exposeSwaggerUI: true,
exposeApiDocs: true,
apiDocsPath: '/api/v1/client/api-docs',
multiple: true,
};

// It is a fictitious configuration
Expand All @@ -41,10 +42,19 @@ const optionsAdminAPIInstance = {
exposeSwaggerUI: true,
exposeApiDocs: true,
apiDocsPath: '/api/v1/admin/api-docs',
multiple: true,
};

expressJSDocSwagger(app)(optionsClientAPIInstance);
expressJSDocSwagger(app)(optionsAdminAPIInstance);
const instance = expressJSDocSwagger(app)(optionsClientAPIInstance);
const instance2 = expressJSDocSwagger(app)(optionsAdminAPIInstance);

instance.on('finish', data => {
console.log(data);
})

instance2.on('finish', data => {
console.log(data);
})


app.listen(port, () => logger.info(`Example app listening at http://localhost:${port}`));
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const swaggerUi = require('swagger-ui-express');
const merge = require('merge');

const defaultOptions = require('./config/default');
const swaggerEventsOptions = require('./config/swaggerEvents');
const processSwagger = require('./processSwagger');
const swaggerEvents = require('./swaggerEvents');


const expressJSDocSwagger = app => (userOptions = {}, userSwagger = {}) => {
const events = swaggerEvents();
const events = swaggerEvents(swaggerEventsOptions(userOptions));
const { instance } = events;
let swaggerObject = {};

Expand Down
4 changes: 2 additions & 2 deletions swaggerEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const finish = eventEmitter => (info, methods) => (
eventEmitter.emit(FINISH_EVENT_NAME, info, methods)
);

const swaggerEvents = () => {
if (api) return api;
const swaggerEvents = ({ multiple } = {}) => {
if (api && !multiple) return api;
const instance = new EventEmitter();

api = {
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/multipleInstance/admin-v1-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* GET /api/admin/v1
* @summary This is the summary of the endpoint
* @return {string} 200 - success response
*/
5 changes: 5 additions & 0 deletions test/e2e/multipleInstance/client-v1-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* GET /api/client/v1
* @summary This is the summary of the endpoint
* @return {string} 200 - success response
*/
13 changes: 13 additions & 0 deletions test/e2e/multipleInstance/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require('express');
const expressJSDocSwagger = require('../../..');

const app = express();

const setupInstance = options => new Promise(resolve => {
const instance = expressJSDocSwagger(app)(options);
instance.on('finish', data => {
resolve(data);
});
});

module.exports = setupInstance;
50 changes: 50 additions & 0 deletions test/e2e/multipleInstance/multipleInstance.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const multipleInstance = require('.');

const instance1Options = {
info: {
version: '1.0.0',
title: 'Admin API',
description: 'Only admin accounts authorized to use this API',
},
security: {
BearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
filesPattern: './admin-v1-docs.js',
swaggerUIPath: '/api/v1/admin/docs',
baseDir: __dirname,
exposeSwaggerUI: true,
exposeApiDocs: true,
apiDocsPath: '/api/v1/admin/api-docs',
};

const instance2Options = {
info: {
version: '1.0.0',
title: 'Client API',
description: 'For client',
},
filesPattern: './client-v1-docs.js',
swaggerUIPath: '/api/v1/client/docs',
baseDir: __dirname,
exposeSwaggerUI: true,
exposeApiDocs: true,
apiDocsPath: '/api/v1/client/api-docs',
};

describe('multipleInstance test', () => {
it('multipleInstance should be different when multiple option is "true"', async () => {
const instance1Data = await multipleInstance({
...instance1Options,
multiple: true,
});
const instance2Data = await multipleInstance({
...instance2Options,
multiple: true,
});
expect(instance1Data).not.toEqual(instance2Data);
});
});