Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@amidoltd/shared-req-res-handler

Package Overview
Dependencies
Maintainers
5
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@amidoltd/shared-req-res-handler

web response/request model classes for router's in express like framework

  • 1.0.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
9
increased by50%
Maintainers
5
Weekly downloads
 
Created
Source

Shared Rest Request/Response parser

Can be used with any web framework like express, should be used to strip out any meta data in the request object, allowing for quick access to headers, body, params, query objects

installation

npm i @amidoltd/shared-req-res-handler

sample usage
full request object parsed
var RestModel = require('@amidoltd/shared-req-res-handler');
var SomeFoo = require('./someFoo');

/**
 * sample PUT request
 */
router.post('/:mandatoryPathParam/:optionalPathParam?', function (req, res) {
    this._req = req;
    // fullObject will parse the entire request and strip out all properties except body, headers, params, query
    var someFoo = new SomeFoo(RestModel.Reqst.fullObject(this._req));
    someFoo.methodRequiringFullRequestObject((e, d) => {
        if (e) {
            res.statusCode = 500;
            res.send(RestModel.Resp.errorResp(e, "ANSIBLE160x5"));  // TODO stringify error response on app side
            res.end();
        } else {
            res.statusCode = 200;
            res.send(RestModel.Resp.successResp(d));
            res.end();
        }
    });
});
params only
var RestModel = require('@amidoltd/shared-req-res-handler');
var SomeFoo = require('./someFoo');

/**
 * sample PUT request
 */
router.put('/:mandatoryPathParam/:optionalPathParam?', function (req, res) {
    this._req = req;
    // params will parse the params of request and strip out all other properties
    var someFoo = new SomeFoo(RestModel.Reqst.fullObject(this._req));
    someFoo.methodRequiringParamsOfTheRequestOnly((e, d) => {
        if (e) {
            res.statusCode = 500;
            res.send(RestModel.Resp.errorResp(e, "ANSIBLE160x5"));  // TODO stringify error response on app side
            res.end();
        } else {
            res.statusCode = 200;
            res.send(RestModel.Resp.successResp(d));
            res.end();
        }
    });
});
request details
var RestModel = require('@amidoltd/shared-req-res-handler');
var requestParsed = RestModel.Reqst.fullObject(this._req);

console.log(requestParsed);
{   
    body: {},
    params: {},
    headers: {},
    query: {}
}
Response Parser

This will create a unified response Model to be consumed in client apps

  • on success returning an object
{ 
    responseData: {}
}
  • on success returning an array
{ 
    responseData: [{}]
}
  • on success returning a string
{ 
    responseData: ""
}
  • on error returns an error object and a code to allow for
{ 
    responseData: {},
    code: ""
}

RELEASE NOTES
  • 1.0.2 * *
TODO
  • improve docs

FAQs

Package last updated on 22 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc