Cookies.ts
Cookies.ts overview
Section titled “Cookies.ts overview”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.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”combinators
Section titled “combinators”expireCookie
Section titled “expireCookie”Adds 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<NonNullable<Cookie["options"]>, "expires" | "maxAge"> ): (self: Cookies) => Result.Result<Cookies, CookiesError> ( self: Cookies, name: string, options?: Omit<NonNullable<Cookie["options"]>, "expires" | "maxAge"> ): Result.Result<Cookies, CookiesError>}Since v4.0.0
expireCookieUnsafe
Section titled “expireCookieUnsafe”Adds an expired cookie to a Cookies object, throwing an error if invalid
Signature
declare const expireCookieUnsafe: { (name: string, options?: Omit<NonNullable<Cookie["options"]>, "expires" | "maxAge">): (self: Cookies) => Cookies (self: Cookies, name: string, options?: Omit<NonNullable<Cookie["options"]>, "expires" | "maxAge">): Cookies}Since v4.0.0
Gets a cookie from a Cookies object safely.
Signature
declare const get: { (name: string): (self: Cookies) => Option.Option<Cookie> (self: Cookies, name: string): Option.Option<Cookie>}Since v4.0.0
getValue
Section titled “getValue”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.Option<string> (self: Cookies, name: string): Option.Option<string>}Since v4.0.0
Combines two Cookies objects, removing duplicates from the first
Signature
declare const merge: { (that: Cookies): (self: Cookies) => Cookies; (self: Cookies, that: Cookies): Cookies }Since v4.0.0
remove
Section titled “remove”Removes a cookie by name
Signature
declare const remove: { (name: string): (self: Cookies) => Cookies; (self: Cookies, name: string): Cookies }Since v4.0.0
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?: Cookie["options"]): (self: Cookies) => Result.Result<Cookies, CookiesError> (self: Cookies, name: string, value: string, options?: Cookie["options"]): Result.Result<Cookies, CookiesError>}Since v4.0.0
setAll
Section titled “setAll”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 [name: string, value: string, options?: Cookie["options"]]> ): (self: Cookies) => Result.Result<Cookies, CookiesError> ( self: Cookies, cookies: Iterable<readonly [name: string, value: string, options?: Cookie["options"]]> ): Result.Result<Cookies, CookiesError>}Since v4.0.0
setAllCookie
Section titled “setAllCookie”Adds multiple cookies to a Cookies object
Signature
declare const setAllCookie: { (cookies: Iterable<Cookie>): (self: Cookies) => Cookies (self: Cookies, cookies: Iterable<Cookie>): Cookies}Since v4.0.0
setAllUnsafe
Section titled “setAllUnsafe”Adds multiple cookies to a Cookies object, throwing an error if invalid
Signature
declare const setAllUnsafe: { (cookies: Iterable<readonly [name: string, value: string, options?: Cookie["options"]]>): (self: Cookies) => Cookies (self: Cookies, cookies: Iterable<readonly [name: string, value: string, options?: Cookie["options"]]>): Cookies}Since v4.0.0
setCookie
Section titled “setCookie”Adds a cookie to a Cookies object
Signature
declare const setCookie: { (cookie: Cookie): (self: Cookies) => Cookies; (self: Cookies, cookie: Cookie): Cookies }Since v4.0.0
setUnsafe
Section titled “setUnsafe”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?: Cookie["options"]): (self: Cookies) => Cookies (self: Cookies, name: string, value: string, options?: Cookie["options"]): Cookies}Since v4.0.0
constructors
Section titled “constructors”An empty Cookies object
Signature
declare const empty: CookiesSince v4.0.0
fromIterable
Section titled “fromIterable”Create a Cookies object from an Iterable
Signature
declare const fromIterable: (cookies: Iterable<Cookie>) => CookiesSince v4.0.0
fromReadonlyRecord
Section titled “fromReadonlyRecord”Creates a Cookies collection from an existing readonly record of cookies keyed by cookie name.
Signature
declare const fromReadonlyRecord: (cookies: Record.ReadonlyRecord<string, Cookie>) => CookiesSince v4.0.0
fromSetCookie
Section titled “fromSetCookie”Create a Cookies object from a set of Set-Cookie headers
Signature
declare const fromSetCookie: (headers: Iterable<string> | string) => CookiesSince v4.0.0
makeCookie
Section titled “makeCookie”Creates 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 const makeCookie: ( name: string, value: string, options?: Cookie["options"] | undefined) => Result.Result<Cookie, CookiesError>Since v4.0.0
makeCookieUnsafe
Section titled “makeCookieUnsafe”Create a new cookie, throwing an error if invalid
Signature
declare const makeCookieUnsafe: (name: string, value: string, options?: Cookie["options"] | undefined) => CookieSince v4.0.0
cookies
Section titled “cookies”Cookie (interface)
Section titled “Cookie (interface)”HTTP cookie value with its decoded value, encoded value, and optional cookie attributes such as domain, path, expiration, security, and same-site settings.
Signature
export interface Cookie extends Inspectable.Inspectable { readonly [CookieTypeId]: typeof CookieTypeId readonly name: string readonly value: string readonly valueEncoded: string readonly options?: | { readonly domain?: string | undefined readonly expires?: Date | undefined readonly maxAge?: Duration.Input | undefined readonly path?: string | undefined readonly priority?: "low" | "medium" | "high" | undefined readonly httpOnly?: boolean | undefined readonly secure?: boolean | undefined readonly partitioned?: boolean | undefined readonly sameSite?: "lax" | "strict" | "none" | undefined } | undefined}Since v4.0.0
decoding
Section titled “decoding”parseHeader
Section titled “parseHeader”Parses a cookie header into a record of key-value pairs
Details
Adapted from https://github.com/fastify/fastify-cookie under MIT License
Signature
declare const parseHeader: (header: string) => Record<string, string>Since v4.0.0
encoding
Section titled “encoding”serializeCookie
Section titled “serializeCookie”Serializes a cookie into a string.
Details
Adapted from https://github.com/fastify/fastify-cookie under MIT License
Signature
declare const serializeCookie: (self: Cookie) => stringSince v4.0.0
toCookieHeader
Section titled “toCookieHeader”Serializes a Cookies object into a Cookie header.
Signature
declare const toCookieHeader: (self: Cookies) => stringSince v4.0.0
toRecord
Section titled “toRecord”Converts a Cookies collection to a record of decoded cookie values keyed by cookie name.
Signature
declare const toRecord: (self: Cookies) => Record<string, string>Since v4.0.0
toSetCookieHeaders
Section titled “toSetCookieHeaders”Serializes a Cookies collection into an array of Set-Cookie header values.
Signature
declare const toSetCookieHeaders: (self: Cookies) => Array<string>Since v4.0.0
errors
Section titled “errors”CookiesError (class)
Section titled “CookiesError (class)”Error 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 CookiesErrorSince v4.0.0
fromReason (static method)
Section titled “fromReason (static method)”Creates a cookie error from a reason tag and optional cause.
Signature
declare const fromReason: (reason: CookiesError["reason"]["_tag"], cause?: unknown) => CookiesErrorSince v4.0.0
[CookieErrorTypeId] (property)
Section titled “[CookieErrorTypeId] (property)”Marks this value as a cookie validation error for runtime guards.
Signature
readonly [CookieErrorTypeId]: "~effect/http/Cookies/CookieError"Since v4.0.0
CookiesErrorReason (class)
Section titled “CookiesErrorReason (class)”Error reason describing why cookie construction failed, such as invalid name, value, domain, path, or infinite max-age.
Signature
declare class CookiesErrorReasonSince v4.0.0
guards
Section titled “guards”isCookie
Section titled “isCookie”Returns true when a value is a Cookie.
Signature
declare const isCookie: (u: unknown) => u is CookieSince v4.0.0
models
Section titled “models”Cookies (interface)
Section titled “Cookies (interface)”Immutable collection of HTTP cookies keyed by cookie name.
Signature
export interface Cookies extends Pipeable, Inspectable.Inspectable { readonly [TypeId]: typeof TypeId readonly cookies: Record.ReadonlyRecord<string, Cookie>}Since v4.0.0
refinements
Section titled “refinements”isCookies
Section titled “isCookies”Returns true when a value is a Cookies collection.
Signature
declare const isCookies: (u: unknown) => u is CookiesSince v4.0.0
isEmpty
Section titled “isEmpty”Returns true when the Cookies collection contains no cookies.
Signature
declare const isEmpty: (self: Cookies) => booleanSince v4.0.0
schemas
Section titled “schemas”CookieSchema
Section titled “CookieSchema”Schema for Cookie values.
Signature
declare const CookieSchema: CookieSchemaSince v4.0.0
CookieSchema (interface)
Section titled “CookieSchema (interface)”Schema interface for validating Cookie values.
Signature
export interface CookieSchema extends Schema.declare<Cookie> {}Since v4.0.0
CookiesSchema
Section titled “CookiesSchema”Schema for Cookies collections.
Details
JSON encoding uses Set-Cookie header strings, while isomorphic encoding uses
a readonly record of cookie values.
Signature
declare const CookiesSchema: CookiesSchemaSince v4.0.0
CookiesSchema (interface)
Section titled “CookiesSchema (interface)”Schema interface for validating and encoding Cookies collections.
Signature
export interface CookiesSchema extends Schema.declare<Cookies, Record.ReadonlyRecord<string, Cookie>> {}Since v4.0.0
schemaRecord
Section titled “schemaRecord”Schema for transforming Cookies into records of decoded string values keyed
by cookie name.
Signature
declare const schemaRecord: Schema.decodeTo<Schema.$Record<Schema.String, Schema.String>, CookiesSchema, never, never>Since v4.0.0