Literal HTTP Codes & Statuses
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
-
TypeError: Cannot read properties of undefined (reading 'OK')
cause Attempting to access `status.OK` before the module is correctly imported, or if the `status` object is undefined.fixEnsure the import statement `import { status } from 'ltrl-http';` is at the top of your file and that your environment supports ES Modules. If using CommonJS, consider transpiling or using dynamic import if available. -
TS2307: Cannot find module 'ltrl-http' or its corresponding type declarations.
cause TypeScript compiler cannot locate the package or its declaration files (`.d.ts`).fixEnsure `ltrl-http` is installed (`npm install ltrl-http` or `pnpm add ltrl-http`) and that `moduleResolution` in your `tsconfig.json` is set to a suitable value like `Node16` or `Bundler` for modern module resolution.
Warnings
- breaking The internal structure of 'congruents' has changed from using 'key' and 'label' fields to a single 'id' field. While `ltrl-http`'s direct exports like `status` are unaffected, custom integrations or direct access to the configuration via `useHTTPCodeConfig()` that rely on the previous field names will break.
- gotcha The `ltrl` project, including `ltrl-http`, is actively developed with frequent minor versions and patches. While this indicates active maintenance, it also means that internal structures and features might evolve rapidly. It's advisable to pin exact versions in production to avoid unexpected behavior changes from sub-minor updates.
Install
-
npm install ltrl-http -
yarn add ltrl-http -
pnpm add ltrl-http
Imports
- status
const { status } = require('ltrl-http');import { status } from 'ltrl-http'; - isHTTPCode
import { isHTTPCode } from 'ltrl-http'; - HTTPCode
import type { HTTPCode } from 'ltrl-http'; - useHTTPStatus
import { useHTTPStatus } from 'ltrl-http';
Quickstart
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