HTTP Status Enum

1.0.2 · active · verified Wed Apr 22

http-status-enum is a lightweight JavaScript/TypeScript library designed to provide a comprehensive, read-only enumeration of standard HTTP status codes. It offers a convenient dual-access mechanism, allowing developers to retrieve status descriptions from numeric codes (e.g., `500` yields `INTERNAL_SERVER_ERROR`) and vice-versa. The package is currently stable at version 1.0.2, with a slow, maintenance-focused release cadence primarily addressing module resolution and type definition correctness. Its key differentiators include its minimal footprint, explicit TypeScript type definitions for enhanced developer experience and type safety, and its straightforward API for consistent access to HTTP status information without external dependencies or complex configurations, making it a reliable choice for various application contexts.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing the enum, accessing status codes by number and description, and a practical use case in response handling.

import HTTP_STATUS_CODES from 'http-status-enum';

// Accessing status description by numeric code
const internalServerErrorDesc = HTTP_STATUS_CODES['500'];
console.log(`Description for 500: ${internalServerErrorDesc}`); // Expected: INTERNAL_SERVER_ERROR

const notFoundDesc = HTTP_STATUS_CODES['404'];
console.log(`Description for 404: ${notFoundDesc}`); // Expected: NOT_FOUND

// Accessing numeric code by string description
const okCode = HTTP_STATUS_CODES.OK;
console.log(`Code for OK: ${okCode}`); // Expected: 200

const badRequestCode = HTTP_STATUS_CODES.BAD_REQUEST;
console.log(`Code for BAD_REQUEST: ${badRequestCode}`); // Expected: 400

// Demonstrating a common pattern for handling HTTP responses
function handleResponse(statusCode: number) {
  const statusDescription = HTTP_STATUS_CODES[statusCode.toString() as keyof typeof HTTP_STATUS_CODES];
  if (statusCode >= 200 && statusCode < 300) {
    console.log(`Success: ${statusDescription}`);
  } else if (statusCode >= 400 && statusCode < 500) {
    console.log(`Client Error: ${statusDescription}`);
  } else if (statusCode >= 500 && statusCode < 600) {
    console.log(`Server Error: ${statusDescription}`);
  }
}

handleResponse(200);
handleResponse(403);
handleResponse(502);

view raw JSON →