HTTP Status Code Utilities

1.0.2 · maintenance · verified Wed Apr 22

The `http-status-response-codes` package (current version 1.0.2) provides a JavaScript utility for handling HTTP status codes, drawing inspiration from Java's Spring Boot framework. It exports an `HttpStatus` class that can be instantiated with a numeric status code (as a string) or a response-like object containing a `status` property. Instances of `HttpStatus` offer methods to categorize the status code (e.g., `is2xxSuccessful()`, `is4xxClientError()`, `isError()`, `is1xxInformational()`, `is3xxRedirection()`, `is5xxServerError()`), and retrieve its name, code value, and description. Additionally, the package exports a `statusCodes` object, providing a convenient enum-like collection of standard HTTP status codes. This library facilitates programmatic handling and classification of HTTP responses in a structured manner. Given its version and CommonJS-centric examples, the package appears to be stable and feature-complete, but not actively undergoing modern JavaScript evolution.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `HttpStatus` with both a string and a mock response object, how to use its categorization methods (`is4xxClientError`, `is2xxSuccessful`, `isError`), and how to access specific status code values from the `statusCodes` object. It highlights common usage patterns for checking HTTP response types.

const HttpStatus = require('http-status-response-codes');
const { statusCodes } = require('http-status-response-codes');

async function demonstrateHttpStatus() {
  // Example 1: Instantiate with a numeric string
  const notFoundStatus = new HttpStatus('404');
  console.log(`Code: ${notFoundStatus.getCodeValue()} Name: ${notFoundStatus.getName()}`);
  console.log(`Is client error? ${notFoundStatus.is4xxClientError()}`); // true
  console.log(`Is error? ${notFoundStatus.isError()}`); // true
  console.log(`Description: ${notFoundStatus.getDescription()}`);

  // Example 2: Instantiate with a mock response object (simulating an HTTP client response)
  // The package expects an object with a '.status' property, similar to older libraries like request-promise.
  const mockSuccessResponse = { status: 200, body: 'OK' }; // Mock a response object
  const successStatus = new HttpStatus(mockSuccessResponse);
  console.log(`Code: ${successStatus.getCodeValue()} Name: ${successStatus.getName()}`);
  console.log(`Is successful? ${successStatus.is2xxSuccessful()}`); // true
  console.log(`Is error? ${successStatus.isError()}`); // false

  // Example 3: Accessing the statusCodes enum for numerical values
  console.log(`HTTP_BAD_REQUEST: ${statusCodes.BAD_REQUEST}`); // 400
  console.log(`HTTP_INTERNAL_SERVER_ERROR: ${statusCodes.INTERNAL_SERVER_ERROR}`); // 500

  // Using a simulated server error response
  const simulatedResponse = { status: 503, statusText: 'Service Unavailable' };
  const serverErrorStatus = new HttpStatus(simulatedResponse);
  if (serverErrorStatus.isError()) {
    console.log(`
Caught an HTTP error: ${serverErrorStatus.getCodeValue()} - ${serverErrorStatus.getDescription()}`);
    console.log(`Is server error? ${serverErrorStatus.is5xxServerError()}`); // true
  }
}

demonstrateHttpStatus();

view raw JSON →