Skip to content

brielov/http-status

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@brielov/http-status

A TypeScript library for HTTP status codes, messages, and descriptions, with utilities for creating and throwing HTTP Responses.

Installation

Install via JSR using Deno, Bun, or npm:

deno add jsr:@brielov/http-status
npx jsr add @brielov/http-status
yarn dlx jsr add @brielov/http-status
pnpm dlx jsr add @brielov/http-status
bunx jsr add @brielov/http-status

Usage

import {
  createResponse,
  getStatusClass,
  getStatusDescription,
  getStatusText,
  HttpStatus,
  isValidStatus,
  throwResponse,
} from "@brielov/http-status";

// Get status text
console.log(getStatusText(HttpStatus.OK)); // "OK"

// Get status description
console.log(getStatusDescription(HttpStatus.NotFound)); // "Server cannot find requested resource."

// Check status class
console.log(getStatusClass(HttpStatus.ImATeapot)); // "ClientError"

// Validate status code
console.log(isValidStatus(HttpStatus.OK)); // true
console.log(isValidStatus(999)); // false

// Create a Response
const response = createResponse(HttpStatus.NotFound, {
  headers: { "X-Custom": "test" },
});
console.log(response.status); // 404
console.log(response.statusText); // "Not Found"

// Throw a Response
try {
  throwResponse(HttpStatus.ImATeapot);
} catch (res) {
  console.log(res.status); // 418
  console.log(res.statusText); // "I'm a teapot"
}

Features

  • HttpStatus: Enum of HTTP status codes (100–599) per RFC 9110.
  • getStatusText: Retrieves status text for a given code.
  • getStatusDescription: Retrieves detailed description for a code.
  • getStatusClass: Classifies codes (Informational, Success, Redirection, ClientError, ServerError).
  • isValidStatus: Checks if a status code is valid.
  • createResponse: Creates a Response with status and custom init options.
  • throwResponse: Throws a Response with status and custom init options.

API Reference

See JSR documentation for detailed API.

License

MIT License. See LICENSE for details.

Source

Hosted on JSR and GitHub.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published