pinia-plugin-persist-state 0.0.0-alpha.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 +113 -0
- package/dist/pinia-plugin-persist-state.js +2814 -0
- package/dist/pinia-plugin-persist-state.umd.cjs +9 -0
- package/dist/services/Crypt.d.ts +23 -0
- package/dist/services/IndexedDB.d.ts +34 -0
- package/dist/services/Persister.d.ts +15 -0
- package/dist/services/WindowStorage.d.ts +10 -0
- package/dist/types/index.d.ts +21 -0
- package/dist/types/storage.d.ts +12 -0
- package/dist/types/store.d.ts +27 -0
- package/dist/utils/constantes.d.ts +1 -0
- package/dist/utils/pluginConsole.d.ts +6 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Alexandre Bidaud
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Persist Pinia State Plugin
|
|
2
|
+
|
|
3
|
+
A small Pinia plugin that adds persistence and optional encryption to your Pinia stores. It integrates with browser storage (localStorage or sessionStorage) or IndexedDB and augments stores with convenient methods to persist, restore and control watching behavior.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 🔧 Features
|
|
8
|
+
|
|
9
|
+
- Persist store state to LocalStorage, SessionStorage or IndexedDB
|
|
10
|
+
- Selective encryption for specific properties using Web Crypto (AES-GCM)
|
|
11
|
+
- Per-store options via `storeOptions` when defining a store
|
|
12
|
+
- Augmented store API: `persistState`, `remember`, `removePersistedState`, `watch`, `stopWatch`
|
|
13
|
+
- Simple initialization through `createPersistStatePlugin(dbName?, cryptKey?)`
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## ⚙️ Installation
|
|
18
|
+
|
|
19
|
+
Install the package (example):
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install --save persist-pinia-state
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Then register the plugin with Pinia in your app entry (see `src/main.ts`):
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { createApp } from 'vue'
|
|
29
|
+
import { createPinia } from 'pinia'
|
|
30
|
+
import { createPlugin } from 'pinia-plugin-subscription'
|
|
31
|
+
import { createPersistStatePlugin } from 'persist-pinia-state'
|
|
32
|
+
|
|
33
|
+
const app = createApp(App)
|
|
34
|
+
const pinia = createPinia()
|
|
35
|
+
|
|
36
|
+
// Pass `dbName` (e.g. 'localStorage' or a DB name for IndexedDB)
|
|
37
|
+
// and an optional `cryptKey` to enable encryption support
|
|
38
|
+
pinia.use(createPlugin([
|
|
39
|
+
createPersistStatePlugin('localStorage', 'my-secret-key')
|
|
40
|
+
], true))
|
|
41
|
+
|
|
42
|
+
app.use(pinia)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 📚 Usage
|
|
48
|
+
|
|
49
|
+
When defining a store you can pass `storeOptions` (type: `PersistedStoreOptions`) as part of the `defineStore` options. Example in `src/stores/test.ts`:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
const storeOptions = {
|
|
53
|
+
persist: true,
|
|
54
|
+
persistedPropertiesToEncrypt: ['myStringEncrypted'],
|
|
55
|
+
watchMutation: true
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const useTestStore = defineStore('testStore', () => {
|
|
59
|
+
const myString = ref('Hello World')
|
|
60
|
+
const myStringEncrypted = ref('Sensitive Data')
|
|
61
|
+
|
|
62
|
+
return { myString, myStringEncrypted }
|
|
63
|
+
}, { storeOptions })
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
If `persist` is `true` the plugin will attempt to persist the store state using the configured persister (localStorage/sessionStorage/IndexedDB).
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## 🔣 PersistedStoreOptions
|
|
71
|
+
|
|
72
|
+
Fields available when setting `storeOptions`:
|
|
73
|
+
|
|
74
|
+
- `dbName?: string` — (Optional) Name of the database/storage to use. Use `'localStorage'` or `'sessionStorage'` for window storage, or any other name to use IndexedDB.
|
|
75
|
+
- `excludedKeys?: string[]` — Keys that should NOT be persisted.
|
|
76
|
+
- `isEncrypted?: boolean` — Internal flag used to reflect whether the persisted state is currently encrypted.
|
|
77
|
+
- `persist?: boolean` — Enable or disable persistence for the store (default: `false`).
|
|
78
|
+
- `persistedPropertiesToEncrypt?: string[]` — List of property names to be encrypted when persisted.
|
|
79
|
+
- `watchMutation?: boolean` — When `true`, plugin watches store mutations and automatically persists changes.
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## 🧰 Augmented Store API
|
|
84
|
+
|
|
85
|
+
When the plugin is active stores gain the following methods (see `PersistedStore` interface):
|
|
86
|
+
|
|
87
|
+
- `persistState(): Promise<void>` — Immediately persist the current store state (ignores empty values and excluded keys).
|
|
88
|
+
- `remember(): Promise<void>` — Load persisted state and apply it to the store (used on plugin init).
|
|
89
|
+
- `removePersistedState(): void` — Delete the persisted entry for this store.
|
|
90
|
+
- `watch(): void` — Start watching for mutations (sets `watchMutation = true`).
|
|
91
|
+
- `stopWatch(): void` — Stop auto-persisting on mutations (sets `watchMutation = false`).
|
|
92
|
+
|
|
93
|
+
Note: encrypted properties are automatically decrypted when remembered (if a crypto key was provided during plugin creation).
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## 🔐 Encryption
|
|
98
|
+
|
|
99
|
+
Optionally supply a `cryptKey` when creating the plugin, e.g. `createPersistStatePlugin(undefined, 'my-secret')`.
|
|
100
|
+
The plugin uses the Web Crypto API (PBKDF2 + AES-GCM) to encrypt properties listed in `persistedPropertiesToEncrypt` on each store. Only the specified properties will be encrypted.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## 💡 Notes
|
|
105
|
+
|
|
106
|
+
- The plugin augments Pinia store definitions using the `pinia-plugin-subscription` helper. It adds `storeOptions` to Pinia's `DefineStoreOptionsBase` type through declaration merging.
|
|
107
|
+
- When using IndexedDB, the persister stores objects with a `storeName` key path.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## 📜 License
|
|
112
|
+
|
|
113
|
+
MIT
|