fix: get SearchParams string (#17445)

* fix: get SearchParams string

* refactor: use ufo

* lock deps
This commit is contained in:
Stephen Zhou 2024-11-05 11:27:13 +08:00 committed by GitHub
parent 0b813e066a
commit e7526eada4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 14 deletions

View File

@ -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;
}

View File

@ -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');
});
});

View File

@ -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();
}

View File

@ -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",

View File

@ -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