Skip to content

Commit

Permalink
refactor: 使用query-string代替qs
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Jul 20, 2024
1 parent fc3d5fa commit eb9fe0e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"object-to-formdata": "^4.5.1",
"openapi-types": "^12.1.3",
"prettier": "^3.2.5",
"qs": "^6.12.3",
"query-string": "^9.0.0",
"tsx": "^4.16.2",
"yaml": "^2.4.5"
},
Expand Down
34 changes: 31 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import qs from 'qs';
import qs from 'query-string';
import objectToFormData from 'object-to-formdata';

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

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

if (uri.includes('?')) {
Expand Down
19 changes: 19 additions & 0 deletions test/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, test } from 'vitest';
import { utils } from '../src/utils';

test('查询字符串', () => {
const result = utils.uriConcatQuery('/users', {
foo: ['bar', 'baz'],
age: 3,
});
expect(result).toMatchInlineSnapshot(`"/users?age=3&foo[]=bar&foo[]=baz"`);
});

test('拼接符号', () => {
expect(utils.uriConcatQuery('/users?', { foo: 'bar' })).toMatchInlineSnapshot(
`"/users?foo=bar"`,
);
expect(utils.uriConcatQuery('/users?ok', { foo: 'bar' })).toMatchInlineSnapshot(
`"/users?ok&foo=bar"`,
);
});

0 comments on commit eb9fe0e

Please sign in to comment.