@tdb/util
Version:
Shared helpers and utilities.
46 lines (39 loc) • 1.27 kB
text/typescript
export type CookieValue = string | object;
export interface ICookies {
[key: string]: CookieValue;
}
export interface ICookieServerContext {
req: {
cookies: ICookies;
};
res: {
setHeader(key: string, value: string | number | string[]): void;
};
}
export interface ICookieOptions extends ICookieAttributes {
ctx?: ICookieServerContext; // Server context from [next.js]
default?: CookieValue;
}
export interface ICookieAttributes {
expires?: number | Date; // Days or date until expiration. Ommit for session cookie.
path?: string; // Path where the cookie is visible: Deafult: '/'.
domain?: string; // The cookie's valid domain, includes sub-domains. Default: current domain and sub-domains.
isSecure?: boolean; // Flag indicating if the cookie transmission requires a secure protocol (https).
}
export type CookiePropertyFunc<T extends CookieValue> = (
value?: T | null,
options?: ICookieOptions,
) => T | undefined;
export type CookieProperty<T extends CookieValue> = CookiePropertyFunc<T> & {
key: string;
isProp: boolean;
options?: ICookieOptions;
};
/**
* An event that is fired when the cookie is written to.
*/
export interface ICookieChangedEvent {
key: string;
value?: CookieValue;
action: 'UPDATE' | 'DELETE';
}