]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(plugins): pass options to plugins
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 May 2021 08:39:32 +0000 (10:39 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 May 2021 09:08:12 +0000 (11:08 +0200)
src/index.ts
src/rootStore.ts
src/store.ts

index 3e62d1238443dc7d2314240a6d878ffbb52239cf..bffa890714e62878ea60b9f107d4ede5dc3ad234 100644 (file)
@@ -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,
index 82236032d5e8ab7b4c9a6863b9b3db2485fe7b17..8bd294c131047917efe32e81ca7714403b6c4578 100644 (file)
@@ -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<Pinia>
 
+/**
+ * Context argument passed to Pinia plugins.
+ */
+export interface PiniaPluginContext<
+  Id extends string = string,
+  S extends StateTree = StateTree,
+  G extends GettersTree<S> = GettersTree<S>,
+  A = Record<string, Method>
+> {
+  /**
+   * pinia instance.
+   */
+  pinia: Pinia
+
+  /**
+   * Current app created with `Vue.createApp()`.
+   */
+  // app: App
+
+  /**
+   * Current store being extended.
+   */
+  store: Store<Id, S, G, A>
+
+  /**
+   * Current store being extended.
+   */
+  options: DefineStoreOptions<Id, S, G, A>
+}
+
 /**
  * Plugin to extend every store
  */
 export interface PiniaStorePlugin {
-  (context: {
-    pinia: Pinia
-    store: GenericStore
-  }): Partial<PiniaCustomProperties> | void
+  (context: PiniaPluginContext): Partial<PiniaCustomProperties> | void
 }
 
 /**
index 5fc6b932868d3dd6c70ed4d2ac3645a0819bbb53..1c74e20d7cf853ab7a864852521fc44177f45182 100644 (file)
@@ -22,6 +22,7 @@ import {
   PiniaCustomProperties,
   StoreDefinition,
   GettersTree,
+  DefineStoreOptions,
 } from './types'
 import { useStoreDevtools } from './devtools'
 import {
@@ -208,7 +209,8 @@ function buildStoreToUse<
   descriptor: StateDescriptor<S>,
   $id: Id,
   getters: G = {} as G,
-  actions: A = {} as A
+  actions: A = {} as A,
+  options: DefineStoreOptions<Id, S, G, A>
 ) {
   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<S> | undefined,
-        actions as Record<string, Method> | undefined
+        actions as Record<string, Method> | 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<S> | undefined,
-      actions as Record<string, Method> | undefined
+      actions as Record<string, Method> | undefined,
+      // @ts-expect-error: because of the extend on Actions
+      options
     )
   }