/** * Typed response values. A route handler returns one instead of writing to * Express `res`; the framework renders it. Descriptors are immutable data: * build them with the `HttpResponse` factories, never by hand. * * @example * async create(req: CreateUserRequest): Promise> { * const user = await this.app.getModel('User').create(req.appInfo.request); * return HttpResponse.json(201, user); * } */ /** Registered, not module-private, so two installed copies of the framework recognize each other's responses. */ declare const BRAND: unique symbol; /** Statuses that never carry a body; only `HttpResponse.empty` accepts them. */ export type NoBodyStatus = 204 | 205 | 304; /** Statuses accepted by `HttpResponse.redirect`. */ export type RedirectStatus = 301 | 302 | 303 | 307 | 308; /** A literal status a body response cannot use (1xx, 204/205/304, 206) becomes `never`. */ type BodyStatus = S extends NoBodyStatus | 206 ? never : `${S}` extends `1${string}` ? never : S; /** A literal status `HttpResponse.empty` cannot use (1xx, 206) becomes `never`. */ type EmptyStatus = S extends 206 ? never : `${S}` extends `1${string}` ? never : S; /** Response headers: lower-case names, one entry per `set-cookie` value. */ export type ResponseHeaders = ReadonlyArray; /** Anything the Web `Headers` constructor accepts, including a descriptor's `headers`. */ export type ResponseHeadersInit = Headers | Record | ReadonlyArray; /** A byte source for `HttpResponse.stream`; a Node `Readable` is an async iterable too. */ export type ByteStream = ReadableStream | AsyncIterable; export interface ResponseOptions { headers?: ResponseHeadersInit; } export interface BodyResponseOptions extends ResponseOptions { /** Media type; set it here, not as a `content-type` header. */ contentType?: M; } interface ResponseBase { readonly [BRAND]: true; readonly kind: K; readonly status: S; readonly headers: ResponseHeaders; } export interface JsonResponse extends ResponseBase<'json', S> { readonly contentType: M; readonly body: B; } export interface TextResponse extends ResponseBase<'text', S> { readonly contentType: M; readonly body: string; } export interface BytesResponse extends ResponseBase<'bytes', S> { readonly contentType: M; readonly body: Uint8Array; } export interface StreamResponse extends ResponseBase<'stream', S> { readonly contentType: M; readonly body: ByteStream; } export interface RedirectResponse extends ResponseBase<'redirect', S> { readonly location: string; } export interface EmptyResponse extends ResponseBase<'empty', S> { } /** A Web `Response` passed through as-is; its status and body types are not tracked. */ export interface NativeResponse extends ResponseBase<'native', number> { readonly response: Response; } /** Statuses that never carry a body, as a runtime set. */ export declare const NO_BODY_STATUSES: ReadonlySet; declare function json(status: BodyStatus, body: B, options?: BodyResponseOptions): JsonResponse; declare function text(status: BodyStatus, body: string, options?: BodyResponseOptions): TextResponse; declare function bytes(status: BodyStatus, body: Uint8Array, options?: BodyResponseOptions): BytesResponse; /** * Stream bytes. The status and headers are sent with the first chunk, so an * error before it is answered like a thrown error; a later error aborts the * response. A client that disconnects stops the source (`return()` on the * iterator, `cancel()` on a `ReadableStream`). */ declare function stream(status: BodyStatus, body: ByteStream, options?: BodyResponseOptions): StreamResponse; declare function redirect(status: S, location: string | URL, options?: ResponseOptions): RedirectResponse; declare function empty(status: EmptyStatus, options?: ResponseOptions): EmptyResponse; /** * Pass a Web `Response` through: its status, status text, headers and body * are sent as they are, except the framing headers the server sets itself * (`connection`, `transfer-encoding`, …). A `fetch()` response (non-empty * `url`) with a body, a status other than 206 and only codings `fetch` decodes * (`gzip`, `deflate`, `br`, `zstd`; under Bun a single one) also loses its * upstream `content-encoding`, `content-length` and digest headers, which * describe the encoded bytes; any other response is passed on as received, * headers intact. * To forward the upstream bytes exactly as received (for example fetched with * decoding turned off), pass `new Response(upstream.body, upstream)`, which * has no `url`. */ declare function native(response: Response): NativeResponse; /** Is `value` a response built by an `HttpResponse` factory? */ declare function is(value: unknown): value is HttpResponse; /** * Response factories. Status is always the first argument. Statuses a kind * cannot use fail to compile when written as literals and throw a * `RangeError` at runtime: 1xx everywhere; 204, 205 and 304 everywhere but * `empty`; 206 (reserved for range-aware file responses) everywhere. */ export declare const HttpResponse: Readonly<{ json: typeof json; text: typeof text; bytes: typeof bytes; stream: typeof stream; redirect: typeof redirect; empty: typeof empty; native: typeof native; is: typeof is; }>; /** Any response a handler can return. */ export type HttpResponse = JsonResponse | TextResponse | BytesResponse | StreamResponse | RedirectResponse | EmptyResponse | NativeResponse; export {};