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.
Files changed (59) hide show
  1. package/README.md +133 -164
  2. package/build/index.d.ts +9 -0
  3. package/build/index.js +9 -0
  4. package/build/lib/collection.d.ts +107 -0
  5. package/build/lib/collection.js +251 -0
  6. package/build/lib/config.d.ts +20 -0
  7. package/build/lib/config.js +23 -0
  8. package/build/lib/error-boundary.d.ts +12 -0
  9. package/build/lib/error-boundary.js +18 -0
  10. package/build/lib/events.d.ts +11 -0
  11. package/build/lib/events.js +12 -0
  12. package/build/lib/model-cache.d.ts +13 -0
  13. package/build/lib/model-cache.js +62 -0
  14. package/build/lib/model.d.ts +48 -0
  15. package/build/lib/model.js +170 -0
  16. package/build/lib/prefetch.d.ts +3 -0
  17. package/build/lib/prefetch.js +27 -0
  18. package/build/lib/request.d.ts +20 -0
  19. package/build/lib/request.js +53 -0
  20. package/build/lib/resourcerer.d.ts +13 -0
  21. package/build/lib/resourcerer.js +406 -0
  22. package/build/lib/sync.d.ts +18 -0
  23. package/build/lib/sync.js +58 -0
  24. package/build/lib/types.d.ts +66 -0
  25. package/build/lib/types.js +1 -0
  26. package/build/lib/utils.d.ts +14 -0
  27. package/build/lib/utils.js +87 -0
  28. package/docs/advanced_topics.md +9 -77
  29. package/docs/collection.md +28 -28
  30. package/docs/migrating_to_2.0.md +72 -0
  31. package/docs/model.md +21 -21
  32. package/docs/typescript.md +72 -22
  33. package/package.json +27 -30
  34. package/resourcerer.d.ts +58 -0
  35. package/test/collection.test.js +305 -316
  36. package/test/config.test.js +21 -75
  37. package/test/events.test.js +42 -41
  38. package/test/model-cache.test.js +99 -77
  39. package/test/model.test.js +204 -209
  40. package/test/prefetch.test.js +35 -61
  41. package/test/request.test.js +104 -109
  42. package/test/sync.test.js +122 -109
  43. package/test/utils.test.js +173 -159
  44. package/index.d.ts +0 -270
  45. package/index.js +0 -14
  46. package/lib/collection.js +0 -584
  47. package/lib/config.js +0 -131
  48. package/lib/constants.js +0 -6
  49. package/lib/error-boundary.js +0 -47
  50. package/lib/events.js +0 -19
  51. package/lib/model-cache.js +0 -131
  52. package/lib/model.js +0 -372
  53. package/lib/prefetch.js +0 -59
  54. package/lib/request.js +0 -126
  55. package/lib/resourcerer.js +0 -985
  56. package/lib/sync.js +0 -111
  57. package/lib/utils.js +0 -220
  58. package/test/model-mocks.js +0 -110
  59. package/test/setup.js +0 -63
package/test/sync.test.js CHANGED
@@ -1,26 +1,28 @@
1
- import sync, {setRequestPrefilter} from '../lib/sync';
1
+ import sync, { setRequestPrefilter } from "../lib/sync";
2
2
 
3
- import Collection from '../lib/collection';
4
- import Model from '../lib/model';
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 '/library';
9
+ return "/library";
9
10
  }
10
11
  }
11
12
 
12
- describe('sync', () => {
13
+ describe("sync", () => {
13
14
  var library,
14
- attrs = {
15
- title: 'The Tempest',
16
- author: 'Bill Shakespeare',
17
- length: 123
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
- jest.spyOn(window, 'fetch').mockResolvedValue(
23
- new Response(new Blob([{test: 'response!'}], {type: 'application/json'}), {status: 200})
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('read', async() => {
35
+ it("read", async () => {
34
36
  await library.fetch();
35
37
 
36
- expect(window.fetch.mock.calls[0][0]).toEqual('/library');
37
- expect(window.fetch.mock.calls[0][1].method).toEqual('GET');
38
- expect(window.fetch.mock.calls[0][1].params).not.toBeDefined();
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: 'two'}});
42
- expect(window.fetch.mock.calls[0][0]).toEqual('/library?one=two');
43
- expect(window.fetch.mock.calls[0][1].method).toEqual('GET');
44
- expect(window.fetch.mock.calls[0][1].params).toEqual({one: 'two'});
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: '/library?one=two', params: {two: 'three'}});
48
- expect(window.fetch.mock.calls[0][0]).toEqual('/library?one=two&two=three');
49
- expect(window.fetch.mock.calls[0][1].method).toEqual('GET');
50
- expect(window.fetch.mock.calls[0][1].params).toEqual({two: 'three'});
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('/library');
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).not.toBeDefined();
59
+ expect(window.fetch.mock.calls[0][1].params).toEqual({});
58
60
  });
59
61
 
60
- it('passing params', async() => {
62
+ it("passing params", async () => {
61
63
  // GET
62
- await library.fetch({params: {a: 'a', one: 1}});
64
+ await library.fetch({ params: { a: "a", one: 1 } });
63
65
 
64
- expect(window.fetch.mock.calls[0][0]).toEqual('/library?a=a&one=1');
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: 'somestring'});
70
+ await library.add(attrs).at(0).save(null, { params: "somestring" });
69
71
 
70
- expect(window.fetch.mock.calls[0][0]).toEqual('/library');
71
- expect(window.fetch.mock.calls[0][1].body).toEqual('somestring');
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: 'a', one: 1}});
76
- expect(window.fetch.mock.calls[0][0]).toEqual('/library');
77
- expect(window.fetch.mock.calls[0][1].body).toEqual(JSON.stringify({a: 'a', one: 1}));
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: 'a', one: 1}, contentType: 'resourcerer/json'});
82
- expect(window.fetch.mock.calls[0][0]).toEqual('/library');
83
- expect(window.fetch.mock.calls[0][1].body).toEqual('a=a&one=1');
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('passes urlOptions to the model to formulate the url path', async() => {
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: 'nature'});
102
- await library.fetch({params: {a: 'a', one: 1}});
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('/library/nature?a=a&one=1');
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: 'a', one: 1}});
108
- expect(window.fetch.mock.calls[1][0]).toEqual('/library/undefined?a=a&one=1');
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({}, {section: 'nature', bookId: 'all-about-frogs'});
111
- await librarySectionBook.fetch({params: {a: 'a', one: 1}});
112
- expect(window.fetch.mock.calls[2][0]).toEqual('/library/nature/all-about-frogs?a=a&one=1');
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: 'a', one: 1}});
117
- expect(window.fetch.mock.calls[3][0]).toEqual('/library/undefined/undefined?a=a&one=1');
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('create', async() => {
121
- await library.create(attrs, {wait: false});
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('/library');
124
- expect(window.fetch.mock.calls[0][1].method).toEqual('POST');
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: 'The Tempest',
127
- author: 'Bill Shakespeare',
128
- length: 123
133
+ title: "The Tempest",
134
+ author: "Bill Shakespeare",
135
+ length: 123,
129
136
  });
