HTTP Method Enum
http-method-enum is a lightweight TypeScript package that provides a strongly typed enum for standard HTTP methods. Currently at version 1.0.0, it offers a consistent and error-resistant way to reference HTTP verbs (like GET, POST, PUT, DELETE, etc.) within TypeScript and JavaScript projects. This package aims to eliminate the use of 'magic strings' for HTTP methods, thereby improving code readability and maintainability, especially in applications that interact heavily with RESTful APIs. As a focused utility, its release cadence is tied primarily to any updates in HTTP method specifications, though it is generally stable and self-contained. It differentiates itself by providing a simple, declarative enum without additional runtime utilities.
Common errors
-
ReferenceError: HTTPMethod is not defined
cause Attempting to use `HTTPMethod` in a CommonJS environment without properly destructuring the `require` call.fixCorrect the CommonJS import: `const { HTTPMethod } = require('http-method-enum');` -
SyntaxError: Named export 'HTTPMethod' not found. The requested module 'http-method-enum' does not provide an export named 'HTTPMethod'
cause Using a default import (`import HTTPMethod from ...`) for the `HTTPMethod` enum, which is a named export.fixChange the import statement to use named import syntax: `import { HTTPMethod } from 'http-method-enum';` -
TypeError: Cannot read properties of undefined (reading 'GET')
cause `HTTPMethod` variable is `undefined` because of an incorrect `import` or `require` statement, or a typo in the variable name.fixVerify that the `import` or `require` statement correctly retrieves the `HTTPMethod` named export, ensuring it matches the module system (ESM or CommonJS) you are using. -
TS2305: Module '"http-method-enum"' has no exported member 'StatusCode'.
cause Attempting to import `StatusCode` from `http-method-enum`, which is not exported by this package (as indicated by the README's incorrect example).fixRemove `StatusCode` from the import statement. This package only provides `HTTPMethod`.
Warnings
- gotcha The README's usage example for ESNext/TypeScript (`if (res.method = HTTPMethod.GET)`) contains a common assignment mistake (`=`) instead of a comparison operator (`===`). Always use `===` for equality checks.
- gotcha The `HTTPMethod` enum uses string literal values (e.g., `'GET'`, `'POST'`). It is not a numeric enum. Ensure your comparisons are based on string equality.
- gotcha The README includes an `import { StatusCode } from 'http-method-enum'` example, but `StatusCode` is not exported by this package. This package is solely for HTTP *methods*.
Install
-
npm install http-method-enum -
yarn add http-method-enum -
pnpm add http-method-enum
Imports
- HTTPMethod
import HTTPMethod from 'http-method-enum';
import { HTTPMethod } from 'http-method-enum'; - HTTPMethod
const HTTPMethod = require('http-method-enum');const { HTTPMethod } = require('http-method-enum'); - HTTPMethod
import type { HTTPMethod } from 'http-method-enum';
Quickstart
import { HTTPMethod } from 'http-method-enum';
// Example: A simple server-side route handler that uses the HTTPMethod enum
function handleRequest(method: string, path: string): string {
// Cast the incoming method string to HTTPMethod for type safety and IntelliSense
switch (method.toUpperCase() as HTTPMethod) {
case HTTPMethod.GET:
if (path === '/api/status') {
return 'Server status: Operational.';
}
return 'GET request handled for: ' + path;
case HTTPMethod.POST:
if (path === '/api/data') {
return 'Data successfully created via POST.';
}
return 'POST request handled for: ' + path;
case HTTPMethod.DELETE:
return 'Resource deleted.';
default:
return `Method ${method} is not supported or implemented for ${path}.`;
}
}
console.log(handleRequest('GET', '/api/status'));
console.log(handleRequest('POST', '/api/data'));
console.log(handleRequest('PUT', '/api/users/123'));