@jetlinks-web/core 2.0.6 → 2.0.8

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 (2) hide show
  1. package/package.json +3 -3
  2. package/src/axios.ts +227 -224
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetlinks-web/core",
3
- "version": "2.0.6",
3
+ "version": "2.0.8",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "module": "index.ts",
@@ -16,9 +16,9 @@
16
16
  "license": "ISC",
17
17
  "dependencies": {
18
18
  "axios": "^1.7.4",
19
+ "@jetlinks-web/types": "^1.0.2",
19
20
  "@jetlinks-web/constants": "^1.0.7",
20
- "@jetlinks-web/utils": "^1.2.5",
21
- "@jetlinks-web/types": "^1.0.2"
21
+ "@jetlinks-web/utils": "^1.2.5"
22
22
  },
23
23
  "publishConfig": {
24
24
  "registry": "https://registry.npmjs.org/",
package/src/axios.ts CHANGED
@@ -1,224 +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
- * 用以获取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 = '用户未登录'
127
- _options.tokenExpiration?.(err, err.response)
128
- break;
129
- default:
130
- break;
131
- }
132
- } else if (err.response === undefined) {
133
- description = err.message.includes('timeout') ? '接口响应超时' : err.message
134
- _status = 'timeout'
135
- }
136
-
137
- if (_options.handleError && isFunction(_options.handleError)) {
138
- _options.handleError(description, _status, err)
139
- }
140
-
141
- return Promise.reject(err)
142
- }
143
-
144
- export const crateAxios = (options: Options) => {
145
- if (options) {
146
- _options = Object.assign(_options, options)
147
- }
148
-
149
- instance = axios.create({
150
- withCredentials: false,
151
- timeout: _options.timeout,
152
- baseURL: BASE_API
153
- })
154
-
155
- instance.interceptors.request.use(
156
- handleRequest,
157
- errorHandler
158
- )
159
-
160
- instance.interceptors.response.use(
161
- handleResponse,
162
- errorHandler
163
- )
164
- }
165
-
166
- export const post = <T = any>(url: string, data: any = {}, ext?: any) => {
167
- return (instance<any, AxiosResponseRewrite<T>>({
168
- method: 'POST',
169
- url,
170
- data,
171
- ...ext,
172
- }))
173
- }
174
-
175
- export const get = <T = any>(url: string, params: any = undefined, ext?: any) => {
176
- return instance<any, AxiosResponseRewrite<T>>({
177
- method: 'GET',
178
- url,
179
- params,
180
- ...ext,
181
- })
182
- }
183
-
184
- export const put = <T = any>(url: string, data: any = {}, ext?: any) => {
185
- return instance<any, AxiosResponseRewrite<T>>({
186
- method: 'PUT',
187
- url,
188
- data,
189
- ...ext,
190
- })
191
- }
192
-
193
- export const patch = <T = any>(url: string, data: any = {}, ext?: any) => {
194
- return instance<any, AxiosResponseRewrite<T>>({
195
- method: 'patch',
196
- url,
197
- data,
198
- ...ext,
199
- })
200
- }
201
-
202
- export const remove = <T = any>(url: string, params: any = undefined, ext?: any) => {
203
- return instance<any, AxiosResponseRewrite<T>>({
204
- method: 'DELETE',
205
- url,
206
- params,
207
- ...ext,
208
- })
209
- }
210
-
211
- export const getStream = (url: string, params?: any, ext?: any) => {
212
- return get(url, params, { responseType: 'arraybuffer', ...ext })
213
- }
214
-
215
- export const postStream = (url: string, data: any, ext?: any) => {
216
- return post(url, data, { responseType: 'arraybuffer', ...ext })
217
- }
218
-
219
- export const request = {
220
- post, get, put, patch, remove, getStream, postStream
221
- }
222
-
223
-
224
-
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
+ if (lang) {
63
+ config.headers[_options.langKey] = lang
64
+ }
65
+
66
+ // 没有token,并且该接口需要token校验
67
+ if (!token && !_options.filter_url?.some((url) => config.url?.includes(url))) {
68
+ // 跳转登录页
69
+ _options.tokenExpiration?.()
70
+ return config
71
+ }
72
+
73
+ if (!config.headers[TOKEN_KEY]) {
74
+ config.headers[TOKEN_KEY] = token
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
+