mhy-http-ts 1.0.0 → 1.0.2
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 +13 -5
- package/lib/abstract/InterceptorsHook.ts +2 -2
- package/lib/abstract/RequestHook.ts +2 -2
- package/lib/class/Creator.ts +37 -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',
|
|
@@ -63,7 +63,9 @@ const defaultInterceptors: any = {
|
|
|
63
63
|
return code ? Promise.resolve(data) : Promise.reject(data);
|
|
64
64
|
},
|
|
65
65
|
|
|
66
|
-
requestCatchHook: (error: AxiosError, config: AxiosRequestConfig) => {
|
|
66
|
+
requestCatchHook: (error: Error | AxiosError, config: AxiosRequestConfig) => {
|
|
67
|
+
return Promise.reject(error)
|
|
68
|
+
}
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
const defApi = new AxiosCreate(defaultRequestConfig, defaultInterceptors);
|
|
@@ -72,9 +74,11 @@ const defApi = new AxiosCreate(defaultRequestConfig, defaultInterceptors);
|
|
|
72
74
|
# API
|
|
73
75
|
|
|
74
76
|
---
|
|
77
|
+
|
|
75
78
|
## get
|
|
76
79
|
|
|
77
80
|
---
|
|
81
|
+
|
|
78
82
|
```
|
|
79
83
|
const getApi = defApi.get({
|
|
80
84
|
url: '/api/test/get',
|
|
@@ -86,6 +90,7 @@ const getApi = defApi.get({
|
|
|
86
90
|
## post
|
|
87
91
|
|
|
88
92
|
---
|
|
93
|
+
|
|
89
94
|
```
|
|
90
95
|
const postApi = defApi.post({
|
|
91
96
|
url: '/api/test/post',
|
|
@@ -103,6 +108,7 @@ const download = defApi.post({
|
|
|
103
108
|
## put
|
|
104
109
|
|
|
105
110
|
---
|
|
111
|
+
|
|
106
112
|
```
|
|
107
113
|
const putApi = defApi.post({
|
|
108
114
|
url: '/api/test/put',
|
|
@@ -114,6 +120,7 @@ const putApi = defApi.post({
|
|
|
114
120
|
## delete
|
|
115
121
|
|
|
116
122
|
---
|
|
123
|
+
|
|
117
124
|
```
|
|
118
125
|
const deleteApi = defApi.post({
|
|
119
126
|
url: '/api/test/delete',
|
|
@@ -125,4 +132,5 @@ const deleteApi = defApi.post({
|
|
|
125
132
|
# License
|
|
126
133
|
|
|
127
134
|
---
|
|
128
|
-
|
|
135
|
+
|
|
136
|
+
MIT
|
|
@@ -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,83 @@ 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
|
-
|
|
116
|
+
return
|
|
117
|
+
} catch (e: any) {
|
|
110
118
|
reject(e || new Error('request has error'))
|
|
111
119
|
}
|
|
112
120
|
} else {
|
|
113
121
|
resolve(res as unknown as Promise<T>)
|
|
114
122
|
}
|
|
115
123
|
})
|
|
116
|
-
.catch((error:
|
|
117
|
-
requestCatchHook && _isFunction(requestCatchHook)
|
|
124
|
+
.catch((error: Error | AxiosError) => {
|
|
125
|
+
if (requestCatchHook && _isFunction(requestCatchHook)) {
|
|
126
|
+
reject(requestCatchHook(error, conf))
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
reject(error)
|
|
118
130
|
})
|
|
119
131
|
})
|
|
120
132
|
}
|
|
121
133
|
/**
|
|
122
134
|
* @description get请求
|
|
123
|
-
* @param config
|
|
135
|
+
* @param config RequestConfig
|
|
124
136
|
* @returns Promise
|
|
125
137
|
*/
|
|
126
|
-
get<T = any>(config:
|
|
138
|
+
get<T = any>(config: RequestConfig): Promise<T> {
|
|
127
139
|
return this.request({ ...config, method: MethodEnum.GET })
|
|
128
140
|
}
|
|
129
141
|
/**
|
|
130
142
|
* @description post请求
|
|
131
|
-
* @param config
|
|
143
|
+
* @param config RequestConfig
|
|
132
144
|
* @returns Promise
|
|
133
145
|
*/
|
|
134
|
-
post<T = any>(config:
|
|
146
|
+
post<T = any>(config: RequestConfig): Promise<T> {
|
|
135
147
|
return this.request({ ...config, method: MethodEnum.POST })
|
|
136
148
|
}
|
|
137
149
|
/**
|
|
138
150
|
* @description put请求
|
|
139
|
-
* @param config
|
|
151
|
+
* @param config RequestConfig
|
|
140
152
|
* @returns Promise
|
|
141
153
|
*/
|
|
142
|
-
put<T = any>(config:
|
|
154
|
+
put<T = any>(config: RequestConfig): Promise<T> {
|
|
143
155
|
return this.request({ ...config, method: MethodEnum.PUT })
|
|
144
156
|
}
|
|
145
157
|
/**
|
|
146
158
|
* @description delete请求
|
|
147
|
-
* @param config
|
|
159
|
+
* @param config RequestConfig
|
|
148
160
|
* @returns Promise
|
|
149
161
|
*/
|
|
150
|
-
delete<T = any>(config:
|
|
162
|
+
delete<T = any>(config: RequestConfig): Promise<T> {
|
|
151
163
|
return this.request({ ...config, method: MethodEnum.DELETE })
|
|
152
164
|
}
|
|
153
165
|
}
|
|
@@ -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
|
+
isAuthorized?: boolean
|
|
10
|
+
// 是否允许重复请求
|
|
11
|
+
isAllowRepeat?: boolean
|
|
12
|
+
|
|
13
|
+
}
|