130
137
  });
131
138
 
132
- it('update', async() => {
139
+ it("update", async () => {
133
140
  library.add(attrs);
134
- await library.at(0).save({id: '1-the-tempest', author: 'William Shakespeare'});
141
+ await library.at(0).save({ id: "1-the-tempest", author: "William Shakespeare" });
135
142
 
136
- expect(window.fetch.mock.calls[0][0]).toEqual('/library/1-the-tempest');
137
- expect(window.fetch.mock.calls[0][1].method).toEqual('PUT');
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: '1-the-tempest',
140
- title: 'The Tempest',
141
- author: 'William Shakespeare',
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('read model', async() => {
153
+ it("read model", async () => {
147
154
  library.add(attrs);
148
- await library.at(0).save({id: '2-the-tempest', author: 'Tim Shakespeare'});
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('/library/2-the-tempest');
152
- expect(window.fetch.mock.calls[1][1].method).toEqual('GET');
153
- expect(window.fetch.mock.calls[1][1].params).not.toBeDefined();
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('destroy', async() => {
163
+ it("destroy", async () => {
157
164
  library.add(attrs);
158
- await library.at(0).save({id: '2-the-tempest', author: 'Tim Shakespeare'});
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('/library/2-the-tempest');
162
- expect(window.fetch.mock.calls[1][1].method).toEqual('DELETE');
163
- expect(window.fetch.mock.calls[1][1].params).not.toBeDefined();
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('rejects non-2xx', async() => {
173
+ it("rejects non-2xx", async () => {
167
174
  window.fetch.mockResolvedValue(
168
- new Response(new Blob(
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: 'womp'});
181
+ expect(err.json).toEqual({ test: "womp" });
178
182
  }
179
183
  });
180
184
 
181
- it('urlError', async() => {
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: '/one/two'});
187
- expect(window.fetch.mock.calls[0][0]).toEqual('/one/two');
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 = '/library/one'
196
+ url = "/library/one";
193
197
  }
194
198
 
195
199
  await new _Model().fetch();
196
- expect(window.fetch.mock.calls[0][0]).toEqual('/library/one');
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('default rejected promise callback just rejects again', async() => {
207
- window.fetch.mockRejectedValue('response');
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: '2-the-tempest', author: 'Tim Shakespeare'});
215
+ await library.at(0).save({ id: "2-the-tempest", author: "Tim Shakespeare" });
212
216
  } catch (err) {
213
- expect(err).toEqual('response');
217
+ expect(err).toEqual("response");
214
218
  }
215
219
 
216
- library.remove(attrs);
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: () => Promise.reject('override')
230
+ error: errorMock,
221
231
  }));
222
232
 
223
233
  try {
224
- await library.at(0).save({id: '2-the-tempest', author: 'Tim Shakespeare'});
234
+ await library.at(0).save({ id: "2-the-tempest", author: "Tim Shakespeare" });
225
235
  } catch (err) {
226
- expect(err).toEqual('override');
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('waits minDuration to resolve if one is passed', async() => {
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('headers', () => {
243
- it('can override contentType in options', async() => {
255
+ describe("headers", () => {
256
+ it("can override contentType in options", async () => {
244
257
  setRequestPrefilter((options) => ({
245
258
  ...options,
246
- contentType: 'resourcerer/json'
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['Content-Type']).toEqual('resourcerer/json');
264
+ expect(window.fetch.mock.calls[0][1].headers["Content-Type"]).toEqual("resourcerer/json");
252
265
  });
253
266
 
254
- it('can add any header in the \'headers\' option', async() => {
267
+ it("can add any header in the 'headers' option", async () => {
255
268
  setRequestPrefilter((options) => ({
256
269
  ...options,
257
- headers: {Accept: 'resourcerer/json', Authorization: 'baaaaaad mofo'}
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: 'resourcerer/json',
264
- Authorization: 'baaaaaad mofo'
276
+ Accept: "resourcerer/json",
277
+ Authorization: "baaaaaad mofo",
265
278
  });
266
279
  });
267
280
  });