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