HTTP Enums (Status, Methods, Headers)

1.0.5 · abandoned · verified Wed Apr 22

http-enums is a utility library providing standardized JavaScript and TypeScript enumerations for common HTTP concepts, including status codes, request methods, and frequently used request and response headers. As of version 1.0.5, last published approximately three years ago (around January 2020), it offers a straightforward and type-safe way to reference these constants, aiming to reduce the use of 'magic strings' in web development projects. Due to its static nature as a collection of constants, it generally does not require frequent updates; however, the lack of recent maintenance means it may not incorporate the latest HTTP specification changes or benefit from community-driven improvements. It differentiates itself by its singular focus on providing comprehensive HTTP enums, suitable for both client-side and server-side applications where explicit constant definitions enhance code clarity.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing various HTTP enumerations (status, method, request/response headers) and using their values in a TypeScript function.

import { HttpStatus, HttpMethod, HttpRequestHeaders, HttpResponseHeaders } from 'http-enums';

function handleRequest(method: HttpMethod, status: HttpStatus, headers: Record<string, string>): void {
  console.log(`Received ${method} request with status ${status}.`);

  if (method === HttpMethod.GET) {
    console.log('This is a GET request.');
  }

  if (status === HttpStatus.NOT_FOUND) {
    console.log('Resource not found.');
  }

  if (headers[HttpRequestHeaders.ACCEPT_LANGUAGE] === 'en-US') {
    console.log('Client prefers US English.');
  }

  console.log(`Setting Content-Type to: ${HttpResponseHeaders.CONTENT_TYPE_APPLICATION_JSON}`);
}

handleRequest(HttpMethod.POST, HttpStatus.CREATED, { 'accept-language': 'en-US', [HttpRequestHeaders.CONTENT_TYPE]: 'application/json' });
handleRequest(HttpMethod.GET, HttpStatus.OK, { 'user-agent': 'MyBrowser/1.0' });

view raw JSON →