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