pinia-react 1.5.2 → 2.1.0
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 +23 -0
- package/dist/index.d.ts +55 -563
- package/dist/index.js +358 -343
- package/dist/index.js.map +1 -1
- package/package.json +11 -13
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 karl
|
|
4
|
+
|
|
5
|
+
Portions of this software are based on Pinia, Copyright (c) Eduardo San Martin Morote
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,578 +1,70 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Draft, Patch } from 'immer';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type StateTree = Record<PropertyKey, any>;
|
|
7
|
-
/**
|
|
8
|
-
* Recursive `Partial<T>`. Used by {@link Store['$patch']}.
|
|
9
|
-
*
|
|
10
|
-
* For internal use **only**
|
|
11
|
-
*/
|
|
12
|
-
type _DeepPartial<T> = {
|
|
13
|
-
[K in keyof T]?: _DeepPartial<T[K]>;
|
|
3
|
+
type StateTree = Record<string, any>;
|
|
4
|
+
type TransformGetters<G> = {
|
|
5
|
+
[K in keyof G]: G[K] extends (...args: any[]) => infer R ? R : never;
|
|
14
6
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*/
|
|
32
|
-
patchObject = "patch object",
|
|
33
|
-
/**
|
|
34
|
-
* Mutated the state with `$patch` and a function
|
|
35
|
-
*
|
|
36
|
-
* - `store.$patch(state => state.name = 'newName')`
|
|
37
|
-
*/
|
|
38
|
-
patchFunction = "patch function"
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Base type for the context passed to a subscription callback. Internal type.
|
|
42
|
-
*/
|
|
43
|
-
interface _SubscriptionCallbackMutationBase {
|
|
44
|
-
/**
|
|
45
|
-
* Type of the mutation.
|
|
46
|
-
*/
|
|
47
|
-
type: MutationType;
|
|
48
|
-
/**
|
|
49
|
-
* `id` of the store doing the mutation.
|
|
50
|
-
*/
|
|
51
|
-
storeId: string;
|
|
52
|
-
/**
|
|
53
|
-
* 🔴 DEV ONLY, DO NOT use for production code. Different mutation calls. Comes from
|
|
54
|
-
* https://vuejs.org/guide/extras/reactivity-in-depth.html#reactivity-debugging and allows to track mutations in
|
|
55
|
-
* devtools and plugins **during development only**.
|
|
56
|
-
*/
|
|
57
|
-
events?: DebuggerEvent[] | DebuggerEvent;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Context passed to a subscription callback when directly mutating the state of
|
|
61
|
-
* a store with `store.someState = newValue` or `store.$state.someState =
|
|
62
|
-
* newValue`.
|
|
63
|
-
*/
|
|
64
|
-
interface SubscriptionCallbackMutationDirect extends _SubscriptionCallbackMutationBase {
|
|
65
|
-
type: MutationType.direct;
|
|
66
|
-
events: DebuggerEvent;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Context passed to a subscription callback when `store.$patch()` is called
|
|
70
|
-
* with an object.
|
|
71
|
-
*/
|
|
72
|
-
interface SubscriptionCallbackMutationPatchObject<S> extends _SubscriptionCallbackMutationBase {
|
|
73
|
-
type: MutationType.patchObject;
|
|
74
|
-
events: DebuggerEvent[];
|
|
75
|
-
/**
|
|
76
|
-
* Object passed to `store.$patch()`.
|
|
77
|
-
*/
|
|
78
|
-
payload: _DeepPartial<UnwrapRef<S>>;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Context passed to a subscription callback when `store.$patch()` is called
|
|
82
|
-
* with a function.
|
|
83
|
-
*/
|
|
84
|
-
interface SubscriptionCallbackMutationPatchFunction extends _SubscriptionCallbackMutationBase {
|
|
85
|
-
type: MutationType.patchFunction;
|
|
86
|
-
events: DebuggerEvent[];
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Context object passed to a subscription callback.
|
|
90
|
-
*/
|
|
91
|
-
type SubscriptionCallbackMutation<S> = SubscriptionCallbackMutationDirect | SubscriptionCallbackMutationPatchObject<S> | SubscriptionCallbackMutationPatchFunction;
|
|
92
|
-
/**
|
|
93
|
-
* Callback of a subscription
|
|
94
|
-
*/
|
|
95
|
-
type SubscriptionCallback<S> = (
|
|
96
|
-
/**
|
|
97
|
-
* Object with information relative to the store mutation that triggered the
|
|
98
|
-
* subscription.
|
|
99
|
-
*/
|
|
100
|
-
mutation: SubscriptionCallbackMutation<S>,
|
|
101
|
-
/**
|
|
102
|
-
* State of the store when the subscription is triggered. Same as
|
|
103
|
-
* `store.$state`.
|
|
104
|
-
*/
|
|
105
|
-
state: UnwrapRef<S>) => void;
|
|
106
|
-
/**
|
|
107
|
-
* Actual type for {@link StoreOnActionListenerContext}. Exists for refactoring
|
|
108
|
-
* purposes. For internal use only.
|
|
109
|
-
* For internal use **only**
|
|
110
|
-
*/
|
|
111
|
-
interface _StoreOnActionListenerContext<Store, ActionName extends string, A> {
|
|
112
|
-
/**
|
|
113
|
-
* Name of the action
|
|
114
|
-
*/
|
|
115
|
-
name: ActionName;
|
|
116
|
-
/**
|
|
117
|
-
* Store that is invoking the action
|
|
118
|
-
*/
|
|
119
|
-
store: Store;
|
|
120
|
-
/**
|
|
121
|
-
* Parameters passed to the action
|
|
122
|
-
*/
|
|
123
|
-
args: A extends Record<ActionName, _Method> ? Parameters<A[ActionName]> : unknown[];
|
|
124
|
-
/**
|
|
125
|
-
* Sets up a hook once the action is finished. It receives the return value
|
|
126
|
-
* of the action, if it's a Promise, it will be unwrapped.
|
|
127
|
-
*/
|
|
128
|
-
after: (callback: A extends Record<ActionName, _Method> ? (resolvedReturn: Awaited<ReturnType<A[ActionName]>>) => void : () => void) => void;
|
|
129
|
-
/**
|
|
130
|
-
* Sets up a hook if the action fails. Return `false` to catch the error and
|
|
131
|
-
* stop it from propagating.
|
|
132
|
-
*/
|
|
133
|
-
onError: (callback: (error: unknown) => void) => void;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Context object passed to callbacks of `store.$onAction(context => {})`
|
|
137
|
-
* TODO: should have only the Id, the Store and Actions to generate the proper object
|
|
138
|
-
*/
|
|
139
|
-
type StoreOnActionListenerContext<Id extends string, S extends StateTree, G, A> = _ActionsTree extends A ? _StoreOnActionListenerContext<StoreGeneric, string, _ActionsTree> : {
|
|
140
|
-
[Name in keyof A]: Name extends string ? _StoreOnActionListenerContext<Store<Id, S, G, A>, Name, A> : never;
|
|
141
|
-
}[keyof A];
|
|
142
|
-
/**
|
|
143
|
-
* Argument of `store.$onAction()`
|
|
144
|
-
*/
|
|
145
|
-
type StoreOnActionListener<Id extends string, S extends StateTree, G, A> = (context: StoreOnActionListenerContext<Id, S, G, {} extends A ? _ActionsTree : A>) => void;
|
|
146
|
-
/**
|
|
147
|
-
* Properties of a store.
|
|
148
|
-
*/
|
|
149
|
-
interface StoreProperties<Id extends string> {
|
|
150
|
-
/**
|
|
151
|
-
* Unique identifier of the store
|
|
152
|
-
*/
|
|
153
|
-
$id: Id;
|
|
154
|
-
/**
|
|
155
|
-
* Private property defining the pinia the store is attached to.
|
|
156
|
-
*
|
|
157
|
-
* @internal
|
|
158
|
-
*/
|
|
159
|
-
_p: Pinia;
|
|
160
|
-
/**
|
|
161
|
-
* Used by devtools plugin to retrieve getters. Removed in production.
|
|
162
|
-
*
|
|
163
|
-
* @internal
|
|
164
|
-
*/
|
|
165
|
-
_getters?: string[];
|
|
166
|
-
/**
|
|
167
|
-
* Used (and added) by devtools plugin to detect Setup vs Options API usage.
|
|
168
|
-
*
|
|
169
|
-
* @internal
|
|
170
|
-
*/
|
|
171
|
-
_isOptionsAPI?: boolean;
|
|
172
|
-
/**
|
|
173
|
-
* Used by devtools plugin to retrieve properties added with plugins. Removed
|
|
174
|
-
* in production. Can be used by the user to add property keys of the store
|
|
175
|
-
* that should be displayed in devtools.
|
|
176
|
-
*/
|
|
177
|
-
_customProperties: Set<string>;
|
|
178
|
-
/**
|
|
179
|
-
* Handles a HMR replacement of this store. Dev Only.
|
|
180
|
-
*
|
|
181
|
-
* @internal
|
|
182
|
-
*/
|
|
183
|
-
_hotUpdate(useStore: StoreGeneric): void;
|
|
184
|
-
/**
|
|
185
|
-
* Allows pausing some of the watching mechanisms while the store is being
|
|
186
|
-
* patched with a newer version.
|
|
187
|
-
*
|
|
188
|
-
* @internal
|
|
189
|
-
*/
|
|
190
|
-
_hotUpdating: boolean;
|
|
191
|
-
/**
|
|
192
|
-
* Payload of the hmr update. Dev only.
|
|
193
|
-
*
|
|
194
|
-
* @internal
|
|
195
|
-
*/
|
|
196
|
-
_hmrPayload: {
|
|
197
|
-
state: string[];
|
|
198
|
-
hotState: Ref<StateTree>;
|
|
199
|
-
actions: _ActionsTree;
|
|
200
|
-
getters: _ActionsTree;
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Base store with state and functions. Should not be used directly.
|
|
205
|
-
*/
|
|
206
|
-
interface _StoreWithState<Id extends string, S extends StateTree, G, A> extends StoreProperties<Id> {
|
|
207
|
-
/**
|
|
208
|
-
* State of the Store. Setting it will internally call `$patch()` to update the state.
|
|
209
|
-
*/
|
|
210
|
-
$state: UnwrapRef<S> & PiniaCustomStateProperties<S>;
|
|
211
|
-
/**
|
|
212
|
-
* Applies a state patch to current state. Allows passing nested values
|
|
213
|
-
*
|
|
214
|
-
* @param partialState - patch to apply to the state
|
|
215
|
-
*/
|
|
216
|
-
$patch(partialState: _DeepPartial<UnwrapRef<S>>): void;
|
|
217
|
-
/**
|
|
218
|
-
* Group multiple changes into one function. Useful when mutating objects like
|
|
219
|
-
* Sets or arrays and applying an object patch isn't practical, e.g. appending
|
|
220
|
-
* to an array. The function passed to `$patch()` **must be synchronous**.
|
|
221
|
-
*
|
|
222
|
-
* @param stateMutator - function that mutates `state`, cannot be asynchronous
|
|
223
|
-
*/
|
|
224
|
-
$patch<F extends (state: UnwrapRef<S>) => any>(stateMutator: ReturnType<F> extends Promise<any> ? never : F): void;
|
|
225
|
-
/**
|
|
226
|
-
* Resets the store to its initial state by building a new state object.
|
|
227
|
-
*/
|
|
228
|
-
$reset(): void;
|
|
229
|
-
/**
|
|
230
|
-
* Setups a callback to be called whenever the state changes. It also returns a function to remove the callback. Note
|
|
231
|
-
* that when calling `store.$subscribe()` inside of a component, it will be automatically cleaned up when the
|
|
232
|
-
* component gets unmounted unless `detached` is set to true.
|
|
233
|
-
*
|
|
234
|
-
* @param callback - callback passed to the watcher
|
|
235
|
-
* @param options - `watch` options + `detached` to detach the subscription from the context (usually a component)
|
|
236
|
-
* this is called from. Note that the `flush` option does not affect calls to `store.$patch()`.
|
|
237
|
-
* @returns function that removes the watcher
|
|
238
|
-
*/
|
|
239
|
-
$subscribe(callback: SubscriptionCallback<S>, options?: {
|
|
240
|
-
detached?: boolean;
|
|
241
|
-
} & WatchOptions): () => void;
|
|
242
|
-
/**
|
|
243
|
-
* Setups a callback to be called every time an action is about to get
|
|
244
|
-
* invoked. The callback receives an object with all the relevant information
|
|
245
|
-
* of the invoked action:
|
|
246
|
-
* - `store`: the store it is invoked on
|
|
247
|
-
* - `name`: The name of the action
|
|
248
|
-
* - `args`: The parameters passed to the action
|
|
249
|
-
*
|
|
250
|
-
* On top of these, it receives two functions that allow setting up a callback
|
|
251
|
-
* once the action finishes or when it fails.
|
|
252
|
-
*
|
|
253
|
-
* It also returns a function to remove the callback. Note than when calling
|
|
254
|
-
* `store.$onAction()` inside of a component, it will be automatically cleaned
|
|
255
|
-
* up when the component gets unmounted unless `detached` is set to true.
|
|
256
|
-
*
|
|
257
|
-
* @example
|
|
258
|
-
*
|
|
259
|
-
*```js
|
|
260
|
-
*store.$onAction(({ after, onError }) => {
|
|
261
|
-
* // Here you could share variables between all of the hooks as well as
|
|
262
|
-
* // setting up watchers and clean them up
|
|
263
|
-
* after((resolvedValue) => {
|
|
264
|
-
* // can be used to cleanup side effects
|
|
265
|
-
* . // `resolvedValue` is the value returned by the action, if it's a
|
|
266
|
-
* . // Promise, it will be the resolved value instead of the Promise
|
|
267
|
-
* })
|
|
268
|
-
* onError((error) => {
|
|
269
|
-
* // can be used to pass up errors
|
|
270
|
-
* })
|
|
271
|
-
*})
|
|
272
|
-
*```
|
|
273
|
-
*
|
|
274
|
-
* @param callback - callback called before every action
|
|
275
|
-
* @param detached - detach the subscription from the context this is called from
|
|
276
|
-
* @returns function that removes the watcher
|
|
277
|
-
*/
|
|
278
|
-
$onAction(callback: StoreOnActionListener<Id, S, G, A>, detached?: boolean): () => void;
|
|
279
|
-
/**
|
|
280
|
-
* Stops the associated effect scope of the store and remove it from the store
|
|
281
|
-
* registry. Plugins can override this method to cleanup any added effects.
|
|
282
|
-
* e.g. devtools plugin stops displaying disposed stores from devtools.
|
|
283
|
-
* Note this doesn't delete the state of the store, you have to do it manually with
|
|
284
|
-
* `delete pinia.state.value[store.$id]` if you want to. If you don't and the
|
|
285
|
-
* store is used again, it will reuse the previous state.
|
|
286
|
-
*/
|
|
287
|
-
$dispose(): void;
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Generic type for a function that can infer arguments and return type
|
|
291
|
-
*
|
|
292
|
-
* For internal use **only**
|
|
293
|
-
*/
|
|
294
|
-
type _Method = (...args: any[]) => any;
|
|
295
|
-
/**
|
|
296
|
-
* Store augmented for actions. For internal usage only.
|
|
297
|
-
* For internal use **only**
|
|
298
|
-
*/
|
|
299
|
-
type _StoreWithActions<A> = {
|
|
300
|
-
[k in keyof A]: A[k] extends (...args: infer P) => infer R ? (...args: P) => R : never;
|
|
301
|
-
};
|
|
302
|
-
/**
|
|
303
|
-
* Store augmented with getters. For internal usage only.
|
|
304
|
-
* For internal use **only**
|
|
305
|
-
*/
|
|
306
|
-
type _StoreWithGetters<G> = _StoreWithGetters_Readonly<G> & _StoreWithGetters_Writable<G>;
|
|
307
|
-
/**
|
|
308
|
-
* Store augmented with readonly getters. For internal usage **only**.
|
|
309
|
-
*/
|
|
310
|
-
type _StoreWithGetters_Readonly<G> = {
|
|
311
|
-
readonly [K in keyof G as G[K] extends (...args: any[]) => any ? K : ComputedRef extends G[K] ? K : never]: G[K] extends (...args: any[]) => infer R ? R : UnwrapRef<G[K]>;
|
|
312
|
-
};
|
|
313
|
-
/**
|
|
314
|
-
* Store augmented with writable getters. For internal usage **only**.
|
|
315
|
-
*/
|
|
316
|
-
type _StoreWithGetters_Writable<G> = {
|
|
317
|
-
[K in keyof G as G[K] extends WritableComputedRef<any> ? K : never]: G[K] extends Readonly<WritableComputedRef<infer R>> ? R : never;
|
|
318
|
-
};
|
|
319
|
-
/**
|
|
320
|
-
* Store type to build a store.
|
|
321
|
-
*/
|
|
322
|
-
type Store<Id extends string = string, S extends StateTree = {}, G = {}, A = {}> = _StoreWithState<Id, S, G, A> & UnwrapRef<S> & _StoreWithGetters<G> & (_ActionsTree extends A ? {} : A) & PiniaCustomProperties<Id, S, G, A> & PiniaCustomStateProperties<S>;
|
|
323
|
-
/**
|
|
324
|
-
* Generic and type-unsafe version of Store. Doesn't fail on access with
|
|
325
|
-
* strings, making it much easier to write generic functions that do not care
|
|
326
|
-
* about the kind of store that is passed.
|
|
327
|
-
*/
|
|
328
|
-
type StoreGeneric = Store<string, StateTree, _GettersTree<StateTree>, _ActionsTree>;
|
|
329
|
-
/**
|
|
330
|
-
* Return type of `defineStore()`. Function that allows instantiating a store.
|
|
331
|
-
*/
|
|
332
|
-
interface StoreDefinition<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
|
|
333
|
-
/**
|
|
334
|
-
* Returns a store, creates it if necessary.
|
|
335
|
-
*
|
|
336
|
-
* @param pinia - Pinia instance to retrieve the store
|
|
337
|
-
* @param hot - dev only hot module replacement
|
|
338
|
-
*/
|
|
339
|
-
(pinia?: Pinia | null | undefined, hot?: StoreGeneric): Store<Id, S, G, A>;
|
|
340
|
-
/**
|
|
341
|
-
* Id of the store. Used by map helpers.
|
|
342
|
-
*/
|
|
343
|
-
$id: Id;
|
|
344
|
-
/**
|
|
345
|
-
* Return to store for use within non-functional components
|
|
346
|
-
*/
|
|
347
|
-
$getStore: () => Store<Id, S, G, A>;
|
|
348
|
-
/**
|
|
349
|
-
* Dev only pinia for HMR.
|
|
350
|
-
*
|
|
351
|
-
* @internal
|
|
352
|
-
*/
|
|
353
|
-
_pinia?: Pinia;
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Interface to be extended by the user when they add properties through plugins.
|
|
357
|
-
*/
|
|
358
|
-
interface PiniaCustomProperties<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Properties that are added to every `store.$state` by `pinia.use()`.
|
|
362
|
-
*/
|
|
363
|
-
interface PiniaCustomStateProperties<S extends StateTree = StateTree> {
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Type of an object of Getters that infers the argument. For internal usage only.
|
|
367
|
-
* For internal use **only**
|
|
368
|
-
*/
|
|
369
|
-
type _GettersTree<S extends StateTree> = Record<string, ((state: UnwrapRef<S> & UnwrapRef<PiniaCustomStateProperties<S>>) => any) | (() => any)>;
|
|
370
|
-
/**
|
|
371
|
-
* Type of an object of Actions. For internal usage only.
|
|
372
|
-
* For internal use **only**
|
|
373
|
-
*/
|
|
374
|
-
type _ActionsTree = Record<string, _Method>;
|
|
375
|
-
/**
|
|
376
|
-
* Type that enables refactoring through IDE.
|
|
377
|
-
* For internal use **only**
|
|
378
|
-
*/
|
|
379
|
-
type _ExtractStateFromSetupStore_Keys<SS> = keyof {
|
|
380
|
-
[K in keyof SS as SS[K] extends _Method | ComputedRef ? never : K]: any;
|
|
381
|
-
};
|
|
382
|
-
/**
|
|
383
|
-
* Type that enables refactoring through IDE.
|
|
384
|
-
* For internal use **only**
|
|
385
|
-
*/
|
|
386
|
-
type _ExtractActionsFromSetupStore_Keys<SS> = keyof {
|
|
387
|
-
[K in keyof SS as SS[K] extends _Method ? K : never]: any;
|
|
7
|
+
type TransformActions<A> = A;
|
|
8
|
+
type SubscriptionCallback<S> = (state: S, prevState: S) => void;
|
|
9
|
+
interface PiniaCustomProperties<Id extends string = string, S extends StateTree = StateTree, G extends Record<string, any> = Record<string, any>, A extends Record<string, any> = Record<string, any>> {
|
|
10
|
+
}
|
|
11
|
+
interface StorePublicApi<S> {
|
|
12
|
+
$patch: (updater: (draft: Draft<S>) => void) => void;
|
|
13
|
+
$reset: () => void;
|
|
14
|
+
$subscribe: (callback: SubscriptionCallback<S>) => () => void;
|
|
15
|
+
$state: S;
|
|
16
|
+
}
|
|
17
|
+
type GetterContext<S, G> = Readonly<S> & TransformGetters<G>;
|
|
18
|
+
type ActionContext<S, G, A> = S & TransformGetters<G> & TransformActions<A> & StorePublicApi<S>;
|
|
19
|
+
type Store<Id extends string, S extends StateTree, G extends Record<string, any>, A extends Record<string, any>> = S & TransformGetters<G> & TransformActions<A> & StorePublicApi<S> & PiniaCustomProperties<Id, S, G, A>;
|
|
20
|
+
type StoreGeneric = Store<string, StateTree, Record<string, any>, Record<string, any>>;
|
|
21
|
+
type GettersImplementation<S> = {
|
|
22
|
+
[K in string]: (state: S) => any;
|
|
388
23
|
};
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
24
|
+
interface DefineStoreOptions<S extends StateTree, G extends Record<string, any>, A extends Record<string, any>> {
|
|
25
|
+
state: () => S;
|
|
26
|
+
getters?: G & ThisType<GetterContext<S, G>> & GettersImplementation<S>;
|
|
27
|
+
actions?: A & ThisType<ActionContext<S, G, A>>;
|
|
28
|
+
}
|
|
29
|
+
type StoreScope = {
|
|
30
|
+
currentState: StateTree;
|
|
31
|
+
listeners: Set<(state: any, prev: any, patches: Patch[]) => void>;
|
|
32
|
+
getterCache: Map<string, any>;
|
|
33
|
+
getterDependencies: Map<string, Set<string>>;
|
|
34
|
+
subscribers: Map<string, Set<string>>;
|
|
35
|
+
createStoreProxy: (onAccess?: (path: string[]) => void) => StoreGeneric;
|
|
395
36
|
};
|
|
396
|
-
/**
|
|
397
|
-
* Type that enables refactoring through IDE.
|
|
398
|
-
* For internal use **only**
|
|
399
|
-
*/
|
|
400
|
-
type _UnwrapAll<SS> = {
|
|
401
|
-
[K in keyof SS]: UnwrapRef<SS[K]>;
|
|
402
|
-
};
|
|
403
|
-
/**
|
|
404
|
-
* For internal use **only**
|
|
405
|
-
*/
|
|
406
|
-
type _ExtractStateFromSetupStore<SS> = SS extends undefined | void ? {} : Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>;
|
|
407
|
-
/**
|
|
408
|
-
* For internal use **only**
|
|
409
|
-
*/
|
|
410
|
-
type _ExtractActionsFromSetupStore<SS> = SS extends undefined | void ? {} : Pick<SS, _ExtractActionsFromSetupStore_Keys<SS>>;
|
|
411
|
-
/**
|
|
412
|
-
* For internal use **only**
|
|
413
|
-
*/
|
|
414
|
-
type _ExtractGettersFromSetupStore<SS> = SS extends undefined | void ? {} : Pick<SS, _ExtractGettersFromSetupStore_Keys<SS>>;
|
|
415
|
-
/**
|
|
416
|
-
* Options passed to `defineStore()` that are common between option and setup
|
|
417
|
-
* stores. Extend this interface if you want to add custom options to both kinds
|
|
418
|
-
* of stores.
|
|
419
|
-
*/
|
|
420
|
-
declare interface DefineStoreOptionsBase<S extends StateTree, Store> {
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Options parameter of `defineStore()` for option stores. Can be extended to
|
|
424
|
-
* augment stores with the plugin API. @see {@link DefineStoreOptionsBase}.
|
|
425
|
-
*/
|
|
426
|
-
interface DefineStoreOptions<Id extends string, S extends StateTree, G extends _GettersTree<S>, A> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
|
|
427
|
-
/**
|
|
428
|
-
* Unique string key to identify the store across the application.
|
|
429
|
-
*/
|
|
430
|
-
id: Id;
|
|
431
|
-
/**
|
|
432
|
-
* Function to create a fresh state. **Must be an arrow function** to ensure
|
|
433
|
-
* correct typings!
|
|
434
|
-
*/
|
|
435
|
-
state?: () => S;
|
|
436
|
-
/**
|
|
437
|
-
* Optional object of getters.
|
|
438
|
-
*/
|
|
439
|
-
getters?: G & ThisType<UnwrapRef<S> & _StoreWithGetters<G> & PiniaCustomProperties> & _GettersTree<S>;
|
|
440
|
-
/**
|
|
441
|
-
* Optional object of actions.
|
|
442
|
-
*/
|
|
443
|
-
actions?: A & ThisType<A & UnwrapRef<S> & _StoreWithState<Id, S, G, A> & _StoreWithGetters<G> & PiniaCustomProperties>;
|
|
444
|
-
/**
|
|
445
|
-
* Allows hydrating the store during SSR when complex state (like client side only refs) are used in the store
|
|
446
|
-
* definition and copying the value from `pinia.state` isn't enough.
|
|
447
|
-
*
|
|
448
|
-
* @example
|
|
449
|
-
* If in your `state`, you use any `customRef`s, any `computed`s, or any `ref`s that have a different value on
|
|
450
|
-
* Server and Client, you need to manually hydrate them. e.g., a custom ref that is stored in the local
|
|
451
|
-
* storage:
|
|
452
|
-
*
|
|
453
|
-
* ```ts
|
|
454
|
-
* const useStore = defineStore('main', {
|
|
455
|
-
* state: () => ({
|
|
456
|
-
* n: useLocalStorage('key', 0)
|
|
457
|
-
* }),
|
|
458
|
-
* hydrate(storeState, initialState) {
|
|
459
|
-
* // @ts-expect-error: https://github.com/microsoft/TypeScript/issues/43826
|
|
460
|
-
* storeState.n = useLocalStorage('key', 0)
|
|
461
|
-
* }
|
|
462
|
-
* })
|
|
463
|
-
* ```
|
|
464
|
-
*
|
|
465
|
-
* @param storeState - the current state in the store
|
|
466
|
-
* @param initialState - initialState
|
|
467
|
-
*/
|
|
468
|
-
hydrate?(storeState: UnwrapRef<S>, initialState: UnwrapRef<S>): void;
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Options parameter of `defineStore()` for setup stores. Can be extended to
|
|
472
|
-
* augment stores with the plugin API. @see {@link DefineStoreOptionsBase}.
|
|
473
|
-
*/
|
|
474
|
-
interface DefineSetupStoreOptions<Id extends string, S extends StateTree, G, A> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
|
|
475
|
-
/**
|
|
476
|
-
* Extracted actions. Added by useStore(). SHOULD NOT be added by the user when
|
|
477
|
-
* creating the store. Can be used in plugins to get the list of actions in a
|
|
478
|
-
* store defined with a setup function. Note this is always defined
|
|
479
|
-
*/
|
|
480
|
-
actions?: A;
|
|
481
|
-
}
|
|
482
|
-
/**
|
|
483
|
-
* Available `options` when creating a pinia plugin.
|
|
484
|
-
*/
|
|
485
|
-
interface DefineStoreOptionsInPlugin<Id extends string, S extends StateTree, G extends _GettersTree<S>, A> extends Omit<DefineStoreOptions<Id, S, G, A>, 'id' | 'actions'> {
|
|
486
|
-
/**
|
|
487
|
-
* Extracted object of actions. Added by useStore() when the store is built
|
|
488
|
-
* using the setup API, otherwise uses the one passed to `defineStore()`.
|
|
489
|
-
* Defaults to an empty object if no actions are defined.
|
|
490
|
-
*/
|
|
491
|
-
actions: A;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
/**
|
|
495
|
-
* Get the currently active pinia if there is any.
|
|
496
|
-
*/
|
|
497
|
-
declare const getActivePinia: () => Pinia | undefined;
|
|
498
|
-
/**
|
|
499
|
-
* Every application must own its own pinia to be able to create stores
|
|
500
|
-
*/
|
|
501
37
|
interface Pinia {
|
|
502
|
-
|
|
503
|
-
* root state
|
|
504
|
-
*/
|
|
505
|
-
state: Ref<Record<string, StateTree>>;
|
|
506
|
-
/**
|
|
507
|
-
* Adds a store plugin to extend every store
|
|
508
|
-
*
|
|
509
|
-
* @param plugin - store plugin to add
|
|
510
|
-
*/
|
|
38
|
+
state: Record<string, StateTree>;
|
|
511
39
|
use(plugin: PiniaPlugin): Pinia;
|
|
512
|
-
/**
|
|
513
|
-
* Installed store plugins
|
|
514
|
-
*
|
|
515
|
-
* @internal
|
|
516
|
-
*/
|
|
517
40
|
_p: PiniaPlugin[];
|
|
518
|
-
/**
|
|
519
|
-
* Effect scope the pinia is attached to
|
|
520
|
-
*
|
|
521
|
-
* @internal
|
|
522
|
-
*/
|
|
523
|
-
_e: EffectScope;
|
|
524
|
-
/**
|
|
525
|
-
* Registry of stores used by this pinia.
|
|
526
|
-
*
|
|
527
|
-
* @internal
|
|
528
|
-
*/
|
|
529
41
|
_s: Map<string, StoreGeneric>;
|
|
530
|
-
|
|
531
|
-
* Added by `createTestingPinia()` to bypass `useStore(pinia)`.
|
|
532
|
-
*
|
|
533
|
-
* @internal
|
|
534
|
-
*/
|
|
535
|
-
_testing?: boolean;
|
|
42
|
+
_scopes: Map<string, StoreScope>;
|
|
536
43
|
}
|
|
537
|
-
declare function setActivePinia(_pinia: Pinia): void;
|
|
538
|
-
type PiniaPluginContext<Id extends string = string, S extends StateTree = StateTree, G extends _GettersTree<S> = _GettersTree<S>, A = _ActionsTree> = {
|
|
539
|
-
/**
|
|
540
|
-
* pinia instance.
|
|
541
|
-
*/
|
|
542
|
-
pinia: Pinia;
|
|
543
|
-
/**
|
|
544
|
-
* Current store being extended.
|
|
545
|
-
*/
|
|
546
|
-
store: Store<Id, S, G, A>;
|
|
547
|
-
/**
|
|
548
|
-
* Initial options defining the store when calling `defineStore()`.
|
|
549
|
-
*/
|
|
550
|
-
options: DefineStoreOptionsInPlugin<Id, S, G, A>;
|
|
551
|
-
};
|
|
552
|
-
/**
|
|
553
|
-
* Plugin to extend every store.
|
|
554
|
-
*/
|
|
555
44
|
interface PiniaPlugin {
|
|
556
|
-
|
|
557
|
-
* Plugin to extend every store. Returns an object to extend the store or
|
|
558
|
-
* nothing.
|
|
559
|
-
*
|
|
560
|
-
* @param context - Context
|
|
561
|
-
*/
|
|
562
|
-
(context: PiniaPluginContext): Partial<PiniaCustomProperties & PiniaCustomStateProperties> | void;
|
|
45
|
+
(context: PiniaPluginContext): Partial<PiniaCustomProperties> | void;
|
|
563
46
|
}
|
|
47
|
+
type PiniaPluginContext<Id extends string = string, S extends StateTree = StateTree, G extends Record<string, any> = Record<string, any>, A extends Record<string, any> = Record<string, any>> = {
|
|
48
|
+
id: Id;
|
|
49
|
+
store: Store<Id, S, G, A>;
|
|
50
|
+
options: DefineStoreOptions<S, G, A>;
|
|
51
|
+
};
|
|
52
|
+
interface StoreDefinition<Id extends string, S extends StateTree, G extends Record<string, any>, A extends Record<string, any>> {
|
|
53
|
+
useStore: () => Store<Id, S, G, A>;
|
|
54
|
+
getStore: () => Store<Id, S, G, A>;
|
|
55
|
+
}
|
|
56
|
+
type NamedStoreDefinition<Id extends string, S extends StateTree, G extends Record<string, any>, A extends Record<string, any>> = string extends Id ? {} : {
|
|
57
|
+
[K in `use${Capitalize<Id>}Store`]: () => Store<Id, S, G, A>;
|
|
58
|
+
} & {
|
|
59
|
+
[K in `get${Capitalize<Id>}Store`]: () => Store<Id, S, G, A>;
|
|
60
|
+
};
|
|
61
|
+
type StoreDefinitionWithNames<Id extends string, S extends StateTree, G extends Record<string, any>, A extends Record<string, any>> = StoreDefinition<Id, S, G, A> & NamedStoreDefinition<Id, S, G, A>;
|
|
564
62
|
|
|
565
|
-
/**
|
|
566
|
-
* Creates a Pinia instance to be used by the application
|
|
567
|
-
*/
|
|
568
63
|
declare function createPinia(): Pinia;
|
|
569
64
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
* @param options - options to define the store
|
|
575
|
-
*/
|
|
576
|
-
declare function defineStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(id: Id, options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>): StoreDefinition<Id, S, G, A>;
|
|
65
|
+
declare function setActivePinia(pinia: Pinia): void;
|
|
66
|
+
declare function getActivePinia(): Pinia;
|
|
67
|
+
|
|
68
|
+
declare function defineStore<Id extends string, S extends StateTree, G extends Record<string, any> = {}, A extends Record<string, any> = {}>(id: Id, options: DefineStoreOptions<S, G, A>): StoreDefinitionWithNames<Id, S, G, A>;
|
|
577
69
|
|
|
578
|
-
export { type
|
|
70
|
+
export { type DefineStoreOptions, type Pinia, type PiniaCustomProperties, type PiniaPlugin, type PiniaPluginContext, type StateTree, type Store, type StoreDefinition, type StoreGeneric, type SubscriptionCallback, createPinia, defineStore, getActivePinia, setActivePinia };
|