API reference
All functions are safe to call without a browser: they do nothing and return null, false, {} or [].
Reading
getCookie(name)
Returns the decoded value, or null if not found.
getCookie(name: string): string | nullhasCookie(name) 2.1.0+
true if the cookie exists, even with an empty value.
hasCookie(name: string): booleangetAllCookies() 2.1.0+
Every cookie visible to JavaScript.
getAllCookies(): Record<string, string>getCookies(pattern) 2.1.0+
Cookies whose name matches a glob (*, ?), RegExp or predicate.
getCookies(pattern: string | RegExp | ((name: string) => boolean)): Record<string, string>getJSONCookie(name) 2.1.0+
Parsed JSON value, or null if missing or invalid. __proto__ keys are dropped.
getJSONCookie<T = unknown>(name: string): T | nullWriting
setCookie(name, value, options?)
Returns true if the cookie is readable with the new value afterwards. 2.1.0+
setCookie(name: string, value: string | number | boolean, options?: SetCookieOptions): boolean| Option | Type | Default |
|---|---|---|
timeToLive | number | 1 |
unit | CookieUnit | 'hour' |
expires | Date | — |
session | boolean | false |
domains | string[] | current hostname |
path | string | '/' |
sameSite | 'Lax' | 'Strict' | 'None' | 'Lax' ('None' with partitioned) |
secure | boolean | true on HTTPS |
partitioned | boolean | false |
See Setting cookies for details.
setJSONCookie(name, value, options?) 2.1.0+
Serializes value with JSON.stringify. false if it can't be serialized.
setJSONCookie(name: string, value: unknown, options?: SetCookieOptions): booleanDeleting
deleteCookie(name, options?)
true if no cookie with that name is readable afterwards. 2.1.0+
deleteCookie(name: string, options?: CookieAttributes): booleanOptions: domains, path, sameSite, secure, partitioned.
deleteCookies(pattern, options?) 2.1.0+
Deletes every match across candidate domains and paths. Returns the names that still exist.
deleteCookies(pattern: CookieNamePattern, options?: DeleteCookiesOptions): string[]Options: domains, paths, sameSite, secure, partitioned.
Factory
createCookieUtils(defaults?) 2.1.0+
Returns all the functions above with default options and an optional namePrefix.
createCookieUtils(defaults?: CookieUtilsDefaults): CookieUtilsSee Default options.