mhy-http-ts 1.0.0 → 1.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/README.md +3 -3
- package/lib/abstract/InterceptorsHook.ts +2 -2
- package/lib/abstract/RequestHook.ts +2 -2
- package/lib/class/Creator.ts +35 -25
- package/lib/interface/index.ts +13 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
```
|
|
6
|
-
npm install
|
|
6
|
+
npm install mhy-http-ts
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
# Usage
|
|
@@ -12,9 +12,9 @@ npm install @types/f-axios
|
|
|
12
12
|
|
|
13
13
|
```
|
|
14
14
|
import type { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
|
|
15
|
-
import { MethodEnum, ContentTypeEnum } from '
|
|
15
|
+
import { MethodEnum, ContentTypeEnum } from 'mhy-http-ts/enum';
|
|
16
16
|
|
|
17
|
-
import { Create as AxiosCreate, Canceler as AxiosCanceler, Retry as AxiosRetry } from '
|
|
17
|
+
import { Create as AxiosCreate, Canceler as AxiosCanceler, Retry as AxiosRetry } from 'mhy-http-ts';
|
|
18
18
|
|
|
19
19
|
const defaultRequestConfig: any = {
|
|
20
20
|
baseURL: '/api',
|
|
@@ -11,7 +11,7 @@ export abstract class InterceptorsHook {
|
|
|
11
11
|
/**
|
|
12
12
|
* @description 请求拦截器异常处理
|
|
13
13
|
*/
|
|
14
|
-
requestInterceptorsCatch?: (error:
|
|
14
|
+
requestInterceptorsCatch?: (error: any) => void
|
|
15
15
|
/**
|
|
16
16
|
* @description 响应拦截器设置
|
|
17
17
|
*/
|
|
@@ -19,5 +19,5 @@ export abstract class InterceptorsHook {
|
|
|
19
19
|
/**
|
|
20
20
|
* @description 响应拦截器异常处理
|
|
21
21
|
*/
|
|
22
|
-
responseInterceptorsCatch?: (error:
|
|
22
|
+
responseInterceptorsCatch?: (error: any, instance: AxiosInstance) => void
|
|
23
23
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AxiosRequestConfig, AxiosResponse } from 'axios'
|
|
1
|
+
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @description
|
|
@@ -15,5 +15,5 @@ export abstract class RequestHook {
|
|
|
15
15
|
/**
|
|
16
16
|
* @description 请求异常处理
|
|
17
17
|
*/
|
|
18
|
-
requestCatchHook?: (error: Error, options?: any) => Promise<any>
|
|
18
|
+
requestCatchHook?: (error: Error | AxiosError, options?: any) => Promise<any>
|
|
19
19
|
}
|
package/lib/class/Creator.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { RequestHook } from '../abstract/RequestHook';
|
|
|
3
3
|
import { InterceptorsHook } from '../abstract/InterceptorsHook';
|
|
4
4
|
import { cloneDeep as _cloneDeep, isFunction as _isFunction } from 'lodash'
|
|
5
5
|
import { MethodEnum } from "../enum"
|
|
6
|
+
import { Canceler } from "./Canceler"
|
|
7
|
+
import { RequestConfig } from "../interface"
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* @description axios创建
|
|
@@ -11,11 +13,11 @@ export class Create {
|
|
|
11
13
|
// ⬇️ axios实例
|
|
12
14
|
private instance: AxiosInstance
|
|
13
15
|
// ⬇️ axios配置
|
|
14
|
-
private readonly requestConfig:
|
|
16
|
+
private readonly requestConfig: RequestConfig
|
|
15
17
|
// ⬇️ 自定义拦截器
|
|
16
18
|
private customInterceptors: RequestHook & InterceptorsHook
|
|
17
19
|
// ⬇️ 构造器
|
|
18
|
-
constructor(requestConfig:
|
|
20
|
+
constructor(requestConfig: RequestConfig, customInterceptors: RequestHook & InterceptorsHook) {
|
|
19
21
|
this.requestConfig = requestConfig // ⬅️ 配置参数
|
|
20
22
|
this.customInterceptors = customInterceptors // ⬅️ 配置自定义拦截器
|
|
21
23
|
|
|
@@ -24,16 +26,16 @@ export class Create {
|
|
|
24
26
|
}
|
|
25
27
|
/**
|
|
26
28
|
* @description 实例创建
|
|
27
|
-
* @param config
|
|
29
|
+
* @param config RequestConfig
|
|
28
30
|
*/
|
|
29
|
-
private createAxios(config:
|
|
31
|
+
private createAxios(config: RequestConfig): void {
|
|
30
32
|
this.instance = axios.create(config)
|
|
31
33
|
}
|
|
32
34
|
/**
|
|
33
35
|
* @description 重新设置实例
|
|
34
|
-
* @param config
|
|
36
|
+
* @param config RequestConfig
|
|
35
37
|
*/
|
|
36
|
-
resetAxios(config:
|
|
38
|
+
resetAxios(config: RequestConfig) {
|
|
37
39
|
if (!this.instance) return
|
|
38
40
|
return this.createAxios(config)
|
|
39
41
|
}
|
|
@@ -61,15 +63,18 @@ export class Create {
|
|
|
61
63
|
responseInterceptors, // ⬅️ 响应拦截器设置
|
|
62
64
|
responseInterceptorsCatch, // ⬅️ 响应拦截器异常处理
|
|
63
65
|
} = this.customInterceptors || {}
|
|
66
|
+
const axiosCanceler = new Canceler();
|
|
64
67
|
// 请求拦截器
|
|
65
68
|
this.instance.interceptors.request.use(
|
|
66
69
|
(config: InternalAxiosRequestConfig) => {
|
|
70
|
+
const isAllowRepeat = this.requestConfig?.customOptions?.isAllowRepeat ?? true
|
|
71
|
+
// !isAllowRepeat && axiosCanceler.addPending(config)
|
|
67
72
|
if (requestInterceptors && _isFunction(requestInterceptors)) {
|
|
68
|
-
config = requestInterceptors(config)
|
|
73
|
+
config = requestInterceptors(config, this.requestConfig)
|
|
69
74
|
}
|
|
70
75
|
return config
|
|
71
76
|
},
|
|
72
|
-
(error:
|
|
77
|
+
(error: any) => {
|
|
73
78
|
requestInterceptorsCatch
|
|
74
79
|
&& _isFunction(requestInterceptorsCatch)
|
|
75
80
|
&& requestInterceptorsCatch(error)
|
|
@@ -78,76 +83,81 @@ export class Create {
|
|
|
78
83
|
// 响应拦截器
|
|
79
84
|
this.instance.interceptors.response.use(
|
|
80
85
|
(res: AxiosResponse<any>) => {
|
|
86
|
+
// res && axiosCanceler.removePending(res.config)
|
|
81
87
|
if (responseInterceptors && _isFunction(responseInterceptors)) {
|
|
82
88
|
res = responseInterceptors(res)
|
|
83
89
|
}
|
|
84
90
|
return res
|
|
85
91
|
},
|
|
86
|
-
(error:
|
|
92
|
+
(error: any) => {
|
|
87
93
|
responseInterceptorsCatch
|
|
88
94
|
&& _isFunction(responseInterceptorsCatch)
|
|
89
|
-
&& responseInterceptorsCatch(this.instance
|
|
95
|
+
&& responseInterceptorsCatch(error, this.instance)
|
|
90
96
|
}
|
|
91
97
|
)
|
|
92
98
|
}
|
|
93
99
|
/**
|
|
94
100
|
* @description 通用请求
|
|
95
|
-
* @param config
|
|
101
|
+
* @param config RequestConfig
|
|
96
102
|
*/
|
|
97
|
-
request<T = any>(config:
|
|
98
|
-
let conf:
|
|
103
|
+
request<T = any>(config: RequestConfig): Promise<T> {
|
|
104
|
+
let conf: RequestConfig = _cloneDeep(config)
|
|
99
105
|
const { beforeRequestHook, afterRequestHook, requestCatchHook } = this.customInterceptors || {}
|
|
100
106
|
if (beforeRequestHook && _isFunction(beforeRequestHook)) {
|
|
101
107
|
conf = beforeRequestHook(conf)
|
|
102
108
|
}
|
|
103
109
|
return new Promise((resolve, reject) => {
|
|
104
|
-
this.instance
|
|
110
|
+
this.instance
|
|
111
|
+
.request<any, AxiosResponse<any>>(conf)
|
|
105
112
|
.then((res: AxiosResponse<any>) => {
|
|
106
113
|
if (afterRequestHook && _isFunction(afterRequestHook)) {
|
|
107
114
|
try {
|
|
108
115
|
resolve(afterRequestHook(res))
|
|
109
|
-
} catch (e) {
|
|
116
|
+
} catch (e: any) {
|
|
110
117
|
reject(e || new Error('request has error'))
|
|
111
118
|
}
|
|
112
119
|
} else {
|
|
113
120
|
resolve(res as unknown as Promise<T>)
|
|
114
121
|
}
|
|
115
122
|
})
|
|
116
|
-
.catch((error:
|
|
117
|
-
requestCatchHook && _isFunction(requestCatchHook)
|
|
123
|
+
.catch((error: Error | AxiosError) => {
|
|
124
|
+
if (requestCatchHook && _isFunction(requestCatchHook)) {
|
|
125
|
+
reject(requestCatchHook(error, conf))
|
|
126
|
+
}
|
|
127
|
+
reject(error)
|
|
118
128
|
})
|
|
119
129
|
})
|
|
120
130
|
}
|
|
121
131
|
/**
|
|
122
132
|
* @description get请求
|
|
123
|
-
* @param config
|
|
133
|
+
* @param config RequestConfig
|
|
124
134
|
* @returns Promise
|
|
125
135
|
*/
|
|
126
|
-
get<T = any>(config:
|
|
136
|
+
get<T = any>(config: RequestConfig): Promise<T> {
|
|
127
137
|
return this.request({ ...config, method: MethodEnum.GET })
|
|
128
138
|
}
|
|
129
139
|
/**
|
|
130
140
|
* @description post请求
|
|
131
|
-
* @param config
|
|
141
|
+
* @param config RequestConfig
|
|
132
142
|
* @returns Promise
|
|
133
143
|
*/
|
|
134
|
-
post<T = any>(config:
|
|
144
|
+
post<T = any>(config: RequestConfig): Promise<T> {
|
|
135
145
|
return this.request({ ...config, method: MethodEnum.POST })
|
|
136
146
|
}
|
|
137
147
|
/**
|
|
138
148
|
* @description put请求
|
|
139
|
-
* @param config
|
|
149
|
+
* @param config RequestConfig
|
|
140
150
|
* @returns Promise
|
|
141
151
|
*/
|
|
142
|
-
put<T = any>(config:
|
|
152
|
+
put<T = any>(config: RequestConfig): Promise<T> {
|
|
143
153
|
return this.request({ ...config, method: MethodEnum.PUT })
|
|
144
154
|
}
|
|
145
155
|
/**
|
|
146
156
|
* @description delete请求
|
|
147
|
-
* @param config
|
|
157
|
+
* @param config RequestConfig
|
|
148
158
|
* @returns Promise
|
|
149
159
|
*/
|
|
150
|
-
delete<T = any>(config:
|
|
160
|
+
delete<T = any>(config: RequestConfig): Promise<T> {
|
|
151
161
|
return this.request({ ...config, method: MethodEnum.DELETE })
|
|
152
162
|
}
|
|
153
163
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from "axios"
|
|
2
|
+
|
|
3
|
+
export interface RequestConfig extends AxiosRequestConfig {
|
|
4
|
+
customOptions: CustomOptions
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface CustomOptions {
|
|
8
|
+
// 是否需要token
|
|
9
|
+
isWithToken?: boolean
|
|
10
|
+
// 是否允许重复请求
|
|
11
|
+
isAllowRepeat?: boolean
|
|
12
|
+
|
|
13
|
+
}
|