Cookie

View Source

Summary

The value of a Cookie HTTP header.

MDN Cookie Reference

HTTP/1.1 Specification

Signature

class Cookie {
  constructor(init: string | CookieInit): Cookie;

  // Accessors
  get names(): string[];
  get size(): number;
  get values(): string[];

  // Methods
  [iterator](): IterableIterator<[string, string]>;
  clear(): void;
  delete(name: string): void;
  entries(): IterableIterator<[string, string]>;
  forEach(
    callback: (name: string, value: string, header: Cookie) => void,
    thisArg: any,
  ): void;
  get(name: string): string | null;
  has(name: string): boolean;
  set(name: string, value: string): void;
  toString(): string;
  from(value: string | CookieInit | null): Cookie;
}

Constructor Params

init

Accessors

names

An array of the names of the cookies in the header.

size

The number of cookies in the header.

values

An array of the values of the cookies in the header.

Methods

iterator: IterableIterator<[string, string]>

Iterates over cookie name and value pairs.

clear(): void

Removes all cookies from the header.

delete(name: string): void

Removes a cookie with the given name from the header.

name

The name of the cookie

entries(): IterableIterator<[string, string]>

Returns an iterator of all cookie name and value pairs.

forEach(callback: (name: string, value: string, header: Cookie) => void, thisArg: any): void

Invokes the callback for each cookie name and value pair.

callback

The function to call for each pair

thisArg

The value to use as this when calling the callback

get(name: string): string | null

Gets the value of a cookie with the given name from the header.

name

The name of the cookie

has(name: string): boolean

True if a cookie with the given name exists in the header.

name

The name of the cookie

set(name: string, value: string): void

Sets a cookie with the given name and value in the header.

name

The name of the cookie

value

The value of the cookie

toString(): string

Returns the string representation of the header value.

from(value: string | CookieInit | null): Cookie

Parse a Cookie header value.

value

The header value (string, init object, or null)