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

response-format

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

response-format

Module which will provide HTTP-friendly error objects

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
189
decreased by-20.92%
Maintainers
1
Weekly downloads
 
Created
Source

Response Format

Module which provides HTTP-friendly error objects

Author: Gaurav Joshi

Start

const Format = require('response-format');

Each utility returns an Object which includes the following properties:

  • error - true, false or null (if not specified).
  • statusCode - the HTTP status code.
  • message - string containing message (if not specified will return a pre-defined set of messages according to status code )
  • data - response payload (null in case of blank or error)

Methods

create(statusCode, [error, message, data])

Generates a decorated response object where:

  • statusCode - an HTTP error code number.
  • message - optional message string.
  • data - response payload.
  • error - true, false or null (if not specified).

success([message, data])

Returns a 200 Success where:

  • statusCode - 200
  • error - false
  • message - optional message.
  • data - optional payload.
Format.success();

Generates the following response payload:

{
    "statusCode": 200,
    "error": false,
    "message": "OK",
    "data": null
}

badRequest([message, data])

Returns a 400 Error where:

  • statusCode - 400
  • error - true
  • message - optional message.
  • data - optional payload.
Format.badRequest();

Generates the following response payload:

{
    "statusCode": 400,
    "error": true,
    "message": "Bad Request",
    "data": null
}

unAuth­orized([message, data])

Returns a 402 Error where:

  • statusCode - 402
  • error - true
  • message - optional message.
  • data - optional payload.
Format.unAuth­orized();

Generates the following response payload:

{
    "statusCode": 402,
    "error": true,
    "message": "Unauth­orized",
    "data": null
}

forbidden([message, data])

Returns a 403 Error where:

  • statusCode - 403
  • error - true
  • message - optional message.
  • data - optional payload.
Format.forbidden();

Generates the following response payload:

{
    "statusCode": 403,
    "error": true,
    "message": "Forbidden",
    "data": null
}

notFound([message, data])

Returns a 404 Error where:

  • statusCode - 404
  • error - true
  • message - optional message.
  • data - optional payload.
Format.notFound();

Generates the following response payload:

{
    "statusCode": 404,
    "error": true,
    "message": "Not Found",
    "data": null
}

notAllowed([message, data])

Returns a 405 Error where:

  • statusCode - 405
  • error - true
  • message - optional message.
  • data - optional payload.
Format.notAllowed();

Generates the following response payload:

{
    "statusCode": 405,
    "error": true,
    "message": "Method Not Allowed",
    "data": null
}

requestTimeout([message, data])

Returns a 408 Error where:

  • statusCode - 408
  • error - true
  • message - optional message.
  • data - optional payload.
Format.requestTimeout();

Generates the following response payload:

{
    "statusCode": 408,
    "error": true,
    "message": "Request Timeout",
    "data": null
}

internalError([message, data])

Returns a 500 Error where:

  • statusCode - 500
  • error - true
  • message - optional message.
  • data - optional payload.
Format.internalError();

Generates the following response payload:

{
    "statusCode": 500,
    "error": true,
    "message": "Internal Server Error",
    "data": null
}

badGateway([message, data])

Returns a 502 Error where:

  • statusCode - 502
  • error - true
  • message - optional message.
  • data - optional payload.
Format.badGateway();

Generates the following response payload:

{
    "statusCode": 502,
    "error": true,
    "message": "Bad Gateway",
    "data": null
}

unavailable([message, data])

Returns a 503 Error where:

  • statusCode - 503
  • error - true
  • message - optional message.
  • data - optional payload.
Format.unavailable();

Generates the following response payload:

{
    "statusCode": 503,
    "error": true,
    "message": "Service Unavai­lable",
    "data": null
}

gatewayTimeout([message, data])

Returns a 504 Error where:

  • statusCode - 504
  • error - true
  • message - optional message.
  • data - optional payload.
Format.gatewayTimeout();

Generates the following response payload:

{
    "statusCode": 504,
    "error": true,
    "message": "Gateway Timeout",
    "data": null
}

Keywords

FAQs

Package last updated on 14 Mar 2019

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