@orveth/http

Shared HTTP constants and tiny response helpers. @orveth/server uses these primitives when serializing JSON and text responses; application code can import the same utilities for consistency outside the built-in runtime.

Exports at a glance

SymbolRole
HttpStatusCurated numeric status constants reused across packages.
HttpStatusCodeUnion type of literals contained in HttpStatus.
ContentTypeCommon Content-Type header values (JSON, plain text, HTML) with UTF-8 charset.
prepareJsonResponseBuilds status, headers, and JSON-encoded body via JSON.stringify.
prepareTextResponseBuilds status, headers, and UTF-8 plain text body.
prepareJsonErrorResponseThin wrapper for minimal { code, message } JSON error payloads.
getHeader, normalizeHeaderName, acceptsJsonHeader inspection helpers operating on a RawHeaders map.
RawHeadersRead-only record compatible with Node's IncomingHttpHeaders shape without importing Node types.

HttpStatus constants

The object is intentionally small. It covers the statuses Orveth libraries reference today, not every valid HTTP code.

KeyValue
HttpStatus.OK200
HttpStatus.CREATED201
HttpStatus.NO_CONTENT204
HttpStatus.BAD_REQUEST400
HttpStatus.UNAUTHORIZED401
HttpStatus.FORBIDDEN403
HttpStatus.NOT_FOUND404
HttpStatus.CONFLICT409
HttpStatus.UNPROCESSABLE_ENTITY422
HttpStatus.TOO_MANY_REQUESTS429
HttpStatus.INTERNAL_SERVER_ERROR500
HttpStatus.NOT_IMPLEMENTED501
HttpStatus.BAD_GATEWAY502
HttpStatus.SERVICE_UNAVAILABLE503

Response preparation

prepareJsonResponse(status, payload) sets content-type to ContentType.jsonUtf8 and returns { status, headers, body }. prepareTextResponse(status, body) mirrors that flow for plain text.

lib/health.ts
import { HttpStatus, prepareJsonResponse } from "@orveth/http";

export function healthPayload() {
  return prepareJsonResponse(HttpStatus.OK, {
    status: "ok",
    uptimeSeconds: process.uptime(),
  });
}

Header helpers

getHeader(headers, name) performs case-insensitive lookup. acceptsJson inspects Accept for an application/json media range, which is useful when deciding whether to emit JSON or HTML from higher-level handlers.

lib/negotiate.ts
import { acceptsJson, type RawHeaders } from "@orveth/http";

export function prefersJson(headers: RawHeaders): boolean {
  return acceptsJson(headers);
}

Relationship to other packages

  • @orveth/server consumes these helpers when writing ctx.json and ctx.text, and when mapping thrown errors to HTTP responses.
  • @orveth/errors remains the home for error classes; this package does not define new error types.