HTTP Status Code Enum

1.0.0 · active · verified Wed Apr 22

status-code-enum is a TypeScript enum that provides a comprehensive collection of standard HTTP status codes. It is currently at version 1.0.0, indicating a stable API suitable for production use. The package utilizes `semantic-release`, suggesting a consistent, automated release cadence driven by commit messages; new versions are published as features or fixes are introduced rather than on a fixed calendar schedule. Its primary differentiation is its direct provision of these codes as a robust TypeScript enum, offering strong type safety and auto-completion benefits within TypeScript projects. This makes it a reliable and convenient alternative to using magic numbers or manually defining constants for HTTP responses. This approach significantly enhances code readability and maintainability by eliminating the need for developers to remember specific numeric codes or to repeatedly define them across various projects. The enum covers informational, success, redirection, client error, and server error codes as defined by HTTP standards.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import the `StatusCode` enum and assign its members to a hypothetical HTTP response object's status code, showcasing type-safe access to common HTTP codes.

import { StatusCode } from 'status-code-enum';

// Example in a hypothetical HTTP response context (e.g., Node.js http module or framework)
interface HttpResponse {
  statusCode: number;
  send: (message: string) => void;
}

const mockResponse: HttpResponse = {
  statusCode: 200, // Default or initial status
  send: (message: string) => console.log(`Sending response: ${message} (Status: ${mockResponse.statusCode})`)
};

// Setting a 400 Bad Request status
mockResponse.statusCode = StatusCode.ClientErrorBadRequest;
mockResponse.send("Your request was malformed.");

// Setting a 200 OK status
mockResponse.statusCode = StatusCode.SuccessOK;
mockResponse.send("Request processed successfully.");

// Setting a 404 Not Found status
mockResponse.statusCode = StatusCode.ClientErrorNotFound;
mockResponse.send("The requested resource could not be found.");

// Accessing the enum member directly
const unauthorizedCode: number = StatusCode.ClientErrorUnauthorized;
console.log(`Unauthorized status code is: ${unauthorizedCode}`);

view raw JSON →