Skip to content

Commit e30a8c0

Browse files
committed
refactor: 使用query-string代替qs
1 parent 39d3314 commit e30a8c0

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"object-to-formdata": "^4.5.1",
8484
"openapi-types": "^12.1.3",
8585
"prettier": "^3.2.5",
86-
"qs": "^6.12.3",
86+
"query-string": "^9.0.0",
8787
"tsx": "^4.16.2",
8888
"yaml": "^2.4.5"
8989
},

pnpm-lock.yaml

Lines changed: 31 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import qs from 'qs';
1+
import qs from 'query-string';
22
import objectToFormData from 'object-to-formdata';
33

44
export const utils = {
@@ -8,7 +8,7 @@ export const utils = {
88
uriConcatQuery(uri: string, query: Record<string, any> | undefined) {
99
if (!query) return uri;
1010

11-
const querystring = qs.stringify(query);
11+
const querystring = qs.stringify(query, { arrayFormat: 'bracket' });
1212
if (!querystring) return uri;
1313

1414
if (uri.includes('?')) {

test/util.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect, test } from 'vitest';
2+
import { utils } from '../src/utils';
3+
4+
test('查询字符串', () => {
5+
const result = utils.uriConcatQuery('/users', {
6+
foo: ['bar', 'baz'],
7+
age: 3,
8+
});
9+
expect(result).toMatchInlineSnapshot(`"/users?age=3&foo[]=bar&foo[]=baz"`);
10+
});
11+
12+
test('拼接符号', () => {
13+
expect(utils.uriConcatQuery('/users?', { foo: 'bar' })).toMatchInlineSnapshot(
14+
`"/users?foo=bar"`,
15+
);
16+
expect(utils.uriConcatQuery('/users?ok', { foo: 'bar' })).toMatchInlineSnapshot(
17+
`"/users?ok&foo=bar"`,
18+
);
19+
});

0 commit comments

Comments
 (0)