@varlet/axle 0.2.5 → 0.2.7-alpha.1701535975035
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 +117 -121
- package/README.zh-CN.md +116 -120
- package/es/use.d.ts +73 -12
- package/es/use.js +179 -88
- package/package.json +36 -7
- package/es/index.d.ts +0 -2
- package/es/index.js +0 -2
- package/es/instance.d.ts +0 -61
- package/es/instance.js +0 -105
- package/es/interceptors/index.d.ts +0 -2
- package/es/interceptors/index.js +0 -2
- package/es/interceptors/requestHeadersInterceptor.d.ts +0 -7
- package/es/interceptors/requestHeadersInterceptor.js +0 -18
- package/es/interceptors/responseBlobInterceptor.d.ts +0 -7
- package/es/interceptors/responseBlobInterceptor.js +0 -15
- package/es/useAverageProgress.d.ts +0 -2
- package/es/useAverageProgress.js +0 -9
- package/es/useHasLoading.d.ts +0 -2
- package/es/useHasLoading.js +0 -8
- package/es/useValues.d.ts +0 -2
- package/es/useValues.js +0 -11
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<span>English</span> |
|
|
1
|
+
<span>English</span> |
|
|
2
2
|
<a href="https://github.com/varletjs/axle/blob/main/README.zh-CN.md">中文</a>
|
|
3
3
|
|
|
4
4
|
### Intro
|
|
@@ -49,24 +49,30 @@ axios.defaults.headers.common['TOKEN'] = TOKEN
|
|
|
49
49
|
axios.defaults.timeout = 2500
|
|
50
50
|
|
|
51
51
|
// Add a request interceptor
|
|
52
|
-
axios.interceptors.request.use(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
axios.interceptors.request.use(
|
|
53
|
+
(config) => {
|
|
54
|
+
// Do something before request is sent
|
|
55
|
+
return config
|
|
56
|
+
},
|
|
57
|
+
(error) => {
|
|
58
|
+
// Do something with request error
|
|
59
|
+
return Promise.reject(error)
|
|
60
|
+
}
|
|
61
|
+
)
|
|
59
62
|
|
|
60
63
|
// Add a response interceptor
|
|
61
|
-
axios.interceptors.response.use(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
64
|
+
axios.interceptors.response.use(
|
|
65
|
+
(response) => {
|
|
66
|
+
// Any status code that lie within the range of 2xx cause this function to trigger
|
|
67
|
+
// Do something with response data
|
|
68
|
+
return response
|
|
69
|
+
},
|
|
70
|
+
(error) => {
|
|
71
|
+
// Any status codes that falls outside the range of 2xx cause this function to trigger
|
|
72
|
+
// Do something with response error
|
|
73
|
+
return Promise.reject(error)
|
|
74
|
+
}
|
|
75
|
+
)
|
|
70
76
|
```
|
|
71
77
|
|
|
72
78
|
## Axle & Axios Request Functions
|
|
@@ -147,10 +153,10 @@ axle.post('/url', { name: 'foo' })
|
|
|
147
153
|
|
|
148
154
|
```js
|
|
149
155
|
// axios
|
|
150
|
-
axios.post('/url', qs.stringify({ name: 'foo' }), {
|
|
151
|
-
headers: {
|
|
152
|
-
'Content-Type': 'application/x-www-form-urlencoded'
|
|
153
|
-
}
|
|
156
|
+
axios.post('/url', qs.stringify({ name: 'foo' }), {
|
|
157
|
+
headers: {
|
|
158
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
159
|
+
},
|
|
154
160
|
})
|
|
155
161
|
// axle
|
|
156
162
|
axle.postUrlEncode('/url', { name: 'foo' })
|
|
@@ -164,10 +170,10 @@ const formData = new FormData()
|
|
|
164
170
|
formData.append('name', 'foo')
|
|
165
171
|
formData.append('file', new File())
|
|
166
172
|
|
|
167
|
-
axios.post('/url', formData, {
|
|
168
|
-
headers: {
|
|
169
|
-
'Content-Type': 'multipart/form-data'
|
|
170
|
-
}
|
|
173
|
+
axios.post('/url', formData, {
|
|
174
|
+
headers: {
|
|
175
|
+
'Content-Type': 'multipart/form-data',
|
|
176
|
+
},
|
|
171
177
|
})
|
|
172
178
|
// axle
|
|
173
179
|
axle.postMultipart('/url', { name: 'foo', file: new File() })
|
|
@@ -197,61 +203,55 @@ Axle provides the usage of Vue Composition API style, which encapsulates the `lo
|
|
|
197
203
|
|
|
198
204
|
```html
|
|
199
205
|
<script setup>
|
|
200
|
-
import { createAxle } from '@varlet/axle'
|
|
201
|
-
import { createUseAxle } from '@varlet/axle/use'
|
|
202
|
-
|
|
203
|
-
const axle = createAxle(/** @see https://axios-http.com **/)
|
|
204
|
-
const useAxle = createUseAxle({
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abort }] = useAxle({
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
// Do request error
|
|
250
|
-
},
|
|
251
|
-
onAfter(refs) {
|
|
252
|
-
// Do request after
|
|
253
|
-
}
|
|
254
|
-
})
|
|
206
|
+
import { createAxle } from '@varlet/axle'
|
|
207
|
+
import { createUseAxle } from '@varlet/axle/use'
|
|
208
|
+
|
|
209
|
+
const axle = createAxle(/** @see https://axios-http.com **/)
|
|
210
|
+
const useAxle = createUseAxle({
|
|
211
|
+
axle,
|
|
212
|
+
// Optional value: Default onTransform of the useAxle
|
|
213
|
+
onTransform: (response) => response,
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abort }] = useAxle({
|
|
217
|
+
// Request initial value
|
|
218
|
+
value: [],
|
|
219
|
+
// Request method
|
|
220
|
+
method: 'get',
|
|
221
|
+
// Request url
|
|
222
|
+
url: '/user',
|
|
223
|
+
// Whether to send the request immediately, defaults false
|
|
224
|
+
immediate: true,
|
|
225
|
+
// The number of retries after a failed request, defaults 0
|
|
226
|
+
retry: 3,
|
|
227
|
+
// Whether the value needs to be reset before requesting, defaults false
|
|
228
|
+
resetValue: true,
|
|
229
|
+
// Request params, defaults {}
|
|
230
|
+
// When params is an object, it will be carried when sending the first request (immediate)
|
|
231
|
+
// When params is a function, it will be carried every time a request is sent.
|
|
232
|
+
params: { current: 1, pageSize: 10 },
|
|
233
|
+
// Axios config, see https://axios-http.com
|
|
234
|
+
config: { headers: {} },
|
|
235
|
+
// lifecycle
|
|
236
|
+
onBefore(refs) {
|
|
237
|
+
const { data, loading, error, uploadProgress, downloadProgress } = refs
|
|
238
|
+
console.log(data.value, loading.value, error.value, uploadProgress.value, downloadProgress.value)
|
|
239
|
+
// Do request before
|
|
240
|
+
},
|
|
241
|
+
onTransform(response, refs) {
|
|
242
|
+
// Handle data transform, The transformed data will be the value of users.
|
|
243
|
+
return response.data
|
|
244
|
+
},
|
|
245
|
+
onSuccess(response, refs) {
|
|
246
|
+
// Do request success
|
|
247
|
+
},
|
|
248
|
+
onError(error, refs) {
|
|
249
|
+
// Do request error
|
|
250
|
+
},
|
|
251
|
+
onAfter(refs) {
|
|
252
|
+
// Do request after
|
|
253
|
+
},
|
|
254
|
+
})
|
|
255
255
|
</script>
|
|
256
256
|
|
|
257
257
|
<template>
|
|
@@ -271,43 +271,43 @@ Axle provides some parallel request processing tools, please refer to the follow
|
|
|
271
271
|
|
|
272
272
|
```html
|
|
273
273
|
<script setup>
|
|
274
|
-
import { createAxle } from '@varlet/axle'
|
|
275
|
-
import { createUseAxle, useValues, useAverageProgress, useHasLoading } from '@varlet/axle/use'
|
|
276
|
-
|
|
277
|
-
const axle = createAxle(/** @see https://axios-http.com **/)
|
|
278
|
-
const useAxle = createUseAxle({ axle })
|
|
279
|
-
|
|
280
|
-
const [users, getUsers, { loading: isUsersLoading, downloadProgress: usersDownloadProgress }] = useAxle({
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
})
|
|
285
|
-
|
|
286
|
-
const [roles, getRoles, { loading: isRolesLoading, downloadProgress: rolesDownloadProgress }] = useAxle({
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
})
|
|
291
|
-
|
|
292
|
-
// At the end of all requests, loading is false
|
|
293
|
-
const loading = useHasLoading(isUsersLoading, isRolesLoading)
|
|
294
|
-
// At the end of all requests, downloadProgress is 1
|
|
295
|
-
const downloadProgress = useAverageProgress(usersDownloadProgress, rolesDownloadProgress)
|
|
296
|
-
// Ref<[
|
|
297
|
-
// [{ name: 'foo' }, { name: 'bar' }],
|
|
298
|
-
// [{ role: 'admin' }, { role: 'user' }]
|
|
299
|
-
// ]> <-
|
|
300
|
-
// [
|
|
301
|
-
// Ref<[{ name: 'foo' }, { name: 'bar' }]>,
|
|
302
|
-
// Ref<[{ role: 'admin' }, { role: 'user' }]>
|
|
303
|
-
// ]
|
|
304
|
-
const usersRoles = useValues(users, roles)
|
|
305
|
-
|
|
306
|
-
function sendAllRequest() {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
274
|
+
import { createAxle } from '@varlet/axle'
|
|
275
|
+
import { createUseAxle, useValues, useAverageProgress, useHasLoading } from '@varlet/axle/use'
|
|
276
|
+
|
|
277
|
+
const axle = createAxle(/** @see https://axios-http.com **/)
|
|
278
|
+
const useAxle = createUseAxle({ axle })
|
|
279
|
+
|
|
280
|
+
const [users, getUsers, { loading: isUsersLoading, downloadProgress: usersDownloadProgress }] = useAxle({
|
|
281
|
+
value: [],
|
|
282
|
+
method: 'get',
|
|
283
|
+
url: '/user',
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
const [roles, getRoles, { loading: isRolesLoading, downloadProgress: rolesDownloadProgress }] = useAxle({
|
|
287
|
+
value: [],
|
|
288
|
+
method: 'get',
|
|
289
|
+
url: '/role',
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
// At the end of all requests, loading is false
|
|
293
|
+
const loading = useHasLoading(isUsersLoading, isRolesLoading)
|
|
294
|
+
// At the end of all requests, downloadProgress is 1
|
|
295
|
+
const downloadProgress = useAverageProgress(usersDownloadProgress, rolesDownloadProgress)
|
|
296
|
+
// Ref<[
|
|
297
|
+
// [{ name: 'foo' }, { name: 'bar' }],
|
|
298
|
+
// [{ role: 'admin' }, { role: 'user' }]
|
|
299
|
+
// ]> <-
|
|
300
|
+
// [
|
|
301
|
+
// Ref<[{ name: 'foo' }, { name: 'bar' }]>,
|
|
302
|
+
// Ref<[{ role: 'admin' }, { role: 'user' }]>
|
|
303
|
+
// ]
|
|
304
|
+
const usersRoles = useValues(users, roles)
|
|
305
|
+
|
|
306
|
+
function sendAllRequest() {
|
|
307
|
+
// parallel
|
|
308
|
+
getUsers()
|
|
309
|
+
getRoles()
|
|
310
|
+
}
|
|
311
311
|
</script>
|
|
312
312
|
|
|
313
313
|
<template>
|
|
@@ -317,7 +317,3 @@ function sendAllRequest() {
|
|
|
317
317
|
<button @click="sendAllRequest">Send All Request</button>
|
|
318
318
|
</template>
|
|
319
319
|
```
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
package/README.zh-CN.md
CHANGED
|
@@ -49,24 +49,30 @@ axios.defaults.headers.common['TOKEN'] = TOKEN
|
|
|
49
49
|
axios.defaults.timeout = 2500
|
|
50
50
|
|
|
51
51
|
// 添加请求前拦截器
|
|
52
|
-
axios.interceptors.request.use(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
axios.interceptors.request.use(
|
|
53
|
+
(config) => {
|
|
54
|
+
// 请求前处理
|
|
55
|
+
return config
|
|
56
|
+
},
|
|
57
|
+
(error) => {
|
|
58
|
+
// 请求错误处理
|
|
59
|
+
return Promise.reject(error)
|
|
60
|
+
}
|
|
61
|
+
)
|
|
59
62
|
|
|
60
63
|
// 添加请求后返回拦截器
|
|
61
|
-
axios.interceptors.response.use(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
64
|
+
axios.interceptors.response.use(
|
|
65
|
+
(response) => {
|
|
66
|
+
// 任何位于 2xx 范围内的状态码都会导致该函数触发
|
|
67
|
+
// 对响应数据做一些事情
|
|
68
|
+
return response
|
|
69
|
+
},
|
|
70
|
+
(error) => {
|
|
71
|
+
// 任何超出 2xx 范围的状态代码都会导致此函数触发
|
|
72
|
+
// 对响应错误做一些事情
|
|
73
|
+
return Promise.reject(error)
|
|
74
|
+
}
|
|
75
|
+
)
|
|
70
76
|
```
|
|
71
77
|
|
|
72
78
|
## Axle & Axios 请求函数
|
|
@@ -147,10 +153,10 @@ axle.post('/url', { name: 'foo' })
|
|
|
147
153
|
|
|
148
154
|
```js
|
|
149
155
|
// axios
|
|
150
|
-
axios.post('/url', qs.stringify({ name: 'foo' }), {
|
|
151
|
-
headers: {
|
|
152
|
-
'Content-Type': 'application/x-www-form-urlencoded'
|
|
153
|
-
}
|
|
156
|
+
axios.post('/url', qs.stringify({ name: 'foo' }), {
|
|
157
|
+
headers: {
|
|
158
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
159
|
+
},
|
|
154
160
|
})
|
|
155
161
|
// axle
|
|
156
162
|
axle.postUrlEncode('/url', { name: 'foo' })
|
|
@@ -164,10 +170,10 @@ const formData = new FormData()
|
|
|
164
170
|
formData.append('name', 'foo')
|
|
165
171
|
formData.append('file', new File())
|
|
166
172
|
|
|
167
|
-
axios.post('/url', formData, {
|
|
168
|
-
headers: {
|
|
169
|
-
'Content-Type': 'multipart/form-data'
|
|
170
|
-
}
|
|
173
|
+
axios.post('/url', formData, {
|
|
174
|
+
headers: {
|
|
175
|
+
'Content-Type': 'multipart/form-data',
|
|
176
|
+
},
|
|
171
177
|
})
|
|
172
178
|
// axle
|
|
173
179
|
axle.postMultipart('/url', { name: 'foo', file: new File() })
|
|
@@ -197,61 +203,55 @@ Axle 提供了 Vue Composition API 风格的用法,封装了请求的 `加载
|
|
|
197
203
|
|
|
198
204
|
```html
|
|
199
205
|
<script setup>
|
|
200
|
-
import { createAxle } from '@varlet/axle'
|
|
201
|
-
import { createUseAxle } from '@varlet/axle/use'
|
|
202
|
-
|
|
203
|
-
const axle = createAxle(/** @see https://axios-http.com **/)
|
|
204
|
-
const useAxle = createUseAxle({
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abort }] = useAxle({
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
// 处理请求错误逻辑
|
|
250
|
-
},
|
|
251
|
-
onAfter(refs) {
|
|
252
|
-
// 处理请求结束逻辑,无论成功失败都会触发。
|
|
253
|
-
}
|
|
254
|
-
})
|
|
206
|
+
import { createAxle } from '@varlet/axle'
|
|
207
|
+
import { createUseAxle } from '@varlet/axle/use'
|
|
208
|
+
|
|
209
|
+
const axle = createAxle(/** @see https://axios-http.com **/)
|
|
210
|
+
const useAxle = createUseAxle({
|
|
211
|
+
axle,
|
|
212
|
+
// 可选项: useAxle 的默认 onTransform
|
|
213
|
+
onTransform: (response) => response,
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abort }] = useAxle({
|
|
217
|
+
// 请求初始化数据
|
|
218
|
+
value: [],
|
|
219
|
+
// 请求方法
|
|
220
|
+
method: 'get',
|
|
221
|
+
// 请求地址
|
|
222
|
+
url: '/user',
|
|
223
|
+
// 是否立即发送请求, 默认值: false
|
|
224
|
+
immediate: true,
|
|
225
|
+
// 错误重试次数, 默认值: 0
|
|
226
|
+
retry: 3,
|
|
227
|
+
// 请求前是否需要重置 value, 默认值: false
|
|
228
|
+
resetValue: true,
|
|
229
|
+
// 请求参数, 默认值: {}
|
|
230
|
+
// 当参数是一个对象时,发送第一个请求(immediate)时将携带它
|
|
231
|
+
// 当参数是一个函数时,每次发送请求时都会携带它。
|
|
232
|
+
params: { current: 1, pageSize: 10 },
|
|
233
|
+
// Axios 配置, see https://axios-http.com
|
|
234
|
+
config: { headers: {} },
|
|
235
|
+
// 生命周期
|
|
236
|
+
onBefore(refs) {
|
|
237
|
+
const { data, loading, error, uploadProgress, downloadProgress } = refs
|
|
238
|
+
console.log(data.value, loading.value, error.value, uploadProgress.value, downloadProgress.value)
|
|
239
|
+
// 处理请求前逻辑
|
|
240
|
+
},
|
|
241
|
+
onTransform(response, refs) {
|
|
242
|
+
// 处理数据转换,转换后的数据将成为 users 的值。
|
|
243
|
+
return response.data
|
|
244
|
+
},
|
|
245
|
+
onSuccess(response, refs) {
|
|
246
|
+
// 处理请求成功逻辑
|
|
247
|
+
},
|
|
248
|
+
onError(error, refs) {
|
|
249
|
+
// 处理请求错误逻辑
|
|
250
|
+
},
|
|
251
|
+
onAfter(refs) {
|
|
252
|
+
// 处理请求结束逻辑,无论成功失败都会触发。
|
|
253
|
+
},
|
|
254
|
+
})
|
|
255
255
|
</script>
|
|
256
256
|
|
|
257
257
|
<template>
|
|
@@ -271,43 +271,43 @@ Axle 提供了一些并行请求处理工具,请参考以下示例。
|
|
|
271
271
|
|
|
272
272
|
```html
|
|
273
273
|
<script setup>
|
|
274
|
-
import { createAxle } from '@varlet/axle'
|
|
275
|
-
import { createUseAxle, useValues, useAverageProgress, useHasLoading } from '@varlet/axle/use'
|
|
276
|
-
|
|
277
|
-
const axle = createAxle(/** @see https://axios-http.com **/)
|
|
278
|
-
const useAxle = createUseAxle({ axle })
|
|
279
|
-
|
|
280
|
-
const [users, getUsers, { loading: isUsersLoading, downloadProgress: usersDownloadProgress }] = useAxle({
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
})
|
|
285
|
-
|
|
286
|
-
const [roles, getRoles, { loading: isRolesLoading, downloadProgress: rolesDownloadProgress }] = useAxle({
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
})
|
|
291
|
-
|
|
292
|
-
// 所有请求结束时,loading 为 false
|
|
293
|
-
const loading = useHasLoading(isUsersLoading, isRolesLoading)
|
|
294
|
-
// 所有请求结束时,downloadProgress 为 1
|
|
295
|
-
const downloadProgress = useAverageProgress(usersDownloadProgress, rolesDownloadProgress)
|
|
296
|
-
// Ref<[
|
|
297
|
-
// [{ name: 'foo' }, { name: 'bar' }],
|
|
298
|
-
// [{ role: 'admin' }, { role: 'user' }]
|
|
299
|
-
// ]> <-
|
|
300
|
-
// [
|
|
301
|
-
// Ref<[{ name: 'foo' }, { name: 'bar' }]>,
|
|
302
|
-
// Ref<[{ role: 'admin' }, { role: 'user' }]>
|
|
303
|
-
// ]
|
|
304
|
-
const usersRoles = useValues(users, roles)
|
|
305
|
-
|
|
306
|
-
function sendAllRequest() {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
274
|
+
import { createAxle } from '@varlet/axle'
|
|
275
|
+
import { createUseAxle, useValues, useAverageProgress, useHasLoading } from '@varlet/axle/use'
|
|
276
|
+
|
|
277
|
+
const axle = createAxle(/** @see https://axios-http.com **/)
|
|
278
|
+
const useAxle = createUseAxle({ axle })
|
|
279
|
+
|
|
280
|
+
const [users, getUsers, { loading: isUsersLoading, downloadProgress: usersDownloadProgress }] = useAxle({
|
|
281
|
+
value: [],
|
|
282
|
+
method: 'get',
|
|
283
|
+
url: '/user',
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
const [roles, getRoles, { loading: isRolesLoading, downloadProgress: rolesDownloadProgress }] = useAxle({
|
|
287
|
+
value: [],
|
|
288
|
+
method: 'get',
|
|
289
|
+
url: '/role',
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
// 所有请求结束时,loading 为 false
|
|
293
|
+
const loading = useHasLoading(isUsersLoading, isRolesLoading)
|
|
294
|
+
// 所有请求结束时,downloadProgress 为 1
|
|
295
|
+
const downloadProgress = useAverageProgress(usersDownloadProgress, rolesDownloadProgress)
|
|
296
|
+
// Ref<[
|
|
297
|
+
// [{ name: 'foo' }, { name: 'bar' }],
|
|
298
|
+
// [{ role: 'admin' }, { role: 'user' }]
|
|
299
|
+
// ]> <-
|
|
300
|
+
// [
|
|
301
|
+
// Ref<[{ name: 'foo' }, { name: 'bar' }]>,
|
|
302
|
+
// Ref<[{ role: 'admin' }, { role: 'user' }]>
|
|
303
|
+
// ]
|
|
304
|
+
const usersRoles = useValues(users, roles)
|
|
305
|
+
|
|
306
|
+
function sendAllRequest() {
|
|
307
|
+
// parallel
|
|
308
|
+
getUsers()
|
|
309
|
+
getRoles()
|
|
310
|
+
}
|
|
311
311
|
</script>
|
|
312
312
|
|
|
313
313
|
<template>
|
|
@@ -317,7 +317,3 @@ function sendAllRequest() {
|
|
|
317
317
|
<button @click="sendAllRequest">发送全部请求</button>
|
|
318
318
|
</template>
|
|
319
319
|
```
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
package/es/use.d.ts
CHANGED
|
@@ -1,21 +1,83 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { Ref, WritableComputedRef } from 'vue';
|
|
2
|
+
import { AxiosRequestConfig, HeadersDefaults, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig, AxiosInterceptorOptions } from 'axios';
|
|
3
|
+
|
|
4
|
+
type AxleRequestConfig = AxiosRequestConfig & {
|
|
5
|
+
exceptionMessage?: any;
|
|
6
|
+
};
|
|
7
|
+
type FetchRunner = <T = any, R = AxiosResponse<T>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
|
|
8
|
+
type ModifyRunner = <T = any, R = AxiosResponse<T>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
|
|
9
|
+
type RunnerMethod = keyof Omit<AxleInstance, 'axios' | 'getHeaders' | 'setHeader' | 'removeHeader' | 'useRequestInterceptor' | 'useResponseInterceptor'>;
|
|
10
|
+
interface Interceptor<V> {
|
|
11
|
+
onFulfilled?: ((value: V) => any) | null;
|
|
12
|
+
onRejected?: ((error: any) => any) | null;
|
|
13
|
+
options?: AxiosInterceptorOptions;
|
|
14
|
+
}
|
|
15
|
+
type RequestInterceptor = Interceptor<InternalAxiosRequestConfig>;
|
|
16
|
+
type ResponseInterceptor<V = AxiosResponse<any, any>> = Interceptor<V>;
|
|
17
|
+
type AxleInstance = {
|
|
18
|
+
get: FetchRunner;
|
|
19
|
+
getBlob: FetchRunner;
|
|
20
|
+
getDocument: FetchRunner;
|
|
21
|
+
getText: FetchRunner;
|
|
22
|
+
getArrayBuffer: FetchRunner;
|
|
23
|
+
getStream: FetchRunner;
|
|
24
|
+
head: FetchRunner;
|
|
25
|
+
headBlob: FetchRunner;
|
|
26
|
+
headDocument: FetchRunner;
|
|
27
|
+
headText: FetchRunner;
|
|
28
|
+
headArrayBuffer: FetchRunner;
|
|
29
|
+
headStream: FetchRunner;
|
|
30
|
+
options: FetchRunner;
|
|
31
|
+
optionsBlob: FetchRunner;
|
|
32
|
+
optionsDocument: FetchRunner;
|
|
33
|
+
optionsText: FetchRunner;
|
|
34
|
+
optionsArrayBuffer: FetchRunner;
|
|
35
|
+
optionsStream: FetchRunner;
|
|
36
|
+
delete: FetchRunner;
|
|
37
|
+
deleteBlob: FetchRunner;
|
|
38
|
+
deleteDocument: FetchRunner;
|
|
39
|
+
deleteText: FetchRunner;
|
|
40
|
+
deleteArrayBuffer: FetchRunner;
|
|
41
|
+
deleteStream: FetchRunner;
|
|
42
|
+
post: ModifyRunner;
|
|
43
|
+
postUrlEncode: ModifyRunner;
|
|
44
|
+
postMultipart: ModifyRunner;
|
|
45
|
+
put: ModifyRunner;
|
|
46
|
+
putUrlEncode: ModifyRunner;
|
|
47
|
+
putMultipart: ModifyRunner;
|
|
48
|
+
patch: ModifyRunner;
|
|
49
|
+
patchUrlEncode: ModifyRunner;
|
|
50
|
+
patchMultipart: ModifyRunner;
|
|
51
|
+
getHeaders(): HeadersDefaults['common'];
|
|
52
|
+
setHeader(key: string, value: string): void;
|
|
53
|
+
removeHeader(key: string | string[]): void;
|
|
54
|
+
useRequestInterceptor(...interceptors: RequestInterceptor[]): void;
|
|
55
|
+
useResponseInterceptor(...interceptors: ResponseInterceptor[]): void;
|
|
56
|
+
axios: AxiosInstance;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
declare function useValues<T extends Ref<any>[]>(...valueCollection: T): WritableComputedRef<T[number]['value'][]>;
|
|
60
|
+
|
|
61
|
+
declare function useHasLoading(...loadings: Ref<boolean>[]): Ref<boolean>;
|
|
62
|
+
|
|
63
|
+
declare function useAverageProgress(...progressCollection: Ref<number>[]): Ref<number>;
|
|
64
|
+
|
|
65
|
+
interface RunOptions<P> {
|
|
4
66
|
url?: string;
|
|
5
67
|
params?: P;
|
|
6
68
|
config?: AxleRequestConfig;
|
|
7
69
|
resetValue?: boolean;
|
|
8
70
|
_retryCount?: number;
|
|
9
71
|
}
|
|
10
|
-
|
|
11
|
-
|
|
72
|
+
type Run<R, P> = (options?: RunOptions<P>) => Promise<R>;
|
|
73
|
+
interface UseAxleRefs<V> {
|
|
12
74
|
value: Ref<V>;
|
|
13
75
|
loading: Ref<boolean>;
|
|
14
76
|
error: Ref<Error | undefined>;
|
|
15
77
|
uploadProgress: Ref<number>;
|
|
16
78
|
downloadProgress: Ref<number>;
|
|
17
79
|
}
|
|
18
|
-
|
|
80
|
+
interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
|
|
19
81
|
url: string;
|
|
20
82
|
method: RunnerMethod;
|
|
21
83
|
value?: V;
|
|
@@ -30,7 +92,7 @@ export interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
|
|
|
30
92
|
onSuccess?(response: R, refs: UseAxleRefs<V>): void;
|
|
31
93
|
onError?(error: Error, refs: UseAxleRefs<V>): void;
|
|
32
94
|
}
|
|
33
|
-
|
|
95
|
+
type UseAxleInstance<V, R, P> = [
|
|
34
96
|
value: Ref<V>,
|
|
35
97
|
run: Run<R, P>,
|
|
36
98
|
extra: {
|
|
@@ -41,11 +103,10 @@ export type UseAxleInstance<V, R, P> = [
|
|
|
41
103
|
abort(): void;
|
|
42
104
|
}
|
|
43
105
|
];
|
|
44
|
-
|
|
106
|
+
interface CreateUseAxleOptions {
|
|
45
107
|
axle: AxleInstance;
|
|
46
108
|
onTransform?(response: any, refs: any): any;
|
|
47
109
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
export
|
|
51
|
-
export * from './useAverageProgress.js';
|
|
110
|
+
declare function createUseAxle(options: CreateUseAxleOptions): <V = any, R = any, P = Record<string, any>>(options: UseAxleOptions<V, R, P>) => UseAxleInstance<V, R, P>;
|
|
111
|
+
|
|
112
|
+
export { CreateUseAxleOptions, Run, RunOptions, UseAxleInstance, UseAxleOptions, UseAxleRefs, createUseAxle, useAverageProgress, useHasLoading, useValues };
|