-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtype.ts
153 lines (135 loc) · 3.91 KB
/
type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { StateTree, SubscriptionCallback } from 'pinia'
import { UnwrapRef } from 'vue'
export interface IStorage {
getItem: (key: string) => any | Promise<any>
setItem: (key: string, value: any) => void | Promise<void>
removeItem: (key: string) => void | Promise<void>
}
/**
* @description
* PluginOptions and StoreOptions shares CommonOptions interface,
*
* @example
*
* You could pass most commonly used option to PluginOptions:
*
* `store1` will persist to and rehydrate from `localStorage`, and
* `store2` will persist to and rehydrate from `sessionStorage`:
*
* ```
* const plugin = createPersistedStatePlugin({ storage: localStorage })
* const store1 = defineStore('store-1', () => {})
* const store2 = defineStore('store-2', () => {}, { persistedState: { storage: sessionStorage } })
* ```
*/
export interface CommonOptions<S extends StateTree = StateTree> {
/**
* Whether to persist store.
* @default true
*/
persist?: boolean
/**
* Where to store persisted state.
*
* @default localStorage
*/
storage?: IStorage
/**
* To ensure storage is available.
*/
assertStorage?: (storage: IStorage) => void | Promise<void> | never
/**
* A function for merging state when rehydrating state.
*
* @default (state, savedState) => savedState
*/
merge?: (state: UnwrapRef<S>, savedState: UnwrapRef<S>) => UnwrapRef<S>
/**
* When rehydrating, overwrite initial state (patch otherwise).
*
* @default false
*/
overwrite?: boolean
/**
* This method will be called right before `storage.setItem`.
*
* @default JSON.stringify
*/
serialize?: (state: UnwrapRef<S>) => any
/**
* This method will be called right after `storage.getItem`.
*
* @default JSON.parse
*/
deserialize?: (value: any) => any
/**
* A function that will be called to filter any mutations which will trigger setState on storage eventually.
*
* @default () => true
*/
filter?: (
mutation: Parameters<SubscriptionCallback<S>>['0'],
state: Parameters<SubscriptionCallback<S>>['1'],
) => boolean
}
export type PluginOptions<S extends StateTree> = CommonOptions<S>
export type StoreOptions<S extends StateTree> = CommonOptions<S> & {
/**
* The key to store the persisted state under.
*
* @default store.$id
*/
key?: string
/**
* An array of any paths to partially persist the state.
*
* Use dot-notation `['key', 'nested.key', ['special.key']]` for nested fields.
*
* @default undefined
*
*/
includePaths?: (string | string[])[]
/**
* Opposite to `includePaths`, An array of any paths to exclude.
*
* Use dot-notation `['key', 'nested.key', ['special.key']]` for nested fields.
*
* Warning: Due to deep copying, `excludePaths` may cause performance issues, if possible, please use `includePaths` instead.
* @default undefined
*/
excludePaths?: (string | string[])[]
/**
* The `migrate` function enables versioning store. This will be called after `deserialize` but before actually overwriting/patching the store.
*
* @default {value => value}
*/
migrate?: (value: any) => any | Promise<any>
/**
* This function gives you the opportunity to perform some tasks before actually overwriting/patching the store, such as cleaning up the old state.
*
* @default {value => value}
*/
beforeHydrate?: (oldState: UnwrapRef<S>) => void
}
declare module 'pinia' {
export interface DefineStoreOptionsBase<S extends StateTree, Store> {
persistedState?: StoreOptions<S>
}
export interface PiniaCustomProperties<
Id extends string = string,
S extends StateTree = StateTree,
G /* extends GettersTree<S> */ = _GettersTree<S>,
A /* extends ActionsTree */ = _ActionsTree,
> {
$persistedState: {
/**
* Whether store is hydrated
*/
isReady: () => Promise<void>
/**
* Whether store is persisting
*/
pending: boolean
}
}
}