From 9d8bf86059dfee689eb132c6f12e2bdb4dd4f93d Mon Sep 17 00:00:00 2001 From: ngyyuusora Date: Mon, 19 Jun 2023 19:38:44 +0800 Subject: [PATCH] feat(axios util): support params stringify (#544) --- src/utils/request/Axios.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/utils/request/Axios.ts b/src/utils/request/Axios.ts index fdb5c9c..5ad0b8e 100644 --- a/src/utils/request/Axios.ts +++ b/src/utils/request/Axios.ts @@ -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(config: AxiosRequestConfig, options?: RequestOptions): Promise { 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