Skip to content

Cookies

32 exports Added in v1.0.0 Source

Combinators

get

Added in v1.0.0 Source

Get a cookie from a Cookies object

Signature

declare const get: {
  (name: string): (self: Cookies) => Option<Cookie>;
  (self: Cookies, name: string): Option<Cookie>;
};

getValue

Added in v1.0.0 Source

Get a cookie from a Cookies object

Signature

declare const getValue: {
  (name: string): (self: Cookies) => Option<string>;
  (self: Cookies, name: string): Option<string>;
};

merge

Added in v1.0.0 Source

Combine two Cookies objects, removing duplicates from the first

Signature

declare const merge: {
  (that: Cookies): (self: Cookies) => Cookies;
  (self: Cookies, that: Cookies): Cookies;
};

remove

Added in v1.0.0 Source

Remove a cookie by name

Signature

declare const remove: {
  (name: string): (self: Cookies) => Cookies;
  (self: Cookies, name: string): Cookies;
};

set

Added in v1.0.0 Source

Add a cookie to a Cookies object

Signature

declare const set: {
  (
    name: string,
    value: string,
    options?: {
      readonly domain?: string;
      readonly expires?: Date;
      readonly httpOnly?: boolean;
      readonly maxAge?: Duration.DurationInput;
      readonly partitioned?: boolean;
      readonly path?: string;
      readonly priority?: "low" | "medium" | "high";
      readonly sameSite?: "lax" | "strict" | "none";
      readonly secure?: boolean;
    },
  ): (self: Cookies) => Either<Cookies, CookiesError>;
  (
    self: Cookies,
    name: string,
    value: string,
    options?: {
      readonly domain?: string;
      readonly expires?: Date;
      readonly httpOnly?: boolean;
      readonly maxAge?: Duration.DurationInput;
      readonly partitioned?: boolean;
      readonly path?: string;
      readonly priority?: "low" | "medium" | "high";
      readonly sameSite?: "lax" | "strict" | "none";
      readonly secure?: boolean;
    },
  ): Either<Cookies, CookiesError>;
};

setAll

Added in v1.0.0 Source

Add multiple cookies to a Cookies object

Signature

declare const setAll: {
  (
    cookies: Iterable<
      readonly [
        string,
        string,
        (
          | {
              readonly domain?: string;
              readonly expires?: Date;
              readonly httpOnly?: boolean;
              readonly maxAge?: Duration.DurationInput;
              readonly partitioned?: boolean;
              readonly path?: string;
              readonly priority?: "low" | "medium" | "high";
              readonly sameSite?: "lax" | "strict" | "none";
              readonly secure?: boolean;
            }
          | undefined
        ),
      ]
    >,
  ): (self: Cookies) => Either<Cookies, CookiesError>;
  (
    self: Cookies,
    cookies: Iterable<
      readonly [
        string,
        string,
        (
          | {
              readonly domain?: string;
              readonly expires?: Date;
              readonly httpOnly?: boolean;
              readonly maxAge?: Duration.DurationInput;
              readonly partitioned?: boolean;
              readonly path?: string;
              readonly priority?: "low" | "medium" | "high";
              readonly sameSite?: "lax" | "strict" | "none";
              readonly secure?: boolean;
            }
          | undefined
        ),
      ]
    >,
  ): Either<Cookies, CookiesError>;
};

setAllCookie

Added in v1.0.0 Source

Add multiple cookies to a Cookies object

Signature

declare const setAllCookie: {
  (cookies: Iterable<Cookie>): (self: Cookies) => Cookies;
  (self: Cookies, cookies: Iterable<Cookie>): Cookies;
};

setCookie

Added in v1.0.0 Source

Add a cookie to a Cookies object

Signature

declare const setCookie: {
  (cookie: Cookie): (self: Cookies) => Cookies;
  (self: Cookies, cookie: Cookie): Cookies;
};

unsafeSet

Added in v1.0.0 Source

Add a cookie to a Cookies object

Signature

declare const unsafeSet: {
  (
    name: string,
    value: string,
    options?: {
      readonly domain?: string;
      readonly expires?: Date;
      readonly httpOnly?: boolean;
      readonly maxAge?: Duration.DurationInput;
      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 httpOnly?: boolean;
      readonly maxAge?: Duration.DurationInput;
      readonly partitioned?: boolean;
      readonly path?: string;
      readonly priority?: "low" | "medium" | "high";
      readonly sameSite?: "lax" | "strict" | "none";
      readonly secure?: boolean;
    },
  ): Cookies;
};

unsafeSetAll

Added in v1.0.0 Source

Add multiple cookies to a Cookies object, throwing an error if invalid

Signature

declare const unsafeSetAll: {
  (
    cookies: Iterable<
      readonly [
        string,
        string,
        (
          | {
              readonly domain?: string;
              readonly expires?: Date;
              readonly httpOnly?: boolean;
              readonly maxAge?: Duration.DurationInput;
              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 httpOnly?: boolean;
              readonly maxAge?: Duration.DurationInput;
              readonly partitioned?: boolean;
              readonly path?: string;
              readonly priority?: "low" | "medium" | "high";
              readonly sameSite?: "lax" | "strict" | "none";
              readonly secure?: boolean;
            }
          | undefined
        ),
      ]
    >,
  ): Cookies;
};

Constructors

empty

Added in v1.0.0 Source

An empty Cookies object

Signature

declare const empty: Cookies;

fromIterable

Added in v1.0.0 Source

Create a Cookies object from an Iterable

Signature

declare function fromIterable(cookies: Iterable<Cookie>): Cookies;

Create a Cookies object from an Iterable

Signature

declare function fromReadonlyRecord(cookies: Record.ReadonlyRecord<string, Cookie>): Cookies;

Create a Cookies object from a set of Set-Cookie headers

Signature

declare function fromSetCookie(headers: string | Iterable<string, any, any>): Cookies;

makeCookie

Added in v1.0.0 Source

Create a new cookie

Signature

declare function makeCookie(
  name: string,
  value: string,
  options?: {
    readonly domain?: string;
    readonly expires?: Date;
    readonly httpOnly?: boolean;
    readonly maxAge?: any;
    readonly partitioned?: boolean;
    readonly path?: string;
    readonly priority?: "low" | "medium" | "high";
    readonly sameSite?: "lax" | "strict" | "none";
    readonly secure?: boolean;
  },
): Either<Cookie, CookiesError>;

Create a new cookie, throwing an error if invalid

Signature

declare function unsafeMakeCookie(
  name: string,
  value: string,
  options?: {
    readonly domain?: string;
    readonly expires?: Date;
    readonly httpOnly?: boolean;
    readonly maxAge?: any;
    readonly partitioned?: boolean;
    readonly path?: string;
    readonly priority?: "low" | "medium" | "high";
    readonly sameSite?: "lax" | "strict" | "none";
    readonly secure?: boolean;
  },
): Cookie;

Decoding

parseHeader

Added in v1.0.0 Source

Parse a cookie header into a record of key-value pairs

Adapted from https://github.com/fastify/fastify-cookie under MIT License

Signature

declare function parseHeader(header: string): Record<string, string>;

Encoding

Serialize a cookie into a string

Adapted from https://github.com/fastify/fastify-cookie under MIT License

Signature

declare function serializeCookie(self: Cookie): string;

Serialize a Cookies object into a Cookie header

Signature

declare function toCookieHeader(self: Cookies): string;

toRecord

Added in v1.0.0 Source

To record

Signature

declare function toRecord(self: Cookies): Record<string, string>;

Serialize a Cookies object into Headers object containing one or more Set-Cookie headers

Signature

declare function toSetCookieHeaders(self: Cookies): Array<string>;

Errors

CookiesError

Added in v1.0.0 Source

Signature

declare class CookiesError extends YieldableError<this> & Record<typeof ErrorTypeId, typeof ErrorTypeId> & {
  readonly _tag: "CookieError";
} & Readonly<{
  readonly reason: "InvalidName" | "InvalidValue" | "InvalidDomain" | "InvalidPath" | "InfinityMaxAge";
}> {
  constructor(args: {
    readonly reason: "InvalidName" | "InvalidValue" | "InvalidDomain" | "InvalidPath" | "InfinityMaxAge";
  });
  message: "InvalidName" | "InvalidValue" | "InvalidDomain" | "InvalidPath" | "InfinityMaxAge";
}

Models

Cookies interface

Added in v1.0.0 Source

Signature

interface Cookies extends Pipeable, Inspectable {
  readonly [TypeId]: typeof TypeId;
  readonly cookies: Record.ReadonlyRecord<string, Cookie>;
}

Refinements

isCookies

Added in v1.0.0 Source

Signature

declare function isCookies(u: unknown): u is Cookies;

isEmpty

Added in v1.0.0 Source

Signature

declare function isEmpty(self: Cookies): boolean;

Type Ids

CookieTypeId

Added in v1.0.0 Source

Signature

declare const CookieTypeId: unique symbol;

CookieTypeId type

Added in v1.0.0 Source

Signature

type CookieTypeId = typeof CookieTypeId;

ErrorTypeId

Added in v1.0.0 Source

Signature

declare const ErrorTypeId: unique symbol;

ErrorTypeId type

Added in v1.0.0 Source

Signature

type ErrorTypeId = typeof ErrorTypeId;

TypeId

Added in v1.0.0 Source

Signature

declare const TypeId: unique symbol;

TypeId type

Added in v1.0.0 Source

Signature

type TypeId = typeof TypeId;