diff --git a/package.json b/package.json index 9eb5e03..fece9a6 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6c16a69..1dae3b7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,9 +23,9 @@ importers: prettier: specifier: ^3.2.5 version: 3.2.5 - qs: - specifier: ^6.12.3 - version: 6.12.3 + query-string: + specifier: ^9.0.0 + version: 9.0.0 tsx: specifier: ^4.16.2 version: 4.16.2 @@ -2412,6 +2412,10 @@ packages: supports-color: optional: true + decode-uri-component@0.4.1: + resolution: {integrity: sha512-+8VxcR21HhTy8nOt6jf20w0c9CADrw1O8d+VZ/YzzCt4bJ3uBjw+D1q2osAB8RnpwwaeYBxy0HyKQxD5JBMuuQ==} + engines: {node: '>=14.16'} + decompress-response@6.0.0: resolution: {integrity: sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==} engines: {node: '>=10'} @@ -2650,6 +2654,10 @@ packages: resolution: {integrity: sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==} engines: {node: '>=8'} + filter-obj@5.1.0: + resolution: {integrity: sha512-qWeTREPoT7I0bifpPUXtxkZJ1XJzxWtfoWWkdVGqa+eCr3SHW/Ocp89o8vLvbUuQnadybJpjOKu4V+RwO6sGng==} + engines: {node: '>=14.16'} + find-cache-dir@2.1.0: resolution: {integrity: sha512-Tq6PixE0w/VMFfCgbONnkiQIVol/JJL7nRMi20fqzA4NRs9AfeqMGeRdPi3wIhYkxjeBaWh2rxwapn5Tu3IqOQ==} engines: {node: '>=6'} @@ -3803,6 +3811,10 @@ packages: resolution: {integrity: sha512-AWJm14H1vVaO/iNZ4/hO+HyaTehuy9nRqVdkTqlJt0HWvBiBIEXFmb4C0DGeYo3Xes9rrEW+TxHsaigCbN5ICQ==} engines: {node: '>=0.6'} + query-string@9.0.0: + resolution: {integrity: sha512-4EWwcRGsO2H+yzq6ddHcVqkCQ2EFUSfDMEjF8ryp8ReymyZhIuaFRGLomeOQLkrzacMHoyky2HW0Qe30UbzkKw==} + engines: {node: '>=18'} + queue-microtask@1.2.3: resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==} @@ -4087,6 +4099,10 @@ packages: spdx-license-ids@3.0.18: resolution: {integrity: sha512-xxRs31BqRYHwiMzudOrpSiHtZ8i/GeionCBDSilhYRj+9gIcI8wCZTlXZKu9vZIVqViP3dcp9qE5G6AlIaD+TQ==} + split-on-first@3.0.0: + resolution: {integrity: sha512-qxQJTx2ryR0Dw0ITYyekNQWpz6f8dGd7vffGNflQQ3Iqj9NJ6qiZ7ELpZsJ/QBhIVAiDfXdag3+Gp8RvWa62AA==} + engines: {node: '>=12'} + split2@4.2.0: resolution: {integrity: sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==} engines: {node: '>= 10.x'} @@ -7014,6 +7030,8 @@ snapshots: dependencies: ms: 2.1.2 + decode-uri-component@0.4.1: {} + decompress-response@6.0.0: dependencies: mimic-response: 3.1.0 @@ -7382,6 +7400,8 @@ snapshots: dependencies: to-regex-range: 5.0.1 + filter-obj@5.1.0: {} + find-cache-dir@2.1.0: dependencies: commondir: 1.0.1 @@ -8470,6 +8490,12 @@ snapshots: dependencies: side-channel: 1.0.6 + query-string@9.0.0: + dependencies: + decode-uri-component: 0.4.1 + filter-obj: 5.1.0 + split-on-first: 3.0.0 + queue-microtask@1.2.3: {} quick-lru@5.1.1: {} @@ -8798,6 +8824,8 @@ snapshots: spdx-license-ids@3.0.18: {} + split-on-first@3.0.0: {} + split2@4.2.0: {} sprintf-js@1.1.3: {} diff --git a/src/utils.ts b/src/utils.ts index 582596d..7b8eaf9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import qs from 'qs'; +import qs from 'query-string'; import objectToFormData from 'object-to-formdata'; export const utils = { @@ -8,7 +8,7 @@ export const utils = { uriConcatQuery(uri: string, query: Record | undefined) { if (!query) return uri; - const querystring = qs.stringify(query); + const querystring = qs.stringify(query, { arrayFormat: 'bracket' }); if (!querystring) return uri; if (uri.includes('?')) { diff --git a/test/util.test.ts b/test/util.test.ts new file mode 100644 index 0000000..1ac2551 --- /dev/null +++ b/test/util.test.ts @@ -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"`, + ); +});