Skip to content

Commit cfb4dfa

Browse files
Chantouch SekChantouch Sek
authored andcommitted
feat(query): 🚀 accept object as query string
1 parent 62a82c7 commit cfb4dfa

File tree

7 files changed

+209
-24493
lines changed

7 files changed

+209
-24493
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"sourceType": "module"
1515
// "project": "./tsconfig.json"
1616
},
17-
"plugins": ["@typescript-eslint", "prettier"],
17+
"plugins": ["@typescript-eslint", "prettier", "promise", "import"],
1818
"rules": {
1919
"prettier/prettier": ["error"],
2020
"@typescript-eslint/no-explicit-any": 0,

package-lock.json

Lines changed: 0 additions & 24396 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-api-queries",
3-
"version": "0.0.15",
3+
"version": "0.0.16",
44
"description": "Elegant and simple way to build requests for REST API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -58,12 +58,13 @@
5858
"@types/jest": "^26.0.14",
5959
"@types/lodash.merge": "^4.6.6",
6060
"@types/node": "^14.11.10",
61-
"@typescript-eslint/eslint-plugin": "^4.4.1",
61+
"@typescript-eslint/eslint-plugin": "^4.15.1",
6262
"@typescript-eslint/parser": "^4.4.1",
6363
"axios-mock-adapter": "^1.18.2",
6464
"bootstrap-vue": "^2.17.3",
65-
"eslint": "^7.11.0",
65+
"eslint": "^7.2.0",
6666
"eslint-config-prettier": "^6.13.0",
67+
"eslint-plugin-import": "^2.22.1",
6768
"eslint-plugin-prettier": "^3.1.4",
6869
"husky": "^4.3.0",
6970
"jest": "^26.5.3",
@@ -85,6 +86,7 @@
8586
"camelcase-keys": "^6.2.2",
8687
"escape-string-regexp": "^4.0.0",
8788
"lodash.merge": "^4.6.2",
89+
"qs": "^6.9.6",
8890
"snakecase-keys": "^3.2.0"
8991
}
9092
}

src/__tests__/base-proxy.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ describe('BaseProxy', () => {
120120
.all()
121121
expect(data).toEqual(items)
122122
})
123+
it('it should accept query params as object', async () => {
124+
const items = [
125+
{ first_name: 'Dara', last_name: 'Hok', id: 1 },
126+
{ first_name: 'Chantouch', last_name: 'Sek', id: 2 },
127+
]
128+
mockAdapter
129+
.onGet('/posts?search[id]=1&first_name=Dara')
130+
.reply(200, { data: items })
131+
const params = {
132+
search: { id: 1 },
133+
first_name: 'Dara',
134+
last_name: 'Hok',
135+
}
136+
const { data } = await proxy
137+
.setParameters(params)
138+
.removeParameters(['last_name'])
139+
.all()
140+
expect(data).toEqual(items)
141+
expect(proxy.parameters).toEqual({ search: { id: 1 }, first_name: 'Dara' })
142+
})
123143

124144
it('it should find an item by id', async () => {
125145
const item = { first_name: 'Chantouch', last_name: 'Sek', id: 1 }

src/core/BaseProxy.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { AxiosError, AxiosInstance, AxiosResponse, Method } from 'axios'
2-
import { isFile, isArray } from '../util/objects'
3-
import type { Errors } from '../'
2+
import { isArray, isFile } from '../util/objects'
3+
import type { Errors } from '..'
44
import Validator from './Validator'
55
import { objectToFormData } from '../util/formData'
6+
import qs from 'qs'
67

78
const validator = Validator
89
const UNPROCESSABLE_ENTITY = 422
@@ -12,9 +13,13 @@ export interface ParametersType {
1213

1314
class BaseProxy {
1415
public errors: Errors
16+
1517
public parameters: any | any[]
18+
1619
public readonly endpoint: string
20+
1721
public static $http: AxiosInstance
22+
1823
public static $errorsKeyName = 'errors'
1924

2025
constructor(endpoint: string, parameters?: any | any[]) {
@@ -52,7 +57,7 @@ class BaseProxy {
5257
}
5358

5459
putWithFile(id: string | number, payload: any): Promise<any> {
55-
payload['_method'] = 'put'
60+
payload._method = 'put'
5661
return this.submit('post', `/${this.endpoint}/${id}`, payload)
5762
}
5863

@@ -96,11 +101,8 @@ class BaseProxy {
96101
}
97102

98103
__getParameterString(url: string): string {
99-
const keys = Object.keys(this.parameters)
100-
const parameters = keys
101-
.filter((key: string) => !!this.parameters[key])
102-
.map((key: string) => `${key}=${this.parameters[key]}`)
103-
return parameters.length === 0 ? url : `${url}?${parameters.join('&')}`
104+
const query = qs.stringify(this.parameters, { encode: false })
105+
return query ? `${url}?${query}` : url
104106
}
105107

106108
__getQueryString(parameter: string): any | any[] {

src/util/formData.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ export function objectToFormData(
77
return formData.append(parent, object)
88
}
99
for (const property in object) {
10-
if (object.hasOwnProperty(property)) {
11-
appendToFormData(formData, getKey(parent, property), object[property])
10+
if (!object.hasOwnProperty(property)) {
11+
continue
1212
}
13+
appendToFormData(formData, getKey(parent, property), object[property])
1314
}
1415
return formData
1516
}

0 commit comments

Comments
 (0)