resourcerer 2.1.2 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/collection.d.ts +2 -0
- package/build/lib/collection.js +15 -3
- package/build/lib/model-cache.d.ts +3 -2
- package/build/lib/model.d.ts +7 -0
- package/build/lib/model.js +23 -3
- package/build/lib/request.d.ts +3 -2
- package/build/lib/sync.js +3 -1
- package/package.json +1 -1
- package/test/model.test.js +107 -0
- package/test/sync.test.js +4 -3
|
@@ -2,6 +2,7 @@ import Events from "./events.js";
|
|
|
2
2
|
import Model, { type CanonicalModelSubscription, type ConstructorOptions, type SetOptions } from "./model.js";
|
|
3
3
|
import sync, { type SyncOptions } from "./sync.js";
|
|
4
4
|
import { ResourceConfigObj } from "./types.js";
|
|
5
|
+
import CanonicalModelConstructor from "./canonical-model.js";
|
|
5
6
|
type CSetOptions = {
|
|
6
7
|
parse?: boolean;
|
|
7
8
|
silent?: boolean;
|
|
@@ -25,6 +26,7 @@ export default class Collection<T extends Record<string, any> = object, O extend
|
|
|
25
26
|
} & CSetOptions);
|
|
26
27
|
urlOptions: Record<string, any>;
|
|
27
28
|
static Model: new (...args: any[]) => Model;
|
|
29
|
+
static CanonicalModel: (new (...args: any[]) => CanonicalModelConstructor<any>) | null;
|
|
28
30
|
static idAttribute: string;
|
|
29
31
|
static subscriptions: CanonicalModelSubscription[];
|
|
30
32
|
static dependencies: Array<string | ((attrs: Record<string, any>) => Record<string, any>)>;
|
package/build/lib/collection.js
CHANGED
|
@@ -25,6 +25,7 @@ export default class Collection extends Events {
|
|
|
25
25
|
}
|
|
26
26
|
urlOptions = {};
|
|
27
27
|
static Model = Model;
|
|
28
|
+
static CanonicalModel = null;
|
|
28
29
|
static idAttribute;
|
|
29
30
|
static subscriptions = [];
|
|
30
31
|
static dependencies = [];
|
|
@@ -185,12 +186,23 @@ export default class Collection extends Events {
|
|
|
185
186
|
this.models.forEach((model) => model.unsubscribe());
|
|
186
187
|
}
|
|
187
188
|
_getModelClass() {
|
|
188
|
-
const { idAttribute, subscriptions } = this.constructor;
|
|
189
|
+
const { idAttribute, subscriptions, CanonicalModel } = this.constructor;
|
|
189
190
|
const DefaultModel = this.constructor.Model;
|
|
190
|
-
if (idAttribute || subscriptions.length) {
|
|
191
|
+
if (idAttribute || subscriptions.length || CanonicalModel) {
|
|
191
192
|
return class extends Model {
|
|
192
193
|
static idAttribute = idAttribute || DefaultModel.idAttribute;
|
|
193
|
-
static subscriptions =
|
|
194
|
+
static subscriptions = [
|
|
195
|
+
...(CanonicalModel ?
|
|
196
|
+
[
|
|
197
|
+
{
|
|
198
|
+
Model: CanonicalModel,
|
|
199
|
+
toSource: (attrs) => attrs,
|
|
200
|
+
fromSource: (attrs) => attrs,
|
|
201
|
+
},
|
|
202
|
+
]
|
|
203
|
+
: []),
|
|
204
|
+
...(subscriptions || []),
|
|
205
|
+
];
|
|
194
206
|
};
|
|
195
207
|
}
|
|
196
208
|
return DefaultModel;
|
|
@@ -3,7 +3,7 @@ import Model from "./model.js";
|
|
|
3
3
|
import { ResourceKeys } from "./types.js";
|
|
4
4
|
type Component = NonNullable<unknown>;
|
|
5
5
|
declare const ModelCache: {
|
|
6
|
-
get(cacheKey: string):
|
|
6
|
+
get(cacheKey: string): Collection<object, object, {
|
|
7
7
|
new (attributes?: object | undefined, options?: object & import("./model.js").SetOptions & import("./model.js").ConstructorOptions): Model<object, object>;
|
|
8
8
|
idAttribute: string;
|
|
9
9
|
dependencies: (string | ((attrs: Record<string, any>) => Record<string, any>))[];
|
|
@@ -11,7 +11,8 @@ declare const ModelCache: {
|
|
|
11
11
|
defaults: {};
|
|
12
12
|
measure: boolean | ((config: import("./types.js").ResourceConfigObj) => boolean);
|
|
13
13
|
subscriptions: import("./model.js").CanonicalModelSubscription<Record<string, any>, Record<string, any>>[];
|
|
14
|
-
|
|
14
|
+
CanonicalModel: (new (...args: any[]) => import("./canonical-model.js").default<any>) | null;
|
|
15
|
+
}> | Model<Record<string, any>, Record<string, any>> | undefined;
|
|
15
16
|
put(cacheKey: string, model: Model | Collection, component?: Component): void;
|
|
16
17
|
register(cacheKey: string, component?: Component): void;
|
|
17
18
|
unregister(component: Component, ...cacheKeys: string[]): void;
|
package/build/lib/model.d.ts
CHANGED
|
@@ -36,6 +36,7 @@ export default class Model<T extends Record<string, any> = Record<string, any>,
|
|
|
36
36
|
static defaults: {};
|
|
37
37
|
static measure: boolean | ((config: ResourceConfigObj) => boolean);
|
|
38
38
|
static subscriptions: CanonicalModelSubscription[];
|
|
39
|
+
static CanonicalModel: (new (...args: any[]) => CanonicalModel<any>) | null;
|
|
39
40
|
toJSON(): T;
|
|
40
41
|
sync(...args: Parameters<typeof sync>): Promise<[any, Response]>;
|
|
41
42
|
get<K extends keyof T>(attr: K): T[K];
|
|
@@ -58,4 +59,10 @@ export default class Model<T extends Record<string, any> = Record<string, any>,
|
|
|
58
59
|
_subscribe(attrs?: Partial<T>): void;
|
|
59
60
|
unsubscribe(): void;
|
|
60
61
|
_updateSubscriptions(attrs?: Partial<T>, options?: SetOptions): void;
|
|
62
|
+
_getSubscriptions(): (CanonicalModelSubscription<Record<string, any>, Record<string, any>> | {
|
|
63
|
+
Model: new (...args: any[]) => CanonicalModel<any>;
|
|
64
|
+
idField: string;
|
|
65
|
+
toSource: (attrs: Partial<Record<string, any>>) => Partial<Record<string, any>>;
|
|
66
|
+
fromSource: (attrs: Partial<Record<string, any>>) => Partial<Record<string, any>>;
|
|
67
|
+
})[];
|
|
61
68
|
}
|
package/build/lib/model.js
CHANGED
|
@@ -44,6 +44,7 @@ export default class Model extends Events {
|
|
|
44
44
|
static defaults = {};
|
|
45
45
|
static measure = false;
|
|
46
46
|
static subscriptions = [];
|
|
47
|
+
static CanonicalModel = null;
|
|
47
48
|
toJSON() {
|
|
48
49
|
return { ...this.attributes };
|
|
49
50
|
}
|
|
@@ -181,7 +182,8 @@ export default class Model extends Events {
|
|
|
181
182
|
return !this.has(this.constructor.idAttribute);
|
|
182
183
|
}
|
|
183
184
|
_subscribe(attrs = {}) {
|
|
184
|
-
const
|
|
185
|
+
const subscriptions = this._getSubscriptions();
|
|
186
|
+
const idAttribute = this.constructor.idAttribute;
|
|
185
187
|
for (const { Model: CanonicalModel, idField = idAttribute, fromSource } of subscriptions) {
|
|
186
188
|
const id = getNestedValue(this.toJSON(), idField) || getNestedValue(attrs || {}, idField);
|
|
187
189
|
if (id && !this.isEmptyModel) {
|
|
@@ -199,7 +201,8 @@ export default class Model extends Events {
|
|
|
199
201
|
}
|
|
200
202
|
}
|
|
201
203
|
unsubscribe() {
|
|
202
|
-
const
|
|
204
|
+
const subscriptions = this._getSubscriptions();
|
|
205
|
+
const idAttribute = this.constructor.idAttribute;
|
|
203
206
|
for (const { Model: CanonicalModel, idField = idAttribute } of subscriptions) {
|
|
204
207
|
const id = getNestedValue(this.toJSON(), idField);
|
|
205
208
|
if (id) {
|
|
@@ -209,7 +212,8 @@ export default class Model extends Events {
|
|
|
209
212
|
}
|
|
210
213
|
}
|
|
211
214
|
_updateSubscriptions(attrs = {}, options = {}) {
|
|
212
|
-
const
|
|
215
|
+
const subscriptions = this._getSubscriptions();
|
|
216
|
+
const idAttribute = this.constructor.idAttribute;
|
|
213
217
|
for (const { Model: CanonicalModel, idField = idAttribute, toSource } of subscriptions) {
|
|
214
218
|
const id = getNestedValue(this.toJSON(), idField) || getNestedValue(attrs, idField);
|
|
215
219
|
if (toSource && id && !this.isEmptyModel) {
|
|
@@ -218,5 +222,21 @@ export default class Model extends Events {
|
|
|
218
222
|
}
|
|
219
223
|
}
|
|
220
224
|
}
|
|
225
|
+
_getSubscriptions() {
|
|
226
|
+
const { subscriptions, idAttribute, CanonicalModel } = this.constructor;
|
|
227
|
+
return [
|
|
228
|
+
...(CanonicalModel ?
|
|
229
|
+
[
|
|
230
|
+
{
|
|
231
|
+
Model: CanonicalModel,
|
|
232
|
+
idField: idAttribute,
|
|
233
|
+
toSource: (attrs) => attrs,
|
|
234
|
+
fromSource: (attrs) => attrs,
|
|
235
|
+
},
|
|
236
|
+
]
|
|
237
|
+
: []),
|
|
238
|
+
...(subscriptions || []),
|
|
239
|
+
];
|
|
240
|
+
}
|
|
221
241
|
}
|
|
222
242
|
Object.assign(Model.prototype, Events);
|
package/build/lib/request.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ interface RequestOptions extends Pick<SyncOptions, "params"> {
|
|
|
10
10
|
force?: boolean;
|
|
11
11
|
lazy?: boolean;
|
|
12
12
|
}
|
|
13
|
-
export declare const getFromCache: (key: string) =>
|
|
13
|
+
export declare const getFromCache: (key: string) => Collection<object, object, {
|
|
14
14
|
new (attributes?: object | undefined, options?: object & import("./model.js").SetOptions & import("./model.js").ConstructorOptions): Model<object, object>;
|
|
15
15
|
idAttribute: string;
|
|
16
16
|
dependencies: (string | ((attrs: Record<string, any>) => Record<string, any>))[];
|
|
@@ -18,7 +18,8 @@ export declare const getFromCache: (key: string) => Model<Record<string, any>, R
|
|
|
18
18
|
defaults: {};
|
|
19
19
|
measure: boolean | ((config: import("./types.js").ResourceConfigObj) => boolean);
|
|
20
20
|
subscriptions: import("./model.js").CanonicalModelSubscription<Record<string, any>, Record<string, any>>[];
|
|
21
|
-
|
|
21
|
+
CanonicalModel: (new (...args: any[]) => import("./canonical-model.js").default<any>) | null;
|
|
22
|
+
}> | Model<Record<string, any>, Record<string, any>> | undefined;
|
|
22
23
|
export declare const existsInCache: (key: string) => boolean;
|
|
23
24
|
declare const _default: (key: string, Model: {
|
|
24
25
|
new (data: Record<string, any>, options: RequestOptions["options"]): Model;
|
package/build/lib/sync.js
CHANGED
|
@@ -38,7 +38,9 @@ export function ajax(options) {
|
|
|
38
38
|
...options,
|
|
39
39
|
headers: {
|
|
40
40
|
Accept: MIME_TYPE_JSON,
|
|
41
|
-
...(hasBodyContent
|
|
41
|
+
...(hasBodyContent && !(options.params instanceof FormData) ?
|
|
42
|
+
{ "Content-Type": options.contentType }
|
|
43
|
+
: {}),
|
|
42
44
|
...options.headers,
|
|
43
45
|
},
|
|
44
46
|
...(hasBodyContent ?
|
package/package.json
CHANGED
package/test/model.test.js
CHANGED
|
@@ -19,6 +19,7 @@ describe("Model", () => {
|
|
|
19
19
|
model = collection = null;
|
|
20
20
|
sync.default.mockRestore();
|
|
21
21
|
callback.mockClear();
|
|
22
|
+
vi.restoreAllMocks();
|
|
22
23
|
});
|
|
23
24
|
|
|
24
25
|
it("is given a cid", () => {
|
|
@@ -774,6 +775,68 @@ describe("Model", () => {
|
|
|
774
775
|
});
|
|
775
776
|
});
|
|
776
777
|
});
|
|
778
|
+
|
|
779
|
+
it("auto-sets a subscription if the model has a CanonicalModel static property", () => {
|
|
780
|
+
CanonicalModel.prototype.onUpdate.mockRestore();
|
|
781
|
+
CanonicalModel.prototype.offUpdate.mockRestore();
|
|
782
|
+
|
|
783
|
+
vi.spyOn(CanonicalModel.prototype, "triggerUpdate");
|
|
784
|
+
|
|
785
|
+
class AutoSubscribingModel extends Model {
|
|
786
|
+
static CanonicalModel = CanonicalTestModel;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
const model = new AutoSubscribingModel({ id: "1234", name: "Zorah" });
|
|
790
|
+
const model2 = new AutoSubscribingModel({ id: "1234", name: "Noah" });
|
|
791
|
+
|
|
792
|
+
expect(canonicalModelCache.get(CanonicalTestModel).get("1234")._callbacks).toHaveLength(2);
|
|
793
|
+
|
|
794
|
+
expect(model.get("name")).toBe("Noah");
|
|
795
|
+
|
|
796
|
+
model.set({ name: "Zorah" });
|
|
797
|
+
expect(model2.get("name")).toBe("Zorah");
|
|
798
|
+
|
|
799
|
+
model.unsubscribe();
|
|
800
|
+
model2.unsubscribe();
|
|
801
|
+
|
|
802
|
+
expect(canonicalModelCache.get(CanonicalTestModel).get("1234")).not.toBeDefined();
|
|
803
|
+
});
|
|
804
|
+
|
|
805
|
+
it("adding a CanonicalModel property and a subscription on the same model makes a single subscription", () => {
|
|
806
|
+
CanonicalModel.prototype.onUpdate.mockRestore();
|
|
807
|
+
CanonicalModel.prototype.offUpdate.mockRestore();
|
|
808
|
+
|
|
809
|
+
vi.spyOn(CanonicalModel.prototype, "onUpdate");
|
|
810
|
+
vi.spyOn(CanonicalModel.prototype, "offUpdate");
|
|
811
|
+
vi.spyOn(CanonicalModel.prototype, "triggerUpdate");
|
|
812
|
+
|
|
813
|
+
class AutoSubscribingModel extends Model {
|
|
814
|
+
static CanonicalModel = CanonicalTestModel;
|
|
815
|
+
|
|
816
|
+
static subscriptions = [
|
|
817
|
+
{
|
|
818
|
+
Model: CanonicalTestModel,
|
|
819
|
+
toSource: (attrs) => attrs,
|
|
820
|
+
fromSource: (attrs) => attrs,
|
|
821
|
+
},
|
|
822
|
+
];
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
const model = new AutoSubscribingModel({ id: "1234", name: "Zorah" });
|
|
826
|
+
const model2 = new AutoSubscribingModel({ id: "1234", name: "Noah" });
|
|
827
|
+
|
|
828
|
+
expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalledTimes(4);
|
|
829
|
+
expect(CanonicalModel.prototype.offUpdate).toHaveBeenCalledTimes(4);
|
|
830
|
+
// only one trigger update per model
|
|
831
|
+
expect(CanonicalModel.prototype.triggerUpdate).toHaveBeenCalledTimes(2);
|
|
832
|
+
|
|
833
|
+
expect(canonicalModelCache.get(CanonicalTestModel).get("1234")._callbacks).toHaveLength(2);
|
|
834
|
+
|
|
835
|
+
model.unsubscribe();
|
|
836
|
+
model2.unsubscribe();
|
|
837
|
+
|
|
838
|
+
expect(canonicalModelCache.get(CanonicalTestModel).get("1234")).not.toBeDefined();
|
|
839
|
+
});
|
|
777
840
|
});
|
|
778
841
|
|
|
779
842
|
describe("unsubscribe", () => {
|
|
@@ -858,6 +921,50 @@ describe("Model", () => {
|
|
|
858
921
|
});
|
|
859
922
|
});
|
|
860
923
|
});
|
|
924
|
+
|
|
925
|
+
describe("_getSubscriptions", () => {
|
|
926
|
+
it("returns the subscriptions for the model", () => {
|
|
927
|
+
const model = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
928
|
+
|
|
929
|
+
expect(model._getSubscriptions()).toEqual([
|
|
930
|
+
{
|
|
931
|
+
Model: CanonicalTestModel,
|
|
932
|
+
idField: "_id",
|
|
933
|
+
toSource: expect.any(Function),
|
|
934
|
+
},
|
|
935
|
+
]);
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
it("combines the subscriptions from the model and the CanonicalModel static property", () => {
|
|
939
|
+
class AutoSubscribingSourceModel extends Model {
|
|
940
|
+
static CanonicalModel = CanonicalTestModel;
|
|
941
|
+
|
|
942
|
+
static subscriptions = [
|
|
943
|
+
{
|
|
944
|
+
Model: CanonicalTestModel,
|
|
945
|
+
toSource: (attrs) => attrs,
|
|
946
|
+
fromSource: (attrs) => attrs,
|
|
947
|
+
},
|
|
948
|
+
];
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
const model = new AutoSubscribingSourceModel({ id: "1234", name: "Zorah" });
|
|
952
|
+
|
|
953
|
+
expect(model._getSubscriptions()).toEqual([
|
|
954
|
+
{
|
|
955
|
+
Model: CanonicalTestModel,
|
|
956
|
+
idField: "id",
|
|
957
|
+
toSource: expect.any(Function),
|
|
958
|
+
fromSource: expect.any(Function),
|
|
959
|
+
},
|
|
960
|
+
{
|
|
961
|
+
Model: CanonicalTestModel,
|
|
962
|
+
toSource: expect.any(Function),
|
|
963
|
+
fromSource: expect.any(Function),
|
|
964
|
+
},
|
|
965
|
+
]);
|
|
966
|
+
});
|
|
967
|
+
});
|
|
861
968
|
});
|
|
862
969
|
|
|
863
970
|
class CanonicalTestModel extends CanonicalModel {}
|
package/test/sync.test.js
CHANGED
|
@@ -22,7 +22,7 @@ describe("sync", () => {
|
|
|
22
22
|
beforeEach(() => {
|
|
23
23
|
library = new Library();
|
|
24
24
|
vi.spyOn(window, "fetch").mockResolvedValue(
|
|
25
|
-
new Response(JSON.stringify({ test: "response!" }), { status: 200 })
|
|
25
|
+
new Response(JSON.stringify({ test: "response!" }), { status: 200 }),
|
|
26
26
|
);
|
|
27
27
|
});
|
|
28
28
|
|
|
@@ -93,6 +93,7 @@ describe("sync", () => {
|
|
|
93
93
|
await library.at(0).save(null, { params: formData, contentType: "multipart/form-data" });
|
|
94
94
|
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
95
95
|
expect(window.fetch.mock.calls[0][1].body).toEqual(formData);
|
|
96
|
+
expect(window.fetch.mock.calls[0][1].headers["Content-Type"]).not.toBeDefined();
|
|
96
97
|
});
|
|
97
98
|
|
|
98
99
|
it("passes urlOptions to the model to formulate the url path", async () => {
|
|
@@ -121,7 +122,7 @@ describe("sync", () => {
|
|
|
121
122
|
|
|
122
123
|
librarySectionBook = new LibrarySectionBook(
|
|
123
124
|
{},
|
|
124
|
-
{ section: "nature", bookId: "all-about-frogs" }
|
|
125
|
+
{ section: "nature", bookId: "all-about-frogs" },
|
|
125
126
|
);
|
|
126
127
|
await librarySectionBook.fetch({ params: { a: "a", one: 1 } });
|
|
127
128
|
expect(window.fetch.mock.calls[2][0]).toEqual("/library/nature/all-about-frogs?a=a&one=1");
|
|
@@ -182,7 +183,7 @@ describe("sync", () => {
|
|
|
182
183
|
|
|
183
184
|
it("rejects non-2xx", async () => {
|
|
184
185
|
window.fetch.mockResolvedValue(
|
|
185
|
-
new Response(JSON.stringify({ test: "womp" }), { status: 500, type: "application/json" })
|
|
186
|
+
new Response(JSON.stringify({ test: "womp" }), { status: 500, type: "application/json" }),
|
|
186
187
|
);
|
|
187
188
|
|
|
188
189
|
try {
|