Skip to content

Commit b44078c

Browse files
Chantouch SekChantouch Sek
authored andcommitted
chore: 📝 update readme
1 parent d1bc751 commit b44078c

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,43 @@ this.data = data
291291
```
292292
**Note**: Query object above will transform into query string like:
293293

294-
```js
294+
```text
295295
https://my-web-url.com?search[first_name]=Sek&search[last_name]=Chantouch&page[limit]=10&page[offset]=1&order[first_name]=asc&order[last_name]=desc&category_id=6
296296
```
297297

298298
if setParameter that value is empty or null it will remove that param for query string
299299

300+
#### setParameter()
301+
302+
#### Example 1
303+
```js
304+
const proxy = new ExampleProxy()
305+
const { data } = await proxy.setParameter('page', 1).all()
306+
this.data = data
307+
```
308+
Expected will be:
309+
```json
310+
{ "page": 1 }
311+
```
312+
313+
#### Example 2
314+
```js
315+
const proxy = new ExampleProxy()
316+
const queryString = 'limit=10&page=1&search[name]=hello'
317+
const { data } = await proxy.setParameter(queryString).all()
318+
this.data = data
319+
```
320+
Expected will be:
321+
```json
322+
{
323+
"limit": 10,
324+
"page": 1,
325+
"search": {
326+
"name": "hello"
327+
}
328+
}
329+
```
330+
300331
Be sure to use only once in `mounted()` or `asyncData()` and `asyncData()` is only available in `NuxtJs`
301332

302333
## Use proxy in components

src/__tests__/base-proxy.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,21 @@ describe('BaseProxy', () => {
109109
pc: null,
110110
})
111111
const items = [user2, { first_name: 'Chantouch', last_name: 'Sek', id: 2 }]
112-
mockAdapter.onGet('/posts?id=1&first_name=Dara').reply(200, { data: items })
112+
mockAdapter
113+
.onGet('/posts?id=1&last_name=Hok&search[name]=hello&first_name=Dara')
114+
.reply(200, { data: items })
113115
const { data } = await proxy
114-
.setParameter('id=1')
116+
.setParameter('id=1&last_name=Hok&search[name]=hello')
115117
.setParameters({ first_name: 'Dara' })
116118
.all()
119+
expect(proxy.parameters).toEqual({
120+
id: '1',
121+
first_name: 'Dara',
122+
last_name: 'Hok',
123+
search: {
124+
name: 'hello',
125+
},
126+
})
117127
expect(data).toEqual(items)
118128
})
119129
it('it should be able to remove parameter(s)', async () => {

src/core/BaseProxy.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isArray, isFile } from '../util/objects'
33
import type { Errors } from '..'
44
import Validator from './Validator'
55
import { objectToFormData } from '../util/formData'
6-
import qs from 'qs'
6+
import qs, { ParsedQs } from 'qs'
77

88
const validator = Validator
99
const UNPROCESSABLE_ENTITY = 422
@@ -105,16 +105,6 @@ class BaseProxy {
105105
return query ? `${url}?${query}` : url
106106
}
107107

108-
__getQueryString(parameter: string): any | any[] {
109-
const queries: string[] = parameter.split('&')
110-
const obj: any = {}
111-
queries.forEach(function (property: string) {
112-
const [key = null, value = null]: string[] = property.split('=')
113-
obj[key] = value
114-
})
115-
return obj
116-
}
117-
118108
__validateRequestType(requestType: Method): void {
119109
const requestTypes: Array<string> = [
120110
'get',
@@ -182,11 +172,13 @@ class BaseProxy {
182172

183173
setParameter(parameter: string, value?: any): this {
184174
if (!value) {
185-
const params = this.__getQueryString(parameter)
186-
Object.entries(params).forEach(
187-
([key, value]) => (this.parameters[key] = value),
188-
)
189-
return this
175+
const options = {
176+
comma: true,
177+
allowDots: true,
178+
ignoreQueryPrefix: true,
179+
}
180+
const params: ParsedQs = qs.parse(parameter, options)
181+
return this.setParameters(params)
190182
}
191183
this.parameters[parameter] = value
192184
return this

0 commit comments

Comments
 (0)