Literal HTTP Codes & Statuses

0.0.20 · active · verified Wed Apr 22

ltrl-http is a JavaScript/TypeScript library that provides a comprehensive, type-safe collection of HTTP status codes and their corresponding descriptions. It offers both numerical codes and string statuses, along with utility functions for validation and resolution. Currently at version 0.0.20, the package demonstrates a rapid release cadence with frequent patch and minor updates, often related to broader `ltrl` monorepo enhancements, particularly around Nuxt.js integration. Its key differentiators include strong TypeScript typing for HTTP codes and statuses, allowing for compile-time safety, and helper functions like `isHTTPCode` and `useHTTPCode` to work with these values programmatically. It aims to simplify handling HTTP responses by providing a consistent and robust interface for standard HTTP semantics.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing `status` for direct code access, `isHTTPCode` for validation, and `HTTPCode` for type safety.

import { status, isHTTPCode, HTTPCode } from 'ltrl-http';

// Accessing HTTP status codes directly
console.log(`OK: ${status.OK}`); // Output: OK: 200
console.log(`Forbidden: ${status.FORBIDDEN}`); // Output: Forbidden: 403

// Using utility functions
const codeToCheck: unknown = 200;
if (isHTTPCode(codeToCheck)) {
  console.log(`${codeToCheck} is a valid HTTP code.`);
} else {
  console.log(`${codeToCheck} is not a valid HTTP code.`);
}

// Type usage example
function handleResponse(code: HTTPCode) {
  switch (code) {
    case status.OK:
      console.log('Request successful!');
      break;
    case status.NOT_FOUND:
      console.log('Resource not found.');
      break;
    default:
      console.log(`Unhandled HTTP code: ${code}`);
  }
}

handleResponse(status.INTERNAL_SERVER_ERROR);
// Expected output: Unhandled HTTP code: 500

view raw JSON →