Skip to content

getStatus / status

An HTTP status code ↔ message lookup table, plus a two-way getStatus helper — the same data Node's own http.STATUS_CODES provides, packaged for use in the browser too.

Usage

ts
import { getStatus, status } from 'ranuts/utils';

getStatus(404); // 'Not Found'
getStatus('404'); // 'Not Found' — numeric strings are parsed first
getStatus('not found'); // 404 — falls back to a message lookup, case-insensitive

status.redirect[302]; // true
status.empty[204]; // true
status.retry[503]; // true

API

getStatus(code)

Parameters

ParameterDescriptionTypeDefault
codeA status code, a numeric string, or a status messagenumber | stringRequired

Return

number | string — pass a number and get the message back; pass a string and get the code back (a numeric string like '404' is parsed as a code first, and only falls back to a message lookup if it isn't a known code). Throws if the input matches neither.

status

FieldDescriptionType
messageCode → messageMap<number, string>
codeLower-cased message → codeMap<string, number>
codesEvery known codenumber[]
redirectCodes that redirect (300, 301, 302, 303, 305, 307, 308)Record<number, true>
emptyCodes with no body (204, 205, 304)Record<number, true>
retryCodes worth retrying (502, 503, 504)Record<number, true>

Notes

  1. getStatus throws on an unknown code or messageTypeError for a non-number/ string argument, Error otherwise. Wrap it in try/catch (or check status.codes.includes(n) first) when the input isn't guaranteed valid, e.g. a status code read off the wire.
  2. status.redirect / empty / retry are plain objects, not Sets — check membership with status.retry[code], not .has().
  3. Runs in both browser and Node (ranuts/utils), so it's usable on the client for the same code↔message mapping a server-side ranuts/node handler would use.

Released under the MIT License.