HTTP Constants for TypeScript

raw JSON →
1.0.8 verified Thu Apr 23 auth: no javascript

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'.

error Property 'content-type' does not exist on type 'typeof Headers'. Did you mean 'CONTENT_TYPE'?
cause Attempting to access a constant using its raw HTTP string or incorrect casing/format instead of the library's normalized, uppercase, underscore-separated key.
fix
Access constants using the library's predefined keys, which typically convert hyphens to underscores and use uppercase for names (e.g., Headers.CONTENT_TYPE for Content-Type).
error TypeError: require is not a function
cause Trying to use CommonJS `require()` syntax in an environment configured for ECMAScript Modules (ESM).
fix
Use ESM 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.
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`).
fix Always refer to the library's internal key names as documented or inferred by TypeScript's intellisense (e.g., `Headers.CONTENT_TYPE`, not `Headers['Content-Type']`).
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`).
fix Be aware of the expected casing when using the constant values in contexts that are case-sensitive. The library provides the correct string literal for each constant.
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.
fix Prefer `import ... from 'http-constants-ts'` syntax. Ensure your project's `tsconfig.json` and `package.json` are configured for modern module resolution (e.g., `"type": "module"` in `package.json`, `"module": "esnext"` in `tsconfig.json`).
npm install http-constants-ts
yarn add http-constants-ts
pnpm add http-constants-ts

Demonstrates importing and using various HTTP constants like headers, MIME types, response codes, and methods, showing how to access their structured values in a type-safe manner.

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}`);