HTTP Core Constants Library
The `http-core-constants` library provides a JavaScript/TypeScript port of standard HTTP constants, encompassing headers, status codes, and request methods. These constants are derived directly from the `org.apache.httpcomponents:httpcore` Java library, offering a familiar set of values for developers migrating from Java environments or seeking a standardized set of HTTP definitions. The current stable version is 1.3.0, released in February 2020. The package has seen no updates since then, indicating an abandoned status. A key differentiator is its inclusion of `SC_` prefixed status codes (mirroring Apache's Java library) alongside non-prefixed, more readable alternatives. It primarily serves to eliminate 'magic strings' in HTTP-related code, promoting type safety and maintainability in TypeScript projects by exposing these constants as enums.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'ACCEPT')
cause This typically occurs when attempting to access a named export from a CommonJS `require` statement without explicitly destructuring it, or when using an incorrect import path.fixFor ESM, ensure named imports are used: `import { HttpHeaders } from 'http-core-constants';`. For CommonJS (if using an older version or transpiled code), ensure destructuring: `const { HttpHeaders } = require('http-core-constants');`. -
TypeError: HttpHeaders is not a constructor
cause The constants (e.g., `HttpHeaders`, `HttpStatus`) are exported as enums (or plain objects in older versions) and are not intended to be instantiated with `new`.fixAccess constants directly as properties of the imported enum/object, for example: `HttpHeaders.ACCEPT` or `HttpStatus.SC_OK`.
Warnings
- breaking Version 1.3.0 converted all constant modules (HttpHeaders, HttpStatus, HttpRequestMethod) from plain objects to TypeScript enum declarations. This change can impact consumers relying on object-like behaviors such as iteration over properties or dynamic property access, and may alter runtime types for some JavaScript environments.
- gotcha The `http-core-constants` package has not been updated since February 2020. This indicates the package is likely abandoned and will not receive updates for new HTTP standards, bug fixes, or security patches. Consider using more actively maintained alternatives for critical projects.
Install
-
npm install http-core-constants -
yarn add http-core-constants -
pnpm add http-core-constants
Imports
- HttpHeaders
const HttpHeaders = require('http-core-constants').HttpHeaders;import { HttpHeaders } from 'http-core-constants'; - HttpStatus
const HttpStatus = require('http-core-constants').HttpStatus;import { HttpStatus } from 'http-core-constants'; - HttpRequestMethod
const HttpRequestMethod = require('http-core-constants').HttpRequestMethod;import { HttpRequestMethod } from 'http-core-constants';
Quickstart
import { HttpHeaders, HttpStatus, HttpRequestMethod } from 'http-core-constants';
console.log(`Accept header: ${HttpHeaders.ACCEPT}`);
console.log(`OK status code (SC_OK): ${HttpStatus.SC_OK}`);
console.log(`OK status code (OK): ${HttpStatus.OK}`);
console.log(`HTTP GET method: ${HttpRequestMethod.GET}`);
// Example using status code in a mock response
class MockHttpResponse {
status: HttpStatus;
constructor(status: HttpStatus) {
this.status = status;
}
send(body: string) {
console.log(`Sending response with status ${this.status}: ${body}`);
}
}
const response = new MockHttpResponse(HttpStatus.OK);
response.send('Operation successful!');