Cookies

View source

Provides direct access to the current document’s cookies as a world resource.

StoragePlugin installs a fresh Cookies resource. Unlike Storage, this resource reads and writes string pairs through document.cookie; it does not serialize values as JSON. Cookie names and values are interpolated into the cookie string without encoding, trimming, or additional attributes, so callers must provide values that are valid for the browser cookie syntax. document.cookie also excludes cookies that are not visible to the current document, such as HttpOnly cookies.

Examples

import { Cookies } from '@wimaengine/storage'

const cookies = world.getResource(Cookies)
cookies.set('theme', 'dark', 3600)
const theme = cookies.get('theme')
cookies.delete('theme')

Constructor


constructor()

Methods


set(n: string, v: string, maxAge?: number): void

View source

Sets one cookie pair on the current document.

The browser interprets maxAge as the Max-Age cookie attribute, in seconds. Its default is 12000 seconds (3 hours and 20 minutes). The source does not set Path, Domain, Secure, or SameSite, so the browser applies its defaults and updates only the cookie matching the current document’s default scope. n and v are inserted verbatim.

Parameters

NameDescriptionDefault
n

Cookie name.

—
v

String cookie value.

—
maxAge?

Cookie lifetime in seconds.

12000

get(n: string): string

View source

Returns the value of a visible cookie by its exact name.

This implementation splits document.cookie on ; and then splits each pair on =. It does not trim cookie names or decode values. Because browsers commonly separate pairs with "; ", a cookie after the first pair can retain a leading space and fail the exact-name comparison. A value containing = is also truncated to the text before its first =. The method returns undefined when no pair matches.

Parameters

NameDescriptionDefault
n

Exact cookie name.

—

Returns

The first matching cookie value, or undefined.

delete(n: string): void

View source

Expires one cookie by setting its Max-Age to zero.

No path or domain is supplied, so the browser applies its default scope; a cookie created with a different scope may remain. The cookie name is inserted verbatim.

Parameters

NameDescriptionDefault
n

Cookie name.

—

clear(): void

View source

Attempts to expire every cookie exposed by document.cookie.

The method passes each text before the first = directly to Cookies.prototype.delete. It therefore inherits the parser’s lack of trimming: standard whitespace after a semicolon can cause later cookie names to be sent with a leading space and fail to delete the actual cookie. Cookies hidden from document.cookie, including HttpOnly cookies and cookies outside the current document’s scope, are not affected.