HTTP Codex

0.6.6 · active · verified Tue Apr 21

http-codex is a lightweight JavaScript/TypeScript library providing constants for standard HTTP status codes and methods, inspired by and closely mirroring the API of Go's `net/http` package. It is currently at version `0.6.6` and appears to be actively maintained, being part of a larger monorepo with recent updates to other packages. The library differentiates itself by its explicit goal to align with Go's `http` package conventions and its focus on bundle size, offering granular imports (e.g., `http-codex/status` or `http-codex/method`) to minimize the loaded code. It includes helper functions like `isNullBodyStatus` for common HTTP response handling patterns. While no strict release cadence is stated, its presence in an active monorepo suggests ongoing development and support.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `http-codex` to construct `Response` objects with proper status codes and texts, use HTTP method constants, and apply the `isNullBodyStatus` helper function.

import { httpMethod, httpStatus, isNullBodyStatus } from 'http-codex';

// Example: Creating a simple HTTP response
const createResponse = (body: string | null, statusCode: number) => {
  const statusText = httpStatus.text(statusCode);
  return new Response(body, {
    status: statusCode,
    statusText: statusText,
    headers: { 'Content-Type': 'text/plain' }
  });
};

const successResponse = createResponse('Operation successful!', httpStatus.OK); // Status 200, "OK"
const notFoundResponse = createResponse('Resource not found', httpStatus.NOT_FOUND); // Status 404, "Not Found"
const noContentResponse = createResponse(null, httpStatus.NO_CONTENT); // Status 204, no body

console.log('Success Response:', successResponse.status, successResponse.statusText);
console.log('Not Found Response:', notFoundResponse.status, notFoundResponse.statusText);
console.log('No Content Response:', noContentResponse.status, noContentResponse.statusText);

// Example: Using HTTP methods
const currentMethod = httpMethod.GET; // 'GET'
const allowedMethods = [httpMethod.GET, httpMethod.POST, httpMethod.PUT];

if (!allowedMethods.includes(currentMethod)) {
  console.error(`Method ${currentMethod} is not allowed.`);
} else {
  console.log(`Method ${currentMethod} is allowed.`);
}

// Example: Handling null body statuses
const mockFetchResult = { status: httpStatus.NO_CONTENT, body: 'unwanted content' };
const finalBody = isNullBodyStatus(mockFetchResult.status) ? null : mockFetchResult.body;
console.log('Final body after null body check:', finalBody);

view raw JSON →