You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

995 lines
34 KiB

2 months ago
  1. import { App } from 'vue-demi';
  2. import { ComputedRef } from 'vue-demi';
  3. import type { DebuggerEvent } from 'vue-demi';
  4. import { EffectScope } from 'vue-demi';
  5. import type { Plugin as Plugin_2 } from 'vue-demi';
  6. import { Ref } from 'vue-demi';
  7. import { ToRef } from 'vue-demi';
  8. import { ToRefs } from 'vue-demi';
  9. import { UnwrapRef } from 'vue-demi';
  10. import type { WatchOptions } from 'vue-demi';
  11. /**
  12. * Creates an _accept_ function to pass to `import.meta.hot` in Vite applications.
  13. *
  14. * @example
  15. * ```js
  16. * const useUser = defineStore(...)
  17. * if (import.meta.hot) {
  18. * import.meta.hot.accept(acceptHMRUpdate(useUser, import.meta.hot))
  19. * }
  20. * ```
  21. *
  22. * @param initialUseStore - return of the defineStore to hot update
  23. * @param hot - `import.meta.hot`
  24. */
  25. export declare function acceptHMRUpdate<Id extends string = string, S extends StateTree = StateTree, G extends _GettersTree<S> = _GettersTree<S>, A = _ActionsTree>(initialUseStore: StoreDefinition<Id, S, G, A>, hot: any): (newModule: any) => any;
  26. /**
  27. * Type of an object of Actions. For internal usage only.
  28. * For internal use **only**
  29. */
  30. export declare type _ActionsTree = Record<string, _Method>;
  31. export declare type _Awaited<T> = T extends null | undefined ? T : T extends object & {
  32. then(onfulfilled: infer F): any;
  33. } ? F extends (value: infer V, ...args: any) => any ? _Awaited<V> : never : T;
  34. /**
  35. * Creates a Pinia instance to be used by the application
  36. */
  37. export declare function createPinia(): Pinia;
  38. /**
  39. * Recursive `Partial<T>`. Used by {@link Store['$patch']}.
  40. *
  41. * For internal use **only**
  42. */
  43. export declare type _DeepPartial<T> = {
  44. [K in keyof T]?: _DeepPartial<T[K]>;
  45. };
  46. /**
  47. * Options parameter of `defineStore()` for setup stores. Can be extended to
  48. * augment stores with the plugin API. @see {@link DefineStoreOptionsBase}.
  49. */
  50. export declare interface DefineSetupStoreOptions<Id extends string, S extends StateTree, G, A> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
  51. /**
  52. * Extracted actions. Added by useStore(). SHOULD NOT be added by the user when
  53. * creating the store. Can be used in plugins to get the list of actions in a
  54. * store defined with a setup function. Note this is always defined
  55. */
  56. actions?: A;
  57. }
  58. /**
  59. * Creates a `useStore` function that retrieves the store instance
  60. *
  61. * @param id - id of the store (must be unique)
  62. * @param options - options to define the store
  63. */
  64. export 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. /**
  66. * Creates a `useStore` function that retrieves the store instance
  67. *
  68. * @param options - options to define the store
  69. */
  70. export declare function defineStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A>;
  71. /**
  72. * Creates a `useStore` function that retrieves the store instance
  73. *
  74. * @param id - id of the store (must be unique)
  75. * @param storeSetup - function that defines the store
  76. * @param options - extra options
  77. */
  78. export declare function defineStore<Id extends string, SS>(id: Id, storeSetup: () => SS, options?: DefineSetupStoreOptions<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>): StoreDefinition<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>;
  79. /**
  80. * Options parameter of `defineStore()` for option stores. Can be extended to
  81. * augment stores with the plugin API. @see {@link DefineStoreOptionsBase}.
  82. */
  83. export declare interface DefineStoreOptions<Id extends string, S extends StateTree, G, A> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
  84. /**
  85. * Unique string key to identify the store across the application.
  86. */
  87. id: Id;
  88. /**
  89. * Function to create a fresh state. **Must be an arrow function** to ensure
  90. * correct typings!
  91. */
  92. state?: () => S;
  93. /**
  94. * Optional object of getters.
  95. */
  96. getters?: G & ThisType<UnwrapRef<S> & _StoreWithGetters<G> & PiniaCustomProperties> & _GettersTree<S>;
  97. /**
  98. * Optional object of actions.
  99. */
  100. actions?: A & ThisType<A & UnwrapRef<S> & _StoreWithState<Id, S, G, A> & _StoreWithGetters<G> & PiniaCustomProperties>;
  101. /**
  102. * Allows hydrating the store during SSR when complex state (like client side only refs) are used in the store
  103. * definition and copying the value from `pinia.state` isn't enough.
  104. *
  105. * @example
  106. * If in your `state`, you use any `customRef`s, any `computed`s, or any `ref`s that have a different value on
  107. * Server and Client, you need to manually hydrate them. e.g., a custom ref that is stored in the local
  108. * storage:
  109. *
  110. * ```ts
  111. * const useStore = defineStore('main', {
  112. * state: () => ({
  113. * n: useLocalStorage('key', 0)
  114. * }),
  115. * hydrate(storeState, initialState) {
  116. * // @ts-expect-error: https://github.com/microsoft/TypeScript/issues/43826
  117. * storeState.n = useLocalStorage('key', 0)
  118. * }
  119. * })
  120. * ```
  121. *
  122. * @param storeState - the current state in the store
  123. * @param initialState - initialState
  124. */
  125. hydrate?(storeState: UnwrapRef<S>, initialState: UnwrapRef<S>): void;
  126. }
  127. /**
  128. * Options passed to `defineStore()` that are common between option and setup
  129. * stores. Extend this interface if you want to add custom options to both kinds
  130. * of stores.
  131. */
  132. export declare interface DefineStoreOptionsBase<S extends StateTree, Store> {
  133. }
  134. /**
  135. * Available `options` when creating a pinia plugin.
  136. */
  137. export declare interface DefineStoreOptionsInPlugin<Id extends string, S extends StateTree, G, A> extends Omit<DefineStoreOptions<Id, S, G, A>, 'id' | 'actions'> {
  138. /**
  139. * Extracted object of actions. Added by useStore() when the store is built
  140. * using the setup API, otherwise uses the one passed to `defineStore()`.
  141. * Defaults to an empty object if no actions are defined.
  142. */
  143. actions: A;
  144. }
  145. /**
  146. * For internal use **only**
  147. */
  148. export declare type _ExtractActionsFromSetupStore<SS> = SS extends undefined | void ? {} : _ExtractActionsFromSetupStore_Keys<SS> extends keyof SS ? Pick<SS, _ExtractActionsFromSetupStore_Keys<SS>> : never;
  149. /**
  150. * Type that enables refactoring through IDE.
  151. * For internal use **only**
  152. */
  153. export declare type _ExtractActionsFromSetupStore_Keys<SS> = keyof {
  154. [K in keyof SS as SS[K] extends _Method ? K : never]: any;
  155. };
  156. /**
  157. * For internal use **only**
  158. */
  159. export declare type _ExtractGettersFromSetupStore<SS> = SS extends undefined | void ? {} : _ExtractGettersFromSetupStore_Keys<SS> extends keyof SS ? Pick<SS, _ExtractGettersFromSetupStore_Keys<SS>> : never;
  160. /**
  161. * Type that enables refactoring through IDE.
  162. * For internal use **only**
  163. */
  164. export declare type _ExtractGettersFromSetupStore_Keys<SS> = keyof {
  165. [K in keyof SS as SS[K] extends ComputedRef ? K : never]: any;
  166. };
  167. /**
  168. * For internal use **only**
  169. */
  170. export declare type _ExtractStateFromSetupStore<SS> = SS extends undefined | void ? {} : _ExtractStateFromSetupStore_Keys<SS> extends keyof SS ? _UnwrapAll<Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>> : never;
  171. /**
  172. * Type that enables refactoring through IDE.
  173. * For internal use **only**
  174. */
  175. export declare type _ExtractStateFromSetupStore_Keys<SS> = keyof {
  176. [K in keyof SS as SS[K] extends _Method | ComputedRef ? never : K]: any;
  177. };
  178. /**
  179. * Get the currently active pinia if there is any.
  180. */
  181. export declare const getActivePinia: () => Pinia | undefined;
  182. /**
  183. * Type of an object of Getters that infers the argument. For internal usage only.
  184. * For internal use **only**
  185. */
  186. export declare type _GettersTree<S extends StateTree> = Record<string, ((state: UnwrapRef<S> & UnwrapRef<PiniaCustomStateProperties<S>>) => any) | (() => any)>;
  187. /**
  188. * Allows directly using actions from your store without using the composition
  189. * API (`setup()`) by generating an object to be spread in the `methods` field
  190. * of a component. The values of the object are the actions while the keys are
  191. * the names of the resulting methods.
  192. *
  193. * @example
  194. * ```js
  195. * export default {
  196. * methods: {
  197. * // other methods properties
  198. * // useCounterStore has two actions named `increment` and `setCount`
  199. * ...mapActions(useCounterStore, { moar: 'increment', setIt: 'setCount' })
  200. * },
  201. *
  202. * created() {
  203. * this.moar()
  204. * this.setIt(2)
  205. * }
  206. * }
  207. * ```
  208. *
  209. * @param useStore - store to map from
  210. * @param keyMapper - object to define new names for the actions
  211. */
  212. export declare function mapActions<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, KeyMapper extends Record<string, keyof A>>(useStore: StoreDefinition<Id, S, G, A>, keyMapper: KeyMapper): _MapActionsObjectReturn<A, KeyMapper>;
  213. /**
  214. * Allows directly using actions from your store without using the composition
  215. * API (`setup()`) by generating an object to be spread in the `methods` field
  216. * of a component.
  217. *
  218. * @example
  219. * ```js
  220. * export default {
  221. * methods: {
  222. * // other methods properties
  223. * ...mapActions(useCounterStore, ['increment', 'setCount'])
  224. * },
  225. *
  226. * created() {
  227. * this.increment()
  228. * this.setCount(2) // pass arguments as usual
  229. * }
  230. * }
  231. * ```
  232. *
  233. * @param useStore - store to map from
  234. * @param keys - array of action names to map
  235. */
  236. export declare function mapActions<Id extends string, S extends StateTree, G extends _GettersTree<S>, A>(useStore: StoreDefinition<Id, S, G, A>, keys: Array<keyof A>): _MapActionsReturn<A>;
  237. /**
  238. * For internal use **only**
  239. */
  240. export declare type _MapActionsObjectReturn<A, T extends Record<string, keyof A>> = {
  241. [key in keyof T]: A[T[key]];
  242. };
  243. /**
  244. * For internal use **only**
  245. */
  246. export declare type _MapActionsReturn<A> = {
  247. [key in keyof A]: A[key];
  248. };
  249. /**
  250. * Alias for `mapState()`. You should use `mapState()` instead.
  251. * @deprecated use `mapState()` instead.
  252. */
  253. export declare const mapGetters: typeof mapState;
  254. /**
  255. * Allows using state and getters from one store without using the composition
  256. * API (`setup()`) by generating an object to be spread in the `computed` field
  257. * of a component. The values of the object are the state properties/getters
  258. * while the keys are the names of the resulting computed properties.
  259. * Optionally, you can also pass a custom function that will receive the store
  260. * as its first argument. Note that while it has access to the component
  261. * instance via `this`, it won't be typed.
  262. *
  263. * @example
  264. * ```js
  265. * export default {
  266. * computed: {
  267. * // other computed properties
  268. * // useCounterStore has a state property named `count` and a getter `double`
  269. * ...mapState(useCounterStore, {
  270. * n: 'count',
  271. * triple: store => store.n * 3,
  272. * // note we can't use an arrow function if we want to use `this`
  273. * custom(store) {
  274. * return this.someComponentValue + store.n
  275. * },
  276. * doubleN: 'double'
  277. * })
  278. * },
  279. *
  280. * created() {
  281. * this.n // 2
  282. * this.doubleN // 4
  283. * }
  284. * }
  285. * ```
  286. *
  287. * @param useStore - store to map from
  288. * @param keyMapper - object of state properties or getters
  289. */
  290. export declare function mapState<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, KeyMapper extends Record<string, keyof S | keyof G | ((store: Store<Id, S, G, A>) => any)>>(useStore: StoreDefinition<Id, S, G, A>, keyMapper: KeyMapper): _MapStateObjectReturn<Id, S, G, A, KeyMapper>;
  291. /**
  292. * Allows using state and getters from one store without using the composition
  293. * API (`setup()`) by generating an object to be spread in the `computed` field
  294. * of a component.
  295. *
  296. * @example
  297. * ```js
  298. * export default {
  299. * computed: {
  300. * // other computed properties
  301. * ...mapState(useCounterStore, ['count', 'double'])
  302. * },
  303. *
  304. * created() {
  305. * this.count // 2
  306. * this.double // 4
  307. * }
  308. * }
  309. * ```
  310. *
  311. * @param useStore - store to map from
  312. * @param keys - array of state properties or getters
  313. */
  314. export declare function mapState<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, Keys extends keyof S | keyof G>(useStore: StoreDefinition<Id, S, G, A>, keys: readonly Keys[]): _MapStateReturn<S, G, Keys>;
  315. /**
  316. * For internal use **only**
  317. */
  318. export declare type _MapStateObjectReturn<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, T extends Record<string, keyof S | keyof G | ((store: Store<Id, S, G, A>) => any)> = {}> = {
  319. [key in keyof T]: () => T[key] extends (store: any) => infer R ? R : T[key] extends keyof Store<Id, S, G, A> ? Store<Id, S, G, A>[T[key]] : never;
  320. };
  321. /**
  322. * For internal use **only**
  323. */
  324. export declare type _MapStateReturn<S extends StateTree, G extends _GettersTree<S>, Keys extends keyof S | keyof G = keyof S | keyof G> = {
  325. [key in Keys]: () => Store<string, S, G, {}>[key];
  326. };
  327. /**
  328. * Allows using stores without the composition API (`setup()`) by generating an
  329. * object to be spread in the `computed` field of a component. It accepts a list
  330. * of store definitions.
  331. *
  332. * @example
  333. * ```js
  334. * export default {
  335. * computed: {
  336. * // other computed properties
  337. * ...mapStores(useUserStore, useCartStore)
  338. * },
  339. *
  340. * created() {
  341. * this.userStore // store with id "user"
  342. * this.cartStore // store with id "cart"
  343. * }
  344. * }
  345. * ```
  346. *
  347. * @param stores - list of stores to map to an object
  348. */
  349. export declare function mapStores<Stores extends any[]>(...stores: [...Stores]): _Spread<Stores>;
  350. /**
  351. * Interface to allow customizing map helpers. Extend this interface with the
  352. * following properties:
  353. *
  354. * - `suffix`: string. Affects the suffix of `mapStores()`, defaults to `Store`.
  355. */
  356. export declare interface MapStoresCustomization {
  357. }
  358. /**
  359. * Same as `mapState()` but creates computed setters as well so the state can be
  360. * modified. Differently from `mapState()`, only `state` properties can be
  361. * added.
  362. *
  363. * @param useStore - store to map from
  364. * @param keyMapper - object of state properties
  365. */
  366. export declare function mapWritableState<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, KeyMapper extends Record<string, keyof S>>(useStore: StoreDefinition<Id, S, G, A>, keyMapper: KeyMapper): _MapWritableStateObjectReturn<S, KeyMapper>;
  367. /**
  368. * Allows using state and getters from one store without using the composition
  369. * API (`setup()`) by generating an object to be spread in the `computed` field
  370. * of a component.
  371. *
  372. * @param useStore - store to map from
  373. * @param keys - array of state properties
  374. */
  375. export declare function mapWritableState<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, Keys extends keyof S>(useStore: StoreDefinition<Id, S, G, A>, keys: readonly Keys[]): {
  376. [K in Keys]: {
  377. get: () => S[K];
  378. set: (value: S[K]) => any;
  379. };
  380. };
  381. /**
  382. * For internal use **only**
  383. */
  384. export declare type _MapWritableStateObjectReturn<S extends StateTree, T extends Record<string, keyof S>> = {
  385. [key in keyof T]: {
  386. get: () => S[T[key]];
  387. set: (value: S[T[key]]) => any;
  388. };
  389. };
  390. /**
  391. * For internal use **only**
  392. */
  393. export declare type _MapWritableStateReturn<S extends StateTree> = {
  394. [key in keyof S]: {
  395. get: () => S[key];
  396. set: (value: S[key]) => any;
  397. };
  398. };
  399. /**
  400. * Generic type for a function that can infer arguments and return type
  401. *
  402. * For internal use **only**
  403. */
  404. export declare type _Method = (...args: any[]) => any;
  405. /**
  406. * Possible types for SubscriptionCallback
  407. */
  408. export declare enum MutationType {
  409. /**
  410. * Direct mutation of the state:
  411. *
  412. * - `store.name = 'new name'`
  413. * - `store.$state.name = 'new name'`
  414. * - `store.list.push('new item')`
  415. */
  416. direct = "direct",
  417. /**
  418. * Mutated the state with `$patch` and an object
  419. *
  420. * - `store.$patch({ name: 'newName' })`
  421. */
  422. patchObject = "patch object",
  423. /**
  424. * Mutated the state with `$patch` and a function
  425. *
  426. * - `store.$patch(state => state.name = 'newName')`
  427. */
  428. patchFunction = "patch function"
  429. }
  430. /**
  431. * Every application must own its own pinia to be able to create stores
  432. */
  433. export declare interface Pinia {
  434. install: (app: App) => void;
  435. /**
  436. * root state
  437. */
  438. state: Ref<Record<string, StateTree>>;
  439. /**
  440. * Adds a store plugin to extend every store
  441. *
  442. * @param plugin - store plugin to add
  443. */
  444. use(plugin: PiniaPlugin): Pinia;
  445. /* Excluded from this release type: _p */
  446. /* Excluded from this release type: _a */
  447. /* Excluded from this release type: _e */
  448. /* Excluded from this release type: _s */
  449. /* Excluded from this release type: _testing */
  450. }
  451. /**
  452. * Interface to be extended by the user when they add properties through plugins.
  453. */
  454. export declare interface PiniaCustomProperties<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
  455. }
  456. /**
  457. * Properties that are added to every `store.$state` by `pinia.use()`.
  458. */
  459. export declare interface PiniaCustomStateProperties<S extends StateTree = StateTree> {
  460. }
  461. /**
  462. * Plugin to extend every store.
  463. */
  464. export declare interface PiniaPlugin {
  465. /**
  466. * Plugin to extend every store. Returns an object to extend the store or
  467. * nothing.
  468. *
  469. * @param context - Context
  470. */
  471. (context: PiniaPluginContext): Partial<PiniaCustomProperties & PiniaCustomStateProperties> | void;
  472. }
  473. /**
  474. * Context argument passed to Pinia plugins.
  475. */
  476. export declare interface PiniaPluginContext<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
  477. /**
  478. * pinia instance.
  479. */
  480. pinia: Pinia;
  481. /**
  482. * Current app created with `Vue.createApp()`.
  483. */
  484. app: App;
  485. /**
  486. * Current store being extended.
  487. */
  488. store: Store<Id, S, G, A>;
  489. /**
  490. * Initial options defining the store when calling `defineStore()`.
  491. */
  492. options: DefineStoreOptionsInPlugin<Id, S, G, A>;
  493. }
  494. /**
  495. * Plugin to extend every store.
  496. * @deprecated use PiniaPlugin instead
  497. */
  498. export declare type PiniaStorePlugin = PiniaPlugin;
  499. /**
  500. * Vue 2 Plugin that must be installed for pinia to work. Note **you don't need
  501. * this plugin if you are using Nuxt.js**. Use the `buildModule` instead:
  502. * https://pinia.vuejs.org/ssr/nuxt.html.
  503. *
  504. * @example
  505. * ```js
  506. * import Vue from 'vue'
  507. * import { PiniaVuePlugin, createPinia } from 'pinia'
  508. *
  509. * Vue.use(PiniaVuePlugin)
  510. * const pinia = createPinia()
  511. *
  512. * new Vue({
  513. * el: '#app',
  514. * // ...
  515. * pinia,
  516. * })
  517. * ```
  518. *
  519. * @param _Vue - `Vue` imported from 'vue'.
  520. */
  521. export declare const PiniaVuePlugin: Plugin_2;
  522. declare interface _SetActivePinia {
  523. (pinia: Pinia): Pinia;
  524. (pinia: undefined): undefined;
  525. (pinia: Pinia | undefined): Pinia | undefined;
  526. }
  527. /**
  528. * Sets or unsets the active pinia. Used in SSR and internally when calling
  529. * actions and getters
  530. *
  531. * @param pinia - Pinia instance
  532. */
  533. export declare const setActivePinia: _SetActivePinia;
  534. /**
  535. * Changes the suffix added by `mapStores()`. Can be set to an empty string.
  536. * Defaults to `"Store"`. Make sure to extend the MapStoresCustomization
  537. * interface if you are using TypeScript.
  538. *
  539. * @param suffix - new suffix
  540. */
  541. export declare function setMapStoreSuffix(suffix: MapStoresCustomization extends Record<'suffix', infer Suffix> ? Suffix : string): void;
  542. /**
  543. * Return type of `defineStore()` with a setup function.
  544. * - `Id` is a string literal of the store's name
  545. * - `SS` is the return type of the setup function
  546. * @see {@link StoreDefinition}
  547. */
  548. export declare interface SetupStoreDefinition<Id extends string, SS> extends StoreDefinition<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>> {
  549. }
  550. /**
  551. * Tells Pinia to skip the hydration process of a given object. This is useful in setup stores (only) when you return a
  552. * stateful object in the store but it isn't really state. e.g. returning a router instance in a setup store.
  553. *
  554. * @param obj - target object
  555. * @returns obj
  556. */
  557. export declare function skipHydrate<T = any>(obj: T): T;
  558. /**
  559. * For internal use **only**.
  560. */
  561. export declare type _Spread<A extends readonly any[]> = A extends [infer L, ...infer R] ? _StoreObject<L> & _Spread<R> : unknown;
  562. /**
  563. * Generic state of a Store
  564. */
  565. export declare type StateTree = Record<string | number | symbol, any>;
  566. /**
  567. * Store type to build a store.
  568. */
  569. export declare 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>;
  570. /**
  571. * Extract the actions of a store type. Works with both a Setup Store or an
  572. * Options Store.
  573. */
  574. export declare type StoreActions<SS> = SS extends Store<string, StateTree, _GettersTree<StateTree>, infer A> ? A : _ExtractActionsFromSetupStore<SS>;
  575. /**
  576. * Return type of `defineStore()`. Function that allows instantiating a store.
  577. */
  578. export declare interface StoreDefinition<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
  579. /**
  580. * Returns a store, creates it if necessary.
  581. *
  582. * @param pinia - Pinia instance to retrieve the store
  583. * @param hot - dev only hot module replacement
  584. */
  585. (pinia?: Pinia | null | undefined, hot?: StoreGeneric): Store<Id, S, G, A>;
  586. /**
  587. * Id of the store. Used by map helpers.
  588. */
  589. $id: Id;
  590. /* Excluded from this release type: _pinia */
  591. }
  592. /**
  593. * Generic and type-unsafe version of Store. Doesn't fail on access with
  594. * strings, making it much easier to write generic functions that do not care
  595. * about the kind of store that is passed.
  596. */
  597. export declare type StoreGeneric = Store<string, StateTree, _GettersTree<StateTree>, _ActionsTree>;
  598. /**
  599. * Extract the getters of a store type. Works with both a Setup Store or an
  600. * Options Store.
  601. */
  602. export declare type StoreGetters<SS> = SS extends Store<string, StateTree, infer G, _ActionsTree> ? _StoreWithGetters<G> : _ExtractGettersFromSetupStore<SS>;
  603. /**
  604. * For internal use **only**.
  605. */
  606. export declare type _StoreObject<S> = S extends StoreDefinition<infer Ids, infer State, infer Getters, infer Actions> ? {
  607. [Id in `${Ids}${MapStoresCustomization extends Record<'suffix', infer Suffix> ? Suffix : 'Store'}`]: () => Store<Id extends `${infer RealId}${MapStoresCustomization extends Record<'suffix', infer Suffix> ? Suffix : 'Store'}` ? RealId : string, State, Getters, Actions>;
  608. } : {};
  609. /**
  610. * Argument of `store.$onAction()`
  611. */
  612. export declare type StoreOnActionListener<Id extends string, S extends StateTree, G, A> = (context: StoreOnActionListenerContext<Id, S, G, {} extends A ? _ActionsTree : A>) => void;
  613. /**
  614. * Context object passed to callbacks of `store.$onAction(context => {})`
  615. * TODO: should have only the Id, the Store and Actions to generate the proper object
  616. */
  617. export declare type StoreOnActionListenerContext<Id extends string, S extends StateTree, G, A> = _ActionsTree extends A ? _StoreOnActionListenerContext<StoreGeneric, string, _ActionsTree> : {
  618. [Name in keyof A]: Name extends string ? _StoreOnActionListenerContext<Store<Id, S, G, A>, Name, A> : never;
  619. }[keyof A];
  620. /**
  621. * Actual type for {@link StoreOnActionListenerContext}. Exists for refactoring
  622. * purposes. For internal use only.
  623. * For internal use **only**
  624. */
  625. export declare interface _StoreOnActionListenerContext<Store, ActionName extends string, A> {
  626. /**
  627. * Name of the action
  628. */
  629. name: ActionName;
  630. /**
  631. * Store that is invoking the action
  632. */
  633. store: Store;
  634. /**
  635. * Parameters passed to the action
  636. */
  637. args: A extends Record<ActionName, _Method> ? Parameters<A[ActionName]> : unknown[];
  638. /**
  639. * Sets up a hook once the action is finished. It receives the return value
  640. * of the action, if it's a Promise, it will be unwrapped.
  641. */
  642. after: (callback: A extends Record<ActionName, _Method> ? (resolvedReturn: _Awaited<ReturnType<A[ActionName]>>) => void : () => void) => void;
  643. /**
  644. * Sets up a hook if the action fails. Return `false` to catch the error and
  645. * stop it from propagating.
  646. */
  647. onError: (callback: (error: unknown) => void) => void;
  648. }
  649. /**
  650. * Properties of a store.
  651. */
  652. export declare interface StoreProperties<Id extends string> {
  653. /**
  654. * Unique identifier of the store
  655. */
  656. $id: Id;
  657. /* Excluded from this release type: _p */
  658. /* Excluded from this release type: _getters */
  659. /* Excluded from this release type: _isOptionsAPI */
  660. /**
  661. * Used by devtools plugin to retrieve properties added with plugins. Removed
  662. * in production. Can be used by the user to add property keys of the store
  663. * that should be displayed in devtools.
  664. */
  665. _customProperties: Set<string>;
  666. /* Excluded from this release type: _hotUpdate */
  667. /* Excluded from this release type: _hotUpdating */
  668. /* Excluded from this release type: _hmrPayload */
  669. }
  670. /**
  671. * Extract the state of a store type. Works with both a Setup Store or an
  672. * Options Store. Note this unwraps refs.
  673. */
  674. export declare type StoreState<SS> = SS extends Store<string, infer S, _GettersTree<StateTree>, _ActionsTree> ? UnwrapRef<S> : _ExtractStateFromSetupStore<SS>;
  675. /**
  676. * Extracts the return type for `storeToRefs`.
  677. * Will convert any `getters` into `ComputedRef`.
  678. */
  679. declare type StoreToRefs<SS extends StoreGeneric> = ToRefs<StoreState<SS> & PiniaCustomStateProperties<StoreState<SS>>> & ToComputedRefs<StoreGetters<SS>>;
  680. /**
  681. * Creates an object of references with all the state, getters, and plugin-added
  682. * state properties of the store. Similar to `toRefs()` but specifically
  683. * designed for Pinia stores so methods and non reactive properties are
  684. * completely ignored.
  685. *
  686. * @param store - store to extract the refs from
  687. */
  688. export declare function storeToRefs<SS extends StoreGeneric>(store: SS): StoreToRefs<SS>;
  689. /**
  690. * Store augmented for actions. For internal usage only.
  691. * For internal use **only**
  692. */
  693. export declare type _StoreWithActions<A> = {
  694. [k in keyof A]: A[k] extends (...args: infer P) => infer R ? (...args: P) => R : never;
  695. };
  696. /**
  697. * Store augmented with getters. For internal usage only.
  698. * For internal use **only**
  699. */
  700. export declare type _StoreWithGetters<G> = {
  701. readonly [k in keyof G]: G[k] extends (...args: any[]) => infer R ? R : UnwrapRef<G[k]>;
  702. };
  703. /**
  704. * Base store with state and functions. Should not be used directly.
  705. */
  706. export declare interface _StoreWithState<Id extends string, S extends StateTree, G, A> extends StoreProperties<Id> {
  707. /**
  708. * State of the Store. Setting it will internally call `$patch()` to update the state.
  709. */
  710. $state: UnwrapRef<S> & PiniaCustomStateProperties<S>;
  711. /**
  712. * Applies a state patch to current state. Allows passing nested values
  713. *
  714. * @param partialState - patch to apply to the state
  715. */
  716. $patch(partialState: _DeepPartial<UnwrapRef<S>>): void;
  717. /**
  718. * Group multiple changes into one function. Useful when mutating objects like
  719. * Sets or arrays and applying an object patch isn't practical, e.g. appending
  720. * to an array. The function passed to `$patch()` **must be synchronous**.
  721. *
  722. * @param stateMutator - function that mutates `state`, cannot be asynchronous
  723. */
  724. $patch<F extends (state: UnwrapRef<S>) => any>(stateMutator: ReturnType<F> extends Promise<any> ? never : F): void;
  725. /**
  726. * Resets the store to its initial state by building a new state object.
  727. * TODO: make this options only
  728. */
  729. $reset(): void;
  730. /**
  731. * Setups a callback to be called whenever the state changes. It also returns a function to remove the callback. Note
  732. * that when calling `store.$subscribe()` inside of a component, it will be automatically cleaned up when the
  733. * component gets unmounted unless `detached` is set to true.
  734. *
  735. * @param callback - callback passed to the watcher
  736. * @param options - `watch` options + `detached` to detach the subscription from the context (usually a component)
  737. * this is called from. Note that the `flush` option does not affect calls to `store.$patch()`.
  738. * @returns function that removes the watcher
  739. */
  740. $subscribe(callback: SubscriptionCallback<S>, options?: {
  741. detached?: boolean;
  742. } & WatchOptions): () => void;
  743. /**
  744. * Setups a callback to be called every time an action is about to get
  745. * invoked. The callback receives an object with all the relevant information
  746. * of the invoked action:
  747. * - `store`: the store it is invoked on
  748. * - `name`: The name of the action
  749. * - `args`: The parameters passed to the action
  750. *
  751. * On top of these, it receives two functions that allow setting up a callback
  752. * once the action finishes or when it fails.
  753. *
  754. * It also returns a function to remove the callback. Note than when calling
  755. * `store.$onAction()` inside of a component, it will be automatically cleaned
  756. * up when the component gets unmounted unless `detached` is set to true.
  757. *
  758. * @example
  759. *
  760. *```js
  761. *store.$onAction(({ after, onError }) => {
  762. * // Here you could share variables between all of the hooks as well as
  763. * // setting up watchers and clean them up
  764. * after((resolvedValue) => {
  765. * // can be used to cleanup side effects
  766. * . // `resolvedValue` is the value returned by the action, if it's a
  767. * . // Promise, it will be the resolved value instead of the Promise
  768. * })
  769. * onError((error) => {
  770. * // can be used to pass up errors
  771. * })
  772. *})
  773. *```
  774. *
  775. * @param callback - callback called before every action
  776. * @param detached - detach the subscription from the context this is called from
  777. * @returns function that removes the watcher
  778. */
  779. $onAction(callback: StoreOnActionListener<Id, S, G, A>, detached?: boolean): () => void;
  780. /**
  781. * Stops the associated effect scope of the store and remove it from the store
  782. * registry. Plugins can override this method to cleanup any added effects.
  783. * e.g. devtools plugin stops displaying disposed stores from devtools.
  784. * Note this doesn't delete the state of the store, you have to do it manually with
  785. * `delete pinia.state.value[store.$id]` if you want to. If you don't and the
  786. * store is used again, it will reuse the previous state.
  787. */
  788. $dispose(): void;
  789. /* Excluded from this release type: _r */
  790. }
  791. /**
  792. * Callback of a subscription
  793. */
  794. export declare type SubscriptionCallback<S> = (
  795. /**
  796. * Object with information relative to the store mutation that triggered the
  797. * subscription.
  798. */
  799. mutation: SubscriptionCallbackMutation<S>,
  800. /**
  801. * State of the store when the subscription is triggered. Same as
  802. * `store.$state`.
  803. */
  804. state: UnwrapRef<S>) => void;
  805. /**
  806. * Context object passed to a subscription callback.
  807. */
  808. export declare type SubscriptionCallbackMutation<S> = SubscriptionCallbackMutationDirect | SubscriptionCallbackMutationPatchObject<S> | SubscriptionCallbackMutationPatchFunction;
  809. /**
  810. * Base type for the context passed to a subscription callback. Internal type.
  811. */
  812. export declare interface _SubscriptionCallbackMutationBase {
  813. /**
  814. * Type of the mutation.
  815. */
  816. type: MutationType;
  817. /**
  818. * `id` of the store doing the mutation.
  819. */
  820. storeId: string;
  821. /**
  822. * 🔴 DEV ONLY, DO NOT use for production code. Different mutation calls. Comes from
  823. * https://vuejs.org/guide/extras/reactivity-in-depth.html#reactivity-debugging and allows to track mutations in
  824. * devtools and plugins **during development only**.
  825. */
  826. events?: DebuggerEvent[] | DebuggerEvent;
  827. }
  828. /**
  829. * Context passed to a subscription callback when directly mutating the state of
  830. * a store with `store.someState = newValue` or `store.$state.someState =
  831. * newValue`.
  832. */
  833. export declare interface SubscriptionCallbackMutationDirect extends _SubscriptionCallbackMutationBase {
  834. type: MutationType.direct;
  835. events: DebuggerEvent;
  836. }
  837. /**
  838. * Context passed to a subscription callback when `store.$patch()` is called
  839. * with a function.
  840. */
  841. export declare interface SubscriptionCallbackMutationPatchFunction extends _SubscriptionCallbackMutationBase {
  842. type: MutationType.patchFunction;
  843. events: DebuggerEvent[];
  844. }
  845. /**
  846. * Context passed to a subscription callback when `store.$patch()` is called
  847. * with an object.
  848. */
  849. export declare interface SubscriptionCallbackMutationPatchObject<S> extends _SubscriptionCallbackMutationBase {
  850. type: MutationType.patchObject;
  851. events: DebuggerEvent[];
  852. /**
  853. * Object passed to `store.$patch()`.
  854. */
  855. payload: _DeepPartial<S>;
  856. }
  857. declare type ToComputedRefs<T> = {
  858. [K in keyof T]: ToRef<T[K]> extends Ref<infer U> ? ComputedRef<U> : ToRef<T[K]>;
  859. };
  860. /**
  861. * Type that enables refactoring through IDE.
  862. * For internal use **only**
  863. */
  864. export declare type _UnwrapAll<SS> = {
  865. [K in keyof SS]: UnwrapRef<SS[K]>;
  866. };
  867. export { }
  868. // Extensions of Vue types to be appended manually
  869. // https://github.com/microsoft/rushstack/issues/2090
  870. // https://github.com/microsoft/rushstack/issues/1709
  871. // @ts-ignore: works on Vue 2, fails in Vue 3
  872. declare module 'vue/types/vue' {
  873. interface Vue {
  874. /**
  875. * Currently installed pinia instance.
  876. */
  877. $pinia: Pinia
  878. /**
  879. * Cache of stores instantiated by the current instance. Used by map
  880. * helpers. Used internally by Pinia.
  881. *
  882. * @internal
  883. */
  884. _pStores?: Record<string, Store>
  885. }
  886. }
  887. // @ts-ignore: works on Vue 2, fails in Vue 3
  888. declare module 'vue/types/options' {
  889. interface ComponentOptions<V> {
  890. /**
  891. * Pinia instance to install in your application. Should be passed to the
  892. * root Vue.
  893. */
  894. pinia?: Pinia
  895. }
  896. }
  897. // TODO: figure out why it cannot be 'vue'
  898. // @ts-ignore: works on Vue 3, fails in Vue 2
  899. declare module '@vue/runtime-core' {
  900. export interface ComponentCustomProperties {
  901. /**
  902. * Access to the application's Pinia
  903. */
  904. $pinia: Pinia
  905. /**
  906. * Cache of stores instantiated by the current instance. Used by devtools to
  907. * list currently used stores. Used internally by Pinia.
  908. *
  909. * @internal
  910. */
  911. _pStores?: Record<string, StoreGeneric>
  912. }
  913. }