Comparing version 2.13.0 to 2.14.0
@@ -60,2 +60,8 @@ import { Server as HttpServer, IncomingMessage, ServerResponse } from 'http' | ||
type ErrorHandler<P extends Protocol> = ( | ||
err: Error, | ||
req: Request<P>, | ||
res: Response<P>, | ||
) => void | Promise<unknown> | ||
interface RegisterRoute<P extends Protocol> { | ||
@@ -108,3 +114,3 @@ ( | ||
interface Router {} | ||
interface Router { } | ||
@@ -120,2 +126,3 @@ interface Options<P extends Protocol> { | ||
disableResponseEvent?: boolean | ||
errorHandler: ErrorHandler<P> | ||
} | ||
@@ -122,0 +129,0 @@ |
17
index.js
/** | ||
* restana Web Framework implemenation | ||
* restana Web Framework implementation | ||
* | ||
@@ -63,4 +63,4 @@ * @license MIT | ||
// routes registration shortcut factory | ||
const addRoute = (method) => (path, ...args) => { | ||
routeRegister(app, method, path, args) | ||
const addRoute = (methods) => (path, ...args) => { | ||
routeRegister(app, methods, path, args) | ||
@@ -71,2 +71,5 @@ // supporting method chaining for routes registration | ||
// error handler | ||
const errorHandler = options.errorHandler || ((err, req, res) => res.send(err)) | ||
// the "restana" service interface | ||
@@ -138,9 +141,9 @@ const app = { | ||
context: {}, | ||
handler: handlerCall(handler, ctx) // -> Function | ||
handler: handlerCall(handler, ctx, errorHandler) // -> Function | ||
} | ||
], req, res)() | ||
], req, res, errorHandler)() | ||
} else { | ||
// directly call the route handler only | ||
// NOTE: we do this to increase performance | ||
handlerCall(handler, ctx)(req, res) | ||
handlerCall(handler, ctx, errorHandler)(req, res) | ||
} | ||
@@ -176,3 +179,3 @@ }) | ||
} | ||
], req, res)() | ||
], req, res, errorHandler)() | ||
} else { | ||
@@ -179,0 +182,0 @@ // directly call the request router |
@@ -7,4 +7,5 @@ /** | ||
* @param {Object} res | ||
* @param {Function} errorHandler | ||
*/ | ||
const next = (middlewares, req, res) => { | ||
const next = (middlewares, req, res, errorHandler) => { | ||
// retrieve next middleware from chain | ||
@@ -20,9 +21,9 @@ const middleware = middlewares.shift() | ||
// invoke each middleware | ||
const result = middleware.handler.call(middleware.context, req, res, next(middlewares, req, res)) | ||
const result = middleware.handler.call(middleware.context, req, res, next(middlewares, req, res, errorHandler)) | ||
if (result instanceof Promise) { | ||
// async support | ||
result.catch(res.send) | ||
result.catch(err => errorHandler(err, req, res)) | ||
} | ||
} catch (err) { | ||
res.send(err) | ||
errorHandler(err, req, res) | ||
} | ||
@@ -29,0 +30,0 @@ } else if (!res.finished) { |
@@ -6,17 +6,17 @@ /** | ||
* @param {Object} ctx The request handler invokation context instance | ||
* @param {Function} errHandler The error handler function | ||
*/ | ||
module.exports = (handler, ctx) => (req, res) => { | ||
module.exports = (handler, ctx, errHandler) => async (req, res) => { | ||
try { | ||
const result = handler.call(ctx, req, res, ctx) | ||
// async support | ||
if (result instanceof Promise) { | ||
// async support | ||
result.then(data => { | ||
if (undefined !== data) { | ||
return res.send(data) | ||
} | ||
}).catch(res.send) | ||
const data = await result | ||
if (undefined !== data) { | ||
return res.send(data) | ||
} | ||
} | ||
} catch (err) { | ||
res.send(err) | ||
errHandler(err, req, res) | ||
} | ||
} |
{ | ||
"name": "restana", | ||
"version": "2.13.0", | ||
"version": "2.14.0", | ||
"description": "Super fast and minimalist web framework for building REST micro-services.", | ||
@@ -44,11 +44,12 @@ "main": "index.js", | ||
"chai": "^4.2.0", | ||
"express": "^4.16.4", | ||
"fastify": "^1.14.5", | ||
"connect-query": "^1.0.0", | ||
"express": "^4.17.0", | ||
"fastify": "^1.14.6", | ||
"hapi": "^17.8.4", | ||
"koa": "^2.7.0", | ||
"koa-router": "^7.4.0", | ||
"mocha": "^6.1.2", | ||
"mocha": "^6.1.4", | ||
"morgan": "^1.9.1", | ||
"muneem": "^2.4.5", | ||
"nyc": "^13.3.0", | ||
"nyc": "^14.1.1", | ||
"pem": "^1.14.2", | ||
@@ -55,0 +56,0 @@ "polka": "^0.4.0", |
@@ -22,6 +22,6 @@ # restana | ||
const service = require('restana')({ | ||
server: https.createServer({ | ||
key: keys.serviceKey, | ||
cert: keys.certificate | ||
}) | ||
server: https.createServer({ | ||
key: keys.serviceKey, | ||
cert: keys.certificate | ||
}) | ||
}) | ||
@@ -43,2 +43,3 @@ ``` | ||
- `disableResponseEvent`: If `TRUE`, there won't be `response` events triggered on the `res` object. Default value: `FALSE` | ||
- `errorHandler`: Optional global error handler function. Default value: `(err, req, res) => res.send(err)` | ||
@@ -73,2 +74,4 @@ ```js | ||
const bodyParser = require('body-parser') | ||
const service = require('restana')() | ||
service.use(bodyParser.json()) | ||
@@ -195,18 +198,15 @@ | ||
```js | ||
service.use((req, res, next) => { | ||
res.on('response', e => { | ||
if (e.code >= 400) { | ||
if (e.data && e.data.errClass) { | ||
console.log(e.data.errClass + ': ' + e.data.message) | ||
} else { | ||
console.log('error response, but not triggered by an Error instance') | ||
} | ||
} | ||
}) | ||
const service = require('restana')({ | ||
errorHandler (err, req, res) { | ||
console.log(`Something was wrong: ${err.message || err}`) | ||
res.send(err) | ||
} | ||
}) | ||
return next() | ||
service.get('/throw', (req, res) => { | ||
throw new Error('Upps!') | ||
}) | ||
``` | ||
Third party middlewares support: | ||
#### Third party middlewares support: | ||
> Almost all middlewares using the *function (req, res, next)* signature format should work, considering that no custom framework feature is used. | ||
@@ -213,0 +213,0 @@ |
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
27983
541
20