]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor(types): expose types as needed
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 May 2021 10:43:56 +0000 (12:43 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 May 2021 10:48:01 +0000 (12:48 +0200)
src/index.ts
src/mapHelpers.ts
src/rootStore.ts
src/store.ts
src/types.ts

index 1b1aad1cbaeb01537a15592ebc0dc34ea863c903..8c9fe2de7d023de656f25839eebf97390405ac05 100644 (file)
@@ -12,6 +12,8 @@ export {
   GenericStore,
   StoreDefinition,
   StoreWithGetters,
+  GettersTree,
+  _Method,
   StoreWithActions,
   StoreWithState,
   PiniaCustomProperties,
@@ -26,6 +28,13 @@ export {
   mapGetters,
   MapStoresCustomization,
   setMapStoreSuffix,
+  _MapActionsObjectReturn,
+  _MapActionsReturn,
+  _MapStateObjectReturn,
+  _MapStateReturn,
+  _MapWritableStateObjectReturn,
+  _MapWritableStateReturn,
+  _Spread,
 } from './mapHelpers'
 
 // TODO: remove in beta
index be8472b0ce8b2937f0c8f455ec7c9bb56f82f922..502ac6f5aab407a08eb1744a1a9a1a91616da41a 100644 (file)
@@ -2,7 +2,7 @@ import { ComponentPublicInstance } from 'vue'
 import {
   GenericStore,
   GettersTree,
-  Method,
+  _Method,
   StateTree,
   Store,
   StoreDefinition,
@@ -42,15 +42,18 @@ type StoreObject<S> = S extends StoreDefinition<
     }
   : {}
 
-type Spread<A extends readonly any[]> = A extends [infer L, ...infer R]
-  ? StoreObject<L> & Spread<R>
+/**
+ * @internal
+ */
+export type _Spread<A extends readonly any[]> = A extends [infer L, ...infer R]
+  ? StoreObject<L> & _Spread<R>
   : unknown
 
 function getCachedStore<
   Id extends string = string,
   S extends StateTree = StateTree,
   G extends GettersTree<S> = GettersTree<S>,
-  A = Record<string, Method>
+  A = Record<string, _Method>
 >(
   vm: ComponentPublicInstance,
   useStore: StoreDefinition<Id, S, G, A>
@@ -101,7 +104,7 @@ export function setMapStoreSuffix(
  */
 export function mapStores<Stores extends any[]>(
   ...stores: [...Stores]
-): Spread<Stores> {
+): _Spread<Stores> {
   if (__DEV__ && Array.isArray(stores[0])) {
     console.warn(
       `[🍍]: Directly pass all stores to "mapStores()" without putting them in an array:\n` +
@@ -120,14 +123,20 @@ export function mapStores<Stores extends any[]>(
       return getCachedStore(this, useStore)
     }
     return reduced
-  }, {} as Spread<Stores>)
+  }, {} as _Spread<Stores>)
 }
 
-type MapStateReturn<S extends StateTree, G extends GettersTree<S>> = {
+/**
+ * @internal
+ */
+export type _MapStateReturn<S extends StateTree, G extends GettersTree<S>> = {
   [key in keyof S | keyof G]: () => Store<string, S, G, {}>[key]
 }
 
-type MapStateObjectReturn<
+/**
+ * @internal
+ */
+export type _MapStateObjectReturn<
   Id extends string,
   S extends StateTree,
   G extends GettersTree<S>,
@@ -192,7 +201,7 @@ export function mapState<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keyMapper: KeyMapper
-): MapStateObjectReturn<Id, S, G, A, KeyMapper>
+): _MapStateObjectReturn<Id, S, G, A, KeyMapper>
 /**
  * Allows using state and getters from one store without using the composition
  * API (`setup()`) by generating an object to be spread in the `computed` field
@@ -224,7 +233,7 @@ export function mapState<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keys: Array<keyof S | keyof G>
-): MapStateReturn<S, G>
+): _MapStateReturn<S, G>
 /**
  * Allows using state and getters from one store without using the composition
  * API (`setup()`) by generating an object to be spread in the `computed` field
@@ -245,14 +254,14 @@ export function mapState<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keysOrMapper: Array<keyof S | keyof G> | KeyMapper
-): MapStateReturn<S, G> | MapStateObjectReturn<Id, S, G, A, KeyMapper> {
+): _MapStateReturn<S, G> | _MapStateObjectReturn<Id, S, G, A, KeyMapper> {
   return Array.isArray(keysOrMapper)
     ? keysOrMapper.reduce((reduced, key) => {
         reduced[key] = function (this: ComponentPublicInstance) {
           return getCachedStore(this, useStore)[key]
         } as () => any
         return reduced
-      }, {} as MapStateReturn<S, G>)
+      }, {} as _MapStateReturn<S, G>)
     : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
         reduced[key] = function (this: ComponentPublicInstance) {
           const store = getCachedStore(this, useStore)
@@ -264,7 +273,7 @@ export function mapState<
             : store[storeKey as keyof S | keyof G]
         }
         return reduced
-      }, {} as MapStateObjectReturn<Id, S, G, A, KeyMapper>)
+      }, {} as _MapStateObjectReturn<Id, S, G, A, KeyMapper>)
 }
 
 /**
@@ -273,11 +282,17 @@ export function mapState<
  */
 export const mapGetters = mapState
 
-type MapActionsReturn<A> = {
+/**
+ * @internal
+ */
+export type _MapActionsReturn<A> = {
   [key in keyof A]: Store<string, StateTree, {}, A>[key]
 }
 
-type MapActionsObjectReturn<A, T extends Record<string, keyof A>> = {
+/**
+ * @internal
+ */
+export type _MapActionsObjectReturn<A, T extends Record<string, keyof A>> = {
   [key in keyof T]: Store<string, StateTree, {}, A>[T[key]]
 }
 
@@ -315,7 +330,7 @@ export function mapActions<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keyMapper: KeyMapper
-): MapActionsObjectReturn<A, KeyMapper>
+): _MapActionsObjectReturn<A, KeyMapper>
 /**
  * Allows directly using actions from your store without using the composition
  * API (`setup()`) by generating an object to be spread in the `methods` field
@@ -347,7 +362,7 @@ export function mapActions<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keys: Array<keyof A>
-): MapActionsReturn<A>
+): _MapActionsReturn<A>
 /**
  * Allows directly using actions from your store without using the composition
  * API (`setup()`) by generating an object to be spread in the `methods` field
@@ -365,17 +380,17 @@ export function mapActions<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keysOrMapper: Array<keyof A> | KeyMapper
-): MapActionsReturn<A> | MapActionsObjectReturn<A, KeyMapper> {
+): _MapActionsReturn<A> | _MapActionsObjectReturn<A, KeyMapper> {
   return Array.isArray(keysOrMapper)
     ? keysOrMapper.reduce((reduced, key) => {
         reduced[key] = function (
           this: ComponentPublicInstance,
           ...args: any[]
         ) {
-          return (getCachedStore(this, useStore)[key] as Method)(...args)
+          return (getCachedStore(this, useStore)[key] as _Method)(...args)
         } as Store<string, StateTree, {}, A>[keyof A]
         return reduced
-      }, {} as MapActionsReturn<A>)
+      }, {} as _MapActionsReturn<A>)
     : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
         reduced[key] = function (
           this: ComponentPublicInstance,
@@ -384,17 +399,23 @@ export function mapActions<
           return getCachedStore(this, useStore)[keysOrMapper[key]](...args)
         } as Store<string, StateTree, {}, A>[keyof KeyMapper[]]
         return reduced
-      }, {} as MapActionsObjectReturn<A, KeyMapper>)
+      }, {} as _MapActionsObjectReturn<A, KeyMapper>)
 }
 
-type MapWritableStateReturn<S extends StateTree> = {
+/**
+ * @internal
+ */
+export type _MapWritableStateReturn<S extends StateTree> = {
   [key in keyof S]: {
     get: () => Store<string, S, {}, {}>[key]
     set: (value: Store<string, S, {}, {}>[key]) => any
   }
 }
 
-type MapWritableStateObjectReturn<
+/**
+ * @internal
+ */
+export type _MapWritableStateObjectReturn<
   S extends StateTree,
   T extends Record<string, keyof S>
 > = {
@@ -421,7 +442,7 @@ export function mapWritableState<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keyMapper: KeyMapper
-): MapWritableStateObjectReturn<S, KeyMapper>
+): _MapWritableStateObjectReturn<S, KeyMapper>
 /**
  * Allows using state and getters from one store without using the composition
  * API (`setup()`) by generating an object to be spread in the `computed` field
@@ -438,7 +459,7 @@ export function mapWritableState<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keys: Array<keyof S>
-): MapWritableStateReturn<S>
+): _MapWritableStateReturn<S>
 /**
  * Allows using state and getters from one store without using the composition
  * API (`setup()`) by generating an object to be spread in the `computed` field
@@ -456,7 +477,7 @@ export function mapWritableState<
 >(
   useStore: StoreDefinition<Id, S, G, A>,
   keysOrMapper: Array<keyof S> | KeyMapper
-): MapWritableStateReturn<S> | MapWritableStateObjectReturn<S, KeyMapper> {
+): _MapWritableStateReturn<S> | _MapWritableStateObjectReturn<S, KeyMapper> {
   return Array.isArray(keysOrMapper)
     ? keysOrMapper.reduce((reduced, key) => {
         // @ts-ignore
@@ -470,7 +491,7 @@ export function mapWritableState<
           },
         }
         return reduced
-      }, {} as MapWritableStateReturn<S>)
+      }, {} as _MapWritableStateReturn<S>)
     : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
         // @ts-ignore
         reduced[key] = {
@@ -485,5 +506,5 @@ export function mapWritableState<
           },
         }
         return reduced
-      }, {} as MapWritableStateObjectReturn<S, KeyMapper>)
+      }, {} as _MapWritableStateObjectReturn<S, KeyMapper>)
 }
index 7838cf8448ecdeb1d2195f713adcaa79290a49ba..419615eaf5f8a940f540fffff9fe623ae6830782 100644 (file)
@@ -6,7 +6,7 @@ import {
   StateDescriptor,
   GenericStore,
   PiniaCustomProperties,
-  Method,
+  _Method,
   DefineStoreOptions,
   Store,
   GettersTree,
@@ -77,7 +77,7 @@ export interface PiniaPluginContext<
   Id extends string = string,
   S extends StateTree = StateTree,
   G extends GettersTree<S> = GettersTree<S>,
-  A = Record<string, Method>
+  A = Record<string, _Method>
 > {
   /**
    * pinia instance.
@@ -104,6 +104,12 @@ export interface PiniaPluginContext<
  * Plugin to extend every store
  */
 export interface PiniaStorePlugin {
+  /**
+   * Plugin to extend every store. Returns an object to extend the store or
+   * nothing.
+   *
+   * @param context - Context
+   */
   (context: PiniaPluginContext): Partial<PiniaCustomProperties> | void
 }
 
index 64bff32bd3f3d0d0ac41a3f01be9c02e91bb4165..5284c6ef3434e67454ae0d0670774a1c3c33fc57 100644 (file)
@@ -18,8 +18,8 @@ import {
   StoreWithGetters,
   Store,
   StoreWithActions,
+  _Method,
   StateDescriptor,
-  Method,
   DefineStoreOptions,
   StoreDefinition,
   GenericStore,
@@ -215,7 +215,7 @@ function buildStoreToUse<
   Id extends string,
   S extends StateTree,
   G extends GettersTree<S>,
-  A extends Record<string, Method>
+  A extends Record<string, _Method>
 >(
   partialStore: StoreWithState<Id, S>,
   descriptor: StateDescriptor<S>,
@@ -315,8 +315,8 @@ export function defineStore<
         storeAndDescriptor[1],
         id,
         getters as GettersTree<S> | undefined,
-        actions as Record<string, Method> | undefined,
-        // @ts-ignore: because we don't have extend on G and A
+        actions as Record<string, _Method> | undefined,
+        // @ts-expect-error: because of the extend on Actions
         options
       )
 
@@ -345,8 +345,8 @@ export function defineStore<
         storeAndDescriptor[1],
         id,
         getters as GettersTree<S> | undefined,
-        actions as Record<string, Method> | undefined,
-        // @ts-ignore: because we don't have extend on G and A
+        actions as Record<string, _Method> | undefined,
+        // @ts-expect-error: because of the extend on Actions
         options
       )
     )
index bf66f7b0f6d1a3d3df9ca5a60e3ddca73401f2b8..320f2541293882900a886baae6f00109e703af06 100644 (file)
@@ -95,7 +95,12 @@ export interface StoreWithState<Id extends string, S extends StateTree> {
   $subscribe(callback: SubscriptionCallback<S>): () => void
 }
 
-export type Method = (...args: any[]) => any
+/**
+ * Generic type for a function that can infer arguments and return type
+ *
+ * @internal
+ */
+export type _Method = (...args: any[]) => any
 
 // export type StoreAction<P extends any[], R> = (...args: P) => R
 // export interface StoreAction<P, R> {
@@ -157,7 +162,16 @@ export interface StoreDefinition<
   G extends GettersTree<S>,
   A /* extends Record<string, StoreAction> */
 > {
+  /**
+   * Returns a store, creates it if necessary.
+   *
+   * @param pinia - Pinia instance to retrieve the store
+   */
   (pinia?: Pinia | null | undefined): Store<Id, S, G, A>
+
+  /**
+   * Id of the store. Used by map helpers.
+   */
   $id: Id
 }
 
@@ -168,7 +182,7 @@ export type GenericStore = Store<
   string,
   StateTree,
   GettersTree<StateTree>,
-  Record<string, Method>
+  Record<string, _Method>
 >
 
 /**
@@ -178,7 +192,7 @@ export type GenericStoreDefinition = StoreDefinition<
   string,
   StateTree,
   GettersTree<StateTree>,
-  Record<string, Method>
+  Record<string, _Method>
 >
 
 /**
@@ -188,9 +202,14 @@ export interface PiniaCustomProperties<
   Id extends string = string,
   S extends StateTree = StateTree,
   G extends GettersTree<S> = GettersTree<S>,
-  A = Record<string, Method>
+  A = Record<string, _Method>
 > {}
 
+/**
+ * Type of an object of Getters that infers the argument
+ *
+ * @internal
+ */
 export type GettersTree<S extends StateTree> = Record<
   string,
   ((state: S) => any) | (() => any)
@@ -206,10 +225,21 @@ export interface DefineStoreOptions<
   G extends GettersTree<S>,
   A /* extends Record<string, StoreAction> */
 > {
+  /**
+   * Unique string key to identify the store across the application.
+   */
   id: Id
+  /**
+   * Function to create a fresh state.
+   */
   state?: () => S
+  /**
+   * Optional object of getters.
+   */
   getters?: G & ThisType<S & StoreWithGetters<G> & PiniaCustomProperties>
-  // allow actions use other actions
+  /**
+   * Optional object of actions.
+   */
   actions?: A &
     ThisType<
       A &