Skip to content

Commit 468eea5

Browse files
authored
Merge pull request #76 from jannishuebl/use_patch_for_updating_in_1.x
use patch for updating a resource
2 parents 5af9d3a + 5001860 commit 468eea5

File tree

6 files changed

+85
-79
lines changed

6 files changed

+85
-79
lines changed

src/model.ts

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

818818
if (this.isPersisted) {
819819
url = this.klass.url(this.id)
820-
verb = "put"
820+
verb = "patch"
821821
}
822822

823823
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/id-map.test.ts

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("ID Map", () => {
2121

2222
fetchMock.get(
2323
"http://example.com/api/v1/authors/1",
24-
responsePayload('John')
24+
responsePayload("John")
2525
)
2626

2727
fetchMock.delete("http://example.com/api/v1/authors/1", {
@@ -57,24 +57,24 @@ describe("ID Map", () => {
5757
it("is added to the ID map", async () => {
5858
let { data } = await Author.find(1)
5959
let stored = ApplicationRecord.store.data
60-
expect(Object.keys(stored)[0]).to.eq('authors-1')
61-
expect(stored['authors-1']).to.deep.eq(data.attributes)
60+
expect(Object.keys(stored)[0]).to.eq("authors-1")
61+
expect(stored["authors-1"]).to.deep.eq(data.attributes)
6262
})
6363

6464
it("syncs with id map", async () => {
6565
let author1 = (await Author.find(1)).data
66-
author1.firstName = 'updated'
66+
author1.firstName = "updated"
6767
let author2 = (await Author.find(1)).data
68-
expect(author1.firstName).to.eq('John')
68+
expect(author1.firstName).to.eq("John")
6969
})
7070

7171
describe("when syncing, then unlistening", () => {
7272
it("no longer syncs with id map", async () => {
7373
let author1 = (await Author.find(1)).data
74-
author1.firstName = 'updated'
74+
author1.firstName = "updated"
7575
author1.unlisten()
7676
let author2 = (await Author.find(1)).data
77-
expect(author1.firstName).to.eq('updated')
77+
expect(author1.firstName).to.eq("updated")
7878
})
7979
})
8080

@@ -89,7 +89,7 @@ describe("ID Map", () => {
8989
// we wouldn't want the user typing in the form to update the grid,
9090
// until the form was saved.
9191
it("syncs multiple instances", async () => {
92-
fetchMock.put("http://example.com/api/v1/authors/1", {
92+
fetchMock.patch("http://example.com/api/v1/authors/1", {
9393
data: {
9494
id: "1",
9595
type: "authors",
@@ -105,17 +105,17 @@ describe("ID Map", () => {
105105
let author2 = response.data
106106

107107
// not synced prior to save
108-
author1.firstName = 'updated'
109-
expect(author2.firstName).to.not.eq('updated')
108+
author1.firstName = "updated"
109+
expect(author2.firstName).to.not.eq("updated")
110110

111111
await author1.save()
112112

113113
// now synced after save
114-
expect(author2.firstName).to.eq('updated')
114+
expect(author2.firstName).to.eq("updated")
115115

116116
// now back to unsynced
117-
author1.firstName = 'updated again'
118-
expect(author2.firstName).to.eq('updated')
117+
author1.firstName = "updated again"
118+
expect(author2.firstName).to.eq("updated")
119119
})
120120
})
121121

@@ -142,48 +142,48 @@ describe("ID Map", () => {
142142
})
143143
})
144144

145-
describe('and associated to a hasMany relationship', () => {
145+
describe("and associated to a hasMany relationship", () => {
146146
let book: Book
147147
let author: Author
148148

149149
beforeEach(() => {
150-
book = new Book({ id: 1, title: 'original' })
150+
book = new Book({ id: 1, title: "original" })
151151
book.isPersisted = true
152152
author = new Author({ id: 1, books: [book] })
153153
})
154154

155155
it("is also updated within the relationship", async () => {
156-
expect(author.books[0].title).to.eq('original')
156+
expect(author.books[0].title).to.eq("original")
157157
let { data } = await Book.find(1)
158-
expect(data.title).to.eq('updated')
159-
expect(author.books[0].title).to.eq('updated')
158+
expect(data.title).to.eq("updated")
159+
expect(author.books[0].title).to.eq("updated")
160160
})
161161
})
162162

163-
describe('and associated to a belongsTo relationship', () => {
163+
describe("and associated to a belongsTo relationship", () => {
164164
let book: Book
165165
let author: Author
166166

167167
beforeEach(() => {
168-
author = new Author({ id: 1, firstName: 'original' })
168+
author = new Author({ id: 1, firstName: "original" })
169169
author.isPersisted = true
170170
book = new Book({ id: 1, author })
171171
book.isPersisted = true
172172
})
173173

174174
it("is also updated within the relationship", async () => {
175-
expect(book.author.firstName).to.eq('original')
175+
expect(book.author.firstName).to.eq("original")
176176
await Author.find(1)
177-
expect(book.author.firstName).to.eq('John')
177+
expect(book.author.firstName).to.eq("John")
178178
})
179179
})
180180

181-
describe('and associated to a hasOne relationship', () => {
181+
describe("and associated to a hasOne relationship", () => {
182182
let author: Author
183183
let bio: Bio
184184

185185
beforeEach(() => {
186-
bio = new Bio({ id: 1, description: 'original' })
186+
bio = new Bio({ id: 1, description: "original" })
187187
bio.isPersisted = true
188188
author = new Author({ id: 1, bio })
189189
author.isPersisted = true
@@ -199,14 +199,14 @@ describe("ID Map", () => {
199199

200200
describe("when destroyed via sideposting", () => {
201201
beforeEach(async () => {
202-
fetchMock.put("http://example.com/api/v1/authors/1", {
202+
fetchMock.patch("http://example.com/api/v1/authors/1", {
203203
data: {
204204
id: "1",
205205
type: "authors"
206206
}
207207
})
208208

209-
fetchMock.put("http://example.com/api/books/1", {
209+
fetchMock.patch("http://example.com/api/books/1", {
210210
data: {
211211
id: "1",
212212
type: "books"
@@ -224,7 +224,7 @@ describe("ID Map", () => {
224224
author = new Author({ id: 1, books: [book] })
225225
author.isPersisted = true
226226
book.isMarkedForDestruction = true
227-
await author.save({ with: 'books' })
227+
await author.save({ with: "books" })
228228
})
229229

230230
it("is removed from the ID map + relationship", async () => {
@@ -244,7 +244,7 @@ describe("ID Map", () => {
244244
book = new Book({ id: 1, author })
245245
book.isPersisted = true
246246
author.isMarkedForDestruction = true
247-
await book.save({ with: 'author' })
247+
await book.save({ with: "author" })
248248
})
249249

250250
it("is removed from the ID map", async () => {
@@ -264,7 +264,7 @@ describe("ID Map", () => {
264264
author = new Author({ id: 1, bio })
265265
author.isPersisted = true
266266
bio.isMarkedForDestruction = true
267-
await author.save({ with: 'bio' })
267+
await author.save({ with: "bio" })
268268
})
269269

270270
it("is removed from the ID map", async () => {
@@ -277,14 +277,14 @@ describe("ID Map", () => {
277277

278278
describe("when disassociated via sideposting", () => {
279279
beforeEach(async () => {
280-
fetchMock.put("http://example.com/api/v1/authors/1", {
280+
fetchMock.patch("http://example.com/api/v1/authors/1", {
281281
data: {
282282
id: "1",
283283
type: "authors"
284284
}
285285
})
286286

287-
fetchMock.put("http://example.com/api/books/1", {
287+
fetchMock.patch("http://example.com/api/books/1", {
288288
data: {
289289
id: "1",
290290
type: "books"
@@ -302,7 +302,7 @@ describe("ID Map", () => {
302302
author = new Author({ id: 1, books: [book] })
303303
author.isPersisted = true
304304
book.isMarkedForDisassociation = true
305-
await author.save({ with: 'books' })
305+
await author.save({ with: "books" })
306306
})
307307

308308
it("is still in the store, but removed from the relation", async () => {
@@ -322,7 +322,7 @@ describe("ID Map", () => {
322322
book = new Book({ id: 1, author })
323323
book.isPersisted = true
324324
author.isMarkedForDisassociation = true
325-
await book.save({ with: 'author' }) || this
325+
;(await book.save({ with: "author" })) || this
326326
})
327327

328328
it("is still in the store, but removed from the relation", async () => {
@@ -342,7 +342,7 @@ describe("ID Map", () => {
342342
author = new Author({ id: 1, bio })
343343
author.isPersisted = true
344344
bio.isMarkedForDisassociation = true
345-
await author.save({ with: 'bio' })
345+
await author.save({ with: "bio" })
346346
})
347347

348348
it("is still in the store, but removed from the relation", async () => {
@@ -361,27 +361,27 @@ describe("ID Map", () => {
361361
expect(ApplicationRecord.store.count).to.eq(0)
362362
})
363363

364-
describe('and associated to a hasMany relationship', () => {
364+
describe("and associated to a hasMany relationship", () => {
365365
let book: Book
366366
let author: Author
367367

368368
beforeEach(() => {
369369
author = new Author({ id: 1 })
370370
author.isPersisted = true
371-
book = new Book({ id: 1})
371+
book = new Book({ id: 1 })
372372
book.isPersisted = true
373373
author.books = [book]
374374
})
375375

376-
it('is no longer returned in the relationship', async () => {
376+
it("is no longer returned in the relationship", async () => {
377377
expect(author.books.length).to.eq(1)
378378
await book.destroy()
379379
expect(ApplicationRecord.store.find(book)).to.eq(undefined)
380380
expect(author.books.length).to.eq(0)
381381
})
382382
})
383383

384-
describe('and associated to a belongsTo relationship', () => {
384+
describe("and associated to a belongsTo relationship", () => {
385385
let author: Author
386386
let book: Book
387387

@@ -392,14 +392,14 @@ describe("ID Map", () => {
392392
book.isPersisted = true
393393
})
394394

395-
it('is no longer returned in the relationship', async () => {
395+
it("is no longer returned in the relationship", async () => {
396396
expect(book.author).to.not.eq(undefined)
397397
await author.destroy()
398398
expect(book.author).to.eq(undefined)
399399
})
400400
})
401401

402-
describe('and associated to a hasOne relationship', () => {
402+
describe("and associated to a hasOne relationship", () => {
403403
let author: Author
404404
let bio: Bio
405405

@@ -410,11 +410,11 @@ describe("ID Map", () => {
410410
author.isPersisted = true
411411
})
412412

413-
it('is no longer returned in the relationship', async () => {
413+
it("is no longer returned in the relationship", async () => {
414414
expect(author.bio).to.not.eq(undefined)
415415
await bio.destroy()
416416
expect(author.bio).to.eq(undefined)
417417
})
418418
})
419419
})
420-
})
420+
})

0 commit comments

Comments
 (0)