Skip to content

Commit 4e220b8

Browse files
committed
use patch
1 parent 54f6612 commit 4e220b8

File tree

5 files changed

+43
-37
lines changed

5 files changed

+43
-37
lines changed

src/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ export class JSORMBase {
783783

784784
if (this.isPersisted) {
785785
url = this.klass.url(this.id)
786-
verb = "put"
786+
verb = "patch"
787787
}
788788

789789
const json = payload.asJSON()

src/request.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ export class Request {
3434
return this._fetchWithLogging(url, options)
3535
}
3636

37-
put(
37+
patch(
3838
url: string,
3939
payload: JsonapiRequestDoc,
4040
options: RequestInit
4141
): Promise<any> {
42-
options.method = "PUT"
42+
options.method = "PATCH"
4343
options.body = JSON.stringify(payload)
4444

4545
return this._fetchWithLogging(url, options)
@@ -98,8 +98,12 @@ export class Request {
9898
return response
9999
}
100100

101-
private async _handleResponse(response: Response, requestOptions: RequestInit) {
102-
let wasDelete = requestOptions.method === 'DELETE' &&
101+
private async _handleResponse(
102+
response: Response,
103+
requestOptions: RequestInit
104+
) {
105+
let wasDelete =
106+
requestOptions.method === "DELETE" &&
103107
[202, 204, 200].indexOf(response.status) > -1
104108
if (wasDelete) return
105109

test/integration/dirty-tracking.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ describe("Dirty tracking", () => {
2323

2424
beforeEach(() => {
2525
let url = "http://example.com/api/v1/authors"
26-
fetchMock.post(url, responsePayload('John'))
27-
fetchMock.put(`${url}/1`, responsePayload('Jake'))
26+
fetchMock.post(url, responsePayload("John"))
27+
fetchMock.patch(`${url}/1`, responsePayload("Jake"))
2828
})
2929

3030
describe("when persisted, dirty, updated", () => {
3131
it("calls reset()", async () => {
32-
let instance = new Author({ firstName: 'John' })
32+
let instance = new Author({ firstName: "John" })
3333
await instance.save()
3434
expect(instance.isPersisted).to.eq(true)
3535
expect(instance.isDirty()).to.eq(false)
36-
instance.firstName = 'Jake'
36+
instance.firstName = "Jake"
3737
expect(instance.isDirty()).to.eq(true)
3838
let spy = sinon.spy()
3939
instance.reset = spy
4040
await instance.save()
4141
expect(spy.callCount).to.eq(2)
4242
})
4343
})
44-
})
44+
})

test/integration/nested-persistence.test.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { JsonapiRequestDoc, JsonapiResponseDoc } from "../../src/index"
55

66
let instance: Author
77
let payloads: JsonapiRequestDoc[]
8-
let putPayloads: JsonapiRequestDoc[]
8+
let patchPayloads: JsonapiRequestDoc[]
99
let deletePayloads: object[]
1010
let serverResponse: JsonapiResponseDoc
1111

@@ -19,10 +19,13 @@ const resetMocks = () => {
1919
return serverResponse
2020
})
2121

22-
fetchMock.put("http://example.com/api/v1/authors/1", (url, payload: any) => {
23-
putPayloads.push(JSON.parse(payload.body))
24-
return serverResponse
25-
})
22+
fetchMock.patch(
23+
"http://example.com/api/v1/authors/1",
24+
(url, payload: any) => {
25+
patchPayloads.push(JSON.parse(payload.body))
26+
return serverResponse
27+
}
28+
)
2629

2730
fetchMock.delete(
2831
"http://example.com/api/v1/authors/1",
@@ -155,7 +158,7 @@ const seedPersistedData = () => {
155158
describe("nested persistence", () => {
156159
beforeEach(() => {
157160
payloads = []
158-
putPayloads = []
161+
patchPayloads = []
159162
deletePayloads = []
160163
instance = new Author({ firstName: "Stephen" })
161164
serverResponse = {
@@ -290,7 +293,7 @@ describe("nested persistence", () => {
290293
it("sends the correct payload", async () => {
291294
await instance.save({ with: { books: "genre" } })
292295

293-
expect(putPayloads[0]).to.deep.equal(expectedUpdatePayload("update"))
296+
expect(patchPayloads[0]).to.deep.equal(expectedUpdatePayload("update"))
294297
})
295298
})
296299

@@ -304,7 +307,7 @@ describe("nested persistence", () => {
304307
instance.books[0].genre.isMarkedForDestruction = true
305308
await instance.save({ with: { books: "genre" } })
306309

307-
expect(putPayloads[0]).to.deep.equal(expectedUpdatePayload("destroy"))
310+
expect(patchPayloads[0]).to.deep.equal(expectedUpdatePayload("destroy"))
308311
})
309312

310313
it("removes the associated has_many data", async () => {
@@ -332,7 +335,7 @@ describe("nested persistence", () => {
332335
instance.books[0].genre.isMarkedForDisassociation = true
333336
await instance.save({ with: { books: "genre" } })
334337

335-
expect(putPayloads[0]).to.deep.equal(
338+
expect(patchPayloads[0]).to.deep.equal(
336339
expectedUpdatePayload("disassociate")
337340
)
338341
})
@@ -352,7 +355,7 @@ describe("nested persistence", () => {
352355
})
353356
})
354357

355-
describe('when sideposting only id', () => {
358+
describe("when sideposting only id", () => {
356359
beforeEach(() => {
357360
let genre = new Genre({ id: 1 })
358361
genre.isPersisted = true
@@ -362,8 +365,8 @@ describe("nested persistence", () => {
362365
instance.books = [book]
363366
})
364367

365-
describe('one level', () => {
366-
it('adds only resource identifier to the payload', async () => {
368+
describe("one level", () => {
369+
it("adds only resource identifier to the payload", async () => {
367370
await instance.save({ with: ["books.id"] })
368371
expect((<any>payloads)[0].data.relationships).to.deep.eq({
369372
books: {
@@ -385,8 +388,8 @@ describe("nested persistence", () => {
385388
})
386389
})
387390

388-
describe('2 levels only id', () => {
389-
it('sends only relationships, no attributes', async () => {
391+
describe("2 levels only id", () => {
392+
it("sends only relationships, no attributes", async () => {
390393
await instance.save({ with: { ["books.id"]: "genre.id" } })
391394

392395
expect((<any>payloads)[0].data.relationships).to.deep.eq({
@@ -422,7 +425,7 @@ describe("nested persistence", () => {
422425
])
423426
})
424427

425-
describe('when relationships are not dirty', () => {
428+
describe("when relationships are not dirty", () => {
426429
beforeEach(() => {
427430
instance.books[0].isPersisted = true
428431
instance.books[0].genre.isPersisted = true
@@ -453,8 +456,8 @@ describe("nested persistence", () => {
453456
})
454457
})
455458

456-
describe('one level id, then full payload', () => {
457-
it('should only send relationships in the payload', async () => {
459+
describe("one level id, then full payload", () => {
460+
it("should only send relationships in the payload", async () => {
458461
await instance.save({ with: { ["books.id"]: "genre" } })
459462

460463
expect((<any>payloads)[0].included).to.deep.eq([
@@ -480,4 +483,4 @@ describe("nested persistence", () => {
480483
})
481484
})
482485
})
483-
})
486+
})

test/integration/persistence.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ after(() => {
88

99
let instance: Person
1010
let payloads: JsonapiRequestDoc[]
11-
let putPayloads: JsonapiRequestDoc[]
11+
let patchPayloads: JsonapiRequestDoc[]
1212
let deletePayloads: object[]
1313
let serverResponse: JsonapiResponseDoc
1414

1515
beforeEach(() => {
1616
payloads = []
17-
putPayloads = []
17+
patchPayloads = []
1818
deletePayloads = []
1919
instance = new Person()
2020
serverResponse = {
@@ -34,8 +34,8 @@ const resetMocks = () => {
3434
return serverResponse
3535
})
3636

37-
fetchMock.put("http://example.com/api/v1/people/1", (url, payload: any) => {
38-
putPayloads.push(JSON.parse(payload.body))
37+
fetchMock.patch("http://example.com/api/v1/people/1", (url, payload: any) => {
38+
patchPayloads.push(JSON.parse(payload.body))
3939
return serverResponse
4040
})
4141

@@ -75,7 +75,7 @@ describe("Model persistence", () => {
7575
instance.firstName = "Joe"
7676
await instance.save()
7777

78-
expect(putPayloads[0]).to.deep.equal({
78+
expect(patchPayloads[0]).to.deep.equal({
7979
data: {
8080
id: "1",
8181
type: "people",
@@ -104,7 +104,7 @@ describe("Model persistence", () => {
104104
it("does not send attributes to the server", async () => {
105105
await instance.save()
106106

107-
expect(putPayloads[0]).to.deep.equal({
107+
expect(patchPayloads[0]).to.deep.equal({
108108
data: {
109109
id: "1",
110110
type: "people"
@@ -274,15 +274,14 @@ describe("Model persistence", () => {
274274
resetMocks()
275275
})
276276

277-
it('does not blow up', async () => {
277+
it("does not blow up", async () => {
278278
expect(instance.isPersisted).to.eq(true)
279279
await instance.destroy()
280280

281281
expect(instance.isPersisted).to.eq(false)
282282
})
283283
})
284284

285-
286285
describe("when the server returns 202 accepted", () => {
287286
beforeEach(() => {
288287
fetchMock.restore()
@@ -297,7 +296,7 @@ describe("Model persistence", () => {
297296
resetMocks()
298297
})
299298

300-
it('does not blow up', async () => {
299+
it("does not blow up", async () => {
301300
expect(instance.isPersisted).to.eq(true)
302301
await instance.destroy()
303302

0 commit comments

Comments
 (0)