@shresht7/http-status-codes
v0.1.2
Published
<h1> HTTP Status Codes</h1>
Downloads
259
Readme
A lightweight TypeScript library providing HTTP status code enums with descriptions, reverse lookups, and type guards.
📦 Install
Deno (JSR)
deno add jsr:@shresht7/http-status-codesnpm (Node)
npm install @shresht7/http-status-codes📖 Usage
import { Status, Code } from 'jsr:@shresht7/http-status-codes'
Status.INFORMATION.PROCESSING // 102
Status.SUCCESS.OK // 200
Status.CLIENT_ERROR[404] // NOT_FOUND
Code.BAD_GATEWAY // 502
Code[200] // OKStatus
| Status Type | Series | Description |
| --------------------- | ------ | --------------------------------------------------------------- |
| Status.INFORMATION | 1xx | The request was received and understood, continuing process |
| Status.SUCCESS | 2xx | Request was received, understood, and accepted |
| Status.REDIRECT | 3xx | Additional action requested from client. Mainly URL Redirection |
| Status.CLIENT_ERROR | 4xx | Request cannot be fulfilled due to a client side error |
| Status.SERVER_ERROR | 5xx | Request cannot be fulfilled due to a server side error |
Status.INFORMATION.PROCESSING // 102
Status.SUCCESS.OK // 200
Status.CLIENT_ERROR[404] // NOT_FOUNDCode
Code.SWITCHING_PROTOCOLS // 101
Code.CREATED // 201
Code.BAD_REQUEST // 400
Code[301] // MOVED_PERMANENTLYStatusText
import { Status, Code, StatusText } from 'jsr:@shresht7/http-status-codes'
StatusText(Code.CONTINUE) // Continue
StatusText(Status.INFORMATION.SWITCHING_PROTOCOLS) // Switching Protocols
StatusText(404) // Not FoundStatusText can be given a custom formatter to customize the output string.
StatusText(Status.CLIENT_ERROR.NOT_FOUND, (text: string) => {
return `[${Code[text]} - ${text}]: Nothing to see here!`
})
// [404 - NOT_FOUND]: Nothing to see here!Checks
isStatus | isInformation | isSuccess | isRedirect | isClientError | isServerError | isError
import {
Code,
Status,
isStatus,
isInformation,
isSuccess,
isRedirect,
isClientError,
isServerError,
isError
} from 'jsr:@shresht7/http-status-codes'
isStatus(Status.REDIRECT.MOVED_PERMANENTLY) // true
isStatus(Code.BAD_REQUEST) // true
isStatus(502) // true
isStatus(987) // false
isInformation(Status.INFORMATION.SWITCHING_PROTOCOLS) // true
isInformation(Code.INTERNAL_SERVER_ERROR) // false
isSuccess(200) // true
isSuccess(Code.NOT_FOUND) // false
isRedirect(301) // true
isClientError(404) // true
isServerError(404) // false
isError(404) // trueJSON
The json/ directory contains the same status codes in plain JSON, useful for non-TypeScript consumers.
Status.json: Categorized by series (INFORMATION,SUCCESS,REDIRECT,CLIENT_ERROR,SERVER_ERROR)StatusCodes.json: Flat map of all codes
{
"100": { "name": "CONTINUE", "description": "Server received request headers…" },
"200": { "name": "OK", "description": "Standard response for successful HTTP requests…" }
}These are generated from the source JSDoc via scripts/generate-json.ps1.
📕 References
- List of HTTP status codes: Wikipedia article with a comprehensive list of HTTP status codes and their descriptions
