resourcerer 2.1.1 → 2.1.3
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/model.js +1 -1
- package/build/lib/sync.js +7 -4
- package/package.json +1 -1
- package/test/model.test.js +9 -0
- package/test/sync.test.js +4 -3
package/build/lib/model.js
CHANGED
|
@@ -118,7 +118,7 @@ export default class Model extends Events {
|
|
|
118
118
|
this.isNew() ? "POST"
|
|
119
119
|
: options.patch ? "PATCH"
|
|
120
120
|
: "PUT";
|
|
121
|
-
if (options.method === "PATCH"
|
|
121
|
+
if (options.method === "PATCH") {
|
|
122
122
|
options.attrs = attrs;
|
|
123
123
|
}
|
|
124
124
|
return this.sync(this, options)
|
package/build/lib/sync.js
CHANGED
|
@@ -5,9 +5,10 @@ let prefilter = (x) => x;
|
|
|
5
5
|
export default function (model, options = {}) {
|
|
6
6
|
return ajax({
|
|
7
7
|
contentType: MIME_TYPE_JSON,
|
|
8
|
-
params: options.params ||
|
|
9
|
-
options.
|
|
10
|
-
|
|
8
|
+
params: options.params ||
|
|
9
|
+
(["POST", "PATCH", "PUT"].includes(options.method || "") ?
|
|
10
|
+
options.attrs || model.toJSON()
|
|
11
|
+
: {}),
|
|
11
12
|
url: options.url || result(model, "url", model.urlOptions) || urlError(),
|
|
12
13
|
error: (response) => response,
|
|
13
14
|
...options,
|
|
@@ -37,7 +38,9 @@ export function ajax(options) {
|
|
|
37
38
|
...options,
|
|
38
39
|
headers: {
|
|
39
40
|
Accept: MIME_TYPE_JSON,
|
|
40
|
-
...(hasBodyContent
|
|
41
|
+
...(hasBodyContent && !(options.params instanceof FormData) ?
|
|
42
|
+
{ "Content-Type": options.contentType }
|
|
43
|
+
: {}),
|
|
41
44
|
...options.headers,
|
|
42
45
|
},
|
|
43
46
|
...(hasBodyContent ?
|
package/package.json
CHANGED
package/test/model.test.js
CHANGED
|
@@ -287,6 +287,15 @@ describe("Model", () => {
|
|
|
287
287
|
method: "PATCH",
|
|
288
288
|
attrs: { one: "one" },
|
|
289
289
|
});
|
|
290
|
+
sync.default.mockClear();
|
|
291
|
+
await model.save({ one: "one" }, { patch: true, wait: true });
|
|
292
|
+
expect(sync.default).toHaveBeenCalledWith(model, {
|
|
293
|
+
parse: true,
|
|
294
|
+
patch: true,
|
|
295
|
+
wait: true,
|
|
296
|
+
method: "PATCH",
|
|
297
|
+
attrs: { one: "one" },
|
|
298
|
+
});
|
|
290
299
|
});
|
|
291
300
|
|
|
292
301
|
it("parses results unless `parse` is set to false", async () => {
|
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 {
|