HTTP Status Codes for TypeScript
http-status-ts is a lightweight, isomorphic utility library designed for convenient access to standard HTTP status codes within TypeScript projects. It provides a comprehensive list of HTTP status codes as a TypeScript enum (`HttpStatus`) and a helper function (`httpStatusTextByCode`) to retrieve the corresponding human-readable text description. Currently stable at version 2.0.1, the library maintains a steady cadence of releases focused on stability and minor enhancements. Its primary differentiation lies in its explicit TypeScript-first design, providing strong type safety out-of-the-box for both Node.js and browser environments, unlike some plain JavaScript alternatives that may require manual type definitions.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'OK')
cause The `HttpStatus` enum was not correctly imported or was imported as a default export, leading to it being `undefined` or a malformed object.fixEnsure `HttpStatus` is imported as a named export: `import { HttpStatus } from 'http-status-ts';`. -
ReferenceError: httpStatusTextByCode is not defined
cause The `httpStatusTextByCode` function was not correctly imported or was not destructuring from the module in a CommonJS context.fixUse a named import for ESM: `import { httpStatusTextByCode } from 'http-status-ts';`. For CJS: `const { httpStatusTextByCode } = require('http-status-ts');`. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a file that is treated as a CommonJS module (e.g., a `.js` file without `"type": "module"` in `package.json`, or a `.ts` file compiled to CJS without proper transpilation setup).fixEither convert your project to ES Modules by adding `"type": "module"` to your `package.json` (and renaming `.js` to `.mjs` if needed), or configure your TypeScript compiler to output ES Modules. Alternatively, use CommonJS `require()` syntax if your project is strictly CommonJS.
Warnings
- gotcha When using `HttpStatus` enum members, remember that `HttpStatus.OK` directly evaluates to the numeric code `200`. Do not try to access a `.code` property on it, as it is the value itself.
- gotcha Incorrect module imports, especially mixing CommonJS `require` with ES Modules `import` syntax or not correctly destructuring named exports, can lead to `undefined` module members.
Install
-
npm install http-status-ts -
yarn add http-status-ts -
pnpm add http-status-ts
Imports
- HttpStatus
import HttpStatus from 'http-status-ts'; const HttpStatus = require('http-status-ts');import { HttpStatus } from 'http-status-ts'; - httpStatusTextByCode
import httpStatusTextByCode from 'http-status-ts'; const httpStatusTextByCode = require('http-status-ts');import { httpStatusTextByCode } from 'http-status-ts'; - All named exports
import * as HttpStatusModule from 'http-status-ts';
Quickstart
import { HttpStatus, httpStatusTextByCode } from 'http-status-ts';
// Get a status code's numeric value from the enum
const notFoundCode = HttpStatus.NOT_FOUND;
console.log(`The numeric code for 'Not Found' is: ${notFoundCode}`); // Output: 404
// Get the text description for a specific status code
const okText = httpStatusTextByCode(HttpStatus.OK);
console.log(`The text description for ${HttpStatus.OK} is: "${okText}"`); // Output: "OK"
// Example: Dynamically getting text for a user-input code (simulated)
const userProvidedCode = 500;
const errorMessage = httpStatusTextByCode(userProvidedCode);
console.log(`For code ${userProvidedCode}, the error message is: "${errorMessage}"`); // Output: "Internal Server Error"
// Example using status codes in a conceptual HTTP response
interface MockResponse {
status: (code: number) => { send: (message: string) => void };
}
const mockResponse: MockResponse = {
status: (code: number) => ({
send: (message: string) => {
console.log(`[HTTP Response Simulator] Status: ${code}, Body: ${message}`);
}
})
};
const createSuccess = HttpStatus.CREATED;
mockResponse.status(createSuccess).send(httpStatusTextByCode(createSuccess));
const clientError = HttpStatus.BAD_REQUEST;
mockResponse.status(clientError).send(httpStatusTextByCode(clientError));