HttpServerResponse.ts
HttpServerResponse.ts overview
Section titled “HttpServerResponse.ts overview”Describes immutable responses returned by Effect HTTP handlers.
An HttpServerResponse stores the status, optional status text, headers,
cookies, and body that the server runtime later turns into a platform
response such as a Web Response. This module includes constructors for
common response bodies, helpers for updating response data, file response
support through HttpPlatform, and conversions to or from Web and Effect
HTTP client responses.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”combinators
Section titled “combinators”expireCookie
Section titled “expireCookie”Sets an expired cookie on an HttpServerResponse.
Details
Returns an effect because cookie encoding can fail. The original response is not mutated; the effect succeeds with a response containing the updated cookie set.
Signature
declare const expireCookie: { ( name: string, options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge"> ): (self: HttpServerResponse) => Effect.Effect<HttpServerResponse, Cookies.CookiesError> ( self: HttpServerResponse, name: string, options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge"> ): Effect.Effect<HttpServerResponse, Cookies.CookiesError>}Since v4.0.0
expireCookieUnsafe
Section titled “expireCookieUnsafe”Sets an expired cookie on an HttpServerResponse, throwing if the expiration cookie
cannot be encoded.
When to use
Use when you need to expire one trusted cookie and want encoding failures to
throw instead of being represented as CookiesError failures.
Signature
declare const expireCookieUnsafe: { ( name: string, options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge"> ): (self: HttpServerResponse) => HttpServerResponse ( self: HttpServerResponse, name: string, options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge"> ): HttpServerResponse}Since v4.0.0
mergeCookies
Section titled “mergeCookies”Merges additional cookies into the cookies attached to an
HttpServerResponse.
Details
The original response is not mutated; a new response is returned with the merged cookie collection.
Signature
declare const mergeCookies: { (cookies: Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, cookies: Cookies.Cookies): HttpServerResponse}Since v4.0.0
removeCookie
Section titled “removeCookie”Returns a response with the cookie of the specified name removed.
Signature
declare const removeCookie: { (name: string): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, name: string): HttpServerResponse}Since v4.0.0
replaceCookies
Section titled “replaceCookies”Returns a response with its cookie collection replaced by the supplied cookies.
Signature
declare const replaceCookies: { (cookies: Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, cookies: Cookies.Cookies): HttpServerResponse}Since v4.0.0
setBody
Section titled “setBody”Replaces the body of an HttpServerResponse.
Details
When the body carries a content type or content length, the returned response includes the corresponding headers.
Signature
declare const setBody: { (body: Body.HttpBody): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, body: Body.HttpBody): HttpServerResponse}Since v4.0.0
setCookie
Section titled “setCookie”Sets a cookie on the response.
Details
The effect fails with CookiesError if the cookie name, value, or options are
invalid.
Signature
declare const setCookie: { ( name: string, value: string, options?: Cookies.Cookie["options"] ): (self: HttpServerResponse) => Effect.Effect<HttpServerResponse, Cookies.CookiesError> ( self: HttpServerResponse, name: string, value: string, options?: Cookies.Cookie["options"] ): Effect.Effect<HttpServerResponse, Cookies.CookiesError>}Since v4.0.0
setCookieUnsafe
Section titled “setCookieUnsafe”Sets a cookie on an HttpServerResponse, throwing if the cookie cannot be
encoded.
When to use
Use when you need to set one trusted cookie and want encoding failures to
throw instead of being represented as CookiesError failures.
Signature
declare const setCookieUnsafe: { (name: string, value: string, options?: Cookies.Cookie["options"]): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, name: string, value: string, options?: Cookies.Cookie["options"]): HttpServerResponse}Since v4.0.0
setCookies
Section titled “setCookies”Sets multiple cookies on an HttpServerResponse.
Details
Each input entry contains a cookie name, value, and optional cookie options. The
returned effect fails with CookiesError if any cookie cannot be encoded.
Signature
declare const setCookies: { ( cookies: Iterable<readonly [name: string, value: string, options?: Cookies.Cookie["options"]]> ): (self: HttpServerResponse) => Effect.Effect<HttpServerResponse, Cookies.CookiesError, never> ( self: HttpServerResponse, cookies: Iterable<readonly [name: string, value: string, options?: Cookies.Cookie["options"]]> ): Effect.Effect<HttpServerResponse, Cookies.CookiesError, never>}Since v4.0.0
setCookiesUnsafe
Section titled “setCookiesUnsafe”Sets multiple cookies on an HttpServerResponse, throwing if any cookie cannot
be encoded.
When to use
Use when you need to set multiple trusted cookies and want encoding failures
to throw instead of being represented as CookiesError failures.
Signature
declare const setCookiesUnsafe: { ( cookies: Iterable<readonly [name: string, value: string, options?: Cookies.Cookie["options"]]> ): (self: HttpServerResponse) => HttpServerResponse ( self: HttpServerResponse, cookies: Iterable<readonly [name: string, value: string, options?: Cookies.Cookie["options"]]> ): HttpServerResponse}Since v4.0.0
setHeader
Section titled “setHeader”Returns a response with the specified header set to the supplied value.
Signature
declare const setHeader: { (key: string, value: string): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, key: string, value: string): HttpServerResponse}Since v4.0.0
setHeaders
Section titled “setHeaders”Returns a response with all supplied headers set on the existing header map.
Signature
declare const setHeaders: { (input: Headers.Input): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, input: Headers.Input): HttpServerResponse}Since v4.0.0
setStatus
Section titled “setStatus”Sets the HTTP status code of an HttpServerResponse.
Details
When statusText is omitted, the existing status text is preserved.
Signature
declare const setStatus: { (status: number, statusText?: string | undefined): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, status: number, statusText?: string | undefined): HttpServerResponse}Since v4.0.0
updateCookies
Section titled “updateCookies”Updates the cookies attached to an HttpServerResponse using the supplied
function.
Details
The original response is not mutated; a new response is returned with the callback result as its cookie collection.
Signature
declare const updateCookies: { (f: (cookies: Cookies.Cookies) => Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse (self: HttpServerResponse, f: (cookies: Cookies.Cookies) => Cookies.Cookies): HttpServerResponse}Since v4.0.0
constructors
Section titled “constructors”Creates an empty HTTP response.
Details
The default status is 204.
Signature
declare const empty: (options?: Options.WithContent | undefined) => HttpServerResponseSince v4.0.0
Creates a streamed file response for a file system path.
Details
The effect requires HttpPlatform, can fail with a platform error, and supports
options for status, headers, offset, and byte range.
Signature
declare const file: ( path: string, options?: | (Options & { readonly bytesToRead?: FileSystem.SizeInput | undefined readonly chunkSize?: FileSystem.SizeInput | undefined readonly offset?: FileSystem.SizeInput | undefined }) | undefined) => Effect.Effect<HttpServerResponse, PlatformError, HttpPlatform>Since v4.0.0
fileWeb
Section titled “fileWeb”Creates a streamed file response for a Web File-like value.
Details
The effect requires HttpPlatform and supports options for status, headers,
offset, and byte range.
Signature
declare const fileWeb: ( file: Body.HttpBody.FileLike, options?: | (Options.WithContent & { readonly bytesToRead?: FileSystem.SizeInput | undefined readonly chunkSize?: FileSystem.SizeInput | undefined readonly offset?: FileSystem.SizeInput | undefined }) | undefined) => Effect.Effect<HttpServerResponse, never, HttpPlatform>Since v4.0.0
formData
Section titled “formData”Creates a response whose body is a Web FormData value.
Signature
declare const formData: (body: FormData, options?: Options.WithContent | undefined) => HttpServerResponseSince v4.0.0
Creates an HTML response with the text/html content type.
Details
Passing a string returns a response directly. Using it as a template tag returns an effect so interpolated values can be rendered with their required services and errors.
Signature
declare const html: { <A extends ReadonlyArray<Template.Interpolated>>( strings: TemplateStringsArray, ...args: A ): Effect.Effect<HttpServerResponse, Template.Interpolated.Error<A[number]>, Template.Interpolated.Context<A[number]>> (html: string): HttpServerResponse}Since v4.0.0
htmlStream
Section titled “htmlStream”Creates a streaming HTML response from a template.
Details
The template is encoded as a byte stream and can use streaming interpolated values from the current context.
Signature
declare const htmlStream: <A extends ReadonlyArray<Template.InterpolatedWithStream>>( strings: TemplateStringsArray, ...args: A) => Effect.Effect<HttpServerResponse, never, Template.Interpolated.Context<A[number]>>Since v4.0.0
Creates a JSON HTTP response.
Details
The body is serialized with JSON.stringify; serialization errors are captured
as HttpBodyError failures.
Signature
declare const json: ( body: unknown, options?: Options.WithContentType | undefined) => Effect.Effect<HttpServerResponse, Body.HttpBodyError>Since v4.0.0
jsonUnsafe
Section titled “jsonUnsafe”Creates a JSON HTTP response synchronously.
When to use
Use when the response body is known to be JSON-serializable and you need a
synchronous HttpServerResponse.
Gotchas
Unlike json, serialization errors from JSON.stringify are not captured in
Effect.
Signature
declare const jsonUnsafe: (body: unknown, options?: Options.WithContentType | undefined) => HttpServerResponseSince v4.0.0
Creates a response with a raw body value.
When to use
Use when you want to pass through a body value already understood by the
underlying runtime, such as a Web Response, Blob, or ReadableStream,
for later platform conversion.
Signature
declare const raw: (body: unknown, options?: Options | undefined) => HttpServerResponseSince v4.0.0
redirect
Section titled “redirect”Creates a redirect response with a Location header.
Details
The default status is 302; custom headers are merged with the generated
Location header.
Signature
declare const redirect: (location: string | URL, options?: Options.WithContent | undefined) => HttpServerResponseSince v4.0.0
schemaJson
Section titled “schemaJson”Creates a JSON response constructor backed by a schema encoder.
Details
The returned function encodes the value with the supplied schema before
serializing it as JSON, and can fail with HttpBodyError if schema encoding or
JSON serialization fails.
Signature
declare const schemaJson: <A, I, RD, RE>( schema: Schema.Codec<A, I, RD, RE>, options?: ParseOptions | undefined) => ( body: A, options?: Options.WithContentType | undefined) => Effect.Effect<HttpServerResponse, Body.HttpBodyError, RE>Since v4.0.0
stream
Section titled “stream”Creates a streaming response from a stream of byte chunks.
Details
Optional response metadata can supply the status, headers, content type, and content length.
Signature
declare const stream: <E>(body: Stream.Stream<Uint8Array, E>, options?: Options | undefined) => HttpServerResponseSince v4.0.0
Creates an HTTP response whose body is a string.
Signature
declare const text: (body: string, options?: Options.WithContentType) => HttpServerResponseSince v4.0.0
uint8Array
Section titled “uint8Array”Creates an HTTP response whose body is a Uint8Array.
Signature
declare const uint8Array: (body: Uint8Array, options?: Options.WithContentType) => HttpServerResponseSince v4.0.0
urlParams
Section titled “urlParams”Creates a response from URL parameters using the
application/x-www-form-urlencoded content type by default.
Signature
declare const urlParams: (body: UrlParams.Input, options?: Options.WithContentType | undefined) => HttpServerResponseSince v4.0.0
converting
Section titled “converting”fromClientResponse
Section titled “fromClientResponse”Converts an HttpClientResponse to an HttpServerResponse.
Details
The response body is streamed from the client response. Set-Cookie headers are
removed from the header map and represented in the response cookie collection.
Signature
declare const fromClientResponse: (response: HttpClientResponse.HttpClientResponse) => HttpServerResponseSince v4.0.0
fromWeb
Section titled “fromWeb”Converts a Web Response to an HttpServerResponse.
Details
Set-Cookie headers are parsed into the response cookie collection and removed
from the header map. A present Web body is exposed as a stream body.
Signature
declare const fromWeb: (response: Response) => HttpServerResponseSince v4.0.0
toClientResponse
Section titled “toClientResponse”Wraps an HttpServerResponse as an HttpClientResponse.
Details
An optional request can be supplied for client-response metadata and decode errors.
Signature
declare const toClientResponse: ( response: HttpServerResponse, options?: { readonly request?: HttpClientRequest.HttpClientRequest | undefined }) => HttpClientResponse.HttpClientResponseSince v4.0.0
Converts an HttpServerResponse to a Web Response.
Details
Cookies are appended as Set-Cookie headers. Stream bodies are converted using
the supplied context, and withoutBody can be used for responses such as HEAD
responses.
Signature
declare const toWeb: ( response: HttpServerResponse, options?: { readonly withoutBody?: boolean | undefined; readonly context?: Context.Context<never> | undefined }) => ResponseSince v4.0.0
guards
Section titled “guards”isHttpServerResponse
Section titled “isHttpServerResponse”Returns true when the supplied value is an HttpServerResponse.
Signature
declare const isHttpServerResponse: (u: unknown) => u is HttpServerResponseSince v4.0.0
models
Section titled “models”HttpServerResponse (interface)
Section titled “HttpServerResponse (interface)”Server-side HTTP response model.
Details
A response contains a status, optional status text, headers, cookies, and an HTTP body that can later be converted to platform-specific response types.
Signature
export interface HttpServerResponse extends Inspectable.Inspectable, Pipeable, ErrorReporter.Reportable { readonly [TypeId]: typeof TypeId readonly status: number readonly statusText?: string | undefined readonly headers: Headers.Headers readonly cookies: Cookies.Cookies readonly body: Body.HttpBody}Since v4.0.0
options
Section titled “options”Options (interface)
Section titled “Options (interface)”Common options accepted by HTTP server response constructors.
Signature
export interface Options { readonly status?: number | undefined readonly statusText?: string | undefined readonly headers?: Headers.Input | undefined readonly cookies?: Cookies.Cookies | undefined readonly contentType?: string | undefined readonly contentLength?: number | undefined}Since v4.0.0
Options (namespace)
Section titled “Options (namespace)”Option variants used by response constructors with different body metadata rules.
Since v4.0.0
WithContent (interface)
Section titled “WithContent (interface)”Response options for constructors whose body determines its own content type and content length.
Signature
export interface WithContent extends Omit<Options, "contentType" | "contentLength"> {}Since v4.0.0
WithContentType (interface)
Section titled “WithContentType (interface)”Response options for constructors that allow overriding the content type while deriving the content length from the body.
Signature
export interface WithContentType extends Omit<Options, "contentLength"> {}Since v4.0.0