diff --git a/lib/utils/got.ts b/lib/utils/got.ts index cf03e74b2..372fe125b 100644 --- a/lib/utils/got.ts +++ b/lib/utils/got.ts @@ -1,9 +1,6 @@ import { destr } from 'destr'; import ofetch from '@/utils/ofetch'; - -function isObject(o) { - return o !== null && typeof o === 'object' && Array.isArray(o) === false; -} +import { getSearchParamsString } from './helpers'; const getFakeGot = (defaultOptions?: any) => { const fakeGot = (request, options?: any) => { @@ -39,15 +36,7 @@ const getFakeGot = (defaultOptions?: any) => { delete options.form; } if (options?.searchParams) { - request += - '?' + - new URLSearchParams( - isObject(options.searchParams) - ? Object.entries(options.searchParams) - .filter(([, value]) => value !== undefined) - .map(([key, value]) => [key, String(value)]) - : options.searchParams - ).toString(); + request += '?' + getSearchParamsString(options.searchParams); delete options.searchParams; } diff --git a/lib/utils/helpers.test.ts b/lib/utils/helpers.test.ts index 4870e54fc..23ac83ca0 100644 --- a/lib/utils/helpers.test.ts +++ b/lib/utils/helpers.test.ts @@ -1,8 +1,20 @@ import { describe, expect, it } from 'vitest'; -import { getRouteNameFromPath } from '@/utils/helpers'; +import { getRouteNameFromPath, getSearchParamsString } from '@/utils/helpers'; describe('helpers', () => { it('getRouteNameFromPath', () => { expect(getRouteNameFromPath('/test/1')).toBe('test'); }); + + it('getSearchParamsString', () => { + expect(getSearchParamsString({ a: 1, b: 2 })).toBe('a=1&b=2'); + expect(getSearchParamsString({ a: 1, b: undefined })).toBe('a=1'); + expect(getSearchParamsString({ a: undefined })).toBe(''); + expect(getSearchParamsString({})).toBe(''); + + const searchParams = new URLSearchParams(); + searchParams.append('ids[]', '1'); + searchParams.append('ids[]', '2'); + expect(getSearchParamsString(searchParams)).toBe('ids%5B%5D=1&ids%5B%5D=2'); + }); }); diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index ff78e73a1..bc222464e 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -1,5 +1,6 @@ import { fileURLToPath } from 'url'; import path from 'node:path'; +import { stringifyQuery } from 'ufo'; export const getRouteNameFromPath = (path: string) => { const p = path.split('/').filter(Boolean); @@ -30,3 +31,12 @@ export const getCurrentPath = (metaUrl: string) => { const __filename = path.join(fileURLToPath(metaUrl)); return path.dirname(__filename); }; + +function isPureObject(o: any) { + return Object.prototype.toString.call(o) === '[object Object]'; +} + +export function getSearchParamsString(searchParams: any) { + const searchParamsString = isPureObject(searchParams) ? stringifyQuery(searchParams) : null; + return searchParamsString ?? new URLSearchParams(searchParams).toString(); +} diff --git a/package.json b/package.json index 9f12932d9..873a0616b 100644 --- a/package.json +++ b/package.json @@ -129,6 +129,7 @@ "tough-cookie": "5.0.0", "tsx": "4.19.2", "twitter-api-v2": "1.18.1", + "ufo": "1.5.4", "undici": "6.20.1", "uuid": "11.0.2", "winston": "3.16.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8a39acbba..0e60ee97c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -245,6 +245,9 @@ importers: twitter-api-v2: specifier: 1.18.1 version: 1.18.1 + ufo: + specifier: 1.5.4 + version: 1.5.4 undici: specifier: 6.20.1 version: 6.20.1