@kitr/storage 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/api.md ADDED
@@ -0,0 +1,466 @@
1
+ ---
2
+ title: API reference
3
+ description: Every export of @kitr/storage, with its signature.
4
+ order: 2
5
+ ---
6
+
7
+ # API reference
8
+
9
+ Every export of `@kitr/storage`, `@kitr/storage/react`, `@kitr/storage/server`, `@kitr/storage/provider`, read from the published type definitions. The [guide](/packages/storage/guide) shows them in use.
10
+
11
+ ## @kitr/storage
12
+
13
+ ### createAsyncStorageDriver
14
+
15
+ ```ts
16
+ function createAsyncStorageDriver(options: {
17
+ name: string;
18
+ load(): Promise<Record<string, string>>;
19
+ write(key: string, raw: string): Promise<void>;
20
+ erase(key: string): Promise<void>;
21
+ onError?: (error: unknown) => void;
22
+ }): StorageDriver;
23
+ ```
24
+
25
+ Build a driver over an async native store. `load` runs once at boot and fills the cache;
26
+ `write`/`erase` run in the background after each change, so reads stay synchronous.
27
+
28
+ ### createMemoryStorageDriver
29
+
30
+ ```ts
31
+ function createMemoryStorageDriver(name?: string): StorageDriver;
32
+ ```
33
+
34
+ In-memory driver: tests, the server, and `session` items on native platforms.
35
+
36
+ ### createWebStorageDriver
37
+
38
+ ```ts
39
+ function createWebStorageDriver(name: StorageArea): StorageDriver | null;
40
+ ```
41
+
42
+ Web Storage as a driver. Null outside a browser, so items fall back to their defaults.
43
+
44
+ ### defineCookie
45
+
46
+ ```ts
47
+ function defineCookie<T = string>(name: string, options: CookieItemOptions<T>): CookieItem<T>;
48
+ ```
49
+
50
+ A typed cookie, readable on both sides and writable from the browser or the server.
51
+
52
+ ```ts
53
+ const locale = defineCookie('locale', { default: 'en', maxAge: 60 * 60 * 24 * 365 })
54
+ locale.get() // client: document.cookie
55
+ locale.get(request.headers) // server: any Headers / Cookie header string
56
+ locale.set('de') // client
57
+ cookies(event).set(locale, 'de') // server (@kitr/storage/server)
58
+ ```
59
+
60
+ ### defineStorage
61
+
62
+ ```ts
63
+ function defineStorage<T>(name: string, options: StorageItemOptions<T>): StorageItem<T>;
64
+ ```
65
+
66
+ A typed item in localStorage or sessionStorage.
67
+
68
+ ```ts
69
+ const sidebar = defineStorage('sidebar', { default: { open: true } })
70
+ sidebar.get().open; sidebar.set((s) => ({ ...s, open: false }))
71
+ ```
72
+ Safe everywhere: on the server (or when storage is blocked) `get()` returns the default
73
+ and `set()` is a no-op, so the same module renders on both sides.
74
+
75
+ ### getStorageDriver
76
+
77
+ ```ts
78
+ function getStorageDriver(name: StorageArea): StorageDriver | null;
79
+ ```
80
+
81
+ The driver behind an area: the platform driver, Web Storage, or null on the server.
82
+
83
+ ### listStorage
84
+
85
+ ```ts
86
+ function listStorage(name: StorageArea): Array<{
87
+ key: string;
88
+ raw: string | null;
89
+ }>;
90
+ ```
91
+
92
+ Every key in an area, for the devtools panel.
93
+
94
+ ### logStorage
95
+
96
+ ```ts
97
+ function logStorage(app: KitrApp, label: string, data?: unknown): void;
98
+ ```
99
+
100
+ Write a line to the devtools `storage` channel. Does nothing when devtools are off.
101
+
102
+ ### notifyCookiesChanged
103
+
104
+ ```ts
105
+ function notifyCookiesChanged(): void;
106
+ ```
107
+
108
+ Call after something else wrote `document.cookie` so `useCookie()` re-reads.
109
+
110
+ ### parseCookies
111
+
112
+ ```ts
113
+ function parseCookies(header: string | null | undefined): Record<string, string>;
114
+ ```
115
+
116
+ Parse a `Cookie` header into a map. Values are decoded with decodeURIComponent.
117
+
118
+ ### removeStorageKey
119
+
120
+ ```ts
121
+ function removeStorageKey(name: StorageArea, key: string): void;
122
+ ```
123
+
124
+ Delete one raw key from an area and notify subscribers. Used by the devtools panel.
125
+
126
+ ### serializeCookie
127
+
128
+ ```ts
129
+ function serializeCookie(name: string, value: string | null, options?: CookieOptions): string;
130
+ ```
131
+
132
+ Build a `Set-Cookie` value. `value === null` expires the cookie.
133
+
134
+ ### setCookieDefaults
135
+
136
+ ```ts
137
+ function setCookieDefaults(defaults: Omit<CookieOptions, 'name'>): void;
138
+ ```
139
+
140
+ Attributes merged into every cookie this app writes. The provider sets them from `cookie` in config.
141
+
142
+ ### setStorageDriver
143
+
144
+ ```ts
145
+ function setStorageDriver(driver: StorageDriver, areas?: {
146
+ local?: StorageDriver;
147
+ session?: StorageDriver;
148
+ }): void;
149
+ ```
150
+
151
+ Replace the Web Storage backing with a platform driver. Called by the storage provider when a
152
+ platform registered one (`app.drivers.register('storage', driver)`); `session` items keep an
153
+ in-memory driver unless `areas` says otherwise.
154
+
155
+ ### setStoragePrefix
156
+
157
+ ```ts
158
+ function setStoragePrefix(prefix: string): void;
159
+ ```
160
+
161
+ Set by the provider from `config/storage.ts`; items created later pick it up.
162
+
163
+ ### defineConfig
164
+
165
+ ```ts
166
+ const defineConfig: <const C extends StorageConfig>(config: C) => C;
167
+ ```
168
+
169
+ Type-checks `config/storage.ts`. Returns the object unchanged.
170
+
171
+ ### jsonCodec
172
+
173
+ ```ts
174
+ const jsonCodec: <T>() => Codec<T>;
175
+ ```
176
+
177
+ The default codec for objects: `JSON.stringify` and `JSON.parse`.
178
+
179
+ ### stringCodec
180
+
181
+ ```ts
182
+ const stringCodec: Codec<string>;
183
+ ```
184
+
185
+ Stores strings as they are, with no quotes. The default for cookies whose default is a string.
186
+
187
+ ### Codec
188
+
189
+ ```ts
190
+ interface Codec<T> {
191
+ /** Value to stored string. */
192
+ encode(value: T): string;
193
+ /** Stored string to value. Throwing makes the item fall back to its default. */
194
+ decode(raw: string): T;
195
+ }
196
+ ```
197
+
198
+ Turns a value into the string that is stored and back. Pass one as `codec` to change the format.
199
+
200
+ ### CookieItem
201
+
202
+ ```ts
203
+ interface CookieItem<T> {
204
+ /** The name it was defined with, without the config prefix. */
205
+ readonly name: string;
206
+ /** Returned when the cookie is missing or invalid. */
207
+ readonly default: T;
208
+ /** The attributes given to `defineCookie`. */
209
+ readonly options: CookieOptions;
210
+ /** Read from `document.cookie`, or from the given source on the server. */
211
+ get(source?: CookieSource): T;
212
+ /** Write `document.cookie` (client). On the server use `cookies(event).set(item, value)`. */
213
+ set(value: T, options?: CookieOptions): void;
214
+ /** Expire the cookie in the browser. A no-op on the server. */
215
+ remove(): void;
216
+ /** Fires after `set`, `remove` or `notifyCookiesChanged()` in this tab. */
217
+ subscribe(listener: (value: T) => void): () => void;
218
+ /** The `Set-Cookie` header value for this item. */
219
+ serialize(value: T | null, options?: CookieOptions): string;
220
+ /** Parse one raw cookie value into T (or the default). */
221
+ decode(raw: string | undefined): T;
222
+ }
223
+ ```
224
+
225
+ A typed cookie returned by `defineCookie`, readable on both sides.
226
+
227
+ ### CookieItemOptions
228
+
229
+ ```ts
230
+ interface CookieItemOptions<T> extends CookieOptions {
231
+ /** Returned when the cookie is missing or invalid. */
232
+ default: T;
233
+ /** Validate the decoded value (Standard Schema). Invalid → default. */
234
+ schema?: StandardSchemaV1<unknown, T>;
235
+ /** Default: JSON for objects, plain string for string defaults. */
236
+ codec?: Codec<T>;
237
+ }
238
+ ```
239
+
240
+ Options for `defineCookie`: the default, validation, format, and the cookie attributes.
241
+
242
+ ### CookieOptions
243
+
244
+ ```ts
245
+ interface CookieOptions {
246
+ /** URL path the cookie is sent for. */
247
+ path?: string;
248
+ /** Domain the cookie is sent to, e.g. `.acme.com` to share it with subdomains. */
249
+ domain?: string;
250
+ /** Seconds. */
251
+ maxAge?: number;
252
+ /** Absolute expiry date. */
253
+ expires?: Date;
254
+ /** Send only over HTTPS. Forced on when `sameSite` is `none`. */
255
+ secure?: boolean;
256
+ /** Server-set cookies only; the browser cannot write httpOnly cookies. */
257
+ httpOnly?: boolean;
258
+ /** Cross-site sending rule. */
259
+ sameSite?: 'lax' | 'strict' | 'none';
260
+ }
261
+ ```
262
+
263
+ Attributes written into `Set-Cookie`. `path: '/'` and `sameSite: 'lax'` apply unless overridden.
264
+
265
+ ### StorageConfig
266
+
267
+ ```ts
268
+ interface StorageConfig {
269
+ /** Prefix for every storage key and cookie name, e.g. `myapp:`. Default none. */
270
+ prefix?: string;
271
+ /** Defaults merged into every cookie. */
272
+ cookie?: Omit<CookieOptions, 'name'>;
273
+ }
274
+ ```
275
+
276
+ Options for `config/storage.ts`: a key prefix and cookie defaults.
277
+
278
+ ### StorageDriver
279
+
280
+ ```ts
281
+ interface StorageDriver {
282
+ /** Shown in the devtools log, e.g. `web:local` or `capacitor:preferences`. */
283
+ name: string;
284
+ /** Load every stored key into memory before the first render. */
285
+ hydrate?(): Promise<void>;
286
+ /** The raw string stored under `key`, or null. Must answer synchronously. */
287
+ get(key: string): string | null;
288
+ /** Store a raw string. May persist in the background. */
289
+ set(key: string, raw: string): void;
290
+ /** Delete one key. */
291
+ remove(key: string): void;
292
+ /** Every stored key, for the devtools panel. */
293
+ keys(): string[];
294
+ /** Changes made elsewhere (another tab, the native side). */
295
+ subscribe?(listener: (key: string | null) => void): () => void;
296
+ }
297
+ ```
298
+
299
+ What a platform supplies to back `defineStorage` items: Capacitor Preferences on mobile,
300
+ the Tauri store or an Electron JSON file on desktop, Web Storage in the browser.
301
+ Reads are synchronous from a cache `hydrate()` fills at boot; writes go through in the background.
302
+
303
+ ### StorageItem
304
+
305
+ ```ts
306
+ interface StorageItem<T> {
307
+ /** The name it was defined with, without the config prefix. */
308
+ readonly key: string;
309
+ /** Which area it lives in. */
310
+ readonly area: StorageArea;
311
+ /** The stored value, or the default when it is missing, invalid, expired or on the server. */
312
+ get(): T;
313
+ /** Store a value, or compute it from the current one. A no-op on the server. */
314
+ set(value: T | ((current: T) => T)): void;
315
+ /** Delete the stored value so `get()` returns the default. */
316
+ remove(): void;
317
+ /** Fires on set/remove here and in other tabs (`storage` event). */
318
+ subscribe(listener: (value: T) => void): () => void;
319
+ /** The default, for server snapshots. */
320
+ readonly default: T;
321
+ }
322
+ ```
323
+
324
+ A typed value in local or session storage, returned by `defineStorage`.
325
+
326
+ ### StorageItemOptions
327
+
328
+ ```ts
329
+ interface StorageItemOptions<T> {
330
+ /** Returned when the key is missing, unparsable, invalid or on the server. */
331
+ default: T;
332
+ /** Where it is stored. Default `local`. */
333
+ area?: StorageArea;
334
+ /** Validate what comes back from storage (Standard Schema: zod, valibot...). Invalid → default. */
335
+ schema?: StandardSchemaV1<unknown, T>;
336
+ /** Format of the stored envelope. Default: JSON. */
337
+ codec?: Codec<T>;
338
+ /** Bump when the shape changes; stored values with another version are migrated or dropped. */
339
+ version?: number;
340
+ /** Converts a value stored under an older `version`. Without it, old values are dropped. */
341
+ migrate?: (raw: unknown, fromVersion: number) => T;
342
+ /** Keep this many ms, then treat as missing. Stored beside the value. */
343
+ ttl?: number;
344
+ }
345
+ ```
346
+
347
+ Options for `defineStorage`: the default, the area, and how stored values are checked.
348
+
349
+ ### CookieSource
350
+
351
+ ```ts
352
+ type CookieSource = string | null | undefined | Headers | {
353
+ cookie?: string | null;
354
+ };
355
+ ```
356
+
357
+ Anything that can hold cookies: a `Cookie` header string, a Headers object or `document`.
358
+
359
+ ### StorageArea
360
+
361
+ ```ts
362
+ type StorageArea = 'local' | 'session';
363
+ ```
364
+
365
+ `local` keeps a value until it is cleared; `session` keeps it for the current tab.
366
+
367
+ ## @kitr/storage/react
368
+
369
+ ### CookieHeader
370
+
371
+ ```ts
372
+ function CookieHeader({ value, children }: {
373
+ value: string | null | undefined;
374
+ children: ReactNode;
375
+ }): import("react").FunctionComponentElement<import("react").ProviderProps<string | null | undefined>>;
376
+ ```
377
+
378
+ Gives `useCookie()` the request's `Cookie` header during server rendering, so the first
379
+ HTML shows the stored value instead of the default. Wrap the tree once, in the root route.
380
+
381
+ ### useCookie
382
+
383
+ ```ts
384
+ function useCookie<T>(item: CookieItem<T>): [
385
+ T,
386
+ (value: T) => void,
387
+ () => void
388
+ ];
389
+ ```
390
+
391
+ Read and write a cookie from a component. On the server the value comes from
392
+ `<CookieHeader>` when present, otherwise the default.
393
+
394
+ ### useStorage
395
+
396
+ ```ts
397
+ function useStorage<T>(item: StorageItem<T>): [
398
+ T,
399
+ (value: T | ((current: T) => T)) => void,
400
+ () => void
401
+ ];
402
+ ```
403
+
404
+ Read and write a storage item from a component. Hydration-safe: the server (and the first
405
+ client render) see the default, then the stored value applies, so markup always matches.
406
+
407
+ ## @kitr/storage/server
408
+
409
+ ### cookies
410
+
411
+ ```ts
412
+ function cookies(event: CookieEventLike): ServerCookies;
413
+
414
+ function cookies(request: Request, responseHeaders: Headers): ServerCookies;
415
+ ```
416
+
417
+ Typed cookies on the server: `cookies(event)` in a controller or middleware,
418
+ `cookies(request, responseHeaders)` anywhere you only have the Fetch primitives.
419
+
420
+ ### CookieEventLike
421
+
422
+ ```ts
423
+ interface CookieEventLike {
424
+ /** The request; only its headers are read. */
425
+ req: {
426
+ headers: Headers;
427
+ };
428
+ /** The response; `Set-Cookie` headers are appended here. */
429
+ res: {
430
+ headers: Headers;
431
+ };
432
+ }
433
+ ```
434
+
435
+ The parts of an h3 v2 event (or anything request/response shaped) that cookies need.
436
+
437
+ ### ServerCookies
438
+
439
+ ```ts
440
+ interface ServerCookies {
441
+ /** The item's value from the request's `Cookie` header, or its default. */
442
+ get<T>(item: CookieItem<T>): T;
443
+ /** Appends a `Set-Cookie` header; `httpOnly` and the other item options apply. */
444
+ set<T>(item: CookieItem<T>, value: T, options?: CookieOptions): void;
445
+ /** Appends a `Set-Cookie` header that expires the cookie. */
446
+ remove(item: CookieItem<unknown>): void;
447
+ /** The raw `Cookie` header, for `<CookieHeader>` during SSR. */
448
+ readonly header: string | null;
449
+ }
450
+ ```
451
+
452
+ Typed cookie access for one request, returned by `cookies()`.
453
+
454
+ ## @kitr/storage/provider
455
+
456
+ ### storageProvider
457
+
458
+ ```ts
459
+ const storageProvider: import("@kitr/core").KitrProvider;
460
+ ```
461
+
462
+ Applies `config/storage.ts` (key prefix, cookie defaults), switches `defineStorage`
463
+ items to the platform's native driver when one is registered, hydrates it before the first
464
+ render, and contributes the Storage panel to the devtools.
465
+
466
+ <!-- GENERATED by scripts/api-docs.mjs from the type definitions. Do not edit. -->