HTTP Enums (Status, Methods, Headers)
http-enums is a utility library providing standardized JavaScript and TypeScript enumerations for common HTTP concepts, including status codes, request methods, and frequently used request and response headers. As of version 1.0.5, last published approximately three years ago (around January 2020), it offers a straightforward and type-safe way to reference these constants, aiming to reduce the use of 'magic strings' in web development projects. Due to its static nature as a collection of constants, it generally does not require frequent updates; however, the lack of recent maintenance means it may not incorporate the latest HTTP specification changes or benefit from community-driven improvements. It differentiates itself by its singular focus on providing comprehensive HTTP enums, suitable for both client-side and server-side applications where explicit constant definitions enhance code clarity.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'GET')
cause Attempting to access an enum member (e.g., `HttpMethod.GET`) when the enum object itself (`HttpMethod`) was not correctly imported or is `undefined`.fixEnsure you are using named imports correctly: `import { HttpMethod } from 'http-enums';`. If using CommonJS, verify the `require` statement loads the module correctly before accessing its properties. -
TS2305: Module '"http-enums"' has no exported member 'MyCustomEnum'.
cause Trying to import an enum symbol that does not exist or has been renamed in the `http-enums` package.fixDouble-check the available exports from the `http-enums` package. Common exports are `HttpStatus`, `HttpMethod`, `HttpRequestHeaders`, and `HttpResponseHeaders`. Ensure correct casing. -
ReferenceError: HttpStatus is not defined
cause Using an enum like `HttpStatus` without first importing it into the current file scope.fixAdd the necessary import statement at the top of your file: `import { HttpStatus } from 'http-enums';`.
Warnings
- gotcha The `http-enums` package has not been updated in approximately three years (since January 2020). While HTTP standards are relatively stable, this means it may not include very recent additions or clarifications to HTTP status codes, methods, or headers. Consider this if working with cutting-edge HTTP specifications.
- gotcha Header names in HTTP are case-insensitive by standard, but JavaScript string comparisons are case-sensitive. While `HttpRequestHeaders` and `HttpResponseHeaders` provide canonical names, always normalize incoming header keys (e.g., to lowercase) before direct comparison if they come from external sources like `req.headers` in Node.js.
- gotcha Attempting to use `require` to import individual named exports directly (e.g., `const { HttpStatus } = require('http-enums');`) might lead to `undefined` or runtime errors in some CommonJS environments, especially if the package's `package.json` specifies `"type": "module"` or uses only ESM syntax internally.
Install
-
npm install http-enums -
yarn add http-enums -
pnpm add http-enums
Imports
- HttpStatus
const HttpStatus = require('http-enums').HttpStatus;import { HttpStatus } from 'http-enums'; - HttpMethod
import HttpMethod from 'http-enums';
import { HttpMethod } from 'http-enums'; - HttpRequestHeaders, HttpResponseHeaders
import * as HttpEnums from 'http-enums';
import { HttpRequestHeaders, HttpResponseHeaders } from 'http-enums';
Quickstart
import { HttpStatus, HttpMethod, HttpRequestHeaders, HttpResponseHeaders } from 'http-enums';
function handleRequest(method: HttpMethod, status: HttpStatus, headers: Record<string, string>): void {
console.log(`Received ${method} request with status ${status}.`);
if (method === HttpMethod.GET) {
console.log('This is a GET request.');
}
if (status === HttpStatus.NOT_FOUND) {
console.log('Resource not found.');
}
if (headers[HttpRequestHeaders.ACCEPT_LANGUAGE] === 'en-US') {
console.log('Client prefers US English.');
}
console.log(`Setting Content-Type to: ${HttpResponseHeaders.CONTENT_TYPE_APPLICATION_JSON}`);
}
handleRequest(HttpMethod.POST, HttpStatus.CREATED, { 'accept-language': 'en-US', [HttpRequestHeaders.CONTENT_TYPE]: 'application/json' });
handleRequest(HttpMethod.GET, HttpStatus.OK, { 'user-agent': 'MyBrowser/1.0' });