From 6b7c3553c24345ad2244a69a92c726568c878c2d Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 3 May 2021 12:43:56 +0200 Subject: [PATCH] refactor(types): expose types as needed --- src/index.ts | 9 ++++++ src/mapHelpers.ts | 77 ++++++++++++++++++++++++++++++----------------- src/rootStore.ts | 10 ++++-- src/store.ts | 12 ++++---- src/types.ts | 40 +++++++++++++++++++++--- 5 files changed, 107 insertions(+), 41 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1b1aad1c..8c9fe2de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/mapHelpers.ts b/src/mapHelpers.ts index be8472b0..502ac6f5 100644 --- a/src/mapHelpers.ts +++ b/src/mapHelpers.ts @@ -2,7 +2,7 @@ import { ComponentPublicInstance } from 'vue' import { GenericStore, GettersTree, - Method, + _Method, StateTree, Store, StoreDefinition, @@ -42,15 +42,18 @@ type StoreObject = S extends StoreDefinition< } : {} -type Spread = A extends [infer L, ...infer R] - ? StoreObject & Spread +/** + * @internal + */ +export type _Spread = A extends [infer L, ...infer R] + ? StoreObject & _Spread : unknown function getCachedStore< Id extends string = string, S extends StateTree = StateTree, G extends GettersTree = GettersTree, - A = Record + A = Record >( vm: ComponentPublicInstance, useStore: StoreDefinition @@ -101,7 +104,7 @@ export function setMapStoreSuffix( */ export function mapStores( ...stores: [...Stores] -): Spread { +): _Spread { 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( return getCachedStore(this, useStore) } return reduced - }, {} as Spread) + }, {} as _Spread) } -type MapStateReturn> = { +/** + * @internal + */ +export type _MapStateReturn> = { [key in keyof S | keyof G]: () => Store[key] } -type MapStateObjectReturn< +/** + * @internal + */ +export type _MapStateObjectReturn< Id extends string, S extends StateTree, G extends GettersTree, @@ -192,7 +201,7 @@ export function mapState< >( useStore: StoreDefinition, keyMapper: KeyMapper -): MapStateObjectReturn +): _MapStateObjectReturn /** * 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, keys: Array -): MapStateReturn +): _MapStateReturn /** * 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, keysOrMapper: Array | KeyMapper -): MapStateReturn | MapStateObjectReturn { +): _MapStateReturn | _MapStateObjectReturn { return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => { reduced[key] = function (this: ComponentPublicInstance) { return getCachedStore(this, useStore)[key] } as () => any return reduced - }, {} as MapStateReturn) + }, {} as _MapStateReturn) : 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) + }, {} as _MapStateObjectReturn) } /** @@ -273,11 +282,17 @@ export function mapState< */ export const mapGetters = mapState -type MapActionsReturn = { +/** + * @internal + */ +export type _MapActionsReturn = { [key in keyof A]: Store[key] } -type MapActionsObjectReturn> = { +/** + * @internal + */ +export type _MapActionsObjectReturn> = { [key in keyof T]: Store[T[key]] } @@ -315,7 +330,7 @@ export function mapActions< >( useStore: StoreDefinition, keyMapper: KeyMapper -): MapActionsObjectReturn +): _MapActionsObjectReturn /** * 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, keys: Array -): MapActionsReturn +): _MapActionsReturn /** * 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, keysOrMapper: Array | KeyMapper -): MapActionsReturn | MapActionsObjectReturn { +): _MapActionsReturn | _MapActionsObjectReturn { 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[keyof A] return reduced - }, {} as MapActionsReturn) + }, {} as _MapActionsReturn) : 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[keyof KeyMapper[]] return reduced - }, {} as MapActionsObjectReturn) + }, {} as _MapActionsObjectReturn) } -type MapWritableStateReturn = { +/** + * @internal + */ +export type _MapWritableStateReturn = { [key in keyof S]: { get: () => Store[key] set: (value: Store[key]) => any } } -type MapWritableStateObjectReturn< +/** + * @internal + */ +export type _MapWritableStateObjectReturn< S extends StateTree, T extends Record > = { @@ -421,7 +442,7 @@ export function mapWritableState< >( useStore: StoreDefinition, keyMapper: KeyMapper -): MapWritableStateObjectReturn +): _MapWritableStateObjectReturn /** * 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, keys: Array -): MapWritableStateReturn +): _MapWritableStateReturn /** * 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, keysOrMapper: Array | KeyMapper -): MapWritableStateReturn | MapWritableStateObjectReturn { +): _MapWritableStateReturn | _MapWritableStateObjectReturn { return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => { // @ts-ignore @@ -470,7 +491,7 @@ export function mapWritableState< }, } return reduced - }, {} as MapWritableStateReturn) + }, {} as _MapWritableStateReturn) : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { // @ts-ignore reduced[key] = { @@ -485,5 +506,5 @@ export function mapWritableState< }, } return reduced - }, {} as MapWritableStateObjectReturn) + }, {} as _MapWritableStateObjectReturn) } diff --git a/src/rootStore.ts b/src/rootStore.ts index 7838cf84..419615ea 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -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 = GettersTree, - A = Record + A = Record > { /** * 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 | void } diff --git a/src/store.ts b/src/store.ts index 64bff32b..5284c6ef 100644 --- a/src/store.ts +++ b/src/store.ts @@ -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, - A extends Record + A extends Record >( partialStore: StoreWithState, descriptor: StateDescriptor, @@ -315,8 +315,8 @@ export function defineStore< storeAndDescriptor[1], id, getters as GettersTree | undefined, - actions as Record | undefined, - // @ts-ignore: because we don't have extend on G and A + actions as Record | undefined, + // @ts-expect-error: because of the extend on Actions options ) @@ -345,8 +345,8 @@ export function defineStore< storeAndDescriptor[1], id, getters as GettersTree | undefined, - actions as Record | undefined, - // @ts-ignore: because we don't have extend on G and A + actions as Record | undefined, + // @ts-expect-error: because of the extend on Actions options ) ) diff --git a/src/types.ts b/src/types.ts index bf66f7b0..320f2541 100644 --- a/src/types.ts +++ b/src/types.ts @@ -95,7 +95,12 @@ export interface StoreWithState { $subscribe(callback: SubscriptionCallback): () => 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

= (...args: P) => R // export interface StoreAction { @@ -157,7 +162,16 @@ export interface StoreDefinition< G extends GettersTree, A /* extends Record */ > { + /** + * Returns a store, creates it if necessary. + * + * @param pinia - Pinia instance to retrieve the store + */ (pinia?: Pinia | null | undefined): Store + + /** + * Id of the store. Used by map helpers. + */ $id: Id } @@ -168,7 +182,7 @@ export type GenericStore = Store< string, StateTree, GettersTree, - Record + Record > /** @@ -178,7 +192,7 @@ export type GenericStoreDefinition = StoreDefinition< string, StateTree, GettersTree, - Record + Record > /** @@ -188,9 +202,14 @@ export interface PiniaCustomProperties< Id extends string = string, S extends StateTree = StateTree, G extends GettersTree = GettersTree, - A = Record + A = Record > {} +/** + * Type of an object of Getters that infers the argument + * + * @internal + */ export type GettersTree = Record< string, ((state: S) => any) | (() => any) @@ -206,10 +225,21 @@ export interface DefineStoreOptions< G extends GettersTree, A /* extends Record */ > { + /** + * 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 & PiniaCustomProperties> - // allow actions use other actions + /** + * Optional object of actions. + */ actions?: A & ThisType< A & -- 2.47.2