@fluvial/cookies
Version:
A cookie management library for Fluvial
51 lines (50 loc) • 2.52 kB
TypeScript
import { Cookie, SupportedCookieInit, SupportedCookieValues } from './cookie.js';
/**
* Creates an empty CookieCollection
*/
export declare function create(): CookieCollection;
/**
* Creates a CookieCollection based on a string value from the `Cookie` header
* @param cookieHeader
*/
export declare function fromHeaderValue(cookieHeader: string): CookieCollection;
/**
* Creates a CookieCollection based on an object with keys and values
* @param obj
* @returns
*/
export declare function fromObject(obj: {
[key: string]: SupportedCookieInit;
}): CookieCollection;
export declare function fromEntries(entries: ([name: string, value: SupportedCookieInit] | Cookie)[]): CookieCollection;
export declare class CookieCollection {
#private;
/** The current length of the collection */
get length(): number;
/** Returns a true if the collection contains the cookie with the given name */
has(name: string): boolean;
/** Adds a cookie-like object to the collection */
add(cookie: Cookie): void;
/** Adds a cookie with a given name and the provided value (string, number, or boolean) */
add(name: string, value: SupportedCookieValues): void;
/** Returns in a cookie if one is found with the provided name; otherwise, undefined */
get(name: string): Cookie;
/** Deletes a cookie with the given name if it exists; if none is found, it does nothing */
remove(name: string): void;
/** This registers a hook for when a cookie is set or removed. It was added since the Express adaptation requires the cookie headers set each time there is a mutation or else it won't work */
onChange(this: CookieCollection, hook: () => void): void;
/** Returns the generic object-stringified string or, if specified, a string representative of the collection */
toString(this: CookieCollection, as?: 'cookie-header' | 'set-cookie'): string;
/** Returns an iterable for all cookies in the collection */
values(): ArrayIterator<Cookie>;
/** Returns an iterable with the key-value pairs of the name itself and the full cookie */
entries(): ArrayIterator<[key: string, cookie: Cookie]>;
/** Returns an iterable with the name of each cookie in the collection */
keys(): ArrayIterator<string>;
[Symbol.iterator](): ArrayIterator<Cookie>;
static create: typeof create;
static fromHeaderValue: typeof fromHeaderValue;
static fromObject: typeof fromObject;
static fromEntries: typeof fromEntries;
}
export default CookieCollection;