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

@clipmx/node.response

v2.1.6

Published

Standard Response module for Clip node applications.

Downloads

8

Readme

node.response

Introduction

This is a nodejs response module for Clip's node applications. This will standardize the errors and OK 2XX responses.

Install

npm install @clipmx/node.response --save

to use the example.js file for dev and testing npm install --only=dev

Setup

const Response = require(‘@clipmx/node.response’);
// -- middleware --
// Body Parser
// Routes here
// response must be used after the routes.. order matters
const responseConfig = {
    useLogger: true,
    logger,
};

const response = new Response(responseConfig);
app.use(response.responseHandler);

pass in your instance of logger into the responseConfig. You can also turn off the logger as well by setting useLogger:false

Endpoints

Endpoints for the example.js are in postman

https://documenter.getpostman.com/view/3824314/RVg59T3u

Usage

Use in your routes! See code below or see the example.js

Pass this object into next() to fully utilize the response module for error responses.

{
    error: {
        httpStatus: 400, // or httpResponseHint will work in the object
        code: 103,
        moreInfo: 'put more info here for the error thrown if needed',
        errorObj: errorObj // this is the actual error object
    }
}

Pass this object into next() to fully utilize the response module for OK 2XX responses. see /lib/httpOk

{ 
    httpStatus: 201,
    results: 'created'
}

or

{ 
    transactionId: 1111,
    merchant: 111,
    comment: 'hey these are my results'
}
  • set the httpStatus code: 3XX, 4XX, 5XX see example.js or the code below to see the status codes available

  • set the code: see the /lib/httpError to see which codes are available for the http status code.

  • set moreInfo: 'whatever additional information you feel is necessary'

  • If you pass in the errorObj the logger will log the file name, method, and line #.

const expressValidator = require('express-validator');
const { validationResult } = require('express-validator/check');
const { check } = require('express-validator/check');

const validator = [
    check('id').exists().withMessage('id is a required attribute').isMongoId()
    .withMessage('Must be MongoId'),
];

const validator2 = [
    check('id').exists().withMessage('id is a required attribute').isMongoId()
    .withMessage('Must be MongoId'),
    check('comment').exists().withMessage('comment is required'),
    check('number').exists().withMessage('number is required').isNumeric()
    .withMessage('must be a number'),
];

const validate = (req, res, next) => {
    const validationRes = validationResult(req);
    if (!validationRes.isEmpty()) {
        const errObj = { error: { httpStatus: 400 } };
        errObj.error.moreInfo = validationRes.mapped();
        next(errObj);
    } else {
        next();
    }
};

app.get('/test200', (req, res, next) => {
    next({ data: 'some data' });
});

app.get('/test201', (req, res, next) => {
    next({ httpStatus: 201, results: 'created' });
});

app.get('/test204', (req, res, next) => {
    next({ httpStatus: 204 });
});

app.get('/test400', (req, res, next) => {
    next({ error: { httpStatus: 400, code: 103, moreInfo: 'Bad Request We have an Error' } });
});

app.get('/test400ParamValidator/:id', validator, validate, (req, res, next) => {
    next({ error: { httpStatus: 400, moreInfo: 'Bad Request We have an Error' } });
});

app.post('/test400ParamValidator2', validator2, validate, (req, res, next) => {
    next({ httpStatus: 200, results: 'Success' });
});

app.get('/test401', (req, res, next) => {
    next({ error: { httpStatus: 401, code: 101, moreInfo: 'Unauthorized' } });
});

// using httpResponseHint also works :D
app.get('/test401', (req, res, next) => {
    next({ error: { httpResponseHint: 401, code: 101, moreInfo: 'Unauthorized' } });
});

app.get('/test403', (req, res, next) => {
    next({ error: { httpStatus: 403, code: 502, moreInfo: 'Forbidden' } });
});

app.get('/test404', (req, res, next) => {
    next({ error: { httpStatus: 404, code: 104, moreInfo: 'Not Found' } });
});

app.get('/test405', (req, res, next) => {
    next({ error: { httpStatus: 405, code: 503, moreInfo: 'Method not allowed' } });
});

app.get('/test409', (req, res, next) => {
    next({ error: { httpStatus: 409, code: 501, moreInfo: 'Conflict' } });
});

app.get('/testOtherError', (req, res, next) => {
    const email = req.user.email;
    next({ data: email });
});

app.get('/testOtherError2', (req, res, next) => {
     next(new Error('an Error was thrown'));
});

app.get('/test500', (req, res, next) => {
    const err = new Error('This is an internal Error.');
    next({ error: { httpStatus: 500, moreInfo: 'woa internal error', errorObj: err } });
});

app.get('/test503', (req, res, next) => {
    next({ error: { httpStatus: 503, code: 106, moreInfo: 'Service Unavailable' } });
});

Codes

Corresponding HTTP STATUS CODES and INTERNAL CODES

  • HTTP STATUS CODE - 400 has 103, 102, 200, 300, 500
  • HTTP STATUS CODE - 401 has 101, 111, 112, 113, 114
  • HTTP STATUS CODE - 403 has 502
  • HTTP STATUS CODE - 404 has 104, 404
  • HTTP STATUS CODE - 405 has 503
  • HTTP STATUS CODE - 409 has 501
  • HTTP STATUS CODE - 500 has 105
  • HTTP STATUS CODE - 503 has 106, 107