HTTP Codex
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
-
TypeError: httpStatus.text is not a function
cause Attempting to call `text()` on `httpStatus` after importing it from `'http-codex/status'`.fixChange the import to `import { httpStatus } from 'http-codex'` to access the `statusText()` helper, or manually map status codes to text if `http-codex/status` is specifically required for bundle size. -
ERR_MODULE_NOT_FOUND: Cannot find module 'http-codex' from '...' or SyntaxError: Cannot use import statement outside a module
cause Attempting to `require('http-codex')` in a CommonJS environment or mixing CJS and ESM import styles.fixEnsure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`) and use `import { ... } from 'http-codex'` syntax. If a CommonJS environment is strictly required, consider using a bundler like Webpack or Rollup to transpile.
Warnings
- gotcha The `statusText()` helper function is only available when importing `httpStatus` from the main `http-codex` package entry point. It is *not* exported when importing `httpStatus` specifically from `http-codex/status` to reduce bundle size.
- gotcha For optimal bundle size, avoid importing the entire `http-codex` package if you only need HTTP methods or status codes. Importing directly from `http-codex/method` or `http-codex/status` significantly reduces the payload.
- gotcha This library is designed to mimic Go's `net/http` package API. Developers accustomed to Node.js-specific HTTP libraries or other JavaScript HTTP utilities might find the API patterns slightly different.
Install
-
npm install http-codex -
yarn add http-codex -
pnpm add http-codex
Imports
- httpStatus
const httpStatus = require('http-codex')import { httpStatus } from 'http-codex' - httpMethod
import { httpMethod } from 'http-codex'import { httpMethod } from 'http-codex/method' - isNullBodyStatus
import { isNullBodyStatus } from 'http-codex/status'import { isNullBodyStatus } from 'http-codex'
Quickstart
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);