ding-res-msg

1.3.1 • Public • Published

ding-res-msg

Travis npm npm npm David David node

Response body formatter

Usage

Installation

npm i --save ding-res-msg

Quick Start

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 }); 
}

Examples

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

JSDoc

InputPayload

resMsg input arguments object define

Type: (Error | Object)

Parameters

  • payload (optional, default {})

Properties

  • error (Error | string) failed response error
  • data any success response data
  • code number? failed response error code
  • isPaging boolean? Whether to update the data object to msg
  • isProduction 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 happend
  • data any? success response data
  • code number? failed response error code
  • error string? failed response error

parseArguments

format input arguments

Parameters

Returns InputPayload formated payload

resMsg

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

Readme

Keywords

Package Sidebar

Install

npm i ding-res-msg

Weekly Downloads

6

Version

1.3.1

License

MIT

Last publish

Collaborators

  • playding