From ac20f59adcd175981cbaac2aff9e08d57363adab Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 12 Mar 2021 17:15:11 +0100 Subject: [PATCH] feat: add mapStores --- __tests__/tds/store.test-d.ts | 24 +++++++++++++++++++++++- src/index.ts | 1 + src/store.ts | 10 ++++++++-- src/types.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/__tests__/tds/store.test-d.ts b/__tests__/tds/store.test-d.ts index 67db2a6b..b998854b 100644 --- a/__tests__/tds/store.test-d.ts +++ b/__tests__/tds/store.test-d.ts @@ -1,5 +1,6 @@ -import { defineStore } from '../../src' +import { defineStore, mapStores, Store } from '../../src' import { expectType, expectError } from 'tsd' +import { StoreDefinition } from 'src/types' const useStore = defineStore({ id: 'name', @@ -11,10 +12,31 @@ const useStore = defineStore({ }, }) +const useCounter = defineStore({ + id: 'counter', + state: () => ({ n: 0 }), +}) + +const useStoreDos = defineStore({ + id: 'dos', + state: () => ({}), +}) + const store = useStore() expectType<{ a: 'on' | 'off' }>(store.$state) +expectType(store.upper) expectType<{ upper: string }>(store) expectError(() => store.nonExistant) + +type MainStore = ReturnType +type DosStore = ReturnType +type CounterStore = ReturnType + +expectType<{ + name: () => MainStore + dos: () => DosStore + counter: () => CounterStore +}>(mapStores(useStore, useStoreDos, useCounter)) diff --git a/src/index.ts b/src/index.ts index 50217f25..97725ae7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,5 +15,6 @@ export { PiniaCustomProperties, } from './types' +export { mapStores } from './mapHelpers' // TODO: remove in beta export { createStore } from './deprecated' diff --git a/src/store.ts b/src/store.ts index 8eff1e26..64beb7c0 100644 --- a/src/store.ts +++ b/src/store.ts @@ -19,6 +19,7 @@ import { Method, StateDescriptor, PiniaCustomProperties, + StoreDefinition, } from './types' import { useStoreDevtools } from './devtools' import { @@ -276,10 +277,10 @@ export function defineStore< StoreWithGetters & PiniaCustomProperties > -}) { +}): StoreDefinition { const { id, state, getters, actions } = options - return function useStore(pinia?: Pinia | null): Store { + function useStore(pinia?: Pinia | null): Store { // const vm = getCurrentInstance() // pinia = pinia || (vm && ((vm as any).$pinia as Pinia)) pinia = pinia || (getCurrentInstance() && inject(piniaSymbol)) @@ -324,4 +325,9 @@ export function defineStore< actions as Record | undefined ) } + + // used by devtools + useStore.$id = id + + return useStore } diff --git a/src/types.ts b/src/types.ts index 3c774617..736fd945 100644 --- a/src/types.ts +++ b/src/types.ts @@ -124,6 +124,22 @@ export type Store< StoreWithActions & PiniaCustomProperties +/** + * Return type of `defineStore()`. Function that allows instantiating a store. + */ +export interface StoreDefinition< + Id extends string, + S extends StateTree, + G /* extends Record */, + A /* extends Record */ +> { + (pinia?: Pinia | null | undefined): Store + $id: Id +} + +/** + * Generic version of Store + */ export type GenericStore = Store< string, StateTree, @@ -131,6 +147,19 @@ export type GenericStore = Store< Record > +/** + * Generic version of `StoreDefinition` + */ +export interface GenericStoreDefinition { + (pinia?: Pinia | null | undefined): Store< + string, + StateTree, + Record, + Record + > + $id: string +} + export interface DevtoolHook { on( event: string, -- 2.47.3