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.

1064 lines
36 KiB

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