From: Eduardo San Martin Morote Date: Mon, 3 May 2021 08:39:32 +0000 (+0200) Subject: feat(plugins): pass options to plugins X-Git-Tag: v0.4.0~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d750adc05a2753f4abfae112fbeb1f2bc212a23;p=thirdparty%2Fvuejs%2Fpinia.git feat(plugins): pass options to plugins --- diff --git a/src/index.ts b/src/index.ts index 3e62d123..bffa8907 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,12 +3,14 @@ export { createPinia, Pinia, PiniaStorePlugin, + PiniaPluginContext, } from './rootStore' export { defineStore } from './store' export { PiniaPlugin } from './plugin' export { StateTree, Store, + StoreDefinition, GenericStore, GenericStoreDefinition, StoreWithGetters, diff --git a/src/rootStore.ts b/src/rootStore.ts index 82236032..8bd294c1 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -5,6 +5,10 @@ import { StateDescriptor, PiniaCustomProperties, GenericStore, + GettersTree, + Method, + Store, + DefineStoreOptions, } from './types' import { VueConstructor } from 'vue' import type Vue from 'vue' @@ -25,14 +29,41 @@ export const piniaSymbol = (__DEV__ : /* istanbul ignore next */ Symbol()) as InjectionKey +/** + * Context argument passed to Pinia plugins. + */ +export interface PiniaPluginContext< + Id extends string = string, + S extends StateTree = StateTree, + G extends GettersTree = GettersTree, + A = Record +> { + /** + * pinia instance. + */ + pinia: Pinia + + /** + * Current app created with `Vue.createApp()`. + */ + // app: App + + /** + * Current store being extended. + */ + store: Store + + /** + * Current store being extended. + */ + options: DefineStoreOptions +} + /** * Plugin to extend every store */ export interface PiniaStorePlugin { - (context: { - pinia: Pinia - store: GenericStore - }): Partial | void + (context: PiniaPluginContext): Partial | void } /** diff --git a/src/store.ts b/src/store.ts index 5fc6b932..1c74e20d 100644 --- a/src/store.ts +++ b/src/store.ts @@ -22,6 +22,7 @@ import { PiniaCustomProperties, StoreDefinition, GettersTree, + DefineStoreOptions, } from './types' import { useStoreDevtools } from './devtools' import { @@ -208,7 +209,8 @@ function buildStoreToUse< descriptor: StateDescriptor, $id: Id, getters: G = {} as G, - actions: A = {} as A + actions: A = {} as A, + options: DefineStoreOptions ) { const pinia = getActivePinia() @@ -250,7 +252,7 @@ function buildStoreToUse< // apply all plugins pinia._p.forEach((extender) => { - assign(store, extender({ store, pinia })) + assign(store, extender({ store, pinia, options })) }) return store @@ -314,7 +316,9 @@ export function defineStore< storeAndDescriptor[1], id, getters as GettersTree | undefined, - actions as Record | undefined + actions as Record | undefined, + // @ts-expect-error: because of the extend on Actions + options ) return store @@ -325,7 +329,9 @@ export function defineStore< storeAndDescriptor[1], id, getters as GettersTree | undefined, - actions as Record | undefined + actions as Record | undefined, + // @ts-expect-error: because of the extend on Actions + options ) }