Skip to content

Commit

Permalink
Merge pull request #155 from robsontenorio/dev
Browse files Browse the repository at this point in the history
Release v1.8.2
  • Loading branch information
JoaoPedroAS51 authored Dec 4, 2020
2 parents fc716dd + 644ab4e commit 90fe3f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')
})
Expand All @@ -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
}
Expand All @@ -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(',')
}
Expand Down Expand Up @@ -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.')
}

Expand Down
15 changes: 15 additions & 0 deletions tests/builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 90fe3f3

Please sign in to comment.