Repository: robsontenorio/vue-api-query
Pull request #109 (2020-05-12 01:15:12)

Handle 'data' wrapped responses for Create and Update
Laravel allows data wrapping for all responses, so create and update requests should support this if it is done.

codecov[bot]: # [Codecov](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109?src=pr&el=h1) Report
> Merging [#109](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109?src=pr&el=desc) into [master](https://codecov.io/gh/robsontenorio/vue-api-query/commit/fa8f83fdb6283810d7ce24c84d99a9cb5deb995f&el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109/graphs/tree.svg?width=650&height=150&src=pr&token=PAbNhAJuWF)](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master      #109   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          268       268           
  Branches        50        52    +2     
=========================================
  Hits           268       268           
```


| [Impacted Files](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/Model.js](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109/diff?src=pr&el=tree#diff-c3JjL01vZGVsLmpz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109?src=pr&el=footer). Last update [fa8f83f...482046f](https://codecov.io/gh/robsontenorio/vue-api-query/pull/109?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

robsontenorio: Thanks for PR. Could you please include tests?
francoism90: @robsontenorio I would like this feature, maybe I could help contributing some tests. Do you maybe have an example?

Patch:
diff --git a/src/Model.js b/src/Model.js
index 8ac1ad2..8f1bbe7 100644
--- a/src/Model.js
+++ b/src/Model.js
@@ -138,7 +138,7 @@ export default class Model extends StaticModel {
   }
 
   isValidId(id) {
-    return id !== undefined && id !== 0 && id !== ''
+    return id !== undefined && id !== 0 && id !== '' && id !== null
   }
 
   endpoint() {
@@ -370,7 +370,7 @@ export default class Model extends StaticModel {
       url: this.endpoint(),
       data: this
     }).then(response => {
-      return this._applyInstance(response.data)
+      return this._applyInstance(response.data.data || response.data)
     })
   }
 
@@ -380,7 +380,7 @@ export default class Model extends StaticModel {
       url: this.endpoint(),
       data: this
     }).then(response => {
-      return this._applyInstance(response.data)
+      return this._applyInstance(response.data.data || response.data)
     })
   }
 


Test patch:
diff --git a/tests/model.test.js b/tests/model.test.js
index 29a59c2..59fccb3 100644
--- a/tests/model.test.js
+++ b/tests/model.test.js
@@ -320,6 +320,22 @@ describe('Model methods', () => {
     comment.save()
   })
 
+  test('save() method makes a POST request when ID of object is null', async () => {
+    let post
+
+    axiosMock.onAny().reply((config) => {
+      expect(config.method).toEqual('post')
+      expect(config.data).toEqual(JSON.stringify(post))
+      expect(config.url).toEqual('http://localhost/posts')
+
+      return [200, {}]
+    })
+
+    post = new Post({ id: null, title: 'Cool!' })
+    await post.save()
+
+  })
+
   test('a request from delete() method hits the right resource', async () => {
 
     axiosMock.onAny().reply((config) => {