Skip to content

Commit

Permalink
feat(axios util): support params stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyyuusora committed Jun 19, 2023
1 parent 83939b7 commit b3a98ab
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils/request/Axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b3a98ab

Please sign in to comment.