]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: remove support for `id` as a property
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 1 Feb 2025 22:28:06 +0000 (23:28 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 1 Feb 2025 22:28:06 +0000 (23:28 +0100)
BREAKING CHANGE: `defineStore({ id: 'id' })` is now removed. Use
`defineStore('id')` instead

17 files changed:
packages/pinia/__tests__/actions.spec.ts
packages/pinia/__tests__/getters.spec.ts
packages/pinia/__tests__/lifespan.spec.ts
packages/pinia/__tests__/mapHelpers.spec.ts
packages/pinia/__tests__/onAction.spec.ts
packages/pinia/__tests__/state.spec.ts
packages/pinia/__tests__/store.patch.spec.ts
packages/pinia/__tests__/store.spec.ts
packages/pinia/__tests__/storePlugins.spec.ts
packages/pinia/__tests__/storeSetup.spec.ts
packages/pinia/__tests__/storeToRefs.spec.ts
packages/pinia/src/store.ts
packages/pinia/test-dts/actions.test-d.ts
packages/pinia/test-dts/customizations.test-d.ts
packages/pinia/test-dts/mapHelpers.test-d.ts
packages/pinia/test-dts/onAction.test-d.ts
packages/pinia/test-dts/store.test-d.ts

index a4b69074991cd46523d8cb4ad8d980fca1ccd3dc..af7661d802747790d65976b23e425515481c7a49 100644 (file)
@@ -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() {
index 73b4e0703531d85ba4d2bd9a6b89538d1a2f47c8..4e04d4a2e4155a05f6eb888b44baa342fae09991 100644 (file)
@@ -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 {
index 1faa059e5a89394267441cca428fa94ed542af66..518f918619f74fd43e36b19b7093092961d01959 100644 (file)
@@ -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 }
index 9eb9e01fc7da1d76ba79a939baaa3297951dcc74..53c99ad28e1afbc96968448d06227133e25a7128 100644 (file)
@@ -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() {
index 7c30f0b3456fc2135d0fe52cadc205ccc6b519d8..2320fc933e3e4c2e47bc8afa6045318dfe83bf1f 100644 (file)
@@ -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',
       }),
index 1c171b73158536ba36ac73545aac35119b63d3d1..c15f3f5885300f8095bf38cef6a151266983b3f9 100644 (file)
@@ -151,8 +151,7 @@ describe('State', () => {
 
     const pinia = createPinia()
     setActivePinia(pinia)
-    const useStore = defineStore({
-      id: 'main',
+    const useStore = defineStore('main', {
       state: () => ({
         name,
         counter,
index 7794eff4551ccdd18d4b356d361c7a99b88ff8fa..5a94c15ac2faa9b2103ef63e4e6b44887d42e21d 100644 (file)
@@ -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',
index b5616f9048995745e8836ac9c370afe47bbf34f0..92344580b31d6859b72acbb143041f43f8c4114c 100644 (file)
@@ -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/
-    )
-  })
 })
index 17f73983c467ceb8f33675e8345c88f643d46c8e..b57d65ca3e89329bd979e3db5bcbaf06cf18393b 100644 (file)
@@ -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] } })
 
index 9e51a97ad4f9c3c439f2a0cbd6739e1c0459aee6..5a566e731b79822e8ce8714933973ffe4a36a4f0 100644 (file)
@@ -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,
index d571f1b94a330cfb9ee4835f4588fe3bb56ceea9..a022012808d2b9ae466b7c4d78703f6e4a5cde72 100644 (file)
@@ -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', () => {
index 2eb7021f90b573c52b75480213011e23bfe8bece..a7be113ce3335972b7e77085e6ba17ec906e84c7 100644 (file)
@@ -800,21 +800,6 @@ export function defineStore<
   options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>
 ): StoreDefinition<Id, S, G, A>
 
-/**
- * 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<S> = {},
-  // cannot extends ActionsTree because we loose the typings
-  A /* extends ActionsTree */ = {},
->(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A>
-
 /**
  * Creates a `useStore` function that retrieves the store instance
  *
@@ -841,11 +826,10 @@ export function defineStore<Id extends string, SS>(
 /*! #__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()
index dcf0002ee9661ea9dfd15fd510d1942c98f3d09f..fefa76bd6c6f1491ff4a8106894beb67a2a52f52 100644 (file)
@@ -1,7 +1,6 @@
 import { defineStore, expectType } from './'
 
-const useStore = defineStore({
-  id: 'name',
+const useStore = defineStore('name', {
   state: () => ({ count: 0 }),
   actions: {
     useOtherAction() {
index 54bd5b303386dd071f7b0a7e5238bff2f218c2e5..e72958a7dd4ef6f7ef2818c0cbc668dc2c2e96d5 100644 (file)
@@ -57,8 +57,7 @@ pinia.use((context) => {
   }
 })
 
-const useStore = defineStore({
-  id: 'main',
+const useStore = defineStore('main', {
   actions: {
     one() {},
     two() {
index 1d2ee3cb8a01fe88f2b4c8cdaa83c4c7451bb31f..459214e64ba119e06f75f236afc24a439193cba2 100644 (file)
@@ -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<typeof useOptionsStore>
   type DosStore = ReturnType<typeof useStoreDos>
index c7d7543f6b32c1031d482f2203461453c2c6e377..9e2731defb688d412b8c50929fb75fa7345fdf90 100644 (file)
@@ -1,7 +1,6 @@
 import { defineStore, expectType } from './'
 
-const useStore = defineStore({
-  id: 'main',
+const useStore = defineStore('main', {
   state: () => ({
     user: 'Eduardo',
   }),
index b8d9abde413d35e1d41e137d0439a148cf7a897d..e97177b8e7eda5f57025e351a37ffea4f1d5b7b7 100644 (file)
@@ -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()