|
1 | 1 | import axios from 'axios'; |
2 | 2 | import type { AxiosRequestConfig, AxiosInstance } from 'axios'; |
3 | 3 | import { |
4 | | - CONTENT_TYPE, |
| 4 | + createDefaultRequestConfig, |
| 5 | + getRequestHeaderContentType, |
5 | 6 | transformRequestData, |
6 | 7 | isHttpSuccess, |
7 | 8 | handleAxiosError, |
8 | 9 | handleHttpError, |
9 | 10 | handleBackendError |
10 | 11 | } from './shared'; |
11 | | -import type { ContentTypeValue } from './shared'; |
12 | | -import type { RequestConfig } from './types'; |
| 12 | +import type { RequestConfig, RequiredRequestConfig } from './types'; |
13 | 13 |
|
14 | 14 | class Request { |
15 | 15 | axiosInstance: AxiosInstance; |
16 | 16 |
|
17 | | - requestConfig: RequestConfig; |
| 17 | + requestConfig: RequiredRequestConfig; |
18 | 18 |
|
19 | | - constructor(axiosConfig: AxiosRequestConfig, requestConfig: RequestConfig) { |
| 19 | + constructor(axiosConfig: AxiosRequestConfig, requestConfig?: RequestConfig) { |
20 | 20 | this.axiosInstance = axios.create(axiosConfig); |
21 | 21 | this.setInterceptor(); |
22 | 22 |
|
23 | | - this.requestConfig = requestConfig; |
| 23 | + this.requestConfig = createDefaultRequestConfig(requestConfig); |
24 | 24 | } |
25 | 25 |
|
26 | 26 | /** 设置请求拦截器 */ |
27 | 27 | setInterceptor() { |
28 | 28 | this.axiosInstance.interceptors.request.use( |
29 | 29 | async config => { |
30 | | - const conf = { ...config }; |
| 30 | + const conf: AxiosRequestConfig = { ...config }; |
31 | 31 |
|
32 | | - if (conf.headers) { |
33 | | - // 数据转换 |
34 | | - const contentType = (conf.headers['Content-Type'] || CONTENT_TYPE.json) as ContentTypeValue; |
35 | | - conf.data = await transformRequestData(config.data, contentType); |
36 | | - } |
| 32 | + const contentType = getRequestHeaderContentType(conf); |
| 33 | + |
| 34 | + conf.data = await transformRequestData(config.data, contentType); |
| 35 | + |
| 36 | + const handledConf = await this.requestConfig.onRequest(conf); |
37 | 37 |
|
38 | | - Object.assign(conf, this.requestConfig.onRequest(conf)); |
| 38 | + Object.assign(conf, handledConf); |
39 | 39 |
|
40 | 40 | return conf; |
41 | 41 | }, |
@@ -68,21 +68,15 @@ class Request { |
68 | 68 | } |
69 | 69 | } |
70 | 70 |
|
71 | | -export function createRequest(axiosConfig: AxiosRequestConfig, requestConfig?: Partial<RequestConfig>) { |
72 | | - const configs: RequestConfig = { |
73 | | - codeKey: 'code', |
74 | | - dataKey: 'data', |
75 | | - msgKey: 'message', |
76 | | - successCode: 200, |
77 | | - onRequest: config => config, |
78 | | - onBackendSuccess: responseData => { |
79 | | - const { codeKey, successCode } = configs; |
80 | | - return responseData[codeKey] === successCode; |
81 | | - } |
82 | | - }; |
83 | | - Object.assign(configs, requestConfig); |
84 | | - |
85 | | - const request = new Request(axiosConfig, configs); |
| 71 | +/** |
| 72 | + * 创建请求 |
| 73 | + * @param axiosConfig axios配置 |
| 74 | + * @param requestConfig 请求配置 |
| 75 | + * @param isRaw 是否返回原始的axios实例, (默认返回自定义的请求实例) |
| 76 | + */ |
| 77 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 78 | +export function createRequest(axiosConfig: AxiosRequestConfig, requestConfig?: RequestConfig, isRaw = false) { |
| 79 | + const request = new Request(axiosConfig, requestConfig); |
86 | 80 |
|
87 | 81 | return request.axiosInstance; |
88 | 82 | } |
0 commit comments