ding-res-msg
Response body formatter
Usage
Installation
npm i --save ding-res-msg
Quick Start
const resMsg = ; // expressconst controller = { const data = 'hello world'; res} // koa2const controller = { const data = 'hello world'; ctxbody = ; }
Examples
const resMsg = ; console;// { success: true, data: undefined } console;// { success: true, data: { hello: 'world' } } console;// { success: false, error: 'test', code: 400 }
More examples can be found on jsdoc
JSDoc
InputPayload
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
Message
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
parseArguments
format input arguments
Parameters
payload
InputPayload (optional, default{}
)
Returns InputPayload formated payload
resMsg
res msg formattor
Parameters
payload
InputPayload input arguments or Error (optional, default{}
)
Examples
const resMsg = ;console;// { success: true, data: undefined } console;// { success: true, total: 100 } console;// { success: false, error: 'test', code: 400 } // Error field supports string errorconsole;// { success: false, error: 'test', code: 400 } // You can put the error directly in the first placeconsole;// { success: false, error: 'test', code: 400 } // Use error.code as msg.codeconst error = 'test';errorcode = 503;console;// { success: false, error: 'test', code: 503 } // customised msg.code without error.code;console;// { 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 environmentconsole;// { 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 environmentconsole;// { 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 environmentconsole;// { success: false, error: 'test', code: 400, stack: ['msg', '...'] } // use boom to create errorconst boom = ;const error = boomconsole;// { 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