HTTP Status Map

1.1.3 · active · verified Wed Apr 22

This package provides a comprehensive, TypeScript-first solution for working with HTTP status codes in JavaScript and Node.js applications. It offers a structured approach to access HTTP status code constants, retrieve human-readable names, and categorize codes into their respective classes (e.g., informational, success, client error, server error). The current stable version is 1.1.3, released in December 2022. While its release cadence has been infrequent, focusing primarily on bug fixes and minor dependency updates, the library remains a reliable choice for static HTTP status code mapping. Key differentiators include its robust TypeScript typings, which ensure compile-time safety and better developer experience, and its utility functions that simplify common operations like checking status categories, providing an abstract layer over raw numerical comparisons. It serves as a lightweight and focused alternative to manually managing HTTP status code logic.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to check specific HTTP status codes, retrieve their human-readable names, and categorize them using the provided constants and utility functions for robust response handling.

import { NHttpStatuses, getHttpStatusName, getHttpStatusCategory, HTTP_STATUS_SUCCESS, HTTP_STATUS_CLIENT_ERROR } from 'http-response-status';

function processHttpResponse(statusCode: number): string {
  if (statusCode === NHttpStatuses.OK) {
    console.log(`Status ${statusCode}: All good, operation successful!`);
    return 'OK';
  }

  const statusName = getHttpStatusName(statusCode);
  if (statusName) {
    console.log(`Status ${statusCode}: ${statusName}`);
  } else {
    console.log(`Status ${statusCode}: Unknown Status Code`);
  }

  const statusCategory = getHttpStatusCategory(statusCode);
  if (statusCategory === HTTP_STATUS_SUCCESS) {
    console.log(`Category for ${statusCode} is Success.`);
    return 'Success';
  } else if (statusCategory === HTTP_STATUS_CLIENT_ERROR) {
    console.log(`Category for ${statusCode} is Client Error.`);
    return 'Client Error';
  } else {
    console.log(`Category for ${statusCode} is ${statusCategory || 'Other'}.`);
    return 'Other';
  }
}

processHttpResponse(200);
processHttpResponse(404);
processHttpResponse(500);
processHttpResponse(201);
processHttpResponse(1000); // Example of an unknown status code

view raw JSON →