@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/LICENSE +21 -0
- package/README.md +14 -2
- package/dist/devtools-BTqcJ52N.js +143 -0
- package/dist/index.d.ts +202 -0
- package/dist/index.js +367 -0
- package/dist/kitr.d.ts +10 -0
- package/dist/kitr.js +46 -0
- package/dist/provider.d.ts +7 -0
- package/dist/provider.js +29 -0
- package/dist/react.d.ts +20 -0
- package/dist/react.js +40 -0
- package/dist/server.d.ts +29 -0
- package/dist/server.js +14 -0
- package/docs/api.md +466 -0
- package/docs/index.md +808 -0
- package/docs/overview.md +68 -0
- package/package.json +88 -3
package/docs/overview.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Overview
|
|
3
|
+
description: Save small values in the browser or in cookies with types, and read the same cookies on the server.
|
|
4
|
+
order: 0
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Storage
|
|
8
|
+
|
|
9
|
+
## What it is
|
|
10
|
+
|
|
11
|
+
`@kitr/storage` stores small values the device owns: preferences, drafts, recent searches. `defineStorage` and `useStorage` cover local and session storage; `defineCookie` and `useCookie` cover cookies on both sides. Everything is typed, versioned and falls back to its default.
|
|
12
|
+
|
|
13
|
+
Storage items run in the browser and native shells; cookies run on both sides. For secrets, use [secure storage](/packages/secure-storage).
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
kitr add storage
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Adds `@kitr/storage`, its provider, `config/storage.ts` and `app/storage.ts` with three sample items. [Every change](/packages/storage/guide#install).
|
|
22
|
+
|
|
23
|
+
## Quick start
|
|
24
|
+
|
|
25
|
+
Declare each value once in `app/storage.ts`, then read and write it like `useState`:
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
// app/components/sidebar-toggle.tsx
|
|
29
|
+
import { useStorage } from '@kitr/storage/react'
|
|
30
|
+
import { sidebar } from '#app/storage'
|
|
31
|
+
|
|
32
|
+
export function SidebarToggle() {
|
|
33
|
+
const [state, setState] = useStorage(sidebar)
|
|
34
|
+
return (
|
|
35
|
+
<button type="button" onClick={() => setState((s) => ({ collapsed: !s.collapsed }))}>
|
|
36
|
+
{state.collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
|
|
37
|
+
</button>
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Put `<SidebarToggle />` in your layout and click it. The label changes to "Expand sidebar". Reload the page: it still says "Expand sidebar". Under Application → Local Storage in the browser's devtools you see one key:
|
|
43
|
+
|
|
44
|
+
```text
|
|
45
|
+
acme:sidebar {"v":1,"d":{"collapsed":true}}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`v` is the version and `d` the data; you never read the envelope yourself. In native shells the same item lands in the OS store. Next in the guide: [Save your first value](/packages/storage/guide#save-your-first-value).
|
|
49
|
+
|
|
50
|
+
## Built on
|
|
51
|
+
|
|
52
|
+
kitr adds types, schema validation, versions with migrations, expiry, cross-tab sync, hydration-safe hooks, the server cookie API and memory drivers.
|
|
53
|
+
|
|
54
|
+
## Problems it solves
|
|
55
|
+
|
|
56
|
+
- **Parsing and defaulting by hand.** [Save your first value](/packages/storage/guide#save-your-first-value).
|
|
57
|
+
- **Old values after a release.** [Change a value's shape with versions](/packages/storage/guide#change-a-values-shape-with-versions).
|
|
58
|
+
- **Tabs that disagree.** [Keep tabs in sync](/packages/storage/guide#keep-tabs-in-sync).
|
|
59
|
+
- **Hydration mismatches.** [Render on the server without a hydration mismatch](/packages/storage/guide#render-on-the-server-without-a-hydration-mismatch).
|
|
60
|
+
- **Cookies read differently on each side.** [Read and set cookies on the server](/packages/storage/guide#read-and-set-cookies-on-the-server).
|
|
61
|
+
- **Blocked or full storage.** [Handle missing, invalid and blocked storage](/packages/storage/guide#handle-missing-invalid-and-blocked-storage).
|
|
62
|
+
|
|
63
|
+
## Further reading
|
|
64
|
+
|
|
65
|
+
- [Guide](/packages/storage/guide).
|
|
66
|
+
- [API reference](/packages/storage/api).
|
|
67
|
+
- [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API) and [Using HTTP cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) on MDN.
|
|
68
|
+
- [Secure storage](/packages/secure-storage), [Offline](/packages/offline) and the platforms ([Desktop](/packages/desktop), [Mobile](/packages/mobile)).
|
package/package.json
CHANGED
|
@@ -1,6 +1,91 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kitr/storage",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "@kitr/storage: typed localStorage/sessionStorage items and cookies with one API on the client and the server; SSR-safe React hooks.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"kitr",
|
|
7
|
+
"kitr-package"
|
|
8
|
+
],
|
|
9
|
+
"type": "module",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/kitrjs/kitr.git",
|
|
14
|
+
"directory": "packages/storage"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://kitr.dev/packages/storage",
|
|
17
|
+
"bugs": "https://github.com/kitrjs/kitr/issues",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./react": {
|
|
28
|
+
"types": "./dist/react.d.ts",
|
|
29
|
+
"default": "./dist/react.js"
|
|
30
|
+
},
|
|
31
|
+
"./server": {
|
|
32
|
+
"types": "./dist/server.d.ts",
|
|
33
|
+
"default": "./dist/server.js"
|
|
34
|
+
},
|
|
35
|
+
"./provider": {
|
|
36
|
+
"types": "./dist/provider.d.ts",
|
|
37
|
+
"default": "./dist/provider.js"
|
|
38
|
+
},
|
|
39
|
+
"./kitr": {
|
|
40
|
+
"types": "./dist/kitr.d.ts",
|
|
41
|
+
"default": "./dist/kitr.js"
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"dist",
|
|
47
|
+
"docs"
|
|
48
|
+
],
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@kitr/core": "0.2.1"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"react": ">=18.0.0",
|
|
54
|
+
"h3": ">=2.0.0"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"react": {
|
|
58
|
+
"optional": true
|
|
59
|
+
},
|
|
60
|
+
"h3": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"tsdown": "^0.23.7",
|
|
66
|
+
"typescript": "^5.9.3",
|
|
67
|
+
"react": "^19.3.1",
|
|
68
|
+
"@types/react": "^19.3.0",
|
|
69
|
+
"h3": "^2.0.8",
|
|
70
|
+
"@types/node": "^22.19.0",
|
|
71
|
+
"react-dom": "^19.3.1",
|
|
72
|
+
"@kitr/devtools": "0.2.1"
|
|
73
|
+
},
|
|
74
|
+
"kitr": {
|
|
75
|
+
"name": "storage",
|
|
76
|
+
"title": "Storage",
|
|
77
|
+
"summary": "Save small values in the browser or in cookies with types, and read the same cookies on the server.",
|
|
78
|
+
"category": "device",
|
|
79
|
+
"runsOn": "both",
|
|
80
|
+
"builtOn": [
|
|
81
|
+
"localStorage",
|
|
82
|
+
"cookies"
|
|
83
|
+
],
|
|
84
|
+
"docs": "docs",
|
|
85
|
+
"setup": "./kitr"
|
|
86
|
+
},
|
|
87
|
+
"scripts": {
|
|
88
|
+
"build": "tsdown",
|
|
89
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
90
|
+
}
|
|
6
91
|
}
|