Comparing version 0.0.2 to 0.0.3
37
index.js
@@ -5,4 +5,2 @@ 'use strict'; | ||
const WebError = require('./web-error'); | ||
function ptry(f, self, args) { | ||
@@ -14,10 +12,10 @@ return new Promise(function(resolve) { | ||
function makeDefaultErrorHandler(serialize) { | ||
function defaultErrorHandler(req, error) { | ||
function createErrorHandler(serialize) { | ||
function errorHandler(req, error) { | ||
let message = 'Something went wrong', | ||
statusCode = 500; | ||
if (error instanceof WebError) { | ||
if (error.name === 'HTTPError' || error.name === 'WebError') { | ||
message = error.message; | ||
statusCode = error.statusCode; | ||
statusCode = error.statusCode || error.status || 500; | ||
} | ||
@@ -30,11 +28,12 @@ | ||
return defaultErrorHandler; | ||
return errorHandler; | ||
} | ||
const defaultErrorHandler = makeDefaultErrorHandler(json); | ||
function cass(options) { | ||
function cass(options, inner) { | ||
if (inner === undefined) { | ||
inner = options, options = {}; | ||
} | ||
options = options || {}; | ||
const serialize = options.serialize || json; | ||
const errorHandler = options.errorHandler || makeDefaultErrorHandler(serialize); | ||
const errorHandler = options.errorHandler || createErrorHandler(serialize); | ||
@@ -46,11 +45,9 @@ function resolveResponse(res) { | ||
function withHandler(inner) { | ||
return function (req) { | ||
return ptry(inner, this, arguments) | ||
.then(resolveResponse) | ||
.catch(errorHandler.bind(null, req)); | ||
}; | ||
function handler(req) { | ||
return ptry(inner, this, arguments) | ||
.then(resolveResponse) | ||
.catch(errorHandler.bind(null, req)); | ||
} | ||
return withHandler; | ||
return handler; | ||
} | ||
@@ -60,4 +57,2 @@ | ||
cass['default'] = cass; | ||
cass['defaultErrorHandler'] = defaultErrorHandler; | ||
cass['makeDefaultErrorHandler'] = makeDefaultErrorHandler; | ||
cass['WebError'] = WebError; | ||
cass['createErrorHandler'] = createErrorHandler; |
{ | ||
"name": "cass", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "It rhymes with jax-rs", | ||
@@ -18,3 +18,3 @@ "main": "index.js", | ||
"concat-stream": "^1.4.8", | ||
"quinn": "^3.2.0", | ||
"node-http-error": "^0.2.0", | ||
"tape": "^4.0.0" | ||
@@ -21,0 +21,0 @@ }, |
@@ -5,10 +5,12 @@ # Cass | ||
A convenience layer to build JSON APIs using [`Quinn`](https://github.com/quinnjs/quinn). | ||
A convenience layer to build JSON APIs using | ||
[`Quinn`](https://github.com/quinnjs/quinn). | ||
```js | ||
const cass = require('cass'), | ||
WebError = cass.WebError; | ||
// HTTPError is whitelisted for providing status codes | ||
HTTPError = require('node-http-error'); | ||
const handler = cass()(req => { | ||
if (req.method !== 'GET') throw new WebError(405); | ||
const handler = cass(req => { | ||
if (req.method !== 'GET') throw new HTTPError(405); | ||
@@ -55,4 +57,4 @@ return { | ||
// node style request listener -> node http server | ||
createServer(createApp(cass()(createRouter(routes)))) | ||
createServer(createApp(cass(createRouter(routes)))) | ||
.listen(3000); | ||
``` |
@@ -5,5 +5,5 @@ 'use strict'; | ||
const respond = require('quinn/respond'); | ||
const HTTPError = require('node-http-error'); | ||
const cass = require('../'), | ||
WebError = cass.WebError; | ||
const cass = require('../'); | ||
@@ -28,3 +28,3 @@ const testQuinnHandler = require('./test-quinn-handler'); | ||
testQuinnHandler(cass()(throwError)) | ||
testQuinnHandler(cass(throwError)) | ||
.then(verify, t.end); | ||
@@ -37,3 +37,3 @@ }); | ||
function throwError() { | ||
throw new WebError('Invalid thing', 422); | ||
throw new HTTPError(422, 'Invalid thing'); | ||
} | ||
@@ -50,3 +50,3 @@ | ||
testQuinnHandler(cass()(throwError)) | ||
testQuinnHandler(cass(throwError)) | ||
.then(verify, t.end); | ||
@@ -77,4 +77,4 @@ }); | ||
const req = { url: '/foo', method: 'GET' }; | ||
testQuinnHandler(cass({ errorHandler: customErrorHandler })(throwError), req) | ||
testQuinnHandler(cass({ errorHandler: customErrorHandler }, throwError), req) | ||
.then(verify, t.end); | ||
}); |
@@ -23,4 +23,4 @@ 'use strict'; | ||
testQuinnHandler(cass()(renderHelloWorld)) | ||
testQuinnHandler(cass(renderHelloWorld)) | ||
.then(verify, t.end); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
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
59
0
7375
9
140