HTTP methods related enums and utilities.
Zero runtime dependencies. Ships both ESM and CommonJS builds, with TypeScript types for each.
Node 22 or newer (engines.node is >=22) — every line still receiving security support. Each release is tested on 22, 24 and 26; the declared floor is the lowest version CI actually runs, not a guess.
npm i @tselect/http-method
pnpm add @tselect/http-method
import { HTTPMethod, isHTTPMethod } from '@tselect/http-method'; HTTPMethod.GET; // 'get' isHTTPMethod(HTTPMethod.GET); // true
Namespace imports and require() both work:
import * as HTTPMethodUtils from '@tselect/http-method';
const { HTTPMethod, isHTTPMethod } = require('@tselect/http-method');
A string enum of the supported HTTP methods. Every member's value is lower case.
HTTPMethod.GET; // 'get' HTTPMethod.POST; // 'post' HTTPMethod.PATCH; // 'patch' HTTPMethod.DELETE; // 'delete' HTTPMethod.HEAD; // 'head' HTTPMethod.OPTIONS; // 'options' HTTPMethod.PUT; // 'put'
A union of every method name in both lower and constant case — 'get' | 'post' | … | 'GET' | 'POST' | …. Every function below accepts it, so the enum, a lower case literal and a constant case literal are all valid arguments.
import type { THTTPMethod } from '@tselect/http-method'; const method: THTTPMethod = 'PATCH';
Returns whether an arbitrary string names an HTTP method. Case-insensitive, and unlike the functions below it accepts any string, so it is the one to reach for when validating untrusted input.
isHTTPMethod('get'); // true isHTTPMethod('GET'); // true isHTTPMethod('gEt'); // true isHTTPMethod('foo'); // false
Returns the lower case form, which is the form the HTTPMethod enum uses.
toLowerCase('GET'); // 'get' toLowerCase('get'); // 'get'
Returns the upper case form, as used in most HTTP tooling and in the Allow header.
toConstantCase('get'); // 'GET' toConstantCase('GET'); // 'GET'
Returns the method with its first character upper cased and the rest lower cased. Useful for building method-derived identifiers such as onGet or handlePost.
toPascalCase('get'); // 'Get' toPascalCase('POST'); // 'Post'
MIT © Sylvain Estevez