Response body formatter
npm i --save ding-res-msg
const resMsg = require('ding-res-msg');
// express
const controller = (req, res) => {
const data = 'hello world';
res.send(resMsg({ data }))
}
// koa2
const controller = (ctx, next) => {
const data = 'hello world';
ctx.body = resMsg({ data });
}
const resMsg = require('ding-res-msg');
console.log(resMsg());
// { success: true, data: undefined }
console.log(resMsg({
data: {
hello: 'world'
}
}));
// { success: true, data: { hello: 'world' } }
console.log(resMsg({ error: new Error('test') }));
// { success: false, error: 'test', code: 400 }
More examples can be found on jsdoc
resMsg input arguments object define
Parameters
payload
(optional, default{}
)
Properties
error
(Error | string) failed response errordata
any success response datacode
number? failed response error codeisPaging
boolean? Whether to update the data object to msgisProduction
boolean? Whether to add the stack to the msg, if true will not add
resMsg return object define
Type: Object
Parameters
payload
(optional, default{}
)
Properties
success
boolean? whether happenddata
any? success response datacode
number? failed response error codeerror
string? failed response error
format input arguments
Parameters
payload
InputPayload (optional, default{}
)
Returns InputPayload formated payload
res msg formattor
Parameters
payload
InputPayload input arguments or Error (optional, default{}
)
Examples
const resMsg = require('ding-res-msg');
console.log(resMsg());
// { success: true, data: undefined }
console.log(resMsg({ data: { total: 100 }, isPaging: true }));
// { success: true, total: 100 }
console.log(resMsg({ error: new Error('test') }));
// { success: false, error: 'test', code: 400 }
// Error field supports string error
console.log(resMsg({ error: 'test' }));
// { success: false, error: 'test', code: 400 }
// You can put the error directly in the first place
console.log(resMsg(new Error('test')));
// { success: false, error: 'test', code: 400 }
// Use error.code as msg.code
const error = new Error('test');
error.code = 503;
console.log(resMsg(error));
// { success: false, error: 'test', code: 503 }
// customised msg.code without error.code;
console.log(resMsg({ error: new Error('test'), code: 500 }));
// { success: false, error: 'test', code: 500 }
// NODE_ENV !== 'prod'
// You can get stack trace in the response body
// As long as you are not running in the production environment
console.log(resMsg(new Error('test')));
// { success: false, error: 'test', code: 400, stack: ['msg', '...'] }
// NODE_ENV !== 'prod'
// You cannot get stack trace in the response body
// event you are not running in a not prod environment
console.log(resMsg({ error: 'test', isProduction: true }));
// { success: false, error: 'test', code: 400 }
// NODE_ENV === 'prod'
// You can get stack trace in the response body
// event you are running in a prod environment
console.log(resMsg({ error: 'test', isProduction: false }));
// { success: false, error: 'test', code: 400, stack: ['msg', '...'] }
// use boom to create error
const boom = require('boom');
const error = boom.create(400)
console.log(resMsg(error));
// { success: false, error: 'Bad Request', code: 400, stack: ['Error', '...'] }
Returns Message formatted response msg body,
if is failed msg and error have code
or statusCode
msg.code would take that first