Cookie

View Source

Summary

Represents a HTTP cookie.

Supports parsing and serializing the cookie to/from Cookie and Set-Cookie headers.

Also supports cryptographic signing of the cookie value to ensure it's not tampered with, and secret rotation to easily rotate secrets without breaking existing cookies.

Signature

class Cookie {
  constructor(name: string, options: CookieOptions): Cookie;

  // Accessors
  get domain(): string | undefined;
  get expires(): Date | undefined;
  get httpOnly(): boolean;
  get maxAge(): number | undefined;
  get name(): string;
  get partitioned(): boolean;
  get path(): string;
  get sameSite(): SameSiteValue;
  get secure(): boolean;
  get signed(): boolean;

  // Methods
  parse(headerValue: string | null): Promise<string | null>;
  serialize(value: string, props: CookieProperties): Promise<string>;
}

Constructor Params

name

The name of the cookie

options

Options for the cookie

Accessors

domain

The domain of the cookie.

MDN Reference

expires

The expiration date of the cookie.

MDN Reference

httpOnly

True if the cookie is HTTP-only.

MDN Reference

maxAge

The maximum age of the cookie in seconds.

MDN Reference

name

The name of the cookie.

MDN Reference

partitioned

True if the cookie is partitioned.

MDN Reference

path

The path of the cookie.

MDN Reference

sameSite

The SameSite attribute of the cookie.

MDN Reference

secure

True if the cookie is secure (only sent over HTTPS).

MDN Reference

signed

True if this cookie uses one or more secrets for verification.

Methods

parse(headerValue: string | null): Promise<string | null>

Extracts the value of this cookie from a Cookie header value.

headerValue

The Cookie header to parse

serialize(value: string, props: CookieProperties): Promise

Returns the value to use in a Set-Cookie header for this cookie.

value

The value to serialize

props

Additional properties to use when serializing the cookie