HTTP Constants for TypeScript
raw JSON →The `http-constants-ts` library provides a comprehensive collection of standardized and commonly used non-standard HTTP constants, including headers, MIME types, tracking statuses, HTTP methods, content encodings, CSP directives and sources, HTTP response codes, and Cache-Control instructions. Currently stable at version 1.0.8, its release cadence is typically slow, reflecting the stable nature of HTTP specifications, with updates primarily driven by new RFCs or significant industry changes. A key differentiator is its TypeScript-first design, offering strong type safety and predictable access patterns through structured objects like `Headers.STRICT_TRANSPORT_SECURITY` or `MimeTypes.Application.JSON`. It ensures consistency by normalizing constant values: headers are capitalized (e.g., `Strict-Transport-Security`), MIME types, content encodings, and cache-control instructions are lowercase (e.g., `application/json`, `deflate`, `max-age`), and HTTP methods are uppercase (e.g., `POST`), reducing the chance of runtime errors from 'magic strings'.
Common errors
error Property 'content-type' does not exist on type 'typeof Headers'. Did you mean 'CONTENT_TYPE'? ↓
Headers.CONTENT_TYPE for Content-Type). error TypeError: require is not a function ↓
import statements: import { Headers } from 'http-constants-ts';. Ensure your package.json has "type": "module" if running directly in Node.js, or your bundler is configured correctly. Warnings
gotcha Constant keys for headers, MIME types, and other values are normalized within the library. Hyphens are typically replaced by underscores, and casing follows specific rules (e.g., `Strict-Transport-Security` becomes `Headers.STRICT_TRANSPORT_SECURITY`). ↓
gotcha The string values of constants are strictly case-normalized: all headers are capitalized (e.g., `Content-Type`), MIME types and content encodings are lowercase (e.g., `application/json`), and methods are uppercase (e.g., `GET`). ↓
gotcha This library is designed for ESM (ECMAScript Modules) usage with TypeScript. While it may transpile to CommonJS, using `require()` directly might lead to issues or require specific TypeScript/bundler configurations. ↓
Install
npm install http-constants-ts yarn add http-constants-ts pnpm add http-constants-ts Imports
- Headers wrong
const Headers = require('http-constants-ts').Headers;correctimport { Headers } from 'http-constants-ts'; - MimeTypes wrong
import MimeTypes from 'http-constants-ts';correctimport { MimeTypes } from 'http-constants-ts'; - ResponseCodes wrong
import { OK } from 'http-constants-ts/ResponseCodes';correctimport { ResponseCodes } from 'http-constants-ts';
Quickstart
import { Headers, MimeTypes, ResponseCodes, Methods, CSPD } from 'http-constants-ts';
console.log("Common HTTP Headers:");
console.log(`Content-Type: ${Headers.CONTENT_TYPE}`);
console.log(`Authorization: ${Headers.AUTHORIZATION}`);
console.log("\nPopular MIME Types:");
console.log(`JSON: ${MimeTypes.Application.JSON}`);
console.log(`PNG: ${MimeTypes.Image.PNG}`);
console.log(`HTML: ${MimeTypes.Text.HTML}`);
console.log("\nHTTP Response Codes:");
console.log(`OK: ${ResponseCodes.OK} - ${ResponseCodes[ResponseCodes.OK]}`);
console.log(`Not Found: ${ResponseCodes.NOT_FOUND} - ${ResponseCodes[ResponseCodes.NOT_FOUND]}`);
console.log(`I'm a Teapot: ${ResponseCodes.I_AM_A_TEAPOT}`);
console.log("\nHTTP Methods:");
console.log(`GET: ${Methods.GET}`);
console.log(`POST: ${Methods.POST}`);
console.log("\nCSP Directives:");
console.log(`Script Source: ${CSPD.SCRIPT_SRC}`);
console.log(`Default Source: ${CSPD.DEFAULT_SRC}`);