Registry / http-networking / http-method-enum

http-method-enum

JSON →
library1.0.0jsnpmunverified

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.

npm install http-method-enum
INSTALL
IMPORT
SIG · HTTP-METHOD-ENUM
H
http-method-enum
http-networkingjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HTTPMethod
import { HTTPMethod } from 'http-method-enum';
import HTTPMethod from 'http-method-enum';
HTTPMethod is a named export. Attempting a default import will fail.
HTTPMethod
const { HTTPMethod } = require('http-method-enum');
const HTTPMethod = require('http-method-enum');
For CommonJS, use destructuring to access the named export.
HTTPMethod
import type { HTTPMethod } from 'http-method-enum';
When only using the type of the enum in TypeScript, a type-only import can be used.

Demonstrates how to import and use the `HTTPMethod` enum for type-safe comparisons and conditional logic within a request handler function.

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'));
Debug
Known issues
gotchaThe 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.
fix
Always use `===` for comparison: `if (res.method === HTTPMethod.GET)`
affects: >=1.0.0
gotchaThe `HTTPMethod` enum uses string literal values (e.g., `'GET'`, `'POST'`). It is not a numeric enum. Ensure your comparisons are based on string equality.
fix
No fix needed, this is by design. Just be aware that `HTTPMethod.GET === 'GET'` is true.
affects: >=1.0.0
gotchaThe 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*.
fix
Remove any imports for `StatusCode` from `http-method-enum`. This package only exports `HTTPMethod`.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: HTTPMethod is not defined
Attempting to use `HTTPMethod` in a CommonJS environment without properly destructuring the `require` call.
fix
Correct 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'
Using a default import (`import HTTPMethod from ...`) for the `HTTPMethod` enum, which is a named export.
fix
Change the import statement to use named import syntax: `import { HTTPMethod } from 'http-method-enum';`
TypeError: Cannot read properties of undefined (reading 'GET')
`HTTPMethod` variable is `undefined` because of an incorrect `import` or `require` statement, or a typo in the variable name.
fix
Verify 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'.
Attempting to import `StatusCode` from `http-method-enum`, which is not exported by this package (as indicated by the README's incorrect example).
fix
Remove `StatusCode` from the import statement. This package only provides `HTTPMethod`.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
http-method-enum — npm install http-method-enum · libregistry