HttpStatus
The HttpStatus class provides a comprehensive, type-safe API for managing HTTP status codes with rich metadata.
HttpStatus
The HttpStatus class provides a comprehensive, type-safe API for managing HTTP status codes with rich metadata. It supports lookup by code or name, custom status code registration, message overriding, and category-based filtering.
Data Source
This utility includes all standard HTTP status codes with metadata adapted from MDN documentation.
- Each entry includes:
code,name,readableName,description,message,categoryand optionallink. - Categories are pre-grouped for easy filtering:
informational,success,redirection,clientError,serverError.
When to Use
- You need multiple independent configurations of status codes.
- You want to add custom status codes without affecting the shared
httpStatusinstance. - You need to override messages for specific status codes in your application.
Features
- Lookup status codes by number or name (both
SOME_NAMEandSome Nameformats). - Built-in standard HTTP status codes loaded on construction.
- Manage status codes dynamically with
addCode(),setMessage(), andaddOrOverrideCode(). - Pre-grouped categories for quick filtering.
- Fully typed for TypeScript with comprehensive utility types.
- Isolated instances for different configurations.
Import
import { HttpStatus } from 'toolbox-x';Basic Usage
Type Definitions
Core Types
type $StatusNameVar = 'name' | 'readableName';
type StatusCategory = 'informational' | 'success' | 'redirection' | 'clientError' | 'serverError';
type StatusCode = LooseLiteral<HttpStatusCode>;
type StatusName = LooseLiteral<HttpStatusName>;
type EntryStatusName = LooseLiteral<HttpStatusName<'name'>>;
type EntryReadableName = LooseLiteral<HttpStatusName<'readableName'>>;
interface StatusEntry {
code: StatusCode;
name: EntryStatusName;
readableName: EntryReadableName;
link?: string;
message: string;
description: string;
category: StatusCategory;
}Utility Types
Two key utility types are provided for type-safe operations:
-
HttpStatusCode<Category>
Extracts standard HTTP status codes filtered by category.type SuccessCodes = HttpStatusCode<'success'>; // 200 | 201 | 202 | ... type ClientErrors = HttpStatusCode<'clientError'>; // 400 | 401 | 403 | 404 | ... -
HttpStatusName<Name, Category>
Extracts standard HTTP status names filtered by variant and category.type ConstantNames = HttpStatusName<'name'>; // "OK" | "CREATED" | "NOT_FOUND" | ... type ReadableNames = HttpStatusName<'readableName'>; // "OK" | "Created" | "Not Found" | ...
These types enable compile-time validation of status codes and names throughout your application.
Extending Status Codes
You can modify your instance without affecting others:
API Reference
Constructor
Initializes a new HttpStatus instance with all standard HTTP status codes.
getByCode()
Get status entry by numeric HTTP code.
Signatures
getByCode(code: HttpStatusCode): StatusEntry;
getByCode(code: StatusCode): StatusEntry | undefined;Example
getByName()
Get status entry by name (either SOME_NAME or Some Name format).
Signatures
getByName(name: HttpStatusName): StatusEntry;
getByName(name: StatusName): StatusEntry | undefined;Example
setMessage()
Override the short message of an existing code.
Signatures
setMessage(code: StatusCode, newMessage: string): booleanExample
addCode()
Add one or more new HTTP status code entries.
Signatures
addCode(...entries: StatusEntry[]): booleanRemarks
- New entries are compared by their
codevalue to determine uniqueness. - If a code already exists, it will be skipped and not overwritten.
- Returns
trueif at least one code was successfully added. - Returns
falseif all provided codes already exist.
Example
addOrOverrideCode()
Add or override one or more HTTP status code entries.
Signatures
addOrOverrideCode(...entries: StatusEntry[]): HttpStatusRemarks
- New entries use their
codevalue as the comparison key and will overwrite existing ones. - Returns the modified instance for method chaining.
Example
list()
List all codes, optionally filtered by category.
Signatures
list(category?: StatusCategory): StatusEntry[]Example
Groups (Static Property)
Pre-grouped HTTP status codes by category for quick reference:
HttpStatus.Groups: {
informational: StatusCode[],
success: StatusCode[],
redirection: StatusCode[],
clientError: StatusCode[],
serverError: StatusCode[]
}Categories
| Category | Description | Example Codes |
|---|---|---|
informational | 1xx status codes | 100, 101, 102, 103 |
success | 2xx status codes | 200, 201, 202, 204 |
redirection | 3xx status codes | 301, 302, 303, 304 |
clientError | 4xx status codes | 400, 401, 403, 404 |
serverError | 5xx status codes | 500, 501, 502, 503 |
Example Usage
See Also
- httpStatus (default instance) — shared instance of
HttpStatusfor standard use cases. - HTTP Status Codes Reference — MDN documentation.
Last updated: Sun, Jun 14, 2026 07:31:07PM (UTC)
