HTTP Core Constants Library

1.3.0 · abandoned · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates importing and using various HTTP constants for headers, status codes, and request methods.

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!');

view raw JSON →