@prexjs/statuses
v1.0.0
Published
HTTP status code and reason phrase utility with category checks for redirects, empty bodies, and retries.
Readme
@prexjs/statuses
HTTP status code and reason phrase utility with category checks for redirects, empty bodies, and retries.
@prexjs/statuses provides lookup between HTTP status codes (e.g. 404) and standard status messages (e.g. 'Not Found'), alongside convenient dictionaries for status categorization (redirect, empty, retry).
Features
- Bi-directional lookup between HTTP status codes and phrases.
- Categorization maps:
statuses.redirect,statuses.empty,statuses.retry. - Complete list of standard codes:
statuses.codes. - Dual ES Module (
.mjs) and CommonJS (.cjs) support with TypeScript definitions. - Zero runtime dependencies.
Installation
Install using your preferred package manager:
# npm
npm install @prexjs/statuses
# pnpm
pnpm add @prexjs/statuses
# Yarn
yarn add @prexjs/statuses
# Bun
bun add @prexjs/statusesQuick start
import statuses from '@prexjs/statuses';
// Status code to message
console.log(statuses(200)); // 'OK'
console.log(statuses(404)); // 'Not Found'
// Message to status code
console.log(statuses('not found')); // 404
console.log(statuses('bad request')); // 400Both default and named exports are supported:
import statuses, {
message,
code,
codes,
redirect,
empty,
retry,
} from '@prexjs/statuses';Examples
Checking status categories
import statuses from '@prexjs/statuses';
// Check if a status requires an empty body (204, 205, 304)
if (statuses.empty[res.statusCode]) {
res.end();
}
// Check if a status is a redirect (300, 301, 302, 303, 305, 307, 308)
if (statuses.redirect[res.statusCode]) {
console.log('Redirecting...');
}
// Check if a status indicates a retryable server error (502, 503, 504)
if (statuses.retry[res.statusCode]) {
// schedule retry
}Compatibility
@prexjs/statuses runs anywhere ECMAScript executes (Node.js, Deno, Bun, browser).
License
This project is licensed under the MIT License. See LICENSE for the full license text.
Related projects
- prex.js — prex.js monorepo and ecosystem.
