Skip to content

Commit 6098689

Browse files
committed
fix(parser): fix behaviour when stringifying arrays
Fixes #214
1 parent a8ef142 commit 6098689

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/Builder.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ export default class Builder {
8686

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

9292
// related entities .select({ posts: ['title', 'content'], user: ['age', 'firstname']} )
9393
if (typeof fields[0] === 'object') {
9494
Object.entries(fields[0]).forEach(([key, value]) => {
95-
this.fields[key] = value.join(',')
95+
this.fields[key] = value
9696
})
9797
}
9898

@@ -127,11 +127,11 @@ export default class Builder {
127127
}
128128

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

132132
this.filters[_key] = { ...this.filters[_key], ..._value }
133133
} else {
134-
this.filters[key] = array.join(',')
134+
this.filters[key] = array
135135
}
136136

137137
return this

src/Parser.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ export default class Parser {
104104
}
105105

106106
let fields = { [this.parameterNames().fields]: this.builder.fields }
107-
this.uri += this.prepend() + qs.stringify(fields, { encode: false })
107+
this.uri +=
108+
this.prepend() +
109+
qs.stringify(fields, { encode: false, arrayFormat: 'comma' })
108110
}
109111

110112
filters() {
@@ -113,7 +115,9 @@ export default class Parser {
113115
}
114116

115117
let filters = { [this.parameterNames().filter]: this.builder.filters }
116-
this.uri += this.prepend() + qs.stringify(filters, { encode: false })
118+
this.uri +=
119+
this.prepend() +
120+
qs.stringify(filters, { encode: false, arrayFormat: 'comma' })
117121
}
118122

119123
sorts() {
@@ -152,6 +156,10 @@ export default class Parser {
152156
}
153157

154158
this.uri +=
155-
this.prepend() + qs.stringify(this.builder.payload, { encode: false })
159+
this.prepend() +
160+
qs.stringify(this.builder.payload, {
161+
encode: false,
162+
arrayFormat: 'comma'
163+
})
156164
}
157165
}

tests/builder.test.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ describe('Query builder', () => {
164164
test('whereIn() sets properly the builder', () => {
165165
let post = Post.whereIn('status', ['ACTIVE', 'ARCHIVED'])
166166

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

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

171171
expect(post._builder.filters).toEqual({
172-
user: { status: 'active,inactive' }
172+
user: { status: ['active', 'inactive'] }
173173
})
174174
expect(post._builder.query()).toEqual(
175175
'?filter[user][status]=active,inactive'
@@ -181,7 +181,10 @@ describe('Query builder', () => {
181181
).whereIn(['schedule', 'end'], ['2020-11-28', '2020-11-29'])
182182

183183
expect(post._builder.filters).toEqual({
184-
schedule: { start: '2020-11-27,2020-11-28', end: '2020-11-28,2020-11-29' }
184+
schedule: {
185+
start: ['2020-11-27', '2020-11-28'],
186+
end: ['2020-11-28', '2020-11-29']
187+
}
185188
})
186189
expect(post._builder.query()).toEqual(
187190
'?filter[schedule][start]=2020-11-27,2020-11-28&filter[schedule][end]=2020-11-28,2020-11-29'
@@ -241,7 +244,7 @@ describe('Query builder', () => {
241244
test('select() for single entity', () => {
242245
let post = Post.select('age', 'firstname')
243246

244-
expect(post._builder.fields.posts).toEqual('age,firstname')
247+
expect(post._builder.fields.posts).toEqual(['age', 'firstname'])
245248
})
246249

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

253-
expect(post._builder.fields.posts).toEqual('title,content')
254-
expect(post._builder.fields.user).toEqual('age,firstname')
256+
expect(post._builder.fields.posts).toEqual(['title', 'content'])
257+
expect(post._builder.fields.user).toEqual(['age', 'firstname'])
255258
})
256259

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

260263
expect(post._builder.payload).toEqual({ doSomething: 'yes' })
264+
265+
post = Post.params({ foo: 'bar', baz: ['a', 'b'] })
266+
267+
expect(post._builder.payload).toEqual({ foo: 'bar', baz: ['a', 'b'] })
268+
expect(post._builder.query()).toEqual('?foo=bar&baz=a,b')
261269
})
262270

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

0 commit comments

Comments
 (0)