Skip to content

Commit

Permalink
Refactor and split code into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Schneider committed Sep 24, 2017
1 parent bec1205 commit 32bee5e
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/add-tags.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const swaggerCombine = require('../src/swagger-combine');
const swaggerCombine = require('../src');

const config = (module.exports = {
"swagger": "2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const swaggerCombine = require('../src/swagger-combine');
const swaggerCombine = require('../src');

const config = (module.exports = {
swagger: '2.0',
Expand Down
2 changes: 1 addition & 1 deletion examples/filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const swaggerCombine = require('../src/swagger-combine');
const swaggerCombine = require('../src');

const config = (module.exports = {
swagger: '2.0',
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const app = (module.exports = require('express')());
const swaggerCombine = require('../src/swagger-combine');
const swaggerCombine = require('../src');

const basicConfig = require('./basic');

Expand Down
2 changes: 1 addition & 1 deletion examples/rename.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const swaggerCombine = require('../src/swagger-combine');
const swaggerCombine = require('../src');

const config = (module.exports = {
swagger: '2.0',
Expand Down
2 changes: 1 addition & 1 deletion examples/security.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const swaggerCombine = require('../src/swagger-combine');
const swaggerCombine = require('../src');

const config = (module.exports = {
swagger: '2.0',
Expand Down
27 changes: 1 addition & 26 deletions src/swagger-combine.js → src/SwaggerCombine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const $RefParser = require('json-schema-ref-parser');
const SwaggerParser = require('swagger-parser');
const maybe = require('call-me-maybe');
const traverse = require('traverse');
const urlJoin = require('url-join');
const _ = require('lodash');
Expand Down Expand Up @@ -322,28 +321,4 @@ class SwaggerCombine {
}
}

function swaggerCombine(config = 'docs/swagger.json', opts, cb) {
if (_.isFunction(opts)) {
cb = opts;
opts = null;
}

return maybe(cb, new SwaggerCombine(config, opts).combineAndReturn());
}

swaggerCombine.middleware = (config, opts = {}) => (req, res, next) => {
new SwaggerCombine(config, opts)
.combine()
.then(sc => {
if (opts && (opts.format === 'yaml' || opts.format === 'yml')) {
return res.type('yaml').send(sc.toString());
}

res.json(sc.combinedSchema);
})
.catch(err => next(err));
};

swaggerCombine.SwaggerCombine = SwaggerCombine;

module.exports = swaggerCombine;
module.exports = SwaggerCombine;
19 changes: 19 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const _ = require('lodash');
const maybe = require('call-me-maybe');

const SwaggerCombine = require('./SwaggerCombine');
const middleware = require('./middleware');

function swaggerCombine(config = 'docs/swagger.json', opts, cb) {
if (_.isFunction(opts)) {
cb = opts;
opts = null;
}

return maybe(cb, new SwaggerCombine(config, opts).combineAndReturn());
}

swaggerCombine.SwaggerCombine = SwaggerCombine;
swaggerCombine.middleware = middleware;

module.exports = swaggerCombine;
16 changes: 16 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const SwaggerCombine = require('./SwaggerCombine');

module.exports = (config, opts = {}) => {
return function(req, res, next) {
new SwaggerCombine(config, opts)
.combine()
.then(sc => {
if (opts && (opts.format === 'yaml' || opts.format === 'yml')) {
return res.type('yaml').send(sc.toString());
}

res.json(sc.combinedSchema);
})
.catch(err => next(err));
};
};
2 changes: 1 addition & 1 deletion test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ chai.use(require('chai-somewhere'));
chai.use(require('chai-http'));

const expect = chai.expect;
const swaggerCombine = require('../src/swagger-combine');
const swaggerCombine = require('../src');
const pkg = require('../package.json');
const addTagsConfig = require('../examples/add-tags');
const basicConfig = require('../examples/basic');
Expand Down
2 changes: 1 addition & 1 deletion test/unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ chai.use(require('sinon-chai'));

const expect = chai.expect;

const swaggerCombine = require('../src/swagger-combine');
const swaggerCombine = require('../src');

const sandbox = sinon.sandbox.create();
let instance;
Expand Down

0 comments on commit 32bee5e

Please sign in to comment.