Skip to content

Commit efa552b

Browse files
committed
feat(model): add config method
1 parent ebfd9b5 commit efa552b

File tree

2 files changed

+118
-32
lines changed

2 files changed

+118
-32
lines changed

Diff for: src/Model.js

+61-32
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export default class Model extends StaticModel {
3535
return Model.$http
3636
}
3737

38+
config(config = {}) {
39+
this._config = config
40+
return this
41+
}
42+
3843
resource() {
3944
return `${this.constructor.name.toLowerCase()}s`
4045
}
@@ -276,6 +281,16 @@ export default class Model extends StaticModel {
276281
}
277282
}
278283

284+
_reqConfig(config = {}, options = { forceMethod: false }) {
285+
const _config = { ...config, ...this._config }
286+
287+
if (options.forceMethod) {
288+
_config.method = config.method
289+
}
290+
291+
return _config
292+
}
293+
279294
first() {
280295
return this.get().then(response => {
281296
let item
@@ -303,10 +318,12 @@ export default class Model extends StaticModel {
303318
let base = this._fromResource || `${this.baseURL()}/${this.resource()}`
304319
let url = `${base}/${identifier}${this._builder.query()}`
305320

306-
return this.request({
307-
url,
308-
method: 'GET'
309-
}).then(response => {
321+
return this.request(
322+
this._reqConfig({
323+
url,
324+
method: 'GET'
325+
})
326+
).then(response => {
310327
return this._applyInstance(response.data)
311328
})
312329
}
@@ -326,10 +343,12 @@ export default class Model extends StaticModel {
326343
base = this._customResource ? `${this.baseURL()}/${this._customResource}` : base
327344
let url = `${base}${this._builder.query()}`
328345

329-
return this.request({
330-
url,
331-
method: 'GET'
332-
}).then(response => {
346+
return this.request(
347+
this._reqConfig({
348+
url,
349+
method: 'GET'
350+
})
351+
).then(response => {
333352
let collection = this._applyInstanceCollection(response.data)
334353

335354
if (response.data.data !== undefined) {
@@ -357,32 +376,38 @@ export default class Model extends StaticModel {
357376
throw new Error('This model has a empty ID.')
358377
}
359378

360-
return this.request({
361-
url: this.endpoint(),
362-
method: 'DELETE'
363-
}).then(response => response)
379+
return this.request(
380+
this._reqConfig({
381+
method: 'DELETE',
382+
url: this.endpoint()
383+
})
384+
).then(response => response)
364385
}
365386

366387
save() {
367388
return this.hasId() ? this._update() : this._create()
368389
}
369390

370391
_create() {
371-
return this.request({
372-
method: 'POST',
373-
url: this.endpoint(),
374-
data: this
375-
}).then(response => {
392+
return this.request(
393+
this._reqConfig({
394+
method: 'POST',
395+
url: this.endpoint(),
396+
data: this
397+
}, { forceMethod: true })
398+
).then(response => {
376399
return this._applyInstance(response.data.data || response.data)
377400
})
378401
}
379402

380403
_update() {
381-
return this.request({
382-
method: 'PUT',
383-
url: this.endpoint(),
384-
data: this
385-
}).then(response => {
404+
return this.request(
405+
this._reqConfig({
406+
method: 'PUT',
407+
url: this.endpoint(),
408+
data: this
409+
})
410+
).then(response => {
386411
return this._applyInstance(response.data.data || response.data)
387412
})
388413
}
@@ -392,18 +417,22 @@ export default class Model extends StaticModel {
392417
*/
393418

394419
attach(params) {
395-
return this.request({
396-
method: 'POST',
397-
url: this.endpoint(),
398-
data: params
399-
}).then(response => response)
420+
return this.request(
421+
this._reqConfig({
422+
method: 'POST',
423+
url: this.endpoint(),
424+
data: params
425+
})
426+
).then(response => response)
400427
}
401428

402429
sync(params) {
403-
return this.request({
404-
method: 'PUT',
405-
url: this.endpoint(),
406-
data: params
407-
}).then(response => response)
430+
return this.request(
431+
this._reqConfig({
432+
method: 'PUT',
433+
url: this.endpoint(),
434+
data: params
435+
})
436+
).then(response => response)
408437
}
409438
}

Diff for: tests/model.test.js

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

386+
test('save() method makes a PATCH request when method is set using `config`', 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+
await post.config({ method: 'PATCH' }).save()
399+
})
400+
401+
test('save() method makes a POST request even when `config` set to PATCH', async () => {
402+
let post
403+
const _postResponse = {
404+
id: 1,
405+
title: 'Cool!',
406+
text: 'Lorem Ipsum Dolor',
407+
user: {
408+
firstname: 'John',
409+
lastname: 'Doe',
410+
age: 25
411+
},
412+
relationships: {
413+
tags: [
414+
{
415+
name: 'super'
416+
},
417+
{
418+
name: 'awesome'
419+
}
420+
]
421+
}
422+
}
423+
424+
axiosMock.onAny().reply((config) => {
425+
expect(config.method).toEqual('post')
426+
expect(config.data).toEqual(JSON.stringify(post))
427+
expect(config.url).toEqual('http://localhost/posts')
428+
429+
return [200, _postResponse]
430+
})
431+
432+
post = new Post({ title: 'Cool!' })
433+
post = await post.config({ method: 'PATCH' }).save()
434+
435+
expect(post).toEqual(_postResponse)
436+
expect(post).toBeInstanceOf(Post)
437+
expect(post.user).toBeInstanceOf(User)
438+
post.relationships.tags.forEach(tag => {
439+
expect(tag).toBeInstanceOf(Tag)
440+
})
441+
})
442+
386443
test('save() method makes a POST request when ID of object is null', async () => {
387444
let post
388445

0 commit comments

Comments
 (0)