Skip to content

Commit f31d84f

Browse files
Merge pull request #42 from suth:json-paginate
Added ability to customize query parameter names
2 parents ddd2896 + bd3f627 commit f31d84f

File tree

5 files changed

+76
-25
lines changed

5 files changed

+76
-25
lines changed

src/Builder.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ export default class Builder {
1515
this.limitValue = null
1616
this.payload = null
1717

18-
this.fields = {
19-
fields: {}
20-
}
21-
this.filters = {
22-
filter: {}
23-
}
18+
this.fields = {}
19+
this.filters = {}
2420

2521
this.parser = new Parser(this)
2622
}
@@ -53,13 +49,13 @@ export default class Builder {
5349

5450
// single entity .select(['age', 'firstname'])
5551
if (fields[0].constructor === String || fields[0].constructor === Array) {
56-
this.fields.fields[this.model.resource()] = fields.join(',')
52+
this.fields[this.model.resource()] = fields.join(',')
5753
}
5854

5955
// related entities .select({ posts: ['title', 'content'], user: ['age', 'firstname']} )
6056
if (fields[0].constructor === Object) {
6157
Object.entries(fields[0]).forEach(([key, value]) => {
62-
this.fields.fields[key] = value.join(',')
58+
this.fields[key] = value.join(',')
6359
})
6460
}
6561

@@ -73,7 +69,7 @@ export default class Builder {
7369
if (Array.isArray(value) || value instanceof Object)
7470
throw new Error('The VALUE must be primitive on where() method.')
7571

76-
this.filters.filter[key] = value
72+
this.filters[key] = value
7773

7874
return this
7975
}
@@ -82,7 +78,7 @@ export default class Builder {
8278
if (!Array.isArray(array))
8379
throw new Error('The second argument on whereIn() method must be an array.')
8480

85-
this.filters.filter[key] = array.join(',')
81+
this.filters[key] = array.join(',')
8682

8783
return this
8884
}

src/Model.js

+12
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ export default class Model extends StaticModel {
119119
}
120120
}
121121

122+
parameterNames () {
123+
return {
124+
include: 'include',
125+
filter: 'filter',
126+
sort: 'sort',
127+
fields: 'fields',
128+
append: 'append',
129+
page: 'page',
130+
limit: 'limit'
131+
}
132+
}
133+
122134
/**
123135
* Query
124136
*/

src/Parser.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ export default class Parser {
3838
}
3939

4040
hasFields () {
41-
return Object.keys(this.builder.fields.fields).length > 0
41+
return Object.keys(this.builder.fields).length > 0
4242
}
4343

4444
hasFilters () {
45-
return Object.keys(this.builder.filters.filter).length > 0
45+
return Object.keys(this.builder.filters).length > 0
4646
}
4747

4848
hasSorts () {
@@ -65,6 +65,10 @@ export default class Parser {
6565
return (this.uri === '') ? '?' : '&'
6666
}
6767

68+
parameterNames () {
69+
return this.builder.model.parameterNames()
70+
}
71+
6872
/**
6973
* Parsers
7074
*/
@@ -74,55 +78,57 @@ export default class Parser {
7478
return
7579
}
7680

77-
this.uri += this.prepend() + 'include=' + this.builder.includes
81+
this.uri += this.prepend() + this.parameterNames().include + '=' + this.builder.includes
7882
}
7983

8084
appends () {
8185
if (!this.hasAppends()) {
8286
return
8387
}
8488

85-
this.uri += this.prepend() + 'append=' + this.builder.appends
89+
this.uri += this.prepend() + this.parameterNames().append + '=' + this.builder.appends
8690
}
8791

8892
fields () {
8993
if (!this.hasFields()) {
9094
return
9195
}
9296

93-
this.uri += this.prepend() + qs.stringify(this.builder.fields, { encode: false })
97+
let fields = { [this.parameterNames().fields]: this.builder.fields }
98+
this.uri += this.prepend() + qs.stringify(fields, { encode: false })
9499
}
95100

96101
filters () {
97102
if (!this.hasFilters()) {
98103
return
99104
}
100105

101-
this.uri += this.prepend() + qs.stringify(this.builder.filters, { encode: false })
106+
let filters = { [this.parameterNames().filter]: this.builder.filters }
107+
this.uri += this.prepend() + qs.stringify(filters, { encode: false })
102108
}
103109

104110
sorts () {
105111
if (!this.hasSorts()) {
106112
return
107113
}
108114

109-
this.uri += this.prepend() + 'sort=' + this.builder.sorts
115+
this.uri += this.prepend() + this.parameterNames().sort + '=' + this.builder.sorts
110116
}
111117

112118
page () {
113119
if (!this.hasPage()) {
114120
return
115121
}
116122

117-
this.uri += this.prepend() + 'page=' + this.builder.pageValue
123+
this.uri += this.prepend() + this.parameterNames().page + '=' + this.builder.pageValue
118124
}
119125

120126
limit () {
121127
if (!this.hasLimit()) {
122128
return
123129
}
124130

125-
this.uri += this.prepend() + 'limit=' + this.builder.limitValue
131+
this.uri += this.prepend() + this.parameterNames().limit + '=' + this.builder.limitValue
126132
}
127133

128134
payload () {

tests/builder.test.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Post from './dummy/models/Post'
2+
import ModelWithParamNames from './dummy/models/ModelWithParamNames';
23
import { Model } from '../src'
34
import axios from 'axios'
45
import MockAdapter from 'axios-mock-adapter';
@@ -36,6 +37,25 @@ describe('Query builder', () => {
3637
expect(post._builder.query()).toEqual(query)
3738
})
3839

40+
test('it builds a complex query with custom param names', () => {
41+
const post = ModelWithParamNames
42+
.include('user')
43+
.append('likes')
44+
.select({
45+
posts: ['title', 'content'],
46+
user: ['age', 'firstname']
47+
})
48+
.where('title', 'Cool')
49+
.where('status', 'ACTIVE')
50+
.page(3)
51+
.limit(10)
52+
.orderBy('created_at')
53+
54+
const query = '?include_custom=user&append_custom=likes&fields_custom[posts]=title,content&fields_custom[user]=age,firstname&filter_custom[title]=Cool&filter_custom[status]=ACTIVE&sort_custom=created_at&page_custom=3&limit_custom=10'
55+
56+
expect(post._builder.query()).toEqual(query)
57+
})
58+
3959
test('include() sets properly the builder', () => {
4060
let post = Post.include('user')
4161

@@ -69,11 +89,11 @@ describe('Query builder', () => {
6989
test('where() sets properly the builder', () => {
7090
let post = Post.where('id', 1)
7191

72-
expect(post._builder.filters.filter).toEqual({ id: 1 })
92+
expect(post._builder.filters).toEqual({ id: 1 })
7393

7494
post = Post.where('id', 1).where('title', 'Cool')
7595

76-
expect(post._builder.filters.filter).toEqual({ id: 1, title: 'Cool' })
96+
expect(post._builder.filters).toEqual({ id: 1, title: 'Cool' })
7797
})
7898

7999
test('where() throws a exception when doest not have params or only first param', () => {
@@ -101,7 +121,7 @@ describe('Query builder', () => {
101121
test('whereIn() sets properly the builder', () => {
102122
let post = Post.whereIn('status', ['ACTIVE', 'ARCHIVED'])
103123

104-
expect(post._builder.filters.filter).toEqual({ status: 'ACTIVE,ARCHIVED' })
124+
expect(post._builder.filters).toEqual({ status: 'ACTIVE,ARCHIVED' })
105125
})
106126

107127
test('whereIn() throws a exception when second parameter is not a array', () => {
@@ -152,7 +172,7 @@ describe('Query builder', () => {
152172
test('select() for single entity', () => {
153173
let post = Post.select('age', 'firstname')
154174

155-
expect(post._builder.fields.fields.posts).toEqual('age,firstname')
175+
expect(post._builder.fields.posts).toEqual('age,firstname')
156176
})
157177

158178
test('select() for related entities', () => {
@@ -161,8 +181,8 @@ describe('Query builder', () => {
161181
user: ['age', 'firstname']
162182
})
163183

164-
expect(post._builder.fields.fields.posts).toEqual('title,content')
165-
expect(post._builder.fields.fields.user).toEqual('age,firstname')
184+
expect(post._builder.fields.posts).toEqual('title,content')
185+
expect(post._builder.fields.user).toEqual('age,firstname')
166186
})
167187

168188
test('params() sets properly the builder', () => {
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import BaseModel from './BaseModel'
2+
3+
export default class ModelWithParamNames extends BaseModel {
4+
5+
parameterNames () {
6+
return {
7+
include: 'include_custom',
8+
filter: 'filter_custom',
9+
sort: 'sort_custom',
10+
fields: 'fields_custom',
11+
append: 'append_custom',
12+
page: 'page_custom',
13+
limit: 'limit_custom'
14+
}
15+
}
16+
17+
}

0 commit comments

Comments
 (0)