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

ISSUE-7 - fix middleware for inclusive paths #28

Merged
merged 2 commits into from
Jul 18, 2019
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
4 changes: 4 additions & 0 deletions .mocharc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
recursive: true
require:
- chai/register-expect
- ./test/globals.js
12 changes: 7 additions & 5 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
.idea/
*.iml
.nyc_output/
coverage/
test/
docs/
examples/
.nyc_output/
test/
.editorconfig
.eslintignore
.eslintrc.yaml
.npmignore
.nvmrc
.nycrc
README.hbs
.mocharc.yaml
.npmignore
.travis.yml
*.iml
6 changes: 0 additions & 6 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,5 @@ If you have node installed, `$ npm install` and you are ready to go.

Before submitting pull request, you can `$ npm run preversion`. It will:

* audit package.json dependencies (we use [nsp](https://nodesecurity.io/)),
* lint the code (we use [eslint](https://eslint.org/) and [@smartrecruiters/eslint-config](https://www.npmjs.com/package/@smartrecruiters/eslint-config) and [eslint-plugin-security](https://github.com/nodesecurity/eslint-plugin-security)),
* test the code and check coverage (we use [mocha](https://mochajs.org/), [chai](http://www.chaijs.com/api/bdd/), [sinon](http://sinonjs.org/) and [istanbul](https://istanbul.js.org/)),
* regenerate README.md (we use [jsdoc-to-markdown](https://www.npmjs.com/package/jsdoc-to-markdown)).

## Fixing README.md

Project's readme is generated using [docs/docs.hbs](docs/docs.hbs), so please modify this file.
181 changes: 0 additions & 181 deletions docs/docs.hbs

This file was deleted.

14 changes: 14 additions & 0 deletions lib/middleware-once-per-req.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const uuid = require('uuid')

module.exports = () => {
const middlewareId = uuid()

return middleware => (req, res, next) => {
req._openApiFirst = req._openApiFirst || {usedMiddlewares: []}
if (req._openApiFirst.usedMiddlewares.includes(middlewareId)) {
return next()
}
req._openApiFirst.usedMiddlewares.push(middlewareId)
middleware(req, res, next)
}
}
8 changes: 5 additions & 3 deletions openapi/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {forOwn, cloneDeep, unionBy} = require('lodash')

const pathUtils = require('./path')
const httpMethods = require('./http-methods')
const {uniquenessCriterion} = require('./parameters')

const middlewareOncePerReq = require('../lib/middleware-once-per-req')
const refResolver = require('../lib/utils/ref-resolver')

module.exports = function (app, spec) {
Expand All @@ -12,12 +12,14 @@ module.exports = function (app, spec) {

return {
use: openApiMiddleware => {
const oncePerReq = middlewareOncePerReq()

forOwn(spec.paths, (pathItem, path) => {

const defaultParams = pathItem.parameters && pathItem.parameters.map(resolveRef)

forOwn(pathItem, (value, key) => {
if (!httpMethods.is(key)) {
if (!value.operationId) {
return
}

Expand All @@ -31,7 +33,7 @@ module.exports = function (app, spec) {

const middleware = openApiMiddleware(operation, {spec})
if (middleware) {
app[key](pathUtils.toRoutePath(path), middleware)
app[key](pathUtils.toRoutePath(path), oncePerReq(middleware))
}
})
})
Expand Down
3 changes: 0 additions & 3 deletions openapi/http-methods.js

This file was deleted.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
"node": ">=6.0.0"
},
"dependencies": {
"lodash": "4.17.11",
"type-is": "1.6.18"
"lodash": "4.17.14",
"type-is": "1.6.18",
"uuid": "3.3.2"
},
"devDependencies": {
"@smartrecruiters/eslint-config": "4.3.0",
"chai": "4.2.0",
"eslint": "5.16.0",
"eslint-plugin-security": "1.4.0",
"express": "4.17.1",
"jsdoc-to-markdown": "5.0.0",
"mocha": "6.1.4",
"nyc": "14.1.1",
"sinon": "7.3.2"
Expand All @@ -30,7 +30,6 @@
"lint": "eslint .",
"test": "mocha",
"test:coverage": "nyc mocha",
"readme": "jsdoc2md --template docs/docs.hbs middlewares/**/*.js openapi/*.js > README.md",
"preversion": "npm run lint && npm run test:coverage && npm run readme"
"preversion": "npm run lint && npm run test:coverage"
}
}
20 changes: 13 additions & 7 deletions test/app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ describe('api', () => {
{in: 'query', name: 'p'}
],
get: {
operationId: 'a',
requestBody: {},
parameters: [{in: 'query', name: 'p'}]
}
},
'/b': {
parameters: [],
get: {},
post: {}
get: {
operationId: 'b1'
},
post: {
operationId: 'b2'
}
},
requestBody: {}
},
Expand All @@ -25,16 +30,17 @@ describe('api', () => {
it('should add middleware to app', () => {
// given
const app = {get: sinon.spy(), post: sinon.spy()}
const middlewareA = () => 'A'
const middlewareB = () => undefined
const middlewareA = sinon.fake.returns('A')
const middlewareB = sinon.fake()

// when
openApi(app, spec).use(middlewareA)
openApi(app, spec).use(middlewareB)

// then
sinon.assert.calledWith(app.get, '/a', middlewareA())
sinon.assert.calledWith(app.get, '/b', middlewareA())
sinon.assert.calledWith(app.post, '/b', middlewareA())
sinon.assert.calledThrice(middlewareA)
sinon.assert.calledWithMatch(app.get, '/a', sinon.match.func)
sinon.assert.calledWithMatch(app.get, '/b', sinon.match.func)
sinon.assert.calledWithMatch(app.post, '/b', sinon.match.func)
})
})
3 changes: 0 additions & 3 deletions test/globals.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const chai = require('chai')
const sinon = require('sinon')

global.chai = chai
global.expect = chai.expect
global.sinon = sinon
21 changes: 21 additions & 0 deletions test/lib/middleware-once-per-path.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const MiddlewareOncePerReq = require('../../lib/middleware-once-per-req')

describe('openapi middleware', () => {

it('should be run once for the same request', () => {
// given
const middleware = sinon.spy()
const next = sinon.spy()
const req = {}

// when
const wrappedMiddleware = MiddlewareOncePerReq()(middleware)
wrappedMiddleware(req, undefined, next)
wrappedMiddleware(req, undefined, next)

// then
sinon.assert.callOrder(middleware, next)
sinon.assert.calledOnce(middleware)
sinon.assert.calledOnce(next)
})
})
3 changes: 0 additions & 3 deletions test/mocha.opts

This file was deleted.