{"id":16472103,"url":"https://github.com/yidinghan/ding-res-msg","last_synced_at":"2025-02-28T03:22:57.965Z","repository":{"id":83738903,"uuid":"91290691","full_name":"yidinghan/ding-res-msg","owner":"yidinghan","description":"response body formatter","archived":false,"fork":false,"pushed_at":"2018-05-31T01:45:50.000Z","size":152,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-10T23:40:33.001Z","etag":null,"topics":["express","formatter","koa","msg","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yidinghan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-15T03:17:19.000Z","updated_at":"2020-04-03T03:09:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"4b477292-f990-4633-b53e-86e1409d8bf4","html_url":"https://github.com/yidinghan/ding-res-msg","commit_stats":{"total_commits":53,"total_committers":1,"mean_commits":53.0,"dds":0.0,"last_synced_commit":"43632b65d0d48663f6e7b6f7abc0b3f5f6c0abaf"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fding-res-msg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fding-res-msg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fding-res-msg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fding-res-msg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidinghan","download_url":"https://codeload.github.com/yidinghan/ding-res-msg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241094300,"owners_count":19908648,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","formatter","koa","msg","nodejs"],"created_at":"2024-10-11T12:16:11.253Z","updated_at":"2025-02-28T03:22:57.930Z","avatar_url":"https://github.com/yidinghan.png","language":"JavaScript","readme":"# ding-res-msg\n\n[![Travis](https://img.shields.io/travis/yidinghan/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)\n[![npm](https://img.shields.io/npm/l/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)\n[![npm](https://img.shields.io/npm/v/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)\n[![npm](https://img.shields.io/npm/dm/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)\n[![David](https://img.shields.io/david/yidinghan/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)\n[![David](https://img.shields.io/david/dev/yidinghan/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)\n[![node](https://img.shields.io/node/v/ding-res-msg.svg?style=flat-square)](https://www.npmjs.com/package/ding-res-msg)\n\nResponse body formatter\n\n\u003c!-- TOC --\u003e\n\n-   [ding-res-msg](#ding-res-msg)\n-   [Usage](#usage)\n    -   [Installation](#installation)\n    -   [Quick Start](#quick-start)\n    -   [Examples](#examples)\n-   [JSDoc](#jsdoc)\n    -   [resMsg](#resmsg)\n\n\u003c!-- /TOC --\u003e\n\n# Usage\n\n## Installation\n\n```shell\nnpm i --save ding-res-msg\n```\n\n## Quick Start\n\n```js\nconst resMsg = require('ding-res-msg');\n\n// express\nconst controller = (req, res) =\u003e {\n    const data = 'hello world';\n    res.send(resMsg({ data }))\n}\n\n// koa2\nconst controller = (ctx, next) =\u003e {\n    const data = 'hello world';\n    ctx.body = resMsg({ data }); \n}\n```\n\n## Examples\n\n```js\nconst resMsg = require('ding-res-msg');\n\nconsole.log(resMsg());\n// { success: true, data: undefined }\n\nconsole.log(resMsg({\n    data: {\n        hello: 'world'\n    }\n}));\n// { success: true, data: { hello: 'world' } }\n\nconsole.log(resMsg({ error: new Error('test') }));\n// { success: false, error: 'test', code: 400 }\n```\n\nMore examples can be found on [jsdoc](#resmsg)\n\n# JSDoc\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n## InputPayload\n\nresMsg input arguments object define\n\nType: ([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))\n\n**Parameters**\n\n-   `payload`   (optional, default `{}`)\n\n**Properties**\n\n-   `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\n-   `data` **any** success response data\n-   `code` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** failed response error code\n-   `isPaging` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** Whether to update the data object to msg\n-   `isProduction` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** Whether to add the stack to the msg,\n                     if true will not add\n\n## Message\n\nresMsg return object define\n\nType: [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\n\n**Parameters**\n\n-   `payload`   (optional, default `{}`)\n\n**Properties**\n\n-   `success` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** whether happend\n-   `data` **any?** success response data\n-   `code` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** failed response error code\n-   `error` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** failed response error\n\n## parseArguments\n\nformat input arguments\n\n**Parameters**\n\n-   `payload` **[InputPayload](#inputpayload)**  (optional, default `{}`)\n\nReturns **[InputPayload](#inputpayload)** formated payload\n\n## resMsg\n\nres msg formattor\n\n**Parameters**\n\n-   `payload` **[InputPayload](#inputpayload)** input arguments or Error (optional, default `{}`)\n\n**Examples**\n\n```javascript\nconst resMsg = require('ding-res-msg');\nconsole.log(resMsg());\n// { success: true, data: undefined }\n\nconsole.log(resMsg({ data: { total: 100 }, isPaging: true }));\n// { success: true, total: 100 }\n\nconsole.log(resMsg({ error: new Error('test') }));\n// { success: false, error: 'test', code: 400 }\n\n// Error field supports string error\nconsole.log(resMsg({ error: 'test' }));\n// { success: false, error: 'test', code: 400 }\n\n// You can put the error directly in the first place\nconsole.log(resMsg(new Error('test')));\n// { success: false, error: 'test', code: 400 }\n\n// Use error.code as msg.code\nconst error = new Error('test');\nerror.code = 503;\nconsole.log(resMsg(error));\n// { success: false, error: 'test', code: 503 }\n\n// customised msg.code without error.code;\nconsole.log(resMsg({ error: new Error('test'), code: 500 }));\n// { success: false, error: 'test', code: 500 }\n\n// NODE_ENV !== 'prod'\n// You can get stack trace in the response body\n// As long as you are not running in the production environment\nconsole.log(resMsg(new Error('test')));\n// { success: false, error: 'test', code: 400, stack: ['msg', '...'] }\n\n// NODE_ENV !== 'prod'\n// You cannot get stack trace in the response body\n// event you are not running in a not prod environment\nconsole.log(resMsg({ error: 'test', isProduction: true }));\n// { success: false, error: 'test', code: 400 }\n\n// NODE_ENV === 'prod'\n// You can get stack trace in the response body\n// event you are running in a prod environment\nconsole.log(resMsg({ error: 'test', isProduction: false }));\n// { success: false, error: 'test', code: 400, stack: ['msg', '...'] }\n\n// use boom to create error\nconst boom = require('boom');\nconst error = boom.create(400)\nconsole.log(resMsg(error));\n// { success: false, error: 'Bad Request', code: 400, stack: ['Error', '...'] }\n```\n\nReturns **[Message](#message)** formatted response msg body,\n                 if is failed msg and error have `code` or `statusCode`\n                 msg.code would take that first\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidinghan%2Fding-res-msg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidinghan%2Fding-res-msg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidinghan%2Fding-res-msg/lists"}