resourcerer 1.2.3 → 2.0.0-beta-1
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/README.md +133 -164
- package/build/index.d.ts +9 -0
- package/build/index.js +9 -0
- package/build/lib/collection.d.ts +107 -0
- package/build/lib/collection.js +251 -0
- package/build/lib/config.d.ts +20 -0
- package/build/lib/config.js +23 -0
- package/build/lib/error-boundary.d.ts +12 -0
- package/build/lib/error-boundary.js +18 -0
- package/build/lib/events.d.ts +11 -0
- package/build/lib/events.js +12 -0
- package/build/lib/model-cache.d.ts +13 -0
- package/build/lib/model-cache.js +62 -0
- package/build/lib/model.d.ts +48 -0
- package/build/lib/model.js +170 -0
- package/build/lib/prefetch.d.ts +3 -0
- package/build/lib/prefetch.js +27 -0
- package/build/lib/request.d.ts +20 -0
- package/build/lib/request.js +53 -0
- package/build/lib/resourcerer.d.ts +13 -0
- package/build/lib/resourcerer.js +406 -0
- package/build/lib/sync.d.ts +18 -0
- package/build/lib/sync.js +58 -0
- package/build/lib/types.d.ts +66 -0
- package/build/lib/types.js +1 -0
- package/build/lib/utils.d.ts +14 -0
- package/build/lib/utils.js +87 -0
- package/docs/advanced_topics.md +9 -77
- package/docs/collection.md +28 -28
- package/docs/migrating_to_2.0.md +72 -0
- package/docs/model.md +21 -21
- package/docs/typescript.md +72 -22
- package/package.json +27 -30
- package/resourcerer.d.ts +58 -0
- package/test/collection.test.js +305 -316
- package/test/config.test.js +21 -75
- package/test/events.test.js +42 -41
- package/test/model-cache.test.js +99 -77
- package/test/model.test.js +204 -209
- package/test/prefetch.test.js +35 -61
- package/test/request.test.js +104 -109
- package/test/sync.test.js +122 -109
- package/test/utils.test.js +173 -159
- package/index.d.ts +0 -270
- package/index.js +0 -14
- package/lib/collection.js +0 -584
- package/lib/config.js +0 -131
- package/lib/constants.js +0 -6
- package/lib/error-boundary.js +0 -47
- package/lib/events.js +0 -19
- package/lib/model-cache.js +0 -131
- package/lib/model.js +0 -372
- package/lib/prefetch.js +0 -59
- package/lib/request.js +0 -126
- package/lib/resourcerer.js +0 -985
- package/lib/sync.js +0 -111
- package/lib/utils.js +0 -220
- package/test/model-mocks.js +0 -110
- package/test/setup.js +0 -63
package/test/utils.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Utils } from "../index";
|
|
2
2
|
import {
|
|
3
3
|
camelize,
|
|
4
4
|
hasErrored,
|
|
@@ -12,148 +12,147 @@ import {
|
|
|
12
12
|
result,
|
|
13
13
|
sortBy,
|
|
14
14
|
uniqueId,
|
|
15
|
-
urlError
|
|
16
|
-
} from
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
describe(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
expect(hasErrored(
|
|
24
|
-
expect(hasErrored(
|
|
25
|
-
expect(hasErrored(
|
|
26
|
-
expect(
|
|
27
|
-
expect(
|
|
28
|
-
expect(haveAnyErrored({myLoadingState: LoadingStates.ERROR})).toBe(true);
|
|
15
|
+
urlError,
|
|
16
|
+
} from "../lib/utils";
|
|
17
|
+
import { vi } from "vitest";
|
|
18
|
+
|
|
19
|
+
describe("Utils", () => {
|
|
20
|
+
describe("hasErrored method", () => {
|
|
21
|
+
it("returns true if any loading state has errored", () => {
|
|
22
|
+
expect(hasErrored(["error", "loading"])).toBe(true);
|
|
23
|
+
expect(hasErrored("error")).toBe(true);
|
|
24
|
+
expect(hasErrored({ myLoadingState: "error" })).toBe(true);
|
|
25
|
+
expect(Utils.hasErrored(["error", "loading"])).toBe(true);
|
|
26
|
+
expect(Utils.hasErrored("error")).toBe(true);
|
|
27
|
+
expect(Utils.hasErrored({ myLoadingState: "error" })).toBe(true);
|
|
29
28
|
});
|
|
30
29
|
|
|
31
|
-
it(
|
|
32
|
-
expect(hasErrored([
|
|
33
|
-
expect(hasErrored(
|
|
34
|
-
expect(hasErrored({myLoadingState:
|
|
35
|
-
expect(
|
|
36
|
-
expect(
|
|
37
|
-
expect(
|
|
30
|
+
it("returns false if no loading states have errored", () => {
|
|
31
|
+
expect(hasErrored(["loaded", "loading"])).toBe(false);
|
|
32
|
+
expect(hasErrored("loaded")).toBe(false);
|
|
33
|
+
expect(hasErrored({ myLoadingState: "loaded" })).toBe(false);
|
|
34
|
+
expect(Utils.hasErrored(["loaded", "loading"])).toBe(false);
|
|
35
|
+
expect(Utils.hasErrored("loaded")).toBe(false);
|
|
36
|
+
expect(Utils.hasErrored({ myLoadingState: "loaded" })).toBe(false);
|
|
38
37
|
});
|
|
39
38
|
|
|
40
|
-
it(
|
|
39
|
+
it("returns false if an undefined loading state is passed", () => {
|
|
41
40
|
expect(hasErrored(undefined)).toBe(false);
|
|
42
|
-
expect(
|
|
41
|
+
expect(Utils.hasErrored(undefined)).toBe(false);
|
|
43
42
|
});
|
|
44
43
|
});
|
|
45
44
|
|
|
46
|
-
describe(
|
|
47
|
-
it(
|
|
48
|
-
expect(isLoading([
|
|
49
|
-
expect(isLoading(
|
|
50
|
-
expect(isLoading({myLoadingState:
|
|
51
|
-
expect(
|
|
52
|
-
expect(
|
|
53
|
-
expect(
|
|
45
|
+
describe("isLoading method", () => {
|
|
46
|
+
it("returns true if any loading state is loading", () => {
|
|
47
|
+
expect(isLoading(["error", "loading"])).toBe(true);
|
|
48
|
+
expect(isLoading("loading")).toBe(true);
|
|
49
|
+
expect(isLoading({ myLoadingState: "loading" })).toBe(true);
|
|
50
|
+
expect(Utils.isLoading(["error", "loading"])).toBe(true);
|
|
51
|
+
expect(Utils.isLoading("loading")).toBe(true);
|
|
52
|
+
expect(Utils.isLoading({ myLoadingState: "loading" })).toBe(true);
|
|
54
53
|
});
|
|
55
54
|
|
|
56
|
-
it(
|
|
57
|
-
expect(isLoading([
|
|
58
|
-
expect(isLoading(
|
|
59
|
-
expect(isLoading({myLoadingState:
|
|
60
|
-
expect(
|
|
61
|
-
expect(
|
|
62
|
-
expect(
|
|
55
|
+
it("returns false if no loading states are loading", () => {
|
|
56
|
+
expect(isLoading(["loaded", "loaded"])).toBe(false);
|
|
57
|
+
expect(isLoading("loaded")).toBe(false);
|
|
58
|
+
expect(isLoading({ myLoadingState: "loaded" })).toBe(false);
|
|
59
|
+
expect(Utils.isLoading(["loaded", "loaded"])).toBe(false);
|
|
60
|
+
expect(Utils.isLoading("loaded")).toBe(false);
|
|
61
|
+
expect(Utils.isLoading({ myLoadingState: "loaded" })).toBe(false);
|
|
63
62
|
});
|
|
64
63
|
|
|
65
|
-
it(
|
|
64
|
+
it("returns false if an undefined loading state is passed", () => {
|
|
66
65
|
expect(isLoading(undefined)).toBe(false);
|
|
67
|
-
expect(
|
|
66
|
+
expect(Utils.isLoading(undefined)).toBe(false);
|
|
68
67
|
});
|
|
69
68
|
});
|
|
70
69
|
|
|
71
|
-
describe(
|
|
72
|
-
it(
|
|
73
|
-
expect(hasLoaded([
|
|
74
|
-
expect(hasLoaded(
|
|
75
|
-
expect(hasLoaded({myLoadingState:
|
|
76
|
-
expect(
|
|
77
|
-
expect(
|
|
78
|
-
expect(
|
|
70
|
+
describe("hasLoaded method", () => {
|
|
71
|
+
it("returns true if all loading states have loaded", () => {
|
|
72
|
+
expect(hasLoaded(["loaded", "loaded"])).toBe(true);
|
|
73
|
+
expect(hasLoaded("loaded")).toBe(true);
|
|
74
|
+
expect(hasLoaded({ myLoadingState: "loaded" })).toBe(true);
|
|
75
|
+
expect(Utils.hasLoaded(["loaded", "loaded"])).toBe(true);
|
|
76
|
+
expect(Utils.hasLoaded("loaded")).toBe(true);
|
|
77
|
+
expect(Utils.hasLoaded({ myLoadingState: "loaded" })).toBe(true);
|
|
79
78
|
});
|
|
80
79
|
|
|
81
|
-
it(
|
|
82
|
-
expect(hasLoaded([
|
|
83
|
-
expect(hasLoaded([
|
|
84
|
-
expect(hasLoaded(
|
|
85
|
-
expect(hasLoaded({myLoadingState:
|
|
86
|
-
expect(
|
|
87
|
-
expect(
|
|
88
|
-
expect(
|
|
89
|
-
expect(
|
|
80
|
+
it("returns false if any state has not loaded", () => {
|
|
81
|
+
expect(hasLoaded(["error", "loaded"])).toBe(false);
|
|
82
|
+
expect(hasLoaded(["loaded", "loading"])).toBe(false);
|
|
83
|
+
expect(hasLoaded("loading")).toBe(false);
|
|
84
|
+
expect(hasLoaded({ myLoadingState: "loading" })).toBe(false);
|
|
85
|
+
expect(Utils.hasLoaded(["error", "loaded"])).toBe(false);
|
|
86
|
+
expect(Utils.hasLoaded(["loaded", "loading"])).toBe(false);
|
|
87
|
+
expect(Utils.hasLoaded("loading")).toBe(false);
|
|
88
|
+
expect(Utils.hasLoaded({ myLoadingState: "loading" })).toBe(false);
|
|
90
89
|
});
|
|
91
90
|
|
|
92
|
-
it(
|
|
91
|
+
it("returns false if an undefined loading state is passed", () => {
|
|
93
92
|
expect(hasLoaded(undefined)).toBe(false);
|
|
94
|
-
expect(
|
|
93
|
+
expect(Utils.hasLoaded(undefined)).toBe(false);
|
|
95
94
|
});
|
|
96
95
|
});
|
|
97
96
|
|
|
98
|
-
describe(
|
|
99
|
-
it(
|
|
100
|
-
expect(isPending([
|
|
101
|
-
expect(isPending(
|
|
102
|
-
expect(isPending({myLoadingState:
|
|
103
|
-
expect(
|
|
104
|
-
expect(
|
|
105
|
-
expect(
|
|
97
|
+
describe("isPending method", () => {
|
|
98
|
+
it("returns true if any loading state is pending", () => {
|
|
99
|
+
expect(isPending(["pending", "loading"])).toBe(true);
|
|
100
|
+
expect(isPending("pending")).toBe(true);
|
|
101
|
+
expect(isPending({ myLoadingState: "pending" })).toBe(true);
|
|
102
|
+
expect(Utils.isPending(["pending", "loading"])).toBe(true);
|
|
103
|
+
expect(Utils.isPending("pending")).toBe(true);
|
|
104
|
+
expect(Utils.isPending({ myLoadingState: "pending" })).toBe(true);
|
|
106
105
|
});
|
|
107
106
|
|
|
108
|
-
it(
|
|
109
|
-
expect(isPending([
|
|
110
|
-
expect(isPending(
|
|
111
|
-
expect(isPending({myLoadingState:
|
|
112
|
-
expect(
|
|
113
|
-
expect(
|
|
114
|
-
expect(
|
|
107
|
+
it("returns false if no loading states are pending", () => {
|
|
108
|
+
expect(isPending(["loaded", "loading"])).toBe(false);
|
|
109
|
+
expect(isPending("loaded")).toBe(false);
|
|
110
|
+
expect(isPending({ myLoadingState: "loaded" })).toBe(false);
|
|
111
|
+
expect(Utils.isPending(["loaded", "loading"])).toBe(false);
|
|
112
|
+
expect(Utils.isPending("loaded")).toBe(false);
|
|
113
|
+
expect(Utils.isPending({ myLoadingState: "loaded" })).toBe(false);
|
|
115
114
|
});
|
|
116
115
|
|
|
117
|
-
it(
|
|
116
|
+
it("returns false if an undefined loading state is passed", () => {
|
|
118
117
|
expect(isPending(undefined)).toBe(false);
|
|
119
|
-
expect(
|
|
118
|
+
expect(Utils.isPending(undefined)).toBe(false);
|
|
120
119
|
});
|
|
121
120
|
});
|
|
122
121
|
|
|
123
|
-
describe(
|
|
124
|
-
it(
|
|
125
|
-
expect(camelize(
|
|
122
|
+
describe("camelize", () => {
|
|
123
|
+
it("passes words that are already camelCase", () => {
|
|
124
|
+
expect(camelize("camelCase")).toEqual("camelCase");
|
|
126
125
|
// also leaves alone PascalCase
|
|
127
|
-
expect(camelize(
|
|
126
|
+
expect(camelize("PascalCase")).toEqual("PascalCase");
|
|
128
127
|
// defaults
|
|
129
|
-
expect(camelize()).toEqual(
|
|
128
|
+
expect(camelize()).toEqual("");
|
|
130
129
|
});
|
|
131
130
|
|
|
132
|
-
it(
|
|
133
|
-
expect(camelize(
|
|
131
|
+
it("passes words that are single word lowercase", () => {
|
|
132
|
+
expect(camelize("lowercase")).toEqual("lowercase");
|
|
134
133
|
});
|
|
135
134
|
|
|
136
|
-
it(
|
|
137
|
-
expect(camelize(
|
|
135
|
+
it("lowercases words that are all uppercase", () => {
|
|
136
|
+
expect(camelize("UPPERCASE")).toEqual("uppercase");
|
|
138
137
|
});
|
|
139
138
|
|
|
140
|
-
it(
|
|
141
|
-
expect(camelize(
|
|
139
|
+
it("turns snake_case words into camelcase", () => {
|
|
140
|
+
expect(camelize("snake_case_words")).toEqual("snakeCaseWords");
|
|
142
141
|
});
|
|
143
142
|
|
|
144
|
-
it(
|
|
145
|
-
expect(camelize(
|
|
143
|
+
it("turns spine-case words into camelcase", () => {
|
|
144
|
+
expect(camelize("spine-case-words")).toEqual("spineCaseWords");
|
|
146
145
|
});
|
|
147
146
|
|
|
148
|
-
it(
|
|
149
|
-
expect(camelize(
|
|
147
|
+
it("turns space-separated words into camelcase", () => {
|
|
148
|
+
expect(camelize("space separated words")).toEqual("spaceSeparatedWords");
|
|
150
149
|
});
|
|
151
150
|
});
|
|
152
151
|
|
|
153
|
-
describe(
|
|
154
|
-
it(
|
|
155
|
-
var testFn =
|
|
156
|
-
|
|
152
|
+
describe("once", () => {
|
|
153
|
+
it("invokes a function maximum once", () => {
|
|
154
|
+
var testFn = vi.fn(),
|
|
155
|
+
testFnOnce = once(testFn);
|
|
157
156
|
|
|
158
157
|
testFnOnce();
|
|
159
158
|
testFnOnce();
|
|
@@ -166,114 +165,129 @@ describe('Utils', () => {
|
|
|
166
165
|
});
|
|
167
166
|
});
|
|
168
167
|
|
|
169
|
-
describe(
|
|
170
|
-
it(
|
|
171
|
-
expect(pick({foo:
|
|
168
|
+
describe("pick", () => {
|
|
169
|
+
it("picks an objects properties and returns a new object", () => {
|
|
170
|
+
expect(pick({ foo: "foo", bar: "bar", baz: "baz" }, "foo", "quux")).toEqual({ foo: "foo" });
|
|
172
171
|
});
|
|
173
172
|
|
|
174
|
-
it(
|
|
173
|
+
it("works with defaults", () => {
|
|
175
174
|
expect(pick()).toEqual({});
|
|
175
|
+
expect(pick({ foo: "foo" }, "bar")).toEqual({});
|
|
176
|
+
expect("bar" in pick({ foo: "foo" }, "bar")).toBe(false);
|
|
176
177
|
});
|
|
177
178
|
});
|
|
178
179
|
|
|
179
|
-
describe(
|
|
180
|
-
it(
|
|
181
|
-
expect(omit({foo:
|
|
182
|
-
|
|
180
|
+
describe("omit", () => {
|
|
181
|
+
it("omits an objects properties and returns a new object", () => {
|
|
182
|
+
expect(omit({ foo: "foo", bar: "bar", baz: "baz" }, "foo", "quux")).toEqual({
|
|
183
|
+
bar: "bar",
|
|
184
|
+
baz: "baz",
|
|
185
|
+
});
|
|
183
186
|
});
|
|
184
187
|
|
|
185
|
-
it(
|
|
188
|
+
it("works with defaults", () => {
|
|
186
189
|
expect(omit()).toEqual({});
|
|
187
190
|
});
|
|
188
191
|
});
|
|
189
192
|
|
|
190
|
-
describe(
|
|
191
|
-
it(
|
|
192
|
-
expect(result({foo: () =>
|
|
193
|
-
expect(result({foo: (x, y) => x + y + 5},
|
|
193
|
+
describe("result", () => {
|
|
194
|
+
it("returns the correct value if a function", () => {
|
|
195
|
+
expect(result({ foo: () => "bar" }, "foo")).toEqual("bar");
|
|
196
|
+
expect(result({ foo: (x, y) => x + y + 5 }, "foo", 10, 5)).toEqual(20);
|
|
194
197
|
});
|
|
195
198
|
|
|
196
|
-
it(
|
|
197
|
-
expect(result({foo:
|
|
199
|
+
it("returns the correct value if not a function", () => {
|
|
200
|
+
expect(result({ foo: "bar" }, "foo")).toEqual("bar");
|
|
198
201
|
// safe
|
|
199
202
|
expect(result()).not.toBeDefined();
|
|
200
203
|
});
|
|
201
204
|
});
|
|
202
205
|
|
|
203
|
-
describe(
|
|
204
|
-
it(
|
|
205
|
-
expect(uniqueId()).toEqual(
|
|
206
|
-
expect(uniqueId(
|
|
207
|
-
expect(uniqueId()).toEqual(
|
|
206
|
+
describe("uniqueId", () => {
|
|
207
|
+
it("returns a unique id with every call, optionally prefixed", () => {
|
|
208
|
+
expect(uniqueId()).toEqual("1");
|
|
209
|
+
expect(uniqueId("noah")).toEqual("noah2");
|
|
210
|
+
expect(uniqueId()).toEqual("3");
|
|
208
211
|
});
|
|
209
212
|
});
|
|
210
213
|
|
|
211
|
-
describe(
|
|
212
|
-
it(
|
|
214
|
+
describe("urlError", () => {
|
|
215
|
+
it("throw an error with a message about a missing url", () => {
|
|
213
216
|
expect(urlError).toThrowError('A "url" property or function must be specified');
|
|
214
217
|
});
|
|
215
218
|
});
|
|
216
219
|
|
|
217
|
-
describe(
|
|
218
|
-
describe(
|
|
219
|
-
it(
|
|
220
|
+
describe("isDeepEqual", () => {
|
|
221
|
+
describe("returns true", () => {
|
|
222
|
+
it("if the same referential object", () => {
|
|
220
223
|
var obj = {};
|
|
221
224
|
|
|
222
225
|
expect(isDeepEqual(obj, obj)).toBe(true);
|
|
223
226
|
});
|
|
224
227
|
|
|
225
|
-
it(
|
|
226
|
-
expect(
|
|
227
|
-
|
|
228
|
+
it("if the objects have equal values, deeply", () => {
|
|
229
|
+
expect(
|
|
230
|
+
isDeepEqual({ one: "one", two: 2, three: null }, { one: "one", two: 2, three: null })
|
|
231
|
+
).toBe(true);
|
|
228
232
|
// nested
|
|
229
|
-
expect(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
233
|
+
expect(
|
|
234
|
+
isDeepEqual(
|
|
235
|
+
{
|
|
236
|
+
one: "one",
|
|
237
|
+
two: { three: "three" },
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
one: "one",
|
|
241
|
+
two: { three: "three" },
|
|
242
|
+
}
|
|
243
|
+
)
|
|
244
|
+
).toBe(true);
|
|
236
245
|
});
|
|
237
246
|
});
|
|
238
247
|
|
|
239
|
-
describe(
|
|
240
|
-
it(
|
|
248
|
+
describe("returns false", () => {
|
|
249
|
+
it("if not objects", () => {
|
|
241
250
|
expect(isDeepEqual(null, false)).toBe(false);
|
|
242
251
|
});
|
|
243
252
|
|
|
244
|
-
it(
|
|
245
|
-
expect(isDeepEqual({one:
|
|
253
|
+
it("if one object has more properties", () => {
|
|
254
|
+
expect(isDeepEqual({ one: "one", two: "two" }, { one: "one" })).toBe(false);
|
|
246
255
|
});
|
|
247
256
|
|
|
248
|
-
it(
|
|
249
|
-
expect(isDeepEqual({one:
|
|
250
|
-
expect(isDeepEqual({one:
|
|
257
|
+
it("if the objects do not have equal values", () => {
|
|
258
|
+
expect(isDeepEqual({ one: "one" }, { one: "two" })).toBe(false);
|
|
259
|
+
expect(isDeepEqual({ one: "one" }, { two: "three" })).toBe(false);
|
|
251
260
|
// nested
|
|
252
|
-
expect(isDeepEqual({one: {two:
|
|
261
|
+
expect(isDeepEqual({ one: { two: "three" } }, { one: { two: "four" } })).toBe(false);
|
|
253
262
|
});
|
|
254
263
|
});
|
|
255
264
|
});
|
|
256
265
|
|
|
257
|
-
describe(
|
|
258
|
-
it(
|
|
259
|
-
expect(
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
{name:
|
|
275
|
-
{name:
|
|
276
|
-
{name:
|
|
266
|
+
describe("sortBy", () => {
|
|
267
|
+
it("sorts a list by a comparator function", () => {
|
|
268
|
+
expect(
|
|
269
|
+
sortBy(
|
|
270
|
+
[
|
|
271
|
+
{ name: "zorah" },
|
|
272
|
+
{ name: "alex" },
|
|
273
|
+
{ name: undefined },
|
|
274
|
+
{ name: "noah" },
|
|
275
|
+
{ name: undefined },
|
|
276
|
+
{ name: "alex" },
|
|
277
|
+
{ name: "zorah" },
|
|
278
|
+
{ name: "alex" },
|
|
279
|
+
],
|
|
280
|
+
({ name }) => name
|
|
281
|
+
)
|
|
282
|
+
).toEqual([
|
|
283
|
+
{ name: "alex" },
|
|
284
|
+
{ name: "alex" },
|
|
285
|
+
{ name: "alex" },
|
|
286
|
+
{ name: "noah" },
|
|
287
|
+
{ name: "zorah" },
|
|
288
|
+
{ name: "zorah" },
|
|
289
|
+
{ name: undefined },
|
|
290
|
+
{ name: undefined },
|
|
277
291
|
]);
|
|
278
292
|
|
|
279
293
|
expect(sortBy()).toEqual([]);
|