HTTP Status Code Utilities
The `http-status-response-codes` package (current version 1.0.2) provides a JavaScript utility for handling HTTP status codes, drawing inspiration from Java's Spring Boot framework. It exports an `HttpStatus` class that can be instantiated with a numeric status code (as a string) or a response-like object containing a `status` property. Instances of `HttpStatus` offer methods to categorize the status code (e.g., `is2xxSuccessful()`, `is4xxClientError()`, `isError()`, `is1xxInformational()`, `is3xxRedirection()`, `is5xxServerError()`), and retrieve its name, code value, and description. Additionally, the package exports a `statusCodes` object, providing a convenient enum-like collection of standard HTTP status codes. This library facilitates programmatic handling and classification of HTTP responses in a structured manner. Given its version and CommonJS-centric examples, the package appears to be stable and feature-complete, but not actively undergoing modern JavaScript evolution.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'status')
cause The `HttpStatus` constructor was called with an `undefined` or `null` value, or an object missing a `status` property, when attempting to extract the status code.fixEnsure the argument passed to `new HttpStatus()` is either a valid numeric string (e.g., `'200'`) or an object with a `status` property (e.g., `{ status: 200 }`). -
TypeError: HttpStatus is not a constructor
cause Attempting to use `HttpStatus` as a class constructor after an incorrect import (e.g., `const HttpStatus = require('http-status-response-codes').HttpStatus;` instead of directly `require`ing the default export).fixThe `HttpStatus` class is the default export. Use `const HttpStatus = require('http-status-response-codes');` to import it correctly. -
SyntaxError: Named export 'statusCodes' not found. The requested module 'http-status-response-codes' does not provide an export named 'statusCodes'
cause Attempting to use ESM `import { statusCodes } from 'http-status-response-codes';` in a CommonJS-only environment or for a package that does not explicitly support ESM named exports.fixFor CommonJS, use `const { statusCodes } = require('http-status-response-codes');` to correctly destructure the named export.
Warnings
- gotcha The package is predominantly CommonJS (CJS) as indicated by all examples using `require()`. Attempting to use ESM `import` statements directly might lead to runtime errors or require specific build tool configurations, as the package may not explicitly export ESM modules.
- gotcha The `HttpStatus` constructor can accept a 'Response Object' as input, but the exact structure is implicitly tied to older HTTP client libraries (e.g., `request-promise` used in the README's main example). Modern `fetch` API or `axios` response objects might require manually extracting the `status` property before passing it to `HttpStatus`.
- gotcha The package appears to be in a maintenance mode. It has a low version number (1.0.2) and the provided examples use older JavaScript patterns and libraries (like `request-promise`). While functional, it might not receive updates for new HTTP status codes, security patches, or modern JavaScript features (like TypeScript definitions or native ESM support).
Install
-
npm install http-status-response-codes -
yarn add http-status-response-codes -
pnpm add http-status-response-codes
Imports
- HttpStatus
import HttpStatus from 'http-status-response-codes';
const HttpStatus = require('http-status-response-codes'); - statusCodes
import { statusCodes } from 'http-status-response-codes';const { statusCodes } = require('http-status-response-codes'); - is2xxSuccessful
const httpStatus = new HttpStatus('200'); httpStatus.is2xxSuccessful();
Quickstart
const HttpStatus = require('http-status-response-codes');
const { statusCodes } = require('http-status-response-codes');
async function demonstrateHttpStatus() {
// Example 1: Instantiate with a numeric string
const notFoundStatus = new HttpStatus('404');
console.log(`Code: ${notFoundStatus.getCodeValue()} Name: ${notFoundStatus.getName()}`);
console.log(`Is client error? ${notFoundStatus.is4xxClientError()}`); // true
console.log(`Is error? ${notFoundStatus.isError()}`); // true
console.log(`Description: ${notFoundStatus.getDescription()}`);
// Example 2: Instantiate with a mock response object (simulating an HTTP client response)
// The package expects an object with a '.status' property, similar to older libraries like request-promise.
const mockSuccessResponse = { status: 200, body: 'OK' }; // Mock a response object
const successStatus = new HttpStatus(mockSuccessResponse);
console.log(`Code: ${successStatus.getCodeValue()} Name: ${successStatus.getName()}`);
console.log(`Is successful? ${successStatus.is2xxSuccessful()}`); // true
console.log(`Is error? ${successStatus.isError()}`); // false
// Example 3: Accessing the statusCodes enum for numerical values
console.log(`HTTP_BAD_REQUEST: ${statusCodes.BAD_REQUEST}`); // 400
console.log(`HTTP_INTERNAL_SERVER_ERROR: ${statusCodes.INTERNAL_SERVER_ERROR}`); // 500
// Using a simulated server error response
const simulatedResponse = { status: 503, statusText: 'Service Unavailable' };
const serverErrorStatus = new HttpStatus(simulatedResponse);
if (serverErrorStatus.isError()) {
console.log(`
Caught an HTTP error: ${serverErrorStatus.getCodeValue()} - ${serverErrorStatus.getDescription()}`);
console.log(`Is server error? ${serverErrorStatus.is5xxServerError()}`); // true
}
}
demonstrateHttpStatus();