Repository: robsontenorio/vue-api-query
Pull request #155 (2020-12-04 04:11:12)

v1.8.2


Patch:
diff --git a/src/Builder.js b/src/Builder.js
index af127cc..8da09e8 100644
--- a/src/Builder.js
+++ b/src/Builder.js
@@ -83,12 +83,12 @@ export default class Builder {
     }
 
     // single entity .select(['age', 'firstname'])
-    if (fields[0].constructor === String || Array.isArray(fields[0])) {
+    if (typeof fields[0] === 'string' || Array.isArray(fields[0])) {
       this.fields[this.model.resource()] = fields.join(',')
     }
 
     // related entities .select({ posts: ['title', 'content'], user: ['age', 'firstname']} )
-    if (fields[0].constructor === Object) {
+    if (typeof fields[0] === 'object') {
       Object.entries(fields[0]).forEach(([key, value]) => {
         this.fields[key] = value.join(',')
       })
@@ -109,7 +109,7 @@ export default class Builder {
     if (Array.isArray(key)) {
       const [_key, _value] = this._nestedFilter(key, value)
 
-      this.filters[_key] = _value
+      this.filters[_key] = { ...this.filters[_key], ..._value }
     } else {
       this.filters[key] = value
     }
@@ -125,7 +125,7 @@ export default class Builder {
     if (Array.isArray(key)) {
       const [_key, _value] = this._nestedFilter(key, array.join(','))
 
-      this.filters[_key] = _value
+      this.filters[_key] = { ...this.filters[_key], ..._value }
     } else {
       this.filters[key] = array.join(',')
     }
@@ -161,7 +161,7 @@ export default class Builder {
   }
 
   params(payload) {
-    if (payload === undefined || payload.constructor !== Object) {
+    if (payload === undefined || typeof payload !== 'object') {
       throw new Error('You must pass a payload/object as param.')
     }
 


Test patch:
diff --git a/tests/builder.test.js b/tests/builder.test.js
index c100662..6355910 100644
--- a/tests/builder.test.js
+++ b/tests/builder.test.js
@@ -121,6 +121,13 @@ describe('Query builder', () => {
 
     expect(post._builder.filters).toEqual({ user: { status: 'active' } })
     expect(post._builder.query()).toEqual('?filter[user][status]=active')
+
+    post = Post
+      .where(['schedule', 'start'], '2020-11-27')
+      .where(['schedule', 'end'], '2020-11-28')
+
+    expect(post._builder.filters).toEqual({ schedule: { start: '2020-11-27', end: '2020-11-28' } })
+    expect(post._builder.query()).toEqual('?filter[schedule][start]=2020-11-27&filter[schedule][end]=2020-11-28')
   })
 
   test('where() throws a exception when doest not have params or only first param', () => {
@@ -152,7 +159,15 @@ describe('Query builder', () => {
 
     post = Post.whereIn(['user', 'status'], ['active', 'inactive'])
 
+    expect(post._builder.filters).toEqual({ user: { status: 'active,inactive'  } })
     expect(post._builder.query()).toEqual('?filter[user][status]=active,inactive')
+
+    post = Post
+      .whereIn(['schedule', 'start'], ['2020-11-27', '2020-11-28'])
+      .whereIn(['schedule', 'end'], ['2020-11-28', '2020-11-29'])
+
+    expect(post._builder.filters).toEqual({ schedule: { start: '2020-11-27,2020-11-28', end: '2020-11-28,2020-11-29' } })
+    expect(post._builder.query()).toEqual('?filter[schedule][start]=2020-11-27,2020-11-28&filter[schedule][end]=2020-11-28,2020-11-29')
   })
 
   test('whereIn() throws a exception when second parameter is not a array', () => {