Skip to content

Commit

Permalink
fix(parser): fix behaviour when stringifying arrays
Browse files Browse the repository at this point in the history
Fixes #214
  • Loading branch information
JoaoPedroAS51 committed Jul 2, 2022
1 parent a8ef142 commit 6098689
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ export default class Builder {

// single entity .select(['age', 'firstname'])
if (typeof fields[0] === 'string' || Array.isArray(fields[0])) {
this.fields[this.model.resource()] = fields.join(',')
this.fields[this.model.resource()] = fields
}

// related entities .select({ posts: ['title', 'content'], user: ['age', 'firstname']} )
if (typeof fields[0] === 'object') {
Object.entries(fields[0]).forEach(([key, value]) => {
this.fields[key] = value.join(',')
this.fields[key] = value
})
}

Expand Down Expand Up @@ -127,11 +127,11 @@ export default class Builder {
}

if (Array.isArray(key)) {
const [_key, _value] = this._nestedFilter(key, array.join(','))
const [_key, _value] = this._nestedFilter(key, array)

this.filters[_key] = { ...this.filters[_key], ..._value }
} else {
this.filters[key] = array.join(',')
this.filters[key] = array
}

return this
Expand Down
14 changes: 11 additions & 3 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ export default class Parser {
}

let fields = { [this.parameterNames().fields]: this.builder.fields }
this.uri += this.prepend() + qs.stringify(fields, { encode: false })
this.uri +=
this.prepend() +
qs.stringify(fields, { encode: false, arrayFormat: 'comma' })
}

filters() {
Expand All @@ -113,7 +115,9 @@ export default class Parser {
}

let filters = { [this.parameterNames().filter]: this.builder.filters }
this.uri += this.prepend() + qs.stringify(filters, { encode: false })
this.uri +=
this.prepend() +
qs.stringify(filters, { encode: false, arrayFormat: 'comma' })
}

sorts() {
Expand Down Expand Up @@ -152,6 +156,10 @@ export default class Parser {
}

this.uri +=
this.prepend() + qs.stringify(this.builder.payload, { encode: false })
this.prepend() +
qs.stringify(this.builder.payload, {
encode: false,
arrayFormat: 'comma'
})
}
}
20 changes: 14 additions & 6 deletions tests/builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ describe('Query builder', () => {
test('whereIn() sets properly the builder', () => {
let post = Post.whereIn('status', ['ACTIVE', 'ARCHIVED'])

expect(post._builder.filters).toEqual({ status: 'ACTIVE,ARCHIVED' })
expect(post._builder.filters).toEqual({ status: ['ACTIVE', 'ARCHIVED'] })

post = Post.whereIn(['user', 'status'], ['active', 'inactive'])

expect(post._builder.filters).toEqual({
user: { status: 'active,inactive' }
user: { status: ['active', 'inactive'] }
})
expect(post._builder.query()).toEqual(
'?filter[user][status]=active,inactive'
Expand All @@ -181,7 +181,10 @@ describe('Query builder', () => {
).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' }
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'
Expand Down Expand Up @@ -241,7 +244,7 @@ describe('Query builder', () => {
test('select() for single entity', () => {
let post = Post.select('age', 'firstname')

expect(post._builder.fields.posts).toEqual('age,firstname')
expect(post._builder.fields.posts).toEqual(['age', 'firstname'])
})

test('select() for related entities', () => {
Expand All @@ -250,14 +253,19 @@ describe('Query builder', () => {
user: ['age', 'firstname']
})

expect(post._builder.fields.posts).toEqual('title,content')
expect(post._builder.fields.user).toEqual('age,firstname')
expect(post._builder.fields.posts).toEqual(['title', 'content'])
expect(post._builder.fields.user).toEqual(['age', 'firstname'])
})

test('params() sets properly the builder', () => {
let post = Post.params({ doSomething: 'yes' })

expect(post._builder.payload).toEqual({ doSomething: 'yes' })

post = Post.params({ foo: 'bar', baz: ['a', 'b'] })

expect(post._builder.payload).toEqual({ foo: 'bar', baz: ['a', 'b'] })
expect(post._builder.query()).toEqual('?foo=bar&baz=a,b')
})

test('params() throws a exception when the payload is not an object', () => {
Expand Down

0 comments on commit 6098689

Please sign in to comment.