chore: trim request query dependency
This commit is contained in:
@@ -5,13 +5,47 @@ import type { RequestClientConfig, RequestClientOptions } from './types';
|
||||
import { bindMethods, isString, merge } from '@vben/utils';
|
||||
|
||||
import axios from 'axios';
|
||||
import qs from 'qs';
|
||||
|
||||
import { FileDownloader } from './modules/downloader';
|
||||
import { InterceptorManager } from './modules/interceptor';
|
||||
import { SSE } from './modules/sse';
|
||||
import { FileUploader } from './modules/uploader';
|
||||
|
||||
function stringifyQuery(
|
||||
params: Record<string, any>,
|
||||
arrayFormat: 'brackets' | 'comma' | 'indices' | 'repeat',
|
||||
) {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
for (const [key, value] of Object.entries(params ?? {})) {
|
||||
if (value === undefined || value === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Array.isArray(value)) {
|
||||
searchParams.append(key, value);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arrayFormat === 'comma') {
|
||||
searchParams.append(key, value.join(','));
|
||||
continue;
|
||||
}
|
||||
|
||||
value.forEach((item, index) => {
|
||||
const arrayKey =
|
||||
arrayFormat === 'brackets'
|
||||
? `${key}[]`
|
||||
: arrayFormat === 'indices'
|
||||
? `${key}[${index}]`
|
||||
: key;
|
||||
searchParams.append(arrayKey, item);
|
||||
});
|
||||
}
|
||||
|
||||
return searchParams.toString();
|
||||
}
|
||||
|
||||
function getParamsSerializer(
|
||||
paramsSerializer: RequestClientOptions['paramsSerializer'],
|
||||
) {
|
||||
@@ -19,17 +53,17 @@ function getParamsSerializer(
|
||||
switch (paramsSerializer) {
|
||||
case 'brackets': {
|
||||
return (params: any) =>
|
||||
qs.stringify(params, { arrayFormat: 'brackets' });
|
||||
stringifyQuery(params, 'brackets');
|
||||
}
|
||||
case 'comma': {
|
||||
return (params: any) => qs.stringify(params, { arrayFormat: 'comma' });
|
||||
return (params: any) => stringifyQuery(params, 'comma');
|
||||
}
|
||||
case 'indices': {
|
||||
return (params: any) =>
|
||||
qs.stringify(params, { arrayFormat: 'indices' });
|
||||
stringifyQuery(params, 'indices');
|
||||
}
|
||||
case 'repeat': {
|
||||
return (params: any) => qs.stringify(params, { arrayFormat: 'repeat' });
|
||||
return (params: any) => stringifyQuery(params, 'repeat');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user