UNPKG

@sovgut/state

Version:

<p align="center"> <b>A lightweight, type-safe, and reactive state management library for modern web applications</b> </p>

95 lines (94 loc) 3.57 kB
import { Observer } from '../Observer/Observer.ts'; /** * Configuration options for retrieving values from state * @template T - The expected type of the value */ type Options<T = unknown> = { /** If true, throws an error when the key doesn't exist */ strict?: boolean; /** Default value to return when the key doesn't exist or has an empty value */ fallback?: T; /** Cast the value to a specific type */ cast?: 'string' | 'number' | 'boolean' | 'bigint'; }; /** * Configuration options for setting cookies */ type CookieOptions = { /** Cookie expiration date or number of days from now */ expires?: Date | number; /** Maximum age of the cookie in seconds */ maxAge?: number; /** Domain where the cookie is accessible */ domain?: string; /** Path where the cookie is accessible */ path?: string; /** Whether the cookie should only be transmitted over secure protocols */ secure?: boolean; /** SameSite attribute to prevent CSRF attacks */ sameSite?: 'strict' | 'lax' | 'none'; }; /** * Cookie-based state management class that persists data using browser cookies. * Data persists based on cookie expiration settings and is accessible across subdomains. * Extends Observer to provide event emission capabilities. */ export declare class CookieState extends Observer { /** * Retrieves a value from cookies with strict type checking * @template T - The expected type of the value * @param key - The key to retrieve * @param options - Options with strict mode enabled * @returns The value associated with the key * @throws {StateDoesNotExist} When the key doesn't exist * @throws {StateInvalidCast} When casting fails in strict mode */ static get<T = unknown>(key: string, options: Options<T> & { strict: true; }): T; /** * Retrieves a value from cookies with a fallback * @template T - The expected type of the value * @param key - The key to retrieve * @param options - Options with a fallback value * @returns The value associated with the key or the fallback */ static get<T = unknown>(key: string, options: Options<T> & { fallback: T; }): T; /** * Retrieves a value from cookies * @template T - The expected type of the value * @param key - The key to retrieve * @param options - Optional configuration * @returns The value associated with the key or undefined */ static get<T = unknown>(key: string, options?: Options<T>): T | undefined; /** * Stores a value in cookies * @template T - The type of the value to store * @param key - The key to store the value under * @param value - The value to store (will be JSON stringified and URI encoded) * @param options - Cookie configuration options * @emits Will emit an event with the key and new value */ static set<T = unknown>(key: string, value: T, options?: CookieOptions): void; /** * Removes a cookie * @param key - The key of the cookie to remove * @emits Will emit an event with the key and null value */ static remove(key: string): void; /** * Clears all cookies accessible to the current document * @emits Will emit an event for each removed cookie with null value */ static clear(): void; /** * Checks if a cookie exists * @param key - The key to check * @returns True if the cookie exists, false otherwise */ static has(key: string): boolean; } export {};