diff --git a/src/Builder.js b/src/Builder.js index af127cc3..8da09e88 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.') } diff --git a/tests/builder.test.js b/tests/builder.test.js index c1006622..6355910b 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', () => {