resourcerer 1.2.3 → 2.0.0-beta
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 +68 -20
- package/package.json +27 -30
- package/resourcerer.d.ts +50 -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/sync.test.js
CHANGED
|
@@ -1,26 +1,28 @@
|
|
|
1
|
-
import sync, {setRequestPrefilter} from
|
|
1
|
+
import sync, { setRequestPrefilter } from "../lib/sync";
|
|
2
2
|
|
|
3
|
-
import Collection from
|
|
4
|
-
import Model from
|
|
3
|
+
import Collection from "../lib/collection";
|
|
4
|
+
import Model from "../lib/model";
|
|
5
|
+
import { vi } from "vitest";
|
|
5
6
|
|
|
6
7
|
class Library extends Collection {
|
|
7
8
|
url() {
|
|
8
|
-
return
|
|
9
|
+
return "/library";
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
describe(
|
|
13
|
+
describe("sync", () => {
|
|
13
14
|
var library,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
attrs = {
|
|
16
|
+
id: "tempest",
|
|
17
|
+
title: "The Tempest",
|
|
18
|
+
author: "Bill Shakespeare",
|
|
19
|
+
length: 123,
|
|
20
|
+
};
|
|
19
21
|
|
|
20
22
|
beforeEach(() => {
|
|
21
|
-
library = new Library;
|
|
22
|
-
|
|
23
|
-
new Response(
|
|
23
|
+
library = new Library();
|
|
24
|
+
vi.spyOn(window, "fetch").mockResolvedValue(
|
|
25
|
+
new Response(JSON.stringify({ test: "response!" }), { status: 200 })
|
|
24
26
|
);
|
|
25
27
|
});
|
|
26
28
|
|
|
@@ -30,207 +32,218 @@ describe('sync', () => {
|
|
|
30
32
|
setRequestPrefilter((options) => options);
|
|
31
33
|
});
|
|
32
34
|
|
|
33
|
-
it(
|
|
35
|
+
it("read", async () => {
|
|
34
36
|
await library.fetch();
|
|
35
37
|
|
|
36
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
37
|
-
expect(window.fetch.mock.calls[0][1].method).toEqual(
|
|
38
|
-
expect(window.fetch.mock.calls[0][1].params).
|
|
38
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
39
|
+
expect(window.fetch.mock.calls[0][1].method).toEqual("GET");
|
|
40
|
+
expect(window.fetch.mock.calls[0][1].params).toEqual({});
|
|
39
41
|
window.fetch.mockClear();
|
|
40
42
|
|
|
41
|
-
await library.fetch({params: {one:
|
|
42
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
43
|
-
expect(window.fetch.mock.calls[0][1].method).toEqual(
|
|
44
|
-
expect(window.fetch.mock.calls[0][1].params).toEqual({one:
|
|
43
|
+
await library.fetch({ params: { one: "two" } });
|
|
44
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library?one=two");
|
|
45
|
+
expect(window.fetch.mock.calls[0][1].method).toEqual("GET");
|
|
46
|
+
expect(window.fetch.mock.calls[0][1].params).toEqual({ one: "two" });
|
|
45
47
|
window.fetch.mockClear();
|
|
46
48
|
|
|
47
|
-
await library.fetch({url:
|
|
48
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
49
|
-
expect(window.fetch.mock.calls[0][1].method).toEqual(
|
|
50
|
-
expect(window.fetch.mock.calls[0][1].params).toEqual({two:
|
|
49
|
+
await library.fetch({ url: "/library?one=two", params: { two: "three" } });
|
|
50
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library?one=two&two=three");
|
|
51
|
+
expect(window.fetch.mock.calls[0][1].method).toEqual("GET");
|
|
52
|
+
expect(window.fetch.mock.calls[0][1].params).toEqual({ two: "three" });
|
|
51
53
|
window.fetch.mockClear();
|
|
52
54
|
|
|
53
55
|
// safe call with no options, even though this won't work
|
|
54
|
-
expect(async() => sync(library)).not.toThrow();
|
|
55
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
56
|
+
expect(async () => sync(library)).not.toThrow();
|
|
57
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
56
58
|
expect(window.fetch.mock.calls[0][1].method).not.toBeDefined();
|
|
57
|
-
expect(window.fetch.mock.calls[0][1].params).
|
|
59
|
+
expect(window.fetch.mock.calls[0][1].params).toEqual({});
|
|
58
60
|
});
|
|
59
61
|
|
|
60
|
-
it(
|
|
62
|
+
it("passing params", async () => {
|
|
61
63
|
// GET
|
|
62
|
-
await library.fetch({params: {a:
|
|
64
|
+
await library.fetch({ params: { a: "a", one: 1 } });
|
|
63
65
|
|
|
64
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
66
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library?a=a&one=1");
|
|
65
67
|
window.fetch.mockClear();
|
|
66
68
|
|
|
67
69
|
// with body content: already stringified
|
|
68
|
-
await library.add(attrs).at(0).save(null, {params:
|
|
70
|
+
await library.add(attrs).at(0).save(null, { params: "somestring" });
|
|
69
71
|
|
|
70
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
71
|
-
expect(window.fetch.mock.calls[0][1].body).toEqual(
|
|
72
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
73
|
+
expect(window.fetch.mock.calls[0][1].body).toEqual("somestring");
|
|
72
74
|
window.fetch.mockClear();
|
|
73
75
|
|
|
74
76
|
// with body content: as JSON
|
|
75
|
-
await library.at(0).save(null, {params: {a:
|
|
76
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
77
|
-
expect(window.fetch.mock.calls[0][1].body).toEqual(JSON.stringify({a:
|
|
77
|
+
await library.at(0).save(null, { params: { a: "a", one: 1 } });
|
|
78
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
79
|
+
expect(window.fetch.mock.calls[0][1].body).toEqual(JSON.stringify({ a: "a", one: 1 }));
|
|
78
80
|
window.fetch.mockClear();
|
|
79
81
|
|
|
80
82
|
// with body content: not as json
|
|
81
|
-
await library.at(0).save(null, {params: {a:
|
|
82
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
83
|
-
expect(window.fetch.mock.calls[0][1].body).toEqual(
|
|
83
|
+
await library.at(0).save(null, { params: { a: "a", one: 1 }, contentType: "resourcerer/json" });
|
|
84
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
85
|
+
expect(window.fetch.mock.calls[0][1].body).toEqual("a=a&one=1");
|
|
84
86
|
});
|
|
85
87
|
|
|
86
|
-
it(
|
|
88
|
+
it("passes urlOptions to the model to formulate the url path", async () => {
|
|
87
89
|
var librarySectionBook;
|
|
88
90
|
|
|
89
91
|
class LibrarySection extends Collection {
|
|
90
|
-
url({section}) {
|
|
92
|
+
url({ section }) {
|
|
91
93
|
return `/library/${section}`;
|
|
92
94
|
}
|
|
93
95
|
}
|
|
94
96
|
|
|
95
97
|
class LibrarySectionBook extends Model {
|
|
96
|
-
url({section, bookId}) {
|
|
98
|
+
url({ section, bookId }) {
|
|
97
99
|
return `/library/${section}/${bookId}`;
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
|
|
101
|
-
library = new LibrarySection([], {section:
|
|
102
|
-
await library.fetch({params: {a:
|
|
103
|
+
library = new LibrarySection([], { section: "nature" });
|
|
104
|
+
await library.fetch({ params: { a: "a", one: 1 } });
|
|
103
105
|
|
|
104
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
106
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library/nature?a=a&one=1");
|
|
105
107
|
// safe
|
|
106
108
|
library = new LibrarySection();
|
|
107
|
-
await library.fetch({params: {a:
|
|
108
|
-
expect(window.fetch.mock.calls[1][0]).toEqual(
|
|
109
|
+
await library.fetch({ params: { a: "a", one: 1 } });
|
|
110
|
+
expect(window.fetch.mock.calls[1][0]).toEqual("/library/undefined?a=a&one=1");
|
|
109
111
|
|
|
110
|
-
librarySectionBook = new LibrarySectionBook(
|
|
111
|
-
|
|
112
|
-
|
|
112
|
+
librarySectionBook = new LibrarySectionBook(
|
|
113
|
+
{},
|
|
114
|
+
{ section: "nature", bookId: "all-about-frogs" }
|
|
115
|
+
);
|
|
116
|
+
await librarySectionBook.fetch({ params: { a: "a", one: 1 } });
|
|
117
|
+
expect(window.fetch.mock.calls[2][0]).toEqual("/library/nature/all-about-frogs?a=a&one=1");
|
|
113
118
|
|
|
114
119
|
// safe
|
|
115
120
|
librarySectionBook = new LibrarySectionBook();
|
|
116
|
-
await librarySectionBook.fetch({params: {a:
|
|
117
|
-
expect(window.fetch.mock.calls[3][0]).toEqual(
|
|
121
|
+
await librarySectionBook.fetch({ params: { a: "a", one: 1 } });
|
|
122
|
+
expect(window.fetch.mock.calls[3][0]).toEqual("/library/undefined/undefined?a=a&one=1");
|
|
118
123
|
});
|
|
119
124
|
|
|
120
|
-
it(
|
|
121
|
-
|
|
125
|
+
it("create", async () => {
|
|
126
|
+
const { id, ...rest } = attrs;
|
|
127
|
+
|
|
128
|
+
await library.create(rest, { wait: false });
|
|
122
129
|
|
|
123
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
124
|
-
expect(window.fetch.mock.calls[0][1].method).toEqual(
|
|
130
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
131
|
+
expect(window.fetch.mock.calls[0][1].method).toEqual("POST");
|
|
125
132
|
expect(window.fetch.mock.calls[0][1].params).toEqual({
|
|
126
|
-
title:
|
|
127
|
-
author:
|
|
128
|
-
length: 123
|
|
133
|
+
title: "The Tempest",
|
|
134
|
+
author: "Bill Shakespeare",
|
|
135
|
+
length: 123,
|
|
129
136
|
});
|
|
130
137
|
});
|
|
131
138
|
|
|
132
|
-
it(
|
|
139
|
+
it("update", async () => {
|
|
133
140
|
library.add(attrs);
|
|
134
|
-
await library.at(0).save({id:
|
|
141
|
+
await library.at(0).save({ id: "1-the-tempest", author: "William Shakespeare" });
|
|
135
142
|
|
|
136
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
137
|
-
expect(window.fetch.mock.calls[0][1].method).toEqual(
|
|
143
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library/1-the-tempest");
|
|
144
|
+
expect(window.fetch.mock.calls[0][1].method).toEqual("PUT");
|
|
138
145
|
expect(window.fetch.mock.calls[0][1].params).toEqual({
|
|
139
|
-
id:
|
|
140
|
-
title:
|
|
141
|
-
author:
|
|
142
|
-
length: 123
|
|
146
|
+
id: "1-the-tempest",
|
|
147
|
+
title: "The Tempest",
|
|
148
|
+
author: "William Shakespeare",
|
|
149
|
+
length: 123,
|
|
143
150
|
});
|
|
144
151
|
});
|
|
145
152
|
|
|
146
|
-
it(
|
|
153
|
+
it("read model", async () => {
|
|
147
154
|
library.add(attrs);
|
|
148
|
-
await library.at(0).save({id:
|
|
155
|
+
await library.at(0).save({ id: "2-the-tempest", author: "Tim Shakespeare" });
|
|
149
156
|
await library.at(0).fetch();
|
|
150
157
|
|
|
151
|
-
expect(window.fetch.mock.calls[1][0]).toEqual(
|
|
152
|
-
expect(window.fetch.mock.calls[1][1].method).toEqual(
|
|
153
|
-
expect(window.fetch.mock.calls[1][1].params).
|
|
158
|
+
expect(window.fetch.mock.calls[1][0]).toEqual("/library/2-the-tempest");
|
|
159
|
+
expect(window.fetch.mock.calls[1][1].method).toEqual("GET");
|
|
160
|
+
expect(window.fetch.mock.calls[1][1].params).toEqual({});
|
|
154
161
|
});
|
|
155
162
|
|
|
156
|
-
it(
|
|
163
|
+
it("destroy", async () => {
|
|
157
164
|
library.add(attrs);
|
|
158
|
-
await library.at(0).save({id:
|
|
159
|
-
await library.at(0).destroy({wait: true});
|
|
165
|
+
await library.at(0).save({ id: "2-the-tempest", author: "Tim Shakespeare" });
|
|
166
|
+
await library.at(0).destroy({ wait: true });
|
|
160
167
|
|
|
161
|
-
expect(window.fetch.mock.calls[1][0]).toEqual(
|
|
162
|
-
expect(window.fetch.mock.calls[1][1].method).toEqual(
|
|
163
|
-
expect(window.fetch.mock.calls[1][1].params).
|
|
168
|
+
expect(window.fetch.mock.calls[1][0]).toEqual("/library/2-the-tempest");
|
|
169
|
+
expect(window.fetch.mock.calls[1][1].method).toEqual("DELETE");
|
|
170
|
+
expect(window.fetch.mock.calls[1][1].params).toEqual({});
|
|
164
171
|
});
|
|
165
172
|
|
|
166
|
-
it(
|
|
173
|
+
it("rejects non-2xx", async () => {
|
|
167
174
|
window.fetch.mockResolvedValue(
|
|
168
|
-
new Response(
|
|
169
|
-
[JSON.stringify({test: 'womp'})],
|
|
170
|
-
{type: 'application/json'}
|
|
171
|
-
), {status: 500})
|
|
175
|
+
new Response(JSON.stringify({ test: "womp" }), { status: 500, type: "application/json" })
|
|
172
176
|
);
|
|
173
177
|
|
|
174
178
|
try {
|
|
175
179
|
await library.fetch();
|
|
176
180
|
} catch (err) {
|
|
177
|
-
expect(err.json).toEqual({test:
|
|
181
|
+
expect(err.json).toEqual({ test: "womp" });
|
|
178
182
|
}
|
|
179
183
|
});
|
|
180
184
|
|
|
181
|
-
it(
|
|
185
|
+
it("urlError", async () => {
|
|
182
186
|
var model = new Model();
|
|
183
187
|
|
|
184
188
|
expect(() => model.fetch()).toThrow();
|
|
185
189
|
|
|
186
|
-
await model.fetch({url:
|
|
187
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
190
|
+
await model.fetch({ url: "/one/two" });
|
|
191
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/one/two");
|
|
188
192
|
window.fetch.mockClear();
|
|
189
193
|
|
|
190
194
|
// url as property
|
|
191
195
|
class _Model extends Model {
|
|
192
|
-
url =
|
|
196
|
+
url = "/library/one";
|
|
193
197
|
}
|
|
194
198
|
|
|
195
199
|
await new _Model().fetch();
|
|
196
|
-
expect(window.fetch.mock.calls[0][0]).toEqual(
|
|
200
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library/one");
|
|
197
201
|
|
|
198
202
|
// url as undefined
|
|
199
203
|
class __Model extends Model {
|
|
200
|
-
url = undefined
|
|
204
|
+
url = undefined;
|
|
201
205
|
}
|
|
202
206
|
|
|
203
207
|
expect(() => new __Model().fetch()).toThrow();
|
|
204
208
|
});
|
|
205
209
|
|
|
206
|
-
it(
|
|
207
|
-
window.fetch.
|
|
210
|
+
it("default rejected promise callback just rejects again", async () => {
|
|
211
|
+
window.fetch.mockRejectedValueOnce("response");
|
|
208
212
|
library.add(attrs);
|
|
209
213
|
|
|
210
214
|
try {
|
|
211
|
-
await library.at(0).save({id:
|
|
215
|
+
await library.at(0).save({ id: "2-the-tempest", author: "Tim Shakespeare" });
|
|
212
216
|
} catch (err) {
|
|
213
|
-
expect(err).toEqual(
|
|
217
|
+
expect(err).toEqual("response");
|
|
214
218
|
}
|
|
215
219
|
|
|
216
|
-
|
|
220
|
+
const errorMock = vi.fn(() => "override");
|
|
221
|
+
// set status to 400 and fetch will have ok: false
|
|
222
|
+
const errorResponse = new Response(JSON.stringify({ test: "response!" }, null, 2), {
|
|
223
|
+
status: 400,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
window.fetch.mockResolvedValue(errorResponse);
|
|
217
227
|
// now use setRequestPrefilter to add error override
|
|
218
228
|
setRequestPrefilter((options) => ({
|
|
219
229
|
...options,
|
|
220
|
-
error:
|
|
230
|
+
error: errorMock,
|
|
221
231
|
}));
|
|
222
232
|
|
|
223
233
|
try {
|
|
224
|
-
await library.at(0).save({id:
|
|
234
|
+
await library.at(0).save({ id: "2-the-tempest", author: "Tim Shakespeare" });
|
|
225
235
|
} catch (err) {
|
|
226
|
-
expect(
|
|
236
|
+
expect(errorMock).toHaveBeenCalledWith(Object.assign(errorResponse, { json: {} }));
|
|
237
|
+
expect(err).toEqual("override");
|
|
227
238
|
}
|
|
239
|
+
|
|
240
|
+
library.remove(attrs);
|
|
228
241
|
});
|
|
229
242
|
|
|
230
|
-
it(
|
|
243
|
+
it("waits minDuration to resolve if one is passed", async () => {
|
|
231
244
|
var resolved;
|
|
232
245
|
|
|
233
|
-
library.fetch({minDuration: 150}).then(() => resolved = true);
|
|
246
|
+
library.fetch({ minDuration: 150 }).then(() => (resolved = true));
|
|
234
247
|
await new Promise((res) => window.setTimeout(res, 50));
|
|
235
248
|
expect(resolved).not.toBeDefined();
|
|
236
249
|
await new Promise((res) => window.setTimeout(res, 50));
|
|
@@ -239,29 +252,29 @@ describe('sync', () => {
|
|
|
239
252
|
expect(resolved).toBe(true);
|
|
240
253
|
});
|
|
241
254
|
|
|
242
|
-
describe(
|
|
243
|
-
it(
|
|
255
|
+
describe("headers", () => {
|
|
256
|
+
it("can override contentType in options", async () => {
|
|
244
257
|
setRequestPrefilter((options) => ({
|
|
245
258
|
...options,
|
|
246
|
-
contentType:
|
|
259
|
+
contentType: "resourcerer/json",
|
|
247
260
|
}));
|
|
248
261
|
|
|
249
262
|
await library.add(attrs).at(0).save();
|
|
250
263
|
|
|
251
|
-
expect(window.fetch.mock.calls[0][1].headers[
|
|
264
|
+
expect(window.fetch.mock.calls[0][1].headers["Content-Type"]).toEqual("resourcerer/json");
|
|
252
265
|
});
|
|
253
266
|
|
|
254
|
-
it(
|
|
267
|
+
it("can add any header in the 'headers' option", async () => {
|
|
255
268
|
setRequestPrefilter((options) => ({
|
|
256
269
|
...options,
|
|
257
|
-
headers: {Accept:
|
|
270
|
+
headers: { Accept: "resourcerer/json", Authorization: "baaaaaad mofo" },
|
|
258
271
|
}));
|
|
259
272
|
|
|
260
273
|
await library.fetch();
|
|
261
274
|
|
|
262
275
|
expect(window.fetch.mock.calls[0][1].headers).toEqual({
|
|
263
|
-
Accept:
|
|
264
|
-
Authorization:
|
|
276
|
+
Accept: "resourcerer/json",
|
|
277
|
+
Authorization: "baaaaaad mofo",
|
|
265
278
|
});
|
|
266
279
|
});
|
|
267
280
|
});
|