diff --git a/src/index.ts b/src/index.ts index 84d1944fdc..3e62d12384 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,8 @@ export { PiniaPlugin } from './plugin' export { StateTree, Store, + GenericStore, + GenericStoreDefinition, StoreWithGetters, StoreWithActions, StoreWithState, diff --git a/src/rootStore.ts b/src/rootStore.ts index cc523f9f43..24df34cdda 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -29,7 +29,10 @@ export const piniaSymbol = (__DEV__ * Plugin to extend every store */ export interface PiniaStorePlugin { - (pinia: Pinia): Partial + (context: { + pinia: Pinia + store: GenericStore + }): Partial | void } /** @@ -44,8 +47,7 @@ export interface Pinia { /** * Adds a store plugin to extend every store * - * @alpha DO NOT USE, The plugin architecture will change to provide more - * customization options. + * @alpha the plugin API could change in the future * * @param plugin - store plugin to add */ @@ -56,7 +58,7 @@ export interface Pinia { * * @internal */ - _p: Array<() => Partial> + _p: PiniaStorePlugin[] /** * Vue constructor retrieved when installing the pinia. @@ -74,6 +76,7 @@ declare module 'vue/types/vue' { /** * Cache of stores instantiated by the current instance. Used by map * helpers. + * * @internal */ _pStores?: Record @@ -105,13 +108,7 @@ export function createPinia(): Pinia { Vue: {} as any, use(plugin) { - /* istanbul ignore next */ - if (__DEV__ && !__TEST__) { - console.warn( - `[🍍]: The plugin API has plans to change to bring better extensibility. "pinia.use()" signature will change in the next release. It is recommended to avoid using this API.` - ) - } - _p.push(plugin.bind(null, pinia)) + _p.push(plugin) }, _p, diff --git a/src/store.ts b/src/store.ts index e6453222ff..388c3a6553 100644 --- a/src/store.ts +++ b/src/store.ts @@ -229,15 +229,9 @@ function buildStoreToUse< } as StoreWithActions[typeof actionName] } - const extensions = pinia._p.reduce( - (extended, extender) => assign({}, extended, extender()), - {} as PiniaCustomProperties - ) - const store: Store = reactive( assign( {}, - extensions, partialStore, // using this means no new properties can be added as state computedFromState(pinia.state, $id), @@ -251,6 +245,11 @@ function buildStoreToUse< // created. Object.defineProperty(store, '$state', descriptor) + // apply all plugins + pinia._p.forEach((extender) => { + assign(store, extender({ store, pinia })) + }) + return store } diff --git a/test-dts/store.test-d.ts b/test-dts/store.test-d.ts index 5350092be1..f068a4b6c3 100644 --- a/test-dts/store.test-d.ts +++ b/test-dts/store.test-d.ts @@ -1,4 +1,11 @@ -import { defineStore, expectType } from './' +import { GenericStore } from 'dist/src/types' +import { defineStore, expectType, createPinia } from './' + +const pinia = createPinia() + +pinia.use(({ store }) => { + expectType(store) +}) const useStore = defineStore({ id: 'name', @@ -10,7 +17,7 @@ const useStore = defineStore({ }, }) -let store = useStore() +const store = useStore() expectType<{ a: 'on' | 'off' }>(store.$state) expectType(store.nested.counter)