seitu
Version:
62 lines • 2.13 kB
text/typescript
import { i as Subscribable, r as ServerReadable, s as Writable, t as Clearable } from "../../index-B0IeflOl.mjs";
import { i as StandardSchemaV1, t as ValidationSchemaErrorProps } from "../../validate-C_H-c0gq.mjs";
//#region src/web/web-storage-value/index.d.ts
interface WebStorageValue<V> extends Subscribable<V>, ServerReadable<V>, Writable<V>, Clearable {}
interface WebStorageValueOptions<S extends StandardSchemaV1<unknown>> {
type: 'localStorage' | 'sessionStorage';
schema: S;
key: string;
defaultValue: StandardSchemaV1.InferOutput<S>;
/** Return a repaired value, or nothing to fall back to `defaultValue`. */
onValidationError?: (props: ValidationSchemaErrorProps<StandardSchemaV1.InferOutput<S>>) => void | StandardSchemaV1.InferOutput<S>;
}
/**
* Reactive handle for one `localStorage`/`sessionStorage` key. On the server,
* `get()` and the SSR snapshot are `defaultValue`.
*
* @example Vanilla
* ```ts twoslash title="session-storage.ts"
* import { createWebStorageValue } from 'seitu/web'
* import * as z from 'zod'
*
* const tokenStorage = createWebStorageValue({
* type: 'sessionStorage',
* key: 'token',
* schema: z.string().nullable(),
* defaultValue: null,
* })
*
* tokenStorage.get()
* tokenStorage.set('abc')
* tokenStorage.get()
* tokenStorage.subscribe(console.log)
* ```
*
* @example React
* ```tsx twoslash title="page.tsx"
* 'use client'
*
* import { createWebStorageValue } from 'seitu/web'
* import { useSubscription } from 'seitu/react'
* import * as z from 'zod'
*
* const countStorage = createWebStorageValue({
* type: 'sessionStorage',
* key: 'count',
* schema: z.number(),
* defaultValue: 0,
* })
*
* export default function Page() {
* const value = useSubscription(countStorage)
* return (
* <div>
* <span>{value}</span>
* </div>
* )
* }
* ```
*/
declare function createWebStorageValue<S extends StandardSchemaV1<unknown>>(options: WebStorageValueOptions<S>): WebStorageValue<StandardSchemaV1.InferOutput<S>>;
//#endregion
export { WebStorageValue, WebStorageValueOptions, createWebStorageValue };