express-contracts
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -1,5 +0,6 @@ | ||
var middleware = require('./src/middleware'), | ||
var contracts = require('./src/contracts'), | ||
middleware = require('./src/middleware'), | ||
errors = require('./src/errors'), | ||
_ = require('underscore'); | ||
module.exports = _({}).extend(errors, middleware); | ||
module.exports = _({}).extend(contracts, errors, middleware); |
{ | ||
"name": "express-contracts", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Express.js plugin for checking request and response with rho-contracts", | ||
@@ -26,7 +26,7 @@ "license": "BSD-2-Clause", | ||
"peerDependencies": { | ||
"rho-contracts": ">=1.0.0" | ||
"rho-contracts": ">=1.2.0" | ||
}, | ||
"devDependencies": { | ||
"body-parser": "~1.9.3", | ||
"bodylabs-javascript-style": "~4.0.0", | ||
"bodylabs-javascript-style": "~5.0.0", | ||
"eslint": "~2.4.0", | ||
@@ -36,6 +36,6 @@ "express": "~4.10.3", | ||
"mocha": "^2.3.4", | ||
"rho-contracts": "git+https://github.com/bodylabs/rho-contracts.js.git#1.0.0", | ||
"should": "^8.1.1", | ||
"rho-contracts": "git+https://github.com/bodylabs/rho-contracts.js.git#1.2.0", | ||
"should": "^8.3.1", | ||
"supertest": "~0.15.0" | ||
} | ||
} |
@@ -1,3 +0,6 @@ | ||
var errors = require('./errors'); | ||
var c = require('rho-contracts'); | ||
var contracts = require('./contracts'), | ||
errors = require('./errors'); | ||
// Given a `requestContract` and a `responseBodyContract`, construct a | ||
@@ -22,2 +25,6 @@ // middleware that acts as a functional contract for the express endpoint. | ||
var useContractsOrError = function (requestContract, responseBodyContract) { | ||
return useContracts(requestContract, c.or(responseBodyContract, contracts.errorBody)); | ||
}; | ||
var extendWithCheckedJson = function (res, responseBodyContract, next) { | ||
@@ -53,2 +60,21 @@ res.checkedJson = function (payload) { | ||
module.exports.useContracts = useContracts; | ||
var createCheckedErrorHandler = function (context) { | ||
return function (err, req, res, next) { | ||
if (! err) { | ||
return next(); | ||
} | ||
if (err instanceof errors.ValidationError) { | ||
res.status(400).checkedJson({ error: err.message }); | ||
} else { | ||
context.logger.error(err); | ||
res.status(500).checkedJson({ error: 'Internal Server Error' }); | ||
} | ||
}; | ||
}; | ||
module.exports = { | ||
useContracts: useContracts, | ||
useContractsOrError: useContractsOrError, | ||
createCheckedErrorHandler: createCheckedErrorHandler, | ||
}; |
@@ -1,11 +0,28 @@ | ||
var impl = require('./middleware.impl'), | ||
c = require('rho-contracts'); | ||
var c = require('rho-contracts'); | ||
var cc = {}; | ||
cc.errorHandlerContext = c.object({ | ||
logger: c.object({ | ||
error: c.any, | ||
}), | ||
}).rename('errorHandlerContext'); | ||
cc.middleware = c.any.rename('middleware'); | ||
cc.useContracts = c.fun({ requestContract: c.contract }, { responseBodyContract: c.contract }) | ||
.returns(cc.middleware); | ||
var middlewareContracts = { | ||
useContracts: c.fun({ requestContract: c.contract }, { responseBodyContract: c.contract }) | ||
.returns(cc.middleware), | ||
module.exports.useContracts = cc.useContracts.wrap(impl.useContracts); | ||
// Reduce boilerplate for controllers of having to union with contracts.errorBody | ||
useContractsOrError: c.fun({ requestContract: c.contract }, { responseBodyContract: c.contract }) | ||
.returns(cc.middleware), | ||
// Construct an error-handling middleware that works with `useContracts` | ||
// and the `errorBody` custom contract. | ||
createCheckedErrorHandler: c.fun({ context: cc.errorHandlerContext }) | ||
.returns(cc.middleware), | ||
}; | ||
module.exports = c.wrapAll(require('./middleware.impl'), | ||
middlewareContracts); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14163
11
212
1