From: Eduardo San Martin Morote Date: Thu, 15 Apr 2021 15:19:42 +0000 (+0200) Subject: feat: pass options to context in plugins X-Git-Tag: v2.0.0-alpha.14~34 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c8ad19f6a959751d456aca93cd670d6b18064d50;p=thirdparty%2Fvuejs%2Fpinia.git feat: pass options to context in plugins --- diff --git a/src/rootStore.ts b/src/rootStore.ts index 2f8b9375..03c5761c 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -6,6 +6,9 @@ import { StateDescriptor, GenericStore, PiniaCustomProperties, + Method, + DefineStoreOptions, + Store, } from './types' /** @@ -59,12 +62,17 @@ export const getClientApp = () => clientApp /** * Context argument passed to Pinia plugins. */ -export interface PiniaPluginContext { +export interface PiniaPluginContext< + Id extends string = string, + S extends StateTree = StateTree, + G = Record, + A = Record +> { /** * pinia instance. */ - pinia: Pinia + /** * Current app created with `Vue.createApp()`. */ @@ -73,7 +81,12 @@ export interface PiniaPluginContext { /** * Current store being extended. */ - store: GenericStore + store: Store + + /** + * Current store being extended. + */ + options: DefineStoreOptions } /** diff --git a/src/store.ts b/src/store.ts index c82512ce..1f8a1845 100644 --- a/src/store.ts +++ b/src/store.ts @@ -207,7 +207,8 @@ function buildStoreToUse< descriptor: StateDescriptor, $id: Id, getters: G = {} as G, - actions: A = {} as A + actions: A = {} as A, + options: DefineStoreOptions ) { const pinia = getActivePinia() @@ -247,7 +248,7 @@ function buildStoreToUse< // apply all plugins pinia._p.forEach((extender) => { - Object.assign(store, extender({ store, app: pinia._a, pinia })) + Object.assign(store, extender({ store, app: pinia._a, pinia, options })) }) return store @@ -263,8 +264,9 @@ let isDevWarned: boolean | undefined export function defineStore< Id extends string, S extends StateTree, - G /* extends Record */, - A /* extends Record */ + // the omission of the extends is necessary for type inference + G /* extends Record */, + A /* extends Record */ >(options: DefineStoreOptions): StoreDefinition { const { id, state, getters, actions } = options @@ -290,7 +292,9 @@ export function defineStore< storeAndDescriptor[1], id, getters as Record | undefined, - actions as Record | undefined + actions as Record | undefined, + // @ts-ignore: because we don't have extend on G and A + options ) if ( @@ -322,7 +326,9 @@ export function defineStore< storeAndDescriptor[1], id, getters as Record | undefined, - actions as Record | undefined + actions as Record | undefined, + // @ts-ignore: because we don't have extend on G and A + options ) }