From 24b2b89c7be4ffda8b6fbc35155757f5780971d8 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sat, 1 Feb 2025 23:28:06 +0100 Subject: [PATCH] refactor: remove support for `id` as a property BREAKING CHANGE: `defineStore({ id: 'id' })` is now removed. Use `defineStore('id')` instead --- packages/pinia/__tests__/actions.spec.ts | 11 +--- packages/pinia/__tests__/getters.spec.ts | 11 +--- packages/pinia/__tests__/lifespan.spec.ts | 6 +- packages/pinia/__tests__/mapHelpers.spec.ts | 6 +- packages/pinia/__tests__/onAction.spec.ts | 6 +- packages/pinia/__tests__/state.spec.ts | 3 +- packages/pinia/__tests__/store.patch.spec.ts | 9 +-- packages/pinia/__tests__/store.spec.ts | 44 ++++---------- packages/pinia/__tests__/storePlugins.spec.ts | 3 +- packages/pinia/__tests__/storeSetup.spec.ts | 3 +- packages/pinia/__tests__/storeToRefs.spec.ts | 1 - packages/pinia/src/store.ts | 34 +---------- packages/pinia/test-dts/actions.test-d.ts | 3 +- .../pinia/test-dts/customizations.test-d.ts | 3 +- packages/pinia/test-dts/mapHelpers.test-d.ts | 13 +--- packages/pinia/test-dts/onAction.test-d.ts | 3 +- packages/pinia/test-dts/store.test-d.ts | 59 ++++--------------- 17 files changed, 52 insertions(+), 166 deletions(-) diff --git a/packages/pinia/__tests__/actions.spec.ts b/packages/pinia/__tests__/actions.spec.ts index a4b69074..af7661d8 100644 --- a/packages/pinia/__tests__/actions.spec.ts +++ b/packages/pinia/__tests__/actions.spec.ts @@ -5,8 +5,7 @@ describe('Actions', () => { const useStore = () => { // create a new store setActivePinia(createPinia()) - return defineStore({ - id: 'main', + return defineStore('main', { state: () => ({ a: true, nested: { @@ -55,13 +54,9 @@ describe('Actions', () => { })() } - const useB = defineStore({ - id: 'B', - state: () => ({ b: 'b' }), - }) + const useB = defineStore('B', { state: () => ({ b: 'b' }) }) - const useA = defineStore({ - id: 'A', + const useA = defineStore('A', { state: () => ({ a: 'a' }), actions: { swap() { diff --git a/packages/pinia/__tests__/getters.spec.ts b/packages/pinia/__tests__/getters.spec.ts index 73b4e070..4e04d4a2 100644 --- a/packages/pinia/__tests__/getters.spec.ts +++ b/packages/pinia/__tests__/getters.spec.ts @@ -9,8 +9,7 @@ describe('Getters', () => { setActivePinia(createPinia()) }) - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ name: 'Eduardo', }), @@ -40,13 +39,9 @@ describe('Getters', () => { }, }) - const useB = defineStore({ - id: 'B', - state: () => ({ b: 'b' }), - }) + const useB = defineStore('B', { state: () => ({ b: 'b' }) }) - const useA = defineStore({ - id: 'A', + const useA = defineStore('A', { state: () => ({ a: 'a' }), getters: { fromB(): string { diff --git a/packages/pinia/__tests__/lifespan.spec.ts b/packages/pinia/__tests__/lifespan.spec.ts index 1faa059e..518f9186 100644 --- a/packages/pinia/__tests__/lifespan.spec.ts +++ b/packages/pinia/__tests__/lifespan.spec.ts @@ -19,8 +19,7 @@ import { describe('Store Lifespan', () => { function defineMyStore() { - return defineStore({ - id: 'main', + return defineStore('main', { state: () => ({ a: true, n: 0, @@ -118,8 +117,7 @@ describe('Store Lifespan', () => { const globalWatch = vi.fn() const destroy = watch(() => pinia.state.value.a?.n, globalWatch) - const useStore = defineStore({ - id: 'a', + const useStore = defineStore('a', { state: () => { n = n || ref(0) return { n } diff --git a/packages/pinia/__tests__/mapHelpers.spec.ts b/packages/pinia/__tests__/mapHelpers.spec.ts index 9eb9e01f..53c99ad2 100644 --- a/packages/pinia/__tests__/mapHelpers.spec.ts +++ b/packages/pinia/__tests__/mapHelpers.spec.ts @@ -14,8 +14,7 @@ import { nextTick, defineComponent, ref, computed } from 'vue' import { mockWarn } from './vitest-mock-warn' describe('Map Helpers', () => { - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ a: true, n: 0, @@ -147,8 +146,7 @@ describe('Map Helpers', () => { }) describe('mapActions', () => { - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ n: 0 }), actions: { increment() { diff --git a/packages/pinia/__tests__/onAction.spec.ts b/packages/pinia/__tests__/onAction.spec.ts index 7c30f0b3..2320fc93 100644 --- a/packages/pinia/__tests__/onAction.spec.ts +++ b/packages/pinia/__tests__/onAction.spec.ts @@ -7,8 +7,7 @@ describe('Subscriptions', () => { const useStore = () => { // create a new store setActivePinia(createPinia()) - return defineStore({ - id: 'main', + return defineStore('main', { state: () => ({ user: 'Eduardo', }), @@ -168,8 +167,7 @@ describe('Subscriptions', () => { }) describe('multiple store instances', () => { - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ name: 'Eduardo', }), diff --git a/packages/pinia/__tests__/state.spec.ts b/packages/pinia/__tests__/state.spec.ts index 1c171b73..c15f3f58 100644 --- a/packages/pinia/__tests__/state.spec.ts +++ b/packages/pinia/__tests__/state.spec.ts @@ -151,8 +151,7 @@ describe('State', () => { const pinia = createPinia() setActivePinia(pinia) - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ name, counter, diff --git a/packages/pinia/__tests__/store.patch.spec.ts b/packages/pinia/__tests__/store.patch.spec.ts index 7794eff4..5a94c15a 100644 --- a/packages/pinia/__tests__/store.patch.spec.ts +++ b/packages/pinia/__tests__/store.patch.spec.ts @@ -6,8 +6,7 @@ describe('store.$patch', () => { const useStore = () => { // create a new store setActivePinia(createPinia()) - return defineStore({ - id: 'main', + return defineStore('main', { state: () => ({ a: true, nested: { @@ -22,8 +21,7 @@ describe('store.$patch', () => { const useArrayStore = () => { // create a new store setActivePinia(createPinia()) - return defineStore({ - id: 'main', + return defineStore('main', { state: () => ({ items: [{ id: 0 }], currentItem: { id: 1 }, @@ -141,8 +139,7 @@ describe('store.$patch', () => { const useStore = (pinia?: Pinia) => { // create a new store setActivePinia(pinia || createPinia()) - return defineStore({ - id: 'main', + return defineStore('main', { state: () => ({ arr: [] as any[], name: 'Eduardo', diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index b5616f90..92344580 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -11,8 +11,7 @@ describe('Store', () => { setActivePinia(createPinia()) }) - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ a: true, nested: { @@ -23,7 +22,7 @@ describe('Store', () => { }) it('reuses a store', () => { - const useStore = defineStore({ id: 'main' }) + const useStore = defineStore('main', {}) expect(useStore()).toBe(useStore()) }) @@ -56,8 +55,7 @@ describe('Store', () => { it('works without setting the active pinia', async () => { setActivePinia(undefined) const pinia = createPinia() - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ n: 0 }), }) const TestComponent = defineComponent({ @@ -100,7 +98,7 @@ describe('Store', () => { }) it('can create an empty state if no state option is provided', () => { - const store = defineStore({ id: 'some' })() + const store = defineStore('some', {})() expect(store.$state).toEqual({}) }) @@ -108,8 +106,7 @@ describe('Store', () => { it('can hydrate the state', () => { const pinia = createPinia() setActivePinia(pinia) - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ a: true, nested: { @@ -177,8 +174,7 @@ describe('Store', () => { it('should outlive components', async () => { const pinia = createPinia() - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ n: 0 }), }) @@ -250,7 +246,7 @@ describe('Store', () => { it('reuses stores from parent components', () => { let s1, s2 - const useStore = defineStore({ id: 'one' }) + const useStore = defineStore('one', {}) const pinia = createPinia() const Child = defineComponent({ @@ -277,7 +273,7 @@ describe('Store', () => { }) it('can share the same pinia in two completely different instances', async () => { - const useStore = defineStore({ id: 'one', state: () => ({ n: 0 }) }) + const useStore = defineStore('one', { state: () => ({ n: 0 }) }) const pinia = createPinia() const Comp = defineComponent({ @@ -314,10 +310,7 @@ describe('Store', () => { it('can be disposed', () => { const pinia = createPinia() - const useStore = defineStore({ - id: 'main', - state: () => ({ n: 0 }), - }) + const useStore = defineStore('main', { state: () => ({ n: 0 }) }) const store = useStore(pinia) const spy = vi.fn() @@ -341,27 +334,20 @@ describe('Store', () => { it('warns when state is created with a class constructor', () => { class MyState {} - const useMyStore = defineStore({ - id: 'store', - state: () => new MyState(), - }) + const useMyStore = defineStore('store', { state: () => new MyState() }) useMyStore() expect(warnTextCheckPlainObject('store')).toHaveBeenWarned() }) it('only warns about constructors when store is initially created', () => { class MyState {} - const useMyStore = defineStore({ - id: 'arrowInit', - state: () => new MyState(), - }) + const useMyStore = defineStore('arrowInit', { state: () => new MyState() }) useMyStore() expect(warnTextCheckPlainObject('arrowInit')).toHaveBeenWarnedTimes(1) }) it('does not warn when state is created with a plain object', () => { - const useMyStore = defineStore({ - id: 'poInit', + const useMyStore = defineStore('poInit', { state: () => ({ someValue: undefined }), }) useMyStore() @@ -379,10 +365,4 @@ describe('Store', () => { `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "anyName" in store "main".` ).toHaveBeenWarnedTimes(1) }) - - it('throws an error if no store id is provided', () => { - expect(() => defineStore({} as any)).toThrowError( - /must be passed a store id/ - ) - }) }) diff --git a/packages/pinia/__tests__/storePlugins.spec.ts b/packages/pinia/__tests__/storePlugins.spec.ts index 17f73983..b57d65ca 100644 --- a/packages/pinia/__tests__/storePlugins.spec.ts +++ b/packages/pinia/__tests__/storePlugins.spec.ts @@ -194,7 +194,6 @@ describe('store plugins', () => { it('passes the options of the options store', async () => { const options = { - id: 'main', state: () => ({ n: 0 }), actions: { increment() { @@ -208,7 +207,7 @@ describe('store plugins', () => { }, }, } - const useStore = defineStore(options) + const useStore = defineStore('main', options) const pinia = createPinia() mount({ template: 'none' }, { global: { plugins: [pinia] } }) diff --git a/packages/pinia/__tests__/storeSetup.spec.ts b/packages/pinia/__tests__/storeSetup.spec.ts index 9e51a97a..5a566e73 100644 --- a/packages/pinia/__tests__/storeSetup.spec.ts +++ b/packages/pinia/__tests__/storeSetup.spec.ts @@ -102,8 +102,7 @@ describe('store with setup syntax', () => { const pinia = createPinia() setActivePinia(pinia) - const useStore = defineStore({ - id: 'main', + const useStore = defineStore('main', { state: () => ({ name, counter, diff --git a/packages/pinia/__tests__/storeToRefs.spec.ts b/packages/pinia/__tests__/storeToRefs.spec.ts index d571f1b9..a0220128 100644 --- a/packages/pinia/__tests__/storeToRefs.spec.ts +++ b/packages/pinia/__tests__/storeToRefs.spec.ts @@ -18,7 +18,6 @@ describe('storeToRefs', () => { it('empty state', () => { expect(storeToRefs(defineStore('a', {})())).toEqual({}) expect(storeToRefs(defineStore('a', () => {})())).toEqual({}) - expect(storeToRefs(defineStore({ id: 'a' })())).toEqual({}) }) it('plain values', () => { diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 2eb7021f..a7be113c 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -800,21 +800,6 @@ export function defineStore< options: Omit, 'id'> ): StoreDefinition -/** - * Creates a `useStore` function that retrieves the store instance - * - * @param options - options to define the store - * - * @deprecated use `defineStore(id, options)` instead - */ -export function defineStore< - Id extends string, - S extends StateTree = {}, - G extends _GettersTree = {}, - // cannot extends ActionsTree because we loose the typings - A /* extends ActionsTree */ = {}, ->(options: DefineStoreOptions): StoreDefinition - /** * Creates a `useStore` function that retrieves the store instance * @@ -841,11 +826,10 @@ export function defineStore( /*! #__NO_SIDE_EFFECTS__ */ export function defineStore( // TODO: add proper types from above - idOrOptions: any, + id: any, setup?: any, setupOptions?: any ): StoreDefinition { - let id: string let options: | DefineStoreOptions< string, @@ -861,20 +845,8 @@ export function defineStore( > const isSetupStore = typeof setup === 'function' - if (typeof idOrOptions === 'string') { - id = idOrOptions - // the option store setup will contain the actual options in this case - options = isSetupStore ? setupOptions : setup - } else { - options = idOrOptions - id = idOrOptions.id - - if (__DEV__ && typeof id !== 'string') { - throw new Error( - `[🍍]: "defineStore()" must be passed a store id as its first argument.` - ) - } - } + // the option store setup will contain the actual options in this case + options = isSetupStore ? setupOptions : setup function useStore(pinia?: Pinia | null, hot?: StoreGeneric): StoreGeneric { const hasContext = hasInjectionContext() diff --git a/packages/pinia/test-dts/actions.test-d.ts b/packages/pinia/test-dts/actions.test-d.ts index dcf0002e..fefa76bd 100644 --- a/packages/pinia/test-dts/actions.test-d.ts +++ b/packages/pinia/test-dts/actions.test-d.ts @@ -1,7 +1,6 @@ import { defineStore, expectType } from './' -const useStore = defineStore({ - id: 'name', +const useStore = defineStore('name', { state: () => ({ count: 0 }), actions: { useOtherAction() { diff --git a/packages/pinia/test-dts/customizations.test-d.ts b/packages/pinia/test-dts/customizations.test-d.ts index 54bd5b30..e72958a7 100644 --- a/packages/pinia/test-dts/customizations.test-d.ts +++ b/packages/pinia/test-dts/customizations.test-d.ts @@ -57,8 +57,7 @@ pinia.use((context) => { } }) -const useStore = defineStore({ - id: 'main', +const useStore = defineStore('main', { actions: { one() {}, two() { diff --git a/packages/pinia/test-dts/mapHelpers.test-d.ts b/packages/pinia/test-dts/mapHelpers.test-d.ts index 1d2ee3cb..459214e6 100644 --- a/packages/pinia/test-dts/mapHelpers.test-d.ts +++ b/packages/pinia/test-dts/mapHelpers.test-d.ts @@ -9,8 +9,7 @@ import { import { describe, it, expectTypeOf } from 'vitest' describe('mapHelpers', () => { - const useOptionsStore = defineStore({ - id: 'name', + const useOptionsStore = defineStore('name', { state: () => ({ a: 'on' as 'on' | 'off', nested: { counter: 0 } }), getters: { upper: (state) => state.a.toUpperCase(), @@ -42,15 +41,9 @@ describe('mapHelpers', () => { return { a, upper, writableUpper, toggleA, setToggle } }) - const useCounter = defineStore({ - id: 'counter', - state: () => ({ n: 0 }), - }) + const useCounter = defineStore('counter', { state: () => ({ n: 0 }) }) - const useStoreDos = defineStore({ - id: 'dos', - state: () => ({}), - }) + const useStoreDos = defineStore('dos', { state: () => ({}) }) type MainStore = ReturnType type DosStore = ReturnType diff --git a/packages/pinia/test-dts/onAction.test-d.ts b/packages/pinia/test-dts/onAction.test-d.ts index c7d7543f..9e2731de 100644 --- a/packages/pinia/test-dts/onAction.test-d.ts +++ b/packages/pinia/test-dts/onAction.test-d.ts @@ -1,7 +1,6 @@ import { defineStore, expectType } from './' -const useStore = defineStore({ - id: 'main', +const useStore = defineStore('main', { state: () => ({ user: 'Eduardo', }), diff --git a/packages/pinia/test-dts/store.test-d.ts b/packages/pinia/test-dts/store.test-d.ts index b8d9abde..e97177b8 100644 --- a/packages/pinia/test-dts/store.test-d.ts +++ b/packages/pinia/test-dts/store.test-d.ts @@ -7,8 +7,7 @@ import { } from './' import { computed, Ref, ref, UnwrapRef, watch, WritableComputedRef } from 'vue' -const useStore = defineStore({ - id: 'name', +const useStore = defineStore('name', { state: () => ({ a: 'on' as 'on' | 'off', nested: { counter: 0 } }), getters: { upper: (state) => { @@ -42,10 +41,6 @@ const useStore = defineStore({ }, }) -defineStore('name', { - // @ts-expect-error: id is passed as the first argument - id: 'name', -}) defineStore('name', {}) // @ts-expect-error defineStore('name') @@ -54,8 +49,7 @@ defineStore('name', { }) // actions on not existing properties -defineStore({ - id: '', +defineStore('', { actions: { a() { // @ts-expect-error @@ -64,8 +58,7 @@ defineStore({ }, }) -defineStore({ - id: '', +defineStore('', { state: () => ({}), actions: { a() { @@ -75,8 +68,7 @@ defineStore({ }, }) -defineStore({ - id: '', +defineStore('', { getters: {}, actions: { a() { @@ -113,8 +105,7 @@ const s = init()() s.set({ id: 1 }) // getters on not existing properties -defineStore({ - id: '', +defineStore('', { getters: { a(): number { // @ts-expect-error @@ -129,8 +120,7 @@ defineStore({ }, }) -defineStore({ - id: '', +defineStore('', { state: () => ({}), getters: { a(): number { @@ -174,36 +164,13 @@ store.$patch(() => { return }) -const useNoSAG = defineStore({ - id: 'noSAG', -}) -const useNoAG = defineStore({ - id: 'noAG', - state: () => ({}), -}) -const useNoSG = defineStore({ - id: 'noAG', - actions: {}, -}) -const useNoSA = defineStore({ - id: 'noAG', - getters: {}, -}) -const useNoS = defineStore({ - id: 'noAG', - actions: {}, - getters: {}, -}) -const useNoA = defineStore({ - id: 'noAG', - state: () => ({}), - getters: {}, -}) -const useNoG = defineStore({ - id: 'noAG', - state: () => ({}), - actions: {}, -}) +const useNoSAG = defineStore('noSAG', {}) +const useNoAG = defineStore('noAG', { state: () => ({}) }) +const useNoSG = defineStore('noAG', { actions: {} }) +const useNoSA = defineStore('noAG', { getters: {} }) +const useNoS = defineStore('noAG', { actions: {}, getters: {} }) +const useNoA = defineStore('noAG', { state: () => ({}), getters: {} }) +const useNoG = defineStore('noAG', { state: () => ({}), actions: {} }) const noSAG = useNoSAG() const noSA = useNoSA() -- 2.47.3