Cookies unstable
Models HTTP cookies and cookie collections for requests and responses.
A Cookie stores a name, value, encoded value, and standard cookie
attributes. A Cookies value is an immutable collection keyed by cookie
name. This module parses request Cookie headers, builds response
Set-Cookie headers, and provides helpers for adding, removing, merging, and
expiring cookies.
For client-side storage, mergeIntoArray and toRequestCookieHeader
work on a plain array of cookies scoped by domain, path and scheme, following
RFC 6265.
Client Storage
mergeIntoArray
unstableMerges cookies set by url into an array of client cookies, returning a new
array.
When to use
Use to record the cookies of a response, or to seed an array of client
cookies from Set-Cookie values for a base URL.
Details
A cookie without a Domain attribute is scoped as hostOnly to the exact
host of url. A Domain attribute is accepted only when the host of url
domain-matches it. Without a Path attribute the cookie uses the default
path of url. Max-Age takes precedence over Expires, and both are
converted to an absolute expires computed from now (epoch milliseconds).
A cookie replaces the existing cookie with the same name, domain and path,
keeping its position. A cookie that is already expired removes that existing
cookie instead. The array is returned unchanged when url is not a valid
absolute URL.
Gotchas
- There is no public suffix list. A
Domainattribute is accepted whenever the request host domain-matches it, unless the host is an IP address or the domain is a single label such ascom, sofoo.co.ukcan setDomain=co.uk.
Signature
declare const mergeIntoArray: { (url: string, cookies: Cookies, now: number): (self: readonly Array<Cookie>) => readonly Array<Cookie>; (self: readonly Array<Cookie>, url: string, cookies: Cookies, now: number): readonly Array<Cookie>;}toRequestCookieHeader
unstableSerializes the client cookies to send with a request to url into a
Cookie header value.
Details
Selects cookies that have not expired at now (epoch milliseconds) and whose
domain and path match url, skipping Secure cookies unless url uses
https. Every matching cookie is kept, including cookies with the same name,
ordered by longest path first. Returns undefined when no cookie matches.
A leading dot and letter case in domain are ignored. A cookie without a
domain is never sent, and a cookie without a path matches every path.
Signature
declare const toRequestCookieHeader: { (url: string, now: number): (self: readonly Array<Cookie>) => string | undefined; (self: readonly Array<Cookie>, url: string, now: number): string | undefined;}Combinators
expireCookie
unstableAdds an expired cookie safely with an empty value, Max-Age=0, and an epoch Expires value.
Details
Returns a CookiesError in the Result failure channel when the name or options are invalid.
Signature
declare const expireCookie: { (name: string, options?: Omit<{ readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; }, "expires" | "maxAge">): (self: Cookies) => Result<Cookies, CookiesError>; (self: Cookies, name: string, options?: Omit<{ readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; }, "expires" | "maxAge">): Result<Cookies, CookiesError>;}expireCookieUnsafe
unstableAdds an expired cookie to a Cookies object, throwing an error if invalid
Signature
declare const expireCookieUnsafe: { (name: string, options?: Omit<{ readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; }, "expires" | "maxAge">): (self: Cookies) => Cookies; (self: Cookies, name: string, options?: Omit<{ readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; }, "expires" | "maxAge">): Cookies;}Gets a cookie from a Cookies object safely.
Signature
declare const get: { (name: string): (self: Cookies) => Option<Cookie>; (self: Cookies, name: string): Option<Cookie>;}Gets the decoded value of a cookie by name safely.
Details
Returns Option.none() when the cookie is not present.
Signature
declare const getValue: { (name: string): (self: Cookies) => Option<string>; (self: Cookies, name: string): Option<string>;}Combines two Cookies objects, removing duplicates from the first
Signature
declare const merge: { (that: Cookies): (self: Cookies) => Cookies; (self: Cookies, that: Cookies): Cookies;}Removes a cookie by name
Signature
declare const remove: { (name: string): (self: Cookies) => Cookies; (self: Cookies, name: string): Cookies;}Creates and adds a cookie safely by name and value.
Details
The cookie fields are validated first; invalid input returns a CookiesError in the Result failure channel.
Signature
declare const set: { (name: string, value: string, options?: { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; }): (self: Cookies) => Result<Cookies, CookiesError>; (self: Cookies, name: string, value: string, options?: { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; }): Result<Cookies, CookiesError>;}Creates and adds multiple cookies safely from name/value/options tuples.
Details
If any tuple is invalid, returns the first CookiesError and leaves the original collection unchanged.
Signature
declare const setAll: { (cookies: Iterable<readonly [string, string, { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; } | undefined]>): (self: Cookies) => Result<Cookies, CookiesError>; (self: Cookies, cookies: Iterable<readonly [string, string, { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; } | undefined]>): Result<Cookies, CookiesError>;}setAllCookie
unstableAdds multiple cookies to a Cookies object
Signature
declare const setAllCookie: { (cookies: Iterable<Cookie>): (self: Cookies) => Cookies; (self: Cookies, cookies: Iterable<Cookie>): Cookies;}setAllUnsafe
unstableAdds multiple cookies to a Cookies object, throwing an error if invalid
Signature
declare const setAllUnsafe: { (cookies: Iterable<readonly [string, string, { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; } | undefined]>): (self: Cookies) => Cookies; (self: Cookies, cookies: Iterable<readonly [string, string, { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; } | undefined]>): Cookies;}Adds a cookie to a Cookies object
Signature
declare const setCookie: { (cookie: Cookie): (self: Cookies) => Cookies; (self: Cookies, cookie: Cookie): Cookies;}Creates and adds a cookie by name and value, throwing if the cookie fields are invalid.
Signature
declare const setUnsafe: { (name: string, value: string, options?: { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; }): (self: Cookies) => Cookies; (self: Cookies, name: string, value: string, options?: { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Duration.Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "lax" | "strict" | "none"; readonly secure?: boolean; }): Cookies;}Constructors
An empty Cookies object
Signature
declare const empty: CookiesfromIterable
unstableCreate a Cookies object from an Iterable
Signature
declare function fromIterable(cookies: Iterable<Cookie>): CookiesfromReadonlyRecord
unstableCreates a Cookies collection from an existing readonly record of cookies keyed by cookie name.
Signature
declare function fromReadonlyRecord(cookies: Record.ReadonlyRecord<string, Cookie>): CookiesfromSetCookie
unstableCreate a Cookies object from a set of Set-Cookie headers
Signature
declare function fromSetCookie(headers: string | Iterable<string, any, any>): CookiesmakeCookie
unstableCreates a cookie, validating the name, encoded value, domain, path, and finite maxAge.
Details
Returns a CookiesError in the Result failure channel when validation fails.
Signature
declare function makeCookie(name: string, value: string, options?: { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "none" | "strict" | "lax"; readonly secure?: boolean;}): Result<Cookie, CookiesError>makeCookieUnsafe
unstableCreate a new cookie, throwing an error if invalid
Signature
declare function makeCookieUnsafe(name: string, value: string, options?: { readonly domain?: string; readonly expires?: Date; readonly hostOnly?: boolean; readonly httpOnly?: boolean; readonly maxAge?: Input; readonly partitioned?: boolean; readonly path?: string; readonly priority?: "low" | "medium" | "high"; readonly sameSite?: "none" | "strict" | "lax"; readonly secure?: boolean;}): CookieDecoding
parseHeader
unstableParses a cookie header into a record of key-value pairs
Details
Adapted from https://github.com/fastify/fastify-cookie under MIT License
Signature
declare function parseHeader(header: string): Record<string, string>Encoding
serializeCookie
unstableSerializes a cookie into a string.
Details
Adapted from https://github.com/fastify/fastify-cookie under MIT License
Signature
declare function serializeCookie(self: Cookie): stringtoCookieHeader
unstableSerializes a Cookies object into a Cookie header.
Signature
declare function toCookieHeader(self: Cookies): stringConverts a Cookies collection to a record of decoded cookie values keyed by cookie name.
Signature
declare function toRecord(self: Cookies): Record<string, string>toSetCookieHeaders
unstableSerializes a Cookies collection into an array of Set-Cookie header values.
Signature
declare function toSetCookieHeaders(self: Cookies): Array<string>Errors
CookiesError
unstableError returned when a cookie name, value, domain, path, or max-age option is invalid.
Details
Inspect reason to determine the specific validation failure.
Signature
declare class CookiesError extends YieldableError<this> & { readonly _tag: "CookiesError";} & Readonly<{ readonly reason: CookiesErrorReason;}> { constructor(args: { readonly reason: CookiesErrorReason; }); readonly "~effect/http/Cookies/CookieError": "~effect/http/Cookies/CookieError"; message: "InvalidCookieName" | "InvalidCookieValue" | "InvalidCookieDomain" | "InvalidCookiePath" | "CookieInfinityMaxAge"; static fromReason(reason: "InvalidCookieName" | "InvalidCookieValue" | "InvalidCookieDomain" | "InvalidCookiePath" | "CookieInfinityMaxAge", cause?: unknown): CookiesError;}CookiesErrorReason
unstableError reason describing why cookie construction failed, such as invalid name, value, domain, path, or infinite max-age.
Signature
declare class CookiesErrorReason extends Error<{ readonly _tag: "InvalidCookieName" | "InvalidCookieValue" | "InvalidCookieDomain" | "InvalidCookiePath" | "CookieInfinityMaxAge"; readonly cause?: unknown;}> { constructor(args: { readonly _tag: "InvalidCookieName" | "InvalidCookieValue" | "InvalidCookieDomain" | "InvalidCookiePath" | "CookieInfinityMaxAge"; readonly cause?: unknown; });}Guards
Returns true when a value is a Cookie.
Signature
declare function isCookie(u: unknown): u is CookieReturns true when a value is a Cookies collection.
Signature
declare function isCookies(u: unknown): u is CookiesisCookiesError
unstableChecks whether a value is a CookiesError.
Signature
declare function isCookiesError(u: unknown): u is CookiesError