{"id":16389,"library":"http-status-response-codes","title":"HTTP Status Code Utilities","description":"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.","status":"maintenance","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/KaustubhKhati/http-status-codes","tags":["javascript","http","nodejs","statuscode"],"install":[{"cmd":"npm install http-status-response-codes","lang":"bash","label":"npm"},{"cmd":"yarn add http-status-response-codes","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-status-response-codes","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package primarily uses CommonJS. Direct ESM `import` statements may not work or could require specific Node.js configuration (e.g., using a CommonJS wrapper).","wrong":"import HttpStatus from 'http-status-response-codes';","symbol":"HttpStatus","correct":"const HttpStatus = require('http-status-response-codes');"},{"note":"Access to the `statusCodes` enum is via named CommonJS `require` destructuring. ESM imports are not directly supported by the examples or implied by the package's age.","wrong":"import { statusCodes } from 'http-status-response-codes';","symbol":"statusCodes","correct":"const { statusCodes } = require('http-status-response-codes');"},{"note":"These are instance methods on the `HttpStatus` class, not direct exports. Ensure you instantiate the class first.","symbol":"is2xxSuccessful","correct":"const httpStatus = new HttpStatus('200'); httpStatus.is2xxSuccessful();"}],"quickstart":{"code":"const HttpStatus = require('http-status-response-codes');\nconst { statusCodes } = require('http-status-response-codes');\n\nasync function demonstrateHttpStatus() {\n  // Example 1: Instantiate with a numeric string\n  const notFoundStatus = new HttpStatus('404');\n  console.log(`Code: ${notFoundStatus.getCodeValue()} Name: ${notFoundStatus.getName()}`);\n  console.log(`Is client error? ${notFoundStatus.is4xxClientError()}`); // true\n  console.log(`Is error? ${notFoundStatus.isError()}`); // true\n  console.log(`Description: ${notFoundStatus.getDescription()}`);\n\n  // Example 2: Instantiate with a mock response object (simulating an HTTP client response)\n  // The package expects an object with a '.status' property, similar to older libraries like request-promise.\n  const mockSuccessResponse = { status: 200, body: 'OK' }; // Mock a response object\n  const successStatus = new HttpStatus(mockSuccessResponse);\n  console.log(`Code: ${successStatus.getCodeValue()} Name: ${successStatus.getName()}`);\n  console.log(`Is successful? ${successStatus.is2xxSuccessful()}`); // true\n  console.log(`Is error? ${successStatus.isError()}`); // false\n\n  // Example 3: Accessing the statusCodes enum for numerical values\n  console.log(`HTTP_BAD_REQUEST: ${statusCodes.BAD_REQUEST}`); // 400\n  console.log(`HTTP_INTERNAL_SERVER_ERROR: ${statusCodes.INTERNAL_SERVER_ERROR}`); // 500\n\n  // Using a simulated server error response\n  const simulatedResponse = { status: 503, statusText: 'Service Unavailable' };\n  const serverErrorStatus = new HttpStatus(simulatedResponse);\n  if (serverErrorStatus.isError()) {\n    console.log(`\nCaught an HTTP error: ${serverErrorStatus.getCodeValue()} - ${serverErrorStatus.getDescription()}`);\n    console.log(`Is server error? ${serverErrorStatus.is5xxServerError()}`); // true\n  }\n}\n\ndemonstrateHttpStatus();","lang":"javascript","description":"This quickstart demonstrates how to instantiate `HttpStatus` with both a string and a mock response object, how to use its categorization methods (`is4xxClientError`, `is2xxSuccessful`, `isError`), and how to access specific status code values from the `statusCodes` object. It highlights common usage patterns for checking HTTP response types."},"warnings":[{"fix":"Use `const HttpStatus = require('http-status-response-codes');` and `const { statusCodes } = require('http-status-response-codes');` for all imports.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure the object passed to `new HttpStatus()` has a `status` property containing the numeric HTTP status code, e.g., `new HttpStatus({ status: response.status })`.","message":"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`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate against more actively maintained alternatives if up-to-date features, comprehensive type support, or continuous maintenance are critical for your project.","message":"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).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure 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 }`).","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.","error":"TypeError: Cannot read properties of undefined (reading 'status')"},{"fix":"The `HttpStatus` class is the default export. Use `const HttpStatus = require('http-status-response-codes');` to import it correctly.","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).","error":"TypeError: HttpStatus is not a constructor"},{"fix":"For CommonJS, use `const { statusCodes } = require('http-status-response-codes');` to correctly destructure the named export.","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.","error":"SyntaxError: Named export 'statusCodes' not found. The requested module 'http-status-response-codes' does not provide an export named 'statusCodes'"}],"ecosystem":"npm"}