Socket
Socket
Sign inDemoInstall

custom-rest-templater

Package Overview
Dependencies
1
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    custom-rest-templater

Simple module for generating custom but uniform responses for REST Requests for NodeJS for avoiding the need to repeat the status code in all the response across the project


Version published
Maintainers
1
Created

Readme

Source

Custom REST Templater for NodeJS

Simple module for generating custom but uniform responses for REST Requests for NodeJS for avoiding the need to repeat the status code in all the response across the project

1. Installation

Using npm:

$ npm install custom-rest-templater

2. Configuration

var customRestTemplater = require('custom-rest-templater');
// all *Status* properties should be a valid Http Status code
customRestTemplater.options(Object);

The options method receives an object with the following properties (all are optional):

PropertyTypeDefault ValueDescription
defaultStatusSuccessInteger200Status code for a successful response
defaultStatusFailureInteger400Status code for a non successful response
getShouldHaveBodyBooleantrueReview if the object sent is empty for generating a successful response for a GET request
getStatusSuccessInteger200Status code for a successful response for a GET request
getStatusFailureInteger404Status code for a non successful response for a GET request
postShouldHaveBodyBooleantrueReview if the object sent is empty for generating a successful response for a POST request
postStatusSuccessInteger201Status code for a successful response for a POST request
postStatusFailureInteger400Status code for a non successful response for a POST request
putShouldHaveBodyBooleantrueReview if the object sent is empty for generating a successful response for a PUT request
putStatusSuccessInteger200Status code for a successful response for a PUT request
putStatusFailureInteger400Status code for a non successful response for a PUT request
patchShouldHaveBodyBooleantrueReview if the object sent is empty for generating a successful response for a PATCH request
patchStatusSuccessInteger200Status code for a successful response for a PATCH request
patchStatusFailureInteger400Status code for a non successful response for a PATCH request
deleteShouldHaveBodyBooleantrueReview if the object sent is empty for generating a successful response for a DELETE request
deleteStatusSuccessInteger200Status code for a successful response for a DELETE request
deleteStatusFailureInteger400Status code for a non successful response for a DELETE request
templateFunctionnullCallback for generating the response body, if not set then it sends the object used in the customRest/customRestSuccess/customRestFailure method

The template function receives three arguments:

PropertyTypeDescription
objectObjectThe object that is been sent as the response body
defaultDataObjectAn object with status and message properties corresponding to the HTTP Status Code and its Description
responseArgsObjectAdditional arguments sent as the second parameter on the customRest method

It should return an object for the response body and it can have any format.

Example

var customRestTemplater = require('custom-rest-templater');
customRestTemplater.options({
  patchShouldHaveBody: false, // patch method may not have a body for a successful response
  putShouldHaveBody: false, // put method may not have a body for a successful response
  deleteShouldHaveBody: false, // delete method may not have a body for a successful response
  template: (object, defaultData, responseArgs) => {
    return {
      data: object,
      additionalData: responseArgs
    };
  }
});

3. Methods added to the Response Object

MethodDescription
customRestcreates the response and tests if the object is empty for sending a successful or not status code
customRestSuccesscreates the response and sends a successful status code
customRestSuccesscreates the response and sends a non successful status code

All the methods have the following parameters

ParameterTypeDefault ValueRequiredDescription
responseBodyObjectnulltrueThe object to be sent as the response body (the one to be tested if is empty)
responseArgsObject{}falseAdditional params to be sent in the response, here we could modify the response status code by sending a custom status key

4. Usage Examples

Sample template using the defaultData sent

var customRestTemplater = require('custom-rest-templater');
customRestTemplater.options({
  template: (object, defaultData, responseArgs) => {
    return {
      data: defaultData.status < 400 ? object : null,
      success: defaultData.status < 400,
      message: responseArgs.hasOwnProperty('message') ?
               responseArgs.message :
               defaultData.message
    };
  }
});

GET Request

If a non empty object is sent

res.customRest({name: 'myName'});

Response

status: 200
body: {"data":{"name":"myName"},"success":true,"message":"Ok"}

If an empty object or "nothing" is sent

res.customRest({});

Response

status: 404
body: {"data":{},"success":false,"message":"Not found"}

POST Request

If a non empty object is sent

res.customRest({name: 'myName'});

Response

status: 201
body: {"data":{"name":"myName"},"success":true,"message":"Created"}

If an empty object or "nothing" is sent

res.customRest({});

Response

status: 400
body: {"data":{},"success":false,"message":"Bad request"}

PUT, PATCH, DELETE Request

If a non empty object is sent

res.customRest({name: 'myName'});

Response

status: 200
body: {"data":{"name":"myName"},"success":true,"message":"Ok"}

If an empty object or "nothing" is sent

res.customRest({});

Response

status: 400
body: {"data":{},"success":false,"message":"Bad request"}

Custom status sent

If a non empty object is sent

res.customRest({name: 'myName'}, {status: 202});

Response

status: 202
body: {"data":{"name":"myName"},"success":true,"message":"Accepted"}

Successful Response on empty object for a GET Request

If a non empty object is sent

res.customRestSuccess({});

Response

status: 200
body: {"data":{},"success":true,"message":"Ok"}

Non Successful Response on non empty object for a GET Request

If a non empty object is sent

res.customRestFailure({name: 'myName'});

Response

status: 404
body: {"data":null,"success":true,"message":"Not Found"}

Here data is sent as null because of the template function

Non Successful Response on non empty object for a GET Request with custom status

If a non empty object is sent

res.customRestFailure({name: 'myName'}, {status: 400});

Response

status: 400
body: {"data":null,"success":true,"message":"Bad request"}

Here data is sent as null because of the template function

5. Contact

You can contact me at felix.carreno@gmail.com

Keywords

FAQs

Last updated on 18 Aug 2017

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc