express-json-rpc-router
Advanced tools
Comparing version 1.1.0 to 1.3.0
19
index.js
@@ -37,3 +37,3 @@ /*! | ||
*/ | ||
async function handleSingleReq(body) { | ||
async function handleSingleReq(body, raw) { | ||
const {id, method, jsonrpc, params} = body; | ||
@@ -45,7 +45,7 @@ try { | ||
if (beforeMethod = (config.beforeMethods[method])) await executeHook(beforeMethod, params); | ||
if (beforeMethod = (config.beforeMethods[method])) await executeHook(beforeMethod, params, null, raw); | ||
const result = await config.methods[method](params); | ||
const result = await config.methods[method](params, raw); | ||
if (afterMethod = (config.afterMethods[method])) await executeHook(afterMethod, params, result); | ||
if (afterMethod = (config.afterMethods[method])) await executeHook(afterMethod, params, result, raw); | ||
@@ -57,4 +57,5 @@ if (!isNil(id)) return {jsonrpc, result, id} | ||
code: Number(err.code || err.status || INTERNAL_ERROR.code), | ||
message: err.message || INTERNAL_ERROR.message | ||
message: err.message || INTERNAL_ERROR.message, | ||
}; | ||
if (err && err.data) error.data = err.data; | ||
return {jsonrpc, error, id: id || null} | ||
@@ -69,6 +70,6 @@ } | ||
*/ | ||
function handleBatchReq(bachBody) { | ||
function handleBatchReq(bachBody, raw) { | ||
return Promise.all( | ||
bachBody.reduce((memo, body) => { | ||
const result = handleSingleReq(body); | ||
const result = handleSingleReq(body, raw); | ||
if (!isNil(body.id)) memo.push(result); | ||
@@ -87,5 +88,5 @@ return memo | ||
if (Array.isArray(rpcData)) { | ||
res.send(await handleBatchReq(rpcData)) | ||
res.send(await handleBatchReq(rpcData, { req, res })) | ||
} else if (typeof rpcData === 'object') { | ||
res.send(await handleSingleReq(rpcData)) | ||
res.send(await handleSingleReq(rpcData, { req, res })) | ||
} else { | ||
@@ -92,0 +93,0 @@ next(new Error('JSON-RPC router error: req.body is required. Ensure that you install body-parser and apply it before json-router.')) |
@@ -106,8 +106,8 @@ const {INVALID_REQUEST, METHOD_NOT_FOUND} = require('./error-codes'); | ||
*/ | ||
exports.executeHook = (hook, params, result) => { | ||
if (this.isFunction(hook)) return hook(params, result); | ||
exports.executeHook = (hook, params, result, raw) => { | ||
if (this.isFunction(hook)) return hook(params, result, raw); | ||
else if (Array.isArray(hook)) return Promise.all( | ||
hook.map(h => h(params, result)) | ||
hook.map(h => h(params, result, raw)) | ||
); | ||
this.throwRpcErr('JSON-RPC error: wrong hook type passed') | ||
}; |
{ | ||
"name": "express-json-rpc-router", | ||
"description": "Json-rpc middleware router for express", | ||
"version": "1.1.0", | ||
"version": "1.3.0", | ||
"author": "Valerii Kuzivanov <valerii.kuzivanov@gmail.com>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -75,3 +75,4 @@ # JSON-RPC node.js implementation | ||
const controller = { | ||
testMethod({ username }) { | ||
// You have access to raw express req/res object as raw.res and raw.req | ||
testMethod({ username }, raw) { | ||
console.log('username: ', username) | ||
@@ -83,5 +84,8 @@ return ['example data 1', 'example data 2'] | ||
const beforeController = { | ||
testMethod() { | ||
// You have access to raw express req/res object as raw.res and raw.req | ||
testMethod(params, _, raw) { | ||
if (Math.random() >= 0.5) { // Random error | ||
throw new Error('something going wrong') | ||
const error = new Error('Something going wrong') | ||
error.data = { hello: 'world' } // its optional | ||
throw error | ||
} | ||
@@ -92,3 +96,6 @@ } | ||
const afterController = { | ||
testMethod: [() => console.log('testMethod executed 1!'), () => console.log('testMethod executed 2!')] | ||
testMethod: [ | ||
// You have access to result and to raw express req/res object as raw.res and raw.req. | ||
(params, result, raw) => console.log('testMethod executed 1!'), () => console.log('testMethod executed 2!') | ||
] | ||
} | ||
@@ -128,3 +135,4 @@ | ||
"code": -32603, | ||
"message": "something going wrong" | ||
"message": "Something going wrong", | ||
"data": { "hello": "world" } | ||
}, | ||
@@ -135,2 +143,9 @@ "id": 1 | ||
### Changelog: | ||
- v1.2.0 | ||
* Added raw { req, res } native express object to controller and hooks as last argument. | ||
* Passed next arguments: `params, null, raw` to beforeController actions and `params, result, raw` to afterController | ||
* Passed additional second argument `params, raw` to controller actions | ||
- v1.3.0 | ||
* Added optional err.data payload to comply with JSON RPC specification: "5.1 Error object". | ||
#### Options | ||
@@ -137,0 +152,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
12945
210
173