]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: pass options to context in plugins
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 15 Apr 2021 15:19:42 +0000 (17:19 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Wed, 28 Apr 2021 17:45:38 +0000 (19:45 +0200)
src/rootStore.ts
src/store.ts

index 2f8b9375bb2418cf3c30c865670dd6ff8f50a5d6..03c5761c46c9220d34db71aef0f90c9736da1548 100644 (file)
@@ -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<string, Method>,
+  A = Record<string, Method>
+> {
   /**
    * 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<Id, S, G, A>
+
+  /**
+   * Current store being extended.
+   */
+  options: DefineStoreOptions<Id, S, G, A>
 }
 
 /**
index c82512cef87545903b13288c6078d1b88b868efe..1f8a18457d623df90efe6d3f7fcddf58725a0ede 100644 (file)
@@ -207,7 +207,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()
 
@@ -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<string, StoreGetterThis> */,
-  A /* extends Record<string, StoreAction> */
+  // the omission of the extends is necessary for type inference
+  G /* extends Record<string, Method> */,
+  A /* extends Record<string, Method> */
 >(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A> {
   const { id, state, getters, actions } = options
 
@@ -290,7 +292,9 @@ export function defineStore<
         storeAndDescriptor[1],
         id,
         getters as Record<string, Method> | undefined,
-        actions as Record<string, Method> | undefined
+        actions as Record<string, Method> | 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<string, Method> | undefined,
-      actions as Record<string, Method> | undefined
+      actions as Record<string, Method> | undefined,
+      // @ts-ignore: because we don't have extend on G and A
+      options
     )
   }