frog-request 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +15 -0
- package/src/axios.ts +52 -0
- package/src/index.ts +1 -0
package/package.json
ADDED
package/src/axios.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import axios, {
|
|
2
|
+
AxiosError,
|
|
3
|
+
type AxiosRequestConfig,
|
|
4
|
+
type AxiosResponse,
|
|
5
|
+
type InternalAxiosRequestConfig,
|
|
6
|
+
} from 'axios';
|
|
7
|
+
|
|
8
|
+
type CreateAxiosRequestOptions = AxiosRequestConfig & {
|
|
9
|
+
handleRequestError?: (error: AxiosError) => Promise<AxiosError>;
|
|
10
|
+
handleResponseError?: (error: AxiosError) => Promise<AxiosError>;
|
|
11
|
+
handleRequestSuccess?: (
|
|
12
|
+
config: InternalAxiosRequestConfig,
|
|
13
|
+
) => InternalAxiosRequestConfig;
|
|
14
|
+
handleResponseSuccess?: (response: AxiosResponse) => AxiosResponse;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 创建 axios 基础实例
|
|
19
|
+
* @param options 配置选项
|
|
20
|
+
* @returns axios 实例
|
|
21
|
+
*/
|
|
22
|
+
export function createAxiosRequest(options: CreateAxiosRequestOptions) {
|
|
23
|
+
// 创建 axios 实例
|
|
24
|
+
const instance = axios.create({
|
|
25
|
+
baseURL: options.baseURL || '/api',
|
|
26
|
+
timeout: options.timeout || 10000,
|
|
27
|
+
headers: options.headers || {},
|
|
28
|
+
...options,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// 请求拦截器
|
|
32
|
+
instance.interceptors.request.use(
|
|
33
|
+
(config) => {
|
|
34
|
+
return options.handleRequestSuccess?.(config) || config;
|
|
35
|
+
},
|
|
36
|
+
(error) => {
|
|
37
|
+
return options.handleRequestError?.(error) || Promise.reject(error);
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
// 响应拦截器
|
|
42
|
+
instance.interceptors.response.use(
|
|
43
|
+
(response) => {
|
|
44
|
+
return options.handleResponseSuccess?.(response) || response;
|
|
45
|
+
},
|
|
46
|
+
(error) => {
|
|
47
|
+
return options.handleResponseError?.(error) || Promise.reject(error);
|
|
48
|
+
},
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return instance;
|
|
52
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './axios';
|