|
1 | 1 | // 默认利用axios的cancelToken进行防重复提交。 |
2 | 2 | // 如需允许多个提交同时发出。则需要在请求配置config中增加 neverCancel 属性,并设置为true |
3 | 3 | import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; |
4 | | -import baseConfig from './axiosrc'; |
| 4 | +import baseConfig from './config/axiosrc'; |
5 | 5 |
|
6 | | -// define AJAX params that should be a key value pair |
7 | | -interface Params { |
8 | | - [key: string]: any |
| 6 | +// define request params that should be a key value pair |
| 7 | +interface Params<T> { |
| 8 | + [key: string]: T; |
9 | 9 | } |
10 | 10 |
|
11 | | -function createInstance (params ? : Params, timeout ? : number) { |
12 | | - if (params !== undefined) baseConfig.params = params; |
13 | | - if (timeout !== undefined) baseConfig.timeout = timeout; |
| 11 | +function createInstance ( params?: Params<object>, timeout?: number ) { |
| 12 | + if ( params !== undefined ) baseConfig.params = params; |
| 13 | + if ( timeout !== undefined ) baseConfig.timeout = timeout; |
14 | 14 |
|
15 | | - const instance = axios.create(baseConfig); |
16 | | - requstInterceptor(instance); |
17 | | - responseInterceptor(instance); |
| 15 | + const instance = axios.create( baseConfig ); |
| 16 | + requstInterceptor( instance ); |
| 17 | + responseInterceptor( instance ); |
18 | 18 |
|
19 | 19 | return instance; |
20 | 20 | } |
21 | 21 |
|
22 | | -function requstInterceptor (instance: AxiosInstance) { |
23 | | - instance.interceptors.request.use((config: AxiosRequestConfig) => { |
24 | | - console.log(config); |
| 22 | +function requstInterceptor ( instance: AxiosInstance ) { |
| 23 | + instance.interceptors.request.use( ( config: AxiosRequestConfig ) => { |
| 24 | + console.log( config ); |
25 | 25 | return config; |
26 | | - }); |
| 26 | + } ); |
27 | 27 | } |
28 | 28 |
|
29 | | -function responseInterceptor (instance: AxiosInstance) { |
30 | | - instance.interceptors.response.use((response: AxiosResponse) => { |
31 | | - console.log(response); |
| 29 | +function responseInterceptor ( instance: AxiosInstance ) { |
| 30 | + instance.interceptors.response.use( ( response: AxiosResponse ) => { |
| 31 | + console.log( response ); |
32 | 32 | return response; |
33 | | - }); |
| 33 | + } ); |
34 | 34 | } |
35 | 35 |
|
36 | | -export function requestGet (url: string, params ? : Params) { |
37 | | - return createInstance(params) |
38 | | - .get(url) |
39 | | - .catch((error: Error) => { |
40 | | - errorHandler(error); |
41 | | - }) |
42 | | - .finally(() => { |
| 36 | +function requestGet ( url: string, params?: Params<object> ) { |
| 37 | + return createInstance( params ) |
| 38 | + .get( url ) |
| 39 | + .catch( ( error: Error ) => { |
| 40 | + errorHandler( error ); |
| 41 | + } ) |
| 42 | + .finally( () => { |
43 | 43 | doFinally(); |
44 | | - }); |
| 44 | + } ); |
45 | 45 | } |
46 | 46 |
|
47 | | -export function requestPost (url: string, params ? : Params) { |
48 | | - return createInstance(params) |
49 | | - .post(url) |
50 | | - .catch((error: Error) => { |
51 | | - errorHandler(error); |
52 | | - }) |
53 | | - .finally(() => { |
| 47 | +function requestPost ( url: string, params?: Params<object> ) { |
| 48 | + return createInstance( params ) |
| 49 | + .post( url ) |
| 50 | + .catch( ( error: Error ) => { |
| 51 | + errorHandler( error ); |
| 52 | + } ) |
| 53 | + .finally( () => { |
54 | 54 | doFinally(); |
55 | | - }); |
| 55 | + } ); |
56 | 56 | } |
57 | | - |
58 | | -function errorHandler (error: Error) { |
59 | | - console.log(error); |
| 57 | +function errorHandler ( error: Error ) { |
| 58 | + console.log( error ); |
60 | 59 | }; |
61 | 60 |
|
62 | 61 | function doFinally () { |
63 | | - console.log('finally'); |
| 62 | + console.log( 'finally' ); |
64 | 63 | } |
| 64 | + |
| 65 | +const axiosHandler: any = {}; |
| 66 | + |
| 67 | +axiosHandler.requestGet = requestGet; |
| 68 | +axiosHandler.requestPost = requestPost; |
| 69 | + |
| 70 | +export default axiosHandler; |
0 commit comments