@edgetime/http
Problem-details errors, size-limited body readers, validation, cookies, conditional and range requests. No dependencies.
npm install @edgetime/httpHTTP building blocks on the Fetch API: errors, body readers, cookies, conditional requests and validation. No dependencies, and no dependency on the router.
Errors
Section titled “Errors”import { errorHandler, HttpError } from '@edgetime/http';
throw new HttpError(404, 'No such order'); // the status must be 400 to 599throw new HttpError(429, { detail: 'Slow down', headers: { 'retry-after': '60' } });
router.onError( errorHandler({ report: (error, c) => log.error('request failed', { error }), extensions: (c) => ({ requestId: c.get('requestId') }), }),);errorHandler() answers with RFC 9457 problem details
(application/problem+json):
- An
HttpErroris sent as written, including itsdetail,headersandextensions. - Any other error with a
statusfrom 400 to 599 keeps that status. Its message is not sent, and of itsheadersonlyAllow,Retry-AfterandWWW-Authenticateare kept. - Everything else becomes
500with a generic title. reportis called for every 5xx.- A
report,extensions,maporbodycallback that throws is ignored; the response keeps its status and headers. - Responses carry
Cache-Control: no-storeunless the error sets its owncache-controlheader. map(error, c)translates your own error types into anHttpErrorbefore rendering; returnundefinedto leave an error alone.reportstill receives the original.unexpectedStatus: 503answers503instead of500for an error that names no status. Any value other than500or503throws when the handler is created.- A callback that returns a promise, or the wrong kind of value, is ignored like one that
throws:
mapmust return anHttpError,extensionsa plain object andbodya value.
To keep a different error shape, pass body. It receives the problem document and returns the
JSON to send; the status and headers are unchanged.
errorHandler({ body: (problem) => ({ error: problem.detail ?? problem.title }) });Bodies
Section titled “Bodies”readBytes, readText, readJson and readForm take a request or response and { limit }. The limit is in
bytes and defaults to DEFAULT_BODY_LIMIT (1 MiB). A larger body is refused with 413, checked
against Content-Length first and then against the bytes actually read. Invalid UTF-8 and
malformed JSON are 400; a limit that is not a number throws; readJson and readForm answer 415 for the wrong content type.
Validation
Section titled “Validation”validate, parseJson and parseQuery accept any Standard Schema
validator, such as Zod 4, Valibot or ArkType. A failure throws 422 with each issue listed under
errors as { path, message }. At most 100 issues are listed, with long messages cut; the
number left out is given as moreErrors.
import { parseJson } from '@edgetime/http';import { z } from 'zod';
const input = await parseJson(c.req, z.object({ title: z.string().min(1) }), { limit: 16 * 1024 });queryObject(params) turns search parameters into an object, with repeated keys as arrays.
Cookies
Section titled “Cookies”parseCookies(header), getCookie(request, name), serializeCookie(name, value, options) and
expireCookie(name, options). Cookies are HttpOnly, Secure, SameSite=Lax and Path=/
unless the options say otherwise. serializeCookie percent-encodes a value that a cookie cannot
carry as is. It throws on an invalid name, path, domain, sameSite or maxAge, on
SameSite=None or Partitioned without Secure, and on a __Host- or __Secure- name, in any
letter case, whose attributes break the prefix rules.
Conditional and range requests
Section titled “Conditional and range requests”| Export | Purpose |
|---|---|
etagMatches(ifNoneMatch, etag) |
Weak comparison of an If-None-Match header. |
isNotModified(request, validators) |
Whether a GET or HEAD can be answered with 304. |
notModified(validators, headers?) |
The 304 response. |
parseRange(header, size) |
A single byte range as { offset, length }, 'unsatisfiable' for 416, or undefined to serve the whole body. size must be a non-negative whole number. |
Responses
Section titled “Responses”json, text, html, noContent and redirect build responses. withHeaders(response, change)
edits headers on any response, copying it first when its headers are immutable. The copy keeps
the original’s url. appendVary adds
to Vary without duplicating.
Source: packages/http on GitHub.