npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ding-res-msg

v1.3.1

Published

response body formatter

Downloads

11

Readme

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