@jetlinks-web/core 2.0.5 → 2.0.7
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 +2 -2
- package/src/axios.ts +227 -214
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jetlinks-web/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.ts",
|
|
6
6
|
"module": "index.ts",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"axios": "^1.7.4",
|
|
19
|
-
"@jetlinks-web/types": "^1.0.2",
|
|
20
19
|
"@jetlinks-web/constants": "^1.0.7",
|
|
20
|
+
"@jetlinks-web/types": "^1.0.2",
|
|
21
21
|
"@jetlinks-web/utils": "^1.2.5"
|
|
22
22
|
},
|
|
23
23
|
"publishConfig": {
|
package/src/axios.ts
CHANGED
|
@@ -1,214 +1,227 @@
|
|
|
1
|
-
import { TOKEN_KEY, BASE_API } from '@jetlinks-web/constants'
|
|
2
|
-
import { getToken } from '@jetlinks-web/utils'
|
|
3
|
-
import axios from 'axios'
|
|
4
|
-
import type {
|
|
5
|
-
AxiosInstance,
|
|
6
|
-
AxiosResponse,
|
|
7
|
-
AxiosError,
|
|
8
|
-
InternalAxiosRequestConfig,
|
|
9
|
-
} from 'axios'
|
|
10
|
-
import type { AxiosResponseRewrite } from '@jetlinks-web/types'
|
|
11
|
-
import {isFunction, isObject} from 'lodash-es'
|
|
12
|
-
|
|
13
|
-
interface Options {
|
|
14
|
-
|
|
15
|
-
tokenExpiration: (err: AxiosError<any>, response: AxiosResponse) => void
|
|
16
|
-
filter_url?: Array<string>
|
|
17
|
-
code?: number
|
|
18
|
-
codeKey?: string
|
|
19
|
-
timeout?: number
|
|
20
|
-
handleRequest?: () => void
|
|
21
|
-
/**
|
|
22
|
-
*
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (!config.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
) {
|
|
97
|
-
response
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export const
|
|
206
|
-
return
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
1
|
+
import { TOKEN_KEY, BASE_API } from '@jetlinks-web/constants'
|
|
2
|
+
import { getToken } from '@jetlinks-web/utils'
|
|
3
|
+
import axios from 'axios'
|
|
4
|
+
import type {
|
|
5
|
+
AxiosInstance,
|
|
6
|
+
AxiosResponse,
|
|
7
|
+
AxiosError,
|
|
8
|
+
InternalAxiosRequestConfig,
|
|
9
|
+
} from 'axios'
|
|
10
|
+
import type { AxiosResponseRewrite } from '@jetlinks-web/types'
|
|
11
|
+
import {isFunction, isObject} from 'lodash-es'
|
|
12
|
+
|
|
13
|
+
interface Options {
|
|
14
|
+
|
|
15
|
+
tokenExpiration: (err: AxiosError<any>, response: AxiosResponse) => void
|
|
16
|
+
filter_url?: Array<string>
|
|
17
|
+
code?: number
|
|
18
|
+
codeKey?: string
|
|
19
|
+
timeout?: number
|
|
20
|
+
handleRequest?: () => void
|
|
21
|
+
/**
|
|
22
|
+
* 用以获取localstorage中的lang
|
|
23
|
+
*/
|
|
24
|
+
langKey?: string
|
|
25
|
+
/**
|
|
26
|
+
* response处理函数
|
|
27
|
+
* @param response AxiosResponse实例
|
|
28
|
+
*/
|
|
29
|
+
handleResponse?: (response: AxiosResponse) => void
|
|
30
|
+
/**
|
|
31
|
+
* 错误处理函数
|
|
32
|
+
* @param msg 错误消息
|
|
33
|
+
* @param status 错误code
|
|
34
|
+
* @param error 错误实例
|
|
35
|
+
*/
|
|
36
|
+
handleError?: (msg: string, status: string | number, error: AxiosError<any>) => void
|
|
37
|
+
requestOptions?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Record<string, any>
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let instance: AxiosInstance
|
|
42
|
+
|
|
43
|
+
let _options: Options = {
|
|
44
|
+
filter_url: [],
|
|
45
|
+
code: 200,
|
|
46
|
+
codeKey: 'status',
|
|
47
|
+
timeout: 1000 * 15,
|
|
48
|
+
handleRequest: undefined,
|
|
49
|
+
handleResponse: undefined,
|
|
50
|
+
handleError: undefined,
|
|
51
|
+
langKey: 'lang',
|
|
52
|
+
requestOptions: (config) => ({}),
|
|
53
|
+
tokenExpiration: () => {},
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const controller = new AbortController();
|
|
57
|
+
|
|
58
|
+
const handleRequest = (config: InternalAxiosRequestConfig) => {
|
|
59
|
+
const token = getToken()
|
|
60
|
+
const lang = localStorage.getItem(_options.langKey)
|
|
61
|
+
|
|
62
|
+
// 没有token,并且该接口需要token校验
|
|
63
|
+
if (!token && !_options.filter_url?.some((url) => config.url?.includes(url))) {
|
|
64
|
+
// 跳转登录页
|
|
65
|
+
_options.tokenExpiration?.()
|
|
66
|
+
return config
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!config.headers[TOKEN_KEY]) {
|
|
70
|
+
config.headers[TOKEN_KEY] = token
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (lang) {
|
|
74
|
+
config.headers[_options.langKey] = lang
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (_options.requestOptions && isFunction(_options.requestOptions)) {
|
|
78
|
+
const extraOptions = _options.requestOptions(config)
|
|
79
|
+
if (extraOptions && isObject(extraOptions)) {
|
|
80
|
+
for (const key in extraOptions) {
|
|
81
|
+
config[key] = extraOptions[key]
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
return config
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const handleResponse = (response: AxiosResponse) => {
|
|
91
|
+
|
|
92
|
+
if (_options.handleResponse && isFunction(_options.handleResponse)) {
|
|
93
|
+
return _options.handleResponse(response)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (response.data instanceof ArrayBuffer) {
|
|
97
|
+
return response
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const status = response.data[_options.codeKey || 'status']
|
|
101
|
+
|
|
102
|
+
// 增加业务接口处理成功判断方式,只需要判断返回参数包含:success为true
|
|
103
|
+
if (
|
|
104
|
+
typeof response.data === 'object' &&
|
|
105
|
+
typeof response.data.success === 'undefined'
|
|
106
|
+
) {
|
|
107
|
+
response.data.success = status === _options.code
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return response.data
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const errorHandler = (err: AxiosError<any>) => {
|
|
114
|
+
let description = err.response?.message || 'Error'
|
|
115
|
+
let _status: string | number = 0
|
|
116
|
+
if (err.response) {
|
|
117
|
+
const {data, status} = err.response
|
|
118
|
+
_status = status
|
|
119
|
+
switch (status) {
|
|
120
|
+
case 400:
|
|
121
|
+
case 403:
|
|
122
|
+
case 500:
|
|
123
|
+
description = (`${data?.message}`).substring(0, 90)
|
|
124
|
+
break;
|
|
125
|
+
case 401:
|
|
126
|
+
description = err.response.data.result.text || '用户未登录'
|
|
127
|
+
_options.tokenExpiration?.(err, err.response)
|
|
128
|
+
break;
|
|
129
|
+
case 404:
|
|
130
|
+
description = err?.response?.data?.message || `${data?.error} ${data?.path}`
|
|
131
|
+
break;
|
|
132
|
+
default:
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
} else if (err.response === undefined) {
|
|
136
|
+
description = err.message.includes('timeout') ? '接口响应超时' : err.message
|
|
137
|
+
_status = 'timeout'
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (_options.handleError && isFunction(_options.handleError)) {
|
|
141
|
+
_options.handleError(description, _status, err)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return Promise.reject(err)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export const crateAxios = (options: Options) => {
|
|
148
|
+
if (options) {
|
|
149
|
+
_options = Object.assign(_options, options)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
instance = axios.create({
|
|
153
|
+
withCredentials: false,
|
|
154
|
+
timeout: _options.timeout,
|
|
155
|
+
baseURL: BASE_API
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
instance.interceptors.request.use(
|
|
159
|
+
handleRequest,
|
|
160
|
+
errorHandler
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
instance.interceptors.response.use(
|
|
164
|
+
handleResponse,
|
|
165
|
+
errorHandler
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export const post = <T = any>(url: string, data: any = {}, ext?: any) => {
|
|
170
|
+
return (instance<any, AxiosResponseRewrite<T>>({
|
|
171
|
+
method: 'POST',
|
|
172
|
+
url,
|
|
173
|
+
data,
|
|
174
|
+
...ext,
|
|
175
|
+
}))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export const get = <T = any>(url: string, params: any = undefined, ext?: any) => {
|
|
179
|
+
return instance<any, AxiosResponseRewrite<T>>({
|
|
180
|
+
method: 'GET',
|
|
181
|
+
url,
|
|
182
|
+
params,
|
|
183
|
+
...ext,
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export const put = <T = any>(url: string, data: any = {}, ext?: any) => {
|
|
188
|
+
return instance<any, AxiosResponseRewrite<T>>({
|
|
189
|
+
method: 'PUT',
|
|
190
|
+
url,
|
|
191
|
+
data,
|
|
192
|
+
...ext,
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export const patch = <T = any>(url: string, data: any = {}, ext?: any) => {
|
|
197
|
+
return instance<any, AxiosResponseRewrite<T>>({
|
|
198
|
+
method: 'patch',
|
|
199
|
+
url,
|
|
200
|
+
data,
|
|
201
|
+
...ext,
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export const remove = <T = any>(url: string, params: any = undefined, ext?: any) => {
|
|
206
|
+
return instance<any, AxiosResponseRewrite<T>>({
|
|
207
|
+
method: 'DELETE',
|
|
208
|
+
url,
|
|
209
|
+
params,
|
|
210
|
+
...ext,
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const getStream = (url: string, params?: any, ext?: any) => {
|
|
215
|
+
return get(url, params, { responseType: 'arraybuffer', ...ext })
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export const postStream = (url: string, data: any, ext?: any) => {
|
|
219
|
+
return post(url, data, { responseType: 'arraybuffer', ...ext })
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export const request = {
|
|
223
|
+
post, get, put, patch, remove, getStream, postStream
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|