{"id":16069,"library":"http-method-enum","title":"HTTP Method Enum","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/typeslick/http-method-enum","tags":["javascript","http","method","enum","typescript"],"install":[{"cmd":"npm install http-method-enum","lang":"bash","label":"npm"},{"cmd":"yarn add http-method-enum","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-method-enum","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"HTTPMethod is a named export. Attempting a default import will fail.","wrong":"import HTTPMethod from 'http-method-enum';","symbol":"HTTPMethod","correct":"import { HTTPMethod } from 'http-method-enum';"},{"note":"For CommonJS, use destructuring to access the named export.","wrong":"const HTTPMethod = require('http-method-enum');","symbol":"HTTPMethod","correct":"const { HTTPMethod } = require('http-method-enum');"},{"note":"When only using the type of the enum in TypeScript, a type-only import can be used.","symbol":"HTTPMethod","correct":"import type { HTTPMethod } from 'http-method-enum';"}],"quickstart":{"code":"import { HTTPMethod } from 'http-method-enum';\n\n// Example: A simple server-side route handler that uses the HTTPMethod enum\nfunction handleRequest(method: string, path: string): string {\n  // Cast the incoming method string to HTTPMethod for type safety and IntelliSense\n  switch (method.toUpperCase() as HTTPMethod) {\n    case HTTPMethod.GET:\n      if (path === '/api/status') {\n        return 'Server status: Operational.';\n      }\n      return 'GET request handled for: ' + path;\n    case HTTPMethod.POST:\n      if (path === '/api/data') {\n        return 'Data successfully created via POST.';\n      }\n      return 'POST request handled for: ' + path;\n    case HTTPMethod.DELETE:\n      return 'Resource deleted.';\n    default:\n      return `Method ${method} is not supported or implemented for ${path}.`;\n  }\n}\n\nconsole.log(handleRequest('GET', '/api/status'));\nconsole.log(handleRequest('POST', '/api/data'));\nconsole.log(handleRequest('PUT', '/api/users/123'));","lang":"typescript","description":"Demonstrates how to import and use the `HTTPMethod` enum for type-safe comparisons and conditional logic within a request handler function."},"warnings":[{"fix":"Always use `===` for comparison: `if (res.method === HTTPMethod.GET)`","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"No fix needed, this is by design. Just be aware that `HTTPMethod.GET === 'GET'` is true.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Remove any imports for `StatusCode` from `http-method-enum`. This package only exports `HTTPMethod`.","message":"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*.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Correct the CommonJS import: `const { HTTPMethod } = require('http-method-enum');`","cause":"Attempting to use `HTTPMethod` in a CommonJS environment without properly destructuring the `require` call.","error":"ReferenceError: HTTPMethod is not defined"},{"fix":"Change the import statement to use named import syntax: `import { HTTPMethod } from 'http-method-enum';`","cause":"Using a default import (`import HTTPMethod from ...`) for the `HTTPMethod` enum, which is a named export.","error":"SyntaxError: Named export 'HTTPMethod' not found. The requested module 'http-method-enum' does not provide an export named 'HTTPMethod'"},{"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.","cause":"`HTTPMethod` variable is `undefined` because of an incorrect `import` or `require` statement, or a typo in the variable name.","error":"TypeError: Cannot read properties of undefined (reading 'GET')"},{"fix":"Remove `StatusCode` from the import statement. This package only provides `HTTPMethod`.","cause":"Attempting to import `StatusCode` from `http-method-enum`, which is not exported by this package (as indicated by the README's incorrect example).","error":"TS2305: Module '\"http-method-enum\"' has no exported member 'StatusCode'."}],"ecosystem":"npm"}