Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
182 lines (152 sloc) 5.2 KB

hsc

Build Status    npm version  npm version   
NPM

Comprehensive HTTP Status Code Information in Node.js Made Easy!

var hsc = require('hsc');

hsc.code(404, function(information) {
  console.log(information.summary);
});
requested resource could not be found

hsc is an npm package that aims to simplify the retrieval of HTTP status code information. hsc relies on the wonderful httpstatus.es API.

Practical uses of hsc include:

  • Translating HTTP status codes into something users can understand.

hsc is simple to install and even more simple to use.

Installation

hsc is an npm module, so simply run:

npm install hsc

or, if you want to add hsc as a dependency to your package.json file, run:

npm install hsc --save

Usage

Standard report with bare-bones information.

hsc.code(200, function(information) {
  console.log(information);
});
{
  "code": "200",
  "title": "OK",
  "summary": "standard response for successful HTTP requests",
  "status": "success"
}

Report with descriptions, but no references.

hsc.code(200, function(information) {
  console.log(information);
}, true);

hsc.code(200, function(information) {
  console.log(information);
}, true, false);
{
  "code": "200",
  "title": "OK",
  "summary": "standard response for successful HTTP requests",
  "descriptions": {
    "wikipedia": {
      "body": "Standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request the response will contain an entity describing or containing the result of the action.",
      "link": "http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#200"
    },
    "ietf": {
      "body": "The request has succeeded. The information returned with the response is dependent on the method used in the request, for example: GET an entity corresponding to the requested resource is sent in the response; HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body; POST an entity describing or containing the result of the action;",
      "link": "http://www.ietf.org/rfc/rfc2616.txt"
    }
  },
  "status": "success"
}

Report with references, but no descriptions.

hsc.code(200, function(information) {
  console.log(information);
}, false, true);
{
  "code": "200",
  "title": "OK",
  "summary": "standard response for successful HTTP requests",
  "references": {
    "rails": {
      "title": "Rails HTTP Status Symbol",
      "value": ":ok"
    }
  },
  "status": "success"
}

Report with both descriptions and references.

hsc.code(200, function(information) {
  console.log(information);
}, true, true);
{
  "code": "200",
  "title": "OK",
  "summary": "standard response for successful HTTP requests",
  "descriptions": {
    "wikipedia": {
      "body": "Standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request the response will contain an entity describing or containing the result of the action.",
      "link": "http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#200"
    },
    "ietf": {
      "body": "The request has succeeded. The information returned with the response is dependent on the method used in the request, for example: GET an entity corresponding to the requested resource is sent in the response; HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body; POST an entity describing or containing the result of the action;",
      "link": "http://www.ietf.org/rfc/rfc2616.txt"
    }
  },
  "references": {
    "rails": {
      "title": "Rails HTTP Status Symbol",
      "value": ":ok"
    }
  },
  "status": "success"
}

In the event of an error, the status property will become "error" and the error property will hold the error.

Invalid HTTP status code.

hsc.code(2500, function(information) {
  console.log(information);
});
{
  "status": "error",
  "error": "Invalid code 2500."
}

Miscellaneous error during request.

// Some call.
{
  "status": "error",
  "error": "Error with request."
}

These examples, excluding the "Miscellaneous error during request.", can also be found in /examples/everything.js.

Contributing

Contributions are always welcome.

We follow Airbnb's coding standard, so make sure you use that as a guideline.

Fork our code, make a new branch, and send a pull request.

TODO: