Skip to content

Commit 5493a9a

Browse files
committed
fix: compatibility with vuex4: no changing of store data outside of mutations
And show that this is needed if the vuex store is in strict mode.
1 parent 92e029c commit 5493a9a

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/HalJsonVuexPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,10 @@ export function HalJsonVuexPlugin<RootEndpoint extends ResourceInterface, FullSt
407407
function setLoadPromiseOnStore<StoreType> (uri: string, loadStoreData: Promise<StoreData<StoreType>> | null = null) {
408408
const promise: SerializablePromise<StoreData<StoreType>> = loadStoreData || Promise.resolve(store.state[opts.apiName][uri])
409409
promise.toJSON = () => '{}' // avoid warning in Nuxt when serializing the complete Vuex store ("Cannot stringify arbitrary non-POJOs Promise")
410-
store.state[opts.apiName][uri]._meta.load = promise
410+
store.commit('setLoadPromise', {
411+
uri,
412+
promise
413+
})
411414
}
412415

413416
/**

src/storeModule.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { del, set } from 'vue-demi'
22

33
import type { MutationTree } from 'vuex/types'
44

5-
import type StoreData from './interfaces/StoreData'
5+
import type { StoreData, SerializablePromise } from './interfaces/StoreData'
66

77
export const state = {}
88
export type State<StoreType> = Record<string, StoreData<StoreType>>
@@ -30,6 +30,14 @@ export const mutations: MutationTree<State<unknown>> = {
3030
set(state[uri]._meta, 'reloading', false)
3131
})
3232
},
33+
/**
34+
* Adds entities loaded from the API to the Vuex store.
35+
* @param state Vuex state
36+
* @param data An object mapping URIs to entities that should be merged into the Vuex state.
37+
*/
38+
setLoadPromise<StoreType> (state: State<StoreType>, data: { uri: string, promise: SerializablePromise<StoreData> }): void {
39+
state[data.uri]._meta.load = data.promise
40+
},
3341
/**
3442
* Marks a single entity in the Vuex store as reloading, meaning a reloading network request is currently ongoin.
3543
* @param state Vuex state

tests/apiOperations.spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@ let axiosMock
2222
let store
2323
let vm
2424

25-
describe('Using dollar methods', () => {
25+
describe.each(
26+
[
27+
false,
28+
true
29+
]
30+
)('Using dollar methods and store.strict: %s', (strict) => {
2631
beforeAll(() => {
2732
axios.defaults.baseURL = 'http://localhost'
2833
})
2934

3035
beforeEach(() => {
3136
axiosMock = new MockAdapter(axios)
3237

33-
store = createStore({})
38+
store = createStore({
39+
strict
40+
})
3441

3542
const installApi = {
3643
install (app) {

0 commit comments

Comments
 (0)