Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yidinghan/ding-res-msg
response body formatter
https://github.com/yidinghan/ding-res-msg
express formatter koa msg nodejs
Last synced: about 1 month ago
JSON representation
response body formatter
- Host: GitHub
- URL: https://github.com/yidinghan/ding-res-msg
- Owner: yidinghan
- License: mit
- Created: 2017-05-15T03:17:19.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-31T01:45:50.000Z (over 6 years ago)
- Last Synced: 2024-11-12T04:02:35.826Z (3 months ago)
- Topics: express, formatter, koa, msg, nodejs
- Language: JavaScript
- Size: 148 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ding-res-msg
[![Travis](https://img.shields.io/travis/yidinghan/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)
[![npm](https://img.shields.io/npm/l/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)
[![npm](https://img.shields.io/npm/v/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)
[![npm](https://img.shields.io/npm/dm/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)
[![David](https://img.shields.io/david/yidinghan/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)
[![David](https://img.shields.io/david/dev/yidinghan/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)
[![node](https://img.shields.io/node/v/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)Response body formatter
- [ding-res-msg](#ding-res-msg)
- [Usage](#usage)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Examples](#examples)
- [JSDoc](#jsdoc)
- [resMsg](#resmsg)# Usage
## Installation
```shell
npm i --save ding-res-msg
```## Quick Start
```js
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
```js
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)
# JSDoc
## InputPayload
resMsg input arguments object define
Type: ([Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))
**Parameters**
- `payload` (optional, default `{}`)
**Properties**
- `error` **([Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) \| [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String))** failed response error
- `data` **any** success response data
- `code` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** failed response error code
- `isPaging` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** Whether to update the data object to msg
- `isProduction` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** Whether to add the stack to the msg,
if true will not add## Message
resMsg return object define
Type: [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
**Parameters**
- `payload` (optional, default `{}`)
**Properties**
- `success` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** whether happend
- `data` **any?** success response data
- `code` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** failed response error code
- `error` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** failed response error## parseArguments
format input arguments
**Parameters**
- `payload` **[InputPayload](#inputpayload)** (optional, default `{}`)
Returns **[InputPayload](#inputpayload)** formated payload
## resMsg
res msg formattor
**Parameters**
- `payload` **[InputPayload](#inputpayload)** input arguments or Error (optional, default `{}`)
**Examples**
```javascript
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](#message)** formatted response msg body,
if is failed msg and error have `code` or `statusCode`
msg.code would take that first