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

customize-response-appender

v1.9.9

Published

append custom methods on res

Downloads

38

Readme

Customize Response Appender

Features

A module that Appends custom response methods on res object of the express route:

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Recommended Use latest stable version of Node.js.

Installation is done using the npm install command:

$ npm install customize-response-appender

Getting Started

After Installing module You need to do the following steps:

  • Create a file named responses-config.js.
  • Require the module and call it by passing a Directory path where responses-config.js exist.
  • After requiring, the module will return you a function.
  • Call that function as a middleware.
const customResponseMethodAppender = require('customize-response-appender')({
    reponsesConfigFilePath: '/config/responses-config.js',
});

const app = express();

app.all('*', customResponseMethodAppender);

Note:

  • You can pass (reponsesConfigFilePath) as you want it is just for explanation
  • Example of file responses-config.js

How responses-config.js File look like

module.exports = {
    '200, 400': {
        response: {
            defaults: {
                string: {
                    field: 'message',
                    defaultValue: 'success',
                },
                object: {
                    field: 'data',
                    defaultValue: {},
                    hideIfNotExist: true,
                },
                array: {
                    field: 'dataItems',
                    defaultValue: [],
                }
            },
        },
        callback: (response = {}, argument = {}) => { }
    },
    500: {
        response: {
            defaults: {
                string: {
                    field: 'error',
                    defaultValue: 'Default Error',
                },
                object: {
                    field: 'error',
                    defaultValue: {},
                    hideIfNotExist: true,
                },
                array: {
                    field: 'errors',
                    defaultValue: [],
                }
            },
        },
        callback: (response = {}, argument = {}) => { }
    },
}

Description of responses-config.js File

'200, 400'

So as you can see I wrote these two values 200 & 400 in a string with a comma-separated them, it means that I am asking this module to make 2 functions with status code 200 & 400.

Note:

Response appender will append 2 functions on your res object in a route with the name

  • res.http200()
  • res.http400()

'response, callback'

Here you can see there are 2 fields defined in the object under the heading I described above.

response

So what response does is it define the structure of your response in res.http200 or other defined method. So there is a key name defaults, which I discuss later on. But first, let me tell you that whatever key you define outside the defaults key will be sent in the response as it is written.

Ok now we are going towards fields I wrote in defaults.

Defaults
  • string
  • number
  • object
  • array

Actually, these are the type of data you pass in res.http200 or other defined method and define how these methods modify your response according to the field define in it.

Example

As you can see there are two fields in the string field of defaults.

  • field
  • defaultValue

what field is for is it will make a key with the name given to it in the response object. For Example:

    res.http200('Everthing seems good to me');
    res.http200(); // if nothing passed default value be setted automatically.

the response will be send is

{
 message: 'Everthing seems good to me',
 data:{},
 dataItems:[]
}

// Example of default value
{
 message: 'success',
 data:{},
 dataItems:[] 
}

Note

There are one more keys to discuss:

  • hideIfNotExist

hideIfNotExist

hideIfNotExist will hide the respective key if you are not sending it. For example, in the above example, you are passing only the string in res.http200 method. but the response appender will append data & dataItems key in the response too. So if you want to send only the key with respective type so you have to add hideIfNotExist key.

Parameters of custom methods like res.http200(param1,param2)

As we discussed above the first parameter, will be mapped according to the type of value you send. so I am going to tell you about the param2, actually, there is a case where we want to add some additional data at the root level of our response object so we can add here.

Example

    res.http200('Everthing seems good to me', { statusCode: 200 });

the response will be send is

{
 statusCode: 200,
 message: 'Everthing seems good to me',
 data:{},
 dataItems:[]
}

Module for

Author

The original author of Customize Responses Appender is Sajjad Ali

Contributors

  • Taimoor Ali - JS Full Stack Engineer