feat(axios util): support params stringify (#544)

This commit is contained in:
ngyyuusora 2023-06-19 19:38:44 +08:00 committed by GitHub
parent 83939b7517
commit 9d8bf86059
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -118,6 +118,21 @@ export class VAxios {
};
}
// 支持params数组参数格式化
supportParamsStringify(config: AxiosRequestConfig) {
const headers = config.headers || this.options.headers;
const contentType = headers?.['Content-Type'] || headers?.['content-type'];
if (contentType === ContentTypeEnum.FormURLEncoded || !Reflect.has(config, 'params')) {
return config;
}
return {
...config,
paramsSerializer: (params: any) => stringify(params, { arrayFormat: 'brackets' }),
};
}
get<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
return this.request({ ...config, method: 'GET' }, options);
}
@ -154,6 +169,8 @@ export class VAxios {
conf.requestOptions = opt;
conf = this.supportFormData(conf);
// 支持params数组参数格式化因axios默认的toFormData即为brackets方式无需配置paramsSerializer为qs有需要可解除注释参数参考qs文档
// conf = this.supportParamsStringify(conf);
return new Promise((resolve, reject) => {
this.instance