HTTP Response Status Code Utilities
This package, `http-response-status-code`, provides a lightweight and comprehensive utility for interacting with HTTP status codes, names, and descriptions. Currently at version 1.7.5, it offers a stable API for retrieving details, validating codes, and categorizing them into informational, success, redirection, client error, and server error groups. The library differentiates itself by providing a full suite of helper functions, such as `getStatusName`, `getStatusDescription`, `getNotFound`, and dedicated checks like `isInformationalCode` or `isClientErrorCode`, in addition to exposing all standard HTTP status codes as named exports. Its release cadence appears to be moderate, with minor updates focusing on feature additions like the "Toolset Overview" in 1.7.2 and continuous code hygiene improvements. It ships with TypeScript types, ensuring robust usage in modern JavaScript and TypeScript environments, making it a reliable choice for handling HTTP response logic.
Common errors
-
TypeError: STATUS_CODES.getStat is not a function
cause Attempting to use a non-existent function name, possibly copied from an incomplete example in release notes.fixCorrect the function call to `STATUS_CODES.getStatusName` or `STATUS_CODES.getStatusCode`. -
TypeError: Cannot read properties of undefined (reading 'OK')
cause Incorrect ESM import syntax, attempting to use a default import when only named or namespace imports are available.fixChange your import statement to `import { OK } from 'http-response-status-code';` for specific codes or `import * as STATUS_CODES from 'http-response-status-code';` to access all exports via `STATUS_CODES.OK`.
Warnings
- gotcha The release notes for v1.7.2 contain a typo in the `getStat` example, which is incomplete and incorrect. The correct function names are `getStatusName` or `getStatusCode`.
- gotcha When migrating from CommonJS `require` to ESM `import`, ensure you use named imports for specific codes and functions (e.g., `import { OK, getStatusName } from 'pkg'`) or a namespace import (`import * as STATUS_CODES from 'pkg'`) if you want to access all exports under a single object.
- gotcha The library primarily uses full upper-case strings for status code names (e.g., 'OK', 'NOT FOUND') when functions like `getStatusCode` are expecting a string input. Mismatched casing might lead to unexpected results.
Install
-
npm install http-response-status-code -
yarn add http-response-status-code -
pnpm add http-response-status-code
Imports
- STATUS_CODES
const STATUS_CODES = require('http-response-status-code');import * as STATUS_CODES from 'http-response-status-code';
- OK
import STATUS_CODES from 'http-response-status-code'; STATUS_CODES.OK;
import { OK, getStatusName, CODES } from 'http-response-status-code'; - getStatusName
import * as STATUS_CODES from 'http-response-status-code'; STATUS_CODES.getStatName(...);
import { getStatusName } from 'http-response-status-code';
Quickstart
import { OK, NOT_FOUND, isSuccessCode, getStatusName, getStatusDescription, getNotFound, getStatusCode, CLIENT_ERROR_CODES } from 'http-response-status-code';
console.log('OK status code:', OK); // 200
console.log('Not Found status code:', NOT_FOUND); // 404
const status200Name = getStatusName(OK);
console.log('Name for 200:', status200Name); // OK
const status404Description = getStatusDescription(NOT_FOUND);
console.log('Description for 404:', status404Description); // Not Found
const nameForNotFound = getNotFound().name;
console.log('Name from getNotFound():', nameForNotFound); // Not Found
const codeForOK = getStatusCode('OK');
console.log('Code for "OK":', codeForOK); // 200
console.log('Is 200 a success code?', isSuccessCode(OK)); // true
console.log('Is 404 a success code?', isSuccessCode(NOT_FOUND)); // false
console.log('First 5 client error codes:', CLIENT_ERROR_CODES.slice(0, 5)); // [400, 401, 402, 403, 404]