Skip to content

Commit eef91b1

Browse files
authoredJan 19, 2021
fix(model): throw an error if operations are used with custom (#173)
Throw an error if CRUD or relationship operations are used in in conjunction with the `custom` method. Fixes #172
1 parent c4644c2 commit eef91b1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
 

‎src/Model.js

+16
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ export default class Model extends StaticModel {
423423
*/
424424

425425
delete() {
426+
if (this._customResource) {
427+
throw Error("The delete() method cannot be used in conjunction with the custom() method. Use for() instead.")
428+
}
429+
426430
if (!this.hasId()) {
427431
throw new Error('This model has a empty ID.')
428432
}
@@ -436,6 +440,10 @@ export default class Model extends StaticModel {
436440
}
437441

438442
save() {
443+
if (this._customResource) {
444+
throw Error("The save() method cannot be used in conjunction with the custom() method. Use for() instead.")
445+
}
446+
439447
return this.hasId() ? this._update() : this._create()
440448
}
441449

@@ -472,6 +480,10 @@ export default class Model extends StaticModel {
472480
*/
473481

474482
attach(params) {
483+
if (this._customResource) {
484+
throw Error("The attach() method cannot be used in conjunction with the custom() method. Use for() instead.")
485+
}
486+
475487
return this.request(
476488
this._reqConfig({
477489
method: 'POST',
@@ -482,6 +494,10 @@ export default class Model extends StaticModel {
482494
}
483495

484496
sync(params) {
497+
if (this._customResource) {
498+
throw Error("The sync() method cannot be used in conjunction with the custom() method. Use for() instead.")
499+
}
500+
485501
return this.request(
486502
this._reqConfig({
487503
method: 'PUT',

‎tests/model.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,38 @@ describe('Model methods', () => {
960960
expect(errorModel).toThrow('Arguments to custom() must be strings or instances of Model.')
961961
})
962962

963+
test('it throws an error when CRUD and relationship operations are used in conjunction with custom()', () => {
964+
errorModel = () => {
965+
const post = new Post({ text: 'Hello' }).custom('foo/bar').save()
966+
}
967+
968+
expect(errorModel).toThrow("The save() method cannot be used in conjunction with the custom() method. Use for() instead.")
969+
970+
errorModel = () => {
971+
const post = new Post({ id: 1 }).custom('foo/bar').delete()
972+
}
973+
974+
expect(errorModel).toThrow("The delete() method cannot be used in conjunction with the custom() method. Use for() instead.")
975+
976+
errorModel = () => {
977+
const post = new Post({ id: 1 })
978+
const comment = post.comments().custom('foo/bar').attach({
979+
text: 'Awesome post!'
980+
})
981+
}
982+
983+
expect(errorModel).toThrow("The attach() method cannot be used in conjunction with the custom() method. Use for() instead.")
984+
985+
errorModel = () => {
986+
const post = new Post({ id: 1 })
987+
const comment = post.comments().custom('foo/bar').sync({
988+
text: 'Awesome post!'
989+
})
990+
}
991+
992+
expect(errorModel).toThrow("The sync() method cannot be used in conjunction with the custom() method. Use for() instead.")
993+
})
994+
963995
test('save() method makes a PUT request to the correct URL on nested object thas was fetched with find() method', async () => {
964996
axiosMock.onGet('http://localhost/posts/1/comments/1').reply(200, commentsResponse[0])
965997
axiosMock.onPut('http://localhost/posts/1/comments/1').reply(200, commentsResponse[0])

0 commit comments

Comments
 (0)
Please sign in to comment.