From 94d12e7f127125c8aa915262471e07aae9d881bf Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 1 Apr 2021 17:28:52 +0200 Subject: [PATCH] feat(types): expose PiniaPluginContext --- docs/core-concepts/plugins.md | 1 + src/index.ts | 1 + src/rootStore.ts | 25 +++++++++++++++++++++---- src/store.ts | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/docs/core-concepts/plugins.md b/docs/core-concepts/plugins.md index df7edc61..a9d832d8 100644 --- a/docs/core-concepts/plugins.md +++ b/docs/core-concepts/plugins.md @@ -27,6 +27,7 @@ A Pinia plugin is a function that optionally returns properties to be added to a ```js export function myPiniaPlugin(context) { context.pinia // the pinia created with `createPinia()` + context.app // the current app created with `createApp()` context.store // the store the plugin is augmenting context.options // the options object defining the store passed to `defineStore()` // ... diff --git a/src/index.ts b/src/index.ts index 39289f6e..6a09a0ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ export { createPinia, Pinia, PiniaStorePlugin, + PiniaPluginContext, } from './rootStore' export { defineStore } from './store' export { diff --git a/src/rootStore.ts b/src/rootStore.ts index faf4e5b5..2f8b9375 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -56,14 +56,31 @@ export let clientApp: App | undefined /*#__PURE__*/ export const setClientApp = (app: App) => (clientApp = app) export const getClientApp = () => clientApp +/** + * Context argument passed to Pinia plugins. + */ +export interface PiniaPluginContext { + /** + * pinia instance. + */ + + pinia: Pinia + /** + * Current app created with `Vue.createApp()`. + */ + app: App + + /** + * Current store being extended. + */ + store: GenericStore +} + /** * Plugin to extend every store */ export interface PiniaStorePlugin { - (context: { - app: App - store: GenericStore - }): Partial | void + (context: PiniaPluginContext): Partial | void } /** diff --git a/src/store.ts b/src/store.ts index 18c296be..c82512ce 100644 --- a/src/store.ts +++ b/src/store.ts @@ -247,7 +247,7 @@ function buildStoreToUse< // apply all plugins pinia._p.forEach((extender) => { - Object.assign(store, extender({ store, app: pinia._a })) + Object.assign(store, extender({ store, app: pinia._a, pinia })) }) return store -- 2.47.3