Skip to content

Commit 6baffaf

Browse files
committed
feat(model): add updateMethod option
1 parent efa552b commit 6baffaf

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Diff for: src/Model.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export default class Model extends StaticModel {
5252
return this[this.primaryKey()]
5353
}
5454

55+
updateMethod() {
56+
return 'PUT'
57+
}
58+
5559
custom(...args) {
5660

5761
if (args.length === 0) {
@@ -403,7 +407,7 @@ export default class Model extends StaticModel {
403407
_update() {
404408
return this.request(
405409
this._reqConfig({
406-
method: 'PUT',
410+
method: this.updateMethod(),
407411
url: this.endpoint(),
408412
data: this
409413
})
@@ -429,7 +433,7 @@ export default class Model extends StaticModel {
429433
sync(params) {
430434
return this.request(
431435
this._reqConfig({
432-
method: 'PUT',
436+
method: this.updateMethod(),
433437
url: this.endpoint(),
434438
data: params
435439
})

Diff for: tests/model.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,22 @@ describe('Model methods', () => {
383383
comment.save()
384384
})
385385

386+
test('save() method makes a PATCH request when method is set using `updateMethod`', async () => {
387+
let post
388+
389+
axiosMock.onAny().reply((config) => {
390+
expect(config.method).toEqual('patch')
391+
expect(config.data).toEqual(JSON.stringify(post))
392+
expect(config.url).toEqual('http://localhost/posts/1')
393+
394+
return [200, {}]
395+
})
396+
397+
post = new Post({ id: 1, title: 'Cool!' })
398+
post.updateMethod = () => 'PATCH'
399+
await post.save()
400+
})
401+
386402
test('save() method makes a PATCH request when method is set using `config`', async () => {
387403
let post
388404

0 commit comments

Comments
 (0)