Skip to content

Commit 34a9fc9

Browse files
committed
Enable constructor function for patch props
1 parent 8e28eb3 commit 34a9fc9

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

lib/ApiStore.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -193,37 +193,31 @@ export default class ApiStore<S> implements StoreOptions<S> {
193193
});
194194
}
195195

196+
const entityAfter = await applyModifier(
197+
"afterGet",
198+
model.name.toLowerCase(),
199+
this.models,
200+
entity
201+
);
202+
196203
const store = state[model.plural];
197204

198205
if (has(store.items, entity.id)) {
199-
forEach(entity, (value, name: string) => {
206+
const storeEntity = store.items[entity.id];
207+
forEach(entityAfter, (value, name: string) => {
200208
if (!isFunction(value) && !has(model.references, name)) {
201-
const storeEntity = store.items[entity.id];
202-
if (
203-
has(storeEntity, name) &&
204-
!isEqual(value, get(storeEntity, name))
205-
) {
209+
if (has(entity, name) && !isEqual(value, get(storeEntity, name))) {
206210
Vue.set(storeEntity, name, value);
207211
}
208212
}
209213
});
210214

211215
return store.items[entity.id];
212216
} else {
213-
const toStoreEntity = await applyModifier(
214-
"afterGet",
215-
model.name.toLowerCase(),
216-
this.models,
217-
entity
218-
);
219-
store.items = { ...store.items, [entity.id]: toStoreEntity };
220-
this.storeOriginItem(
221-
store.originItems,
222-
toStoreEntity,
223-
model.beforeQueue
224-
);
217+
store.items = { ...store.items, [entity.id]: entityAfter };
218+
this.storeOriginItem(store.originItems, entityAfter, model.beforeQueue);
225219

226-
return toStoreEntity;
220+
return entityAfter;
227221
}
228222
}
229223
}

tests/unit/apistoreCustomModel.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,50 @@ describe("ApiStore custom model", function() {
466466
await flushPromises();
467467
expect(store.getters["api/users"].items[user.id].localProp).toBe(true);
468468
});
469+
470+
it("test class with constructor", async () => {
471+
class CustomUser {
472+
id: string;
473+
name: string;
474+
constructor(o: any) {
475+
this.id = o.id;
476+
this.name = "test_constructor";
477+
}
478+
}
479+
480+
const store = new Vuex.Store({
481+
plugins: [
482+
ApiStorePlugin({
483+
axios: axiosInstance,
484+
models: {
485+
user: {
486+
name: "USER",
487+
plural: "USERS",
488+
type: new ApiState(),
489+
afterGet: (v: any) => new CustomUser(v)
490+
}
491+
}
492+
})
493+
]
494+
});
495+
const user = data[0].user;
496+
mock.onGet(`/user/${user.id}`).reply(200, user);
497+
store.dispatch("api/get", {
498+
id: user.id,
499+
type: "user"
500+
});
501+
await flushPromises();
502+
503+
mock.onGet(`/user/${user.id}`).reply(200, user);
504+
store.dispatch("api/get", {
505+
id: user.id,
506+
type: "user",
507+
forceFetch: true,
508+
clear: false
509+
});
510+
await flushPromises();
511+
expect(store.getters["api/users"].items[user.id].name).toBe(
512+
"test_constructor"
513+
);
514+
});
469515
});

0 commit comments

Comments
 (0)