resp-codes
v1.0.1
Published
HTTP status codes constants and utilities for TypeScript
Maintainers
Readme
resp-codes
HTTP status codes constants and utilities for TypeScript.
Based on IETF RFCs including RFC 7231 (HTTP/1.1), RFC 6585 (Additional Status Codes), RFC 4918 (WebDAV), RFC 7538 (Permanent Redirect), RFC 8470 (Early Hints), and RFC 7725 (Legal Obstacles).
Installation
# bun
bun add resp-codes
# npm
npm add resp-codes
# pnpm
pnpm add resp-codes
# yarn
yarn add resp-codes
# deno
deno add npm:resp-codesUsage
import { OK, NOT_FOUND, getPhrase } from 'resp-codes'
// Constants
new Response('Success', { status: OK }) // 200
new Response('Not Found', { status: NOT_FOUND }) // 404
// Reason phrases
getPhrase(200) // "OK"
getPhrase(404) // "Not Found"
// Type guards
import { isSuccessful, isError } from 'resp-codes'
isSuccessful(200) // true
isError(404) // trueAPI
Constants
1xx Informational
| Constant | Value | Phrase |
|----------|-------|--------|
| CONTINUE | 100 | Continue |
| SWITCHING_PROTOCOLS | 101 | Switching Protocols |
| PROCESSING | 102 | Processing (deprecated) |
| EARLY_HINTS | 103 | Early Hints |
2xx Successful
| Constant | Value | Phrase |
|----------|-------|--------|
| OK | 200 | OK |
| CREATED | 201 | Created |
| ACCEPTED | 202 | Accepted |
| NON_AUTHORITATIVE_INFORMATION | 203 | Non-Authoritative Information |
| NO_CONTENT | 204 | No Content |
| RESET_CONTENT | 205 | Reset Content |
| PARTIAL_CONTENT | 206 | Partial Content |
| MULTI_STATUS | 207 | Multi-Status |
| ALREADY_REPORTED | 208 | Already Reported |
| IM_USED | 226 | IM Used |
3xx Redirection
| Constant | Value | Phrase |
|----------|-------|--------|
| MULTIPLE_CHOICES | 300 | Multiple Choices |
| MOVED_PERMANENTLY | 301 | Moved Permanently |
| FOUND | 302 | Found |
| SEE_OTHER | 303 | See Other |
| NOT_MODIFIED | 304 | Not Modified |
| USE_PROXY | 305 | Use Proxy (deprecated) |
| UNUSED | 306 | (Unused) |
| TEMPORARY_REDIRECT | 307 | Temporary Redirect |
| PERMANENT_REDIRECT | 308 | Permanent Redirect |
4xx Client Error
| Constant | Value | Phrase |
|----------|-------|--------|
| BAD_REQUEST | 400 | Bad Request |
| UNAUTHORIZED | 401 | Unauthorized |
| PAYMENT_REQUIRED | 402 | Payment Required |
| FORBIDDEN | 403 | Forbidden |
| NOT_FOUND | 404 | Not Found |
| METHOD_NOT_ALLOWED | 405 | Method Not Allowed |
| NOT_ACCEPTABLE | 406 | Not Acceptable |
| PROXY_AUTHENTICATION_REQUIRED | 407 | Proxy Authentication Required |
| REQUEST_TIMEOUT | 408 | Request Timeout |
| CONFLICT | 409 | Conflict |
| GONE | 410 | Gone |
| LENGTH_REQUIRED | 411 | Length Required |
| PRECONDITION_FAILED | 412 | Precondition Failed |
| CONTENT_TOO_LARGE | 413 | Content Too Large |
| URI_TOO_LONG | 414 | URI Too Long |
| UNSUPPORTED_MEDIA_TYPE | 415 | Unsupported Media Type |
| RANGE_NOT_SATISFIABLE | 416 | Range Not Satisfiable |
| EXPECTATION_FAILED | 417 | Expectation Failed |
| IM_A_TEAPOT | 418 | I'm a teapot |
| MISDIRECTED_REQUEST | 421 | Misdirected Request |
| UNPROCESSABLE_CONTENT | 422 | Unprocessable Content |
| LOCKED | 423 | Locked |
| FAILED_DEPENDENCY | 424 | Failed Dependency |
| TOO_EARLY | 425 | Too Early |
| UPGRADE_REQUIRED | 426 | Upgrade Required |
| PRECONDITION_REQUIRED | 428 | Precondition Required |
| TOO_MANY_REQUESTS | 429 | Too Many Requests |
| REQUEST_HEADER_FIELDS_TOO_LARGE | 431 | Request Header Fields Too Large |
| UNAVAILABLE_FOR_LEGAL_REASONS | 451 | Unavailable For Legal Reasons |
5xx Server Error
| Constant | Value | Phrase |
|----------|-------|--------|
| INTERNAL_SERVER_ERROR | 500 | Internal Server Error |
| NOT_IMPLEMENTED | 501 | Not Implemented |
| BAD_GATEWAY | 502 | Bad Gateway |
| SERVICE_UNAVAILABLE | 503 | Service Unavailable |
| GATEWAY_TIMEOUT | 504 | Gateway Timeout |
| HTTP_VERSION_NOT_SUPPORTED | 505 | HTTP Version Not Supported |
| VARIANT_ALSO_NEGOTIATES | 506 | Variant Also Negotiates |
| INSUFFICIENT_STORAGE | 507 | Insufficient Storage |
| LOOP_DETECTED | 508 | Loop Detected |
| NOT_EXTENDED | 510 | Not Extended |
| NETWORK_AUTHENTICATION_REQUIRED | 511 | Network Authentication Required |
Functions
| Function | Returns |
|----------|---------|
| getPhrase(code) | Reason phrase string (e.g. "OK") |
| getStatusInfo(code) | { code, phrase, category, description, deprecated } |
| getStatusLine(code) | Status line (e.g. "200 OK") |
| getCategory(code) | Category name ("Informational", "Successful", "Redirection", "Client Error", "Server Error") |
| getDescription(code) | Full description string |
| isInformational(code) | true if 1xx |
| isSuccessful(code) | true if 2xx |
| isRedirection(code) | true if 3xx |
| isClientError(code) | true if 4xx |
| isServerError(code) | true if 5xx |
| isError(code) | true if 4xx or 5xx |
| isValid(code) | true if 100-599 |
| isDeprecated(code) | true if deprecated (102, 305, 306) |
| preservesMethod(code) | true if 307 or 308 |
| isCacheable(code) | true if cacheable status code |
Category Arrays
import { informational, successful, redirection, clientError, serverError, all } from 'resp-codes'
informational // [100, 101, 102, 103]
successful // [200, 201, 202, 203, 204, 205, 206, 207, 208, 226]
redirection // [300, 301, 302, 303, 304, 305, 306, 307, 308]
clientError // [400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451]
serverError // [500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511]
all // [100, 101, ... 511]Phrase Constants
Individual phrase constants for tree-shaking:
import {
PHRASE_OK, // "OK"
PHRASE_NOT_FOUND, // "Not Found"
PHRASE_UNAUTHORIZED, // "Unauthorized"
// ... and more
} from 'resp-codes'License
MIT
