HTTP Method Enum

1.0.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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'));

view raw JSON →