@varlet/axle 0.9.4 → 0.10.0
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 +149 -7
- package/README.zh-CN.md +149 -7
- package/lib/index.cjs +12 -7
- package/lib/index.js +3 -0
- package/lib/use.cjs +69 -62
- package/lib/use.d.cts +13 -14
- package/lib/use.d.ts +13 -14
- package/lib/use.js +69 -62
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -295,21 +295,27 @@ const axle = createAxle(/** @see https://axios-http.com **/)
|
|
|
295
295
|
|
|
296
296
|
const useAxle = createUseAxle({
|
|
297
297
|
axle,
|
|
298
|
-
// Optional value: Default immediate of the useAxle
|
|
299
|
-
immediate:
|
|
298
|
+
// Optional value: Default immediate of the useAxle, defaults true
|
|
299
|
+
immediate: false,
|
|
300
300
|
// Optional value: Default onTransform of the useAxle
|
|
301
301
|
onTransform: (response) => response,
|
|
302
302
|
})
|
|
303
303
|
|
|
304
|
-
const [
|
|
304
|
+
const [
|
|
305
|
+
users,
|
|
306
|
+
// request runner/invoker
|
|
307
|
+
getUsers,
|
|
308
|
+
// extra properties
|
|
309
|
+
{ loading, error, uploadProgress, downloadProgress, abort, resetValue }
|
|
310
|
+
] = useAxle({
|
|
305
311
|
// Request initial value
|
|
306
312
|
value: [],
|
|
307
313
|
// Request method
|
|
308
314
|
method: 'get',
|
|
309
315
|
// Request url can be a getter function
|
|
310
316
|
url: '/user',
|
|
311
|
-
// Whether to send the request immediately, defaults
|
|
312
|
-
immediate:
|
|
317
|
+
// Whether to send the request immediately, defaults true
|
|
318
|
+
immediate: false,
|
|
313
319
|
// Whether the value needs to be reset before requesting, defaults false
|
|
314
320
|
resetValue: true,
|
|
315
321
|
// Whether to clone when resetting value, defaults false
|
|
@@ -367,13 +373,11 @@ const axle = createAxle(/** @see https://axios-http.com **/)
|
|
|
367
373
|
const useAxle = createUseAxle({ axle })
|
|
368
374
|
|
|
369
375
|
const [users, getUsers, { loading: isUsersLoading, downloadProgress: usersDownloadProgress }] = useAxle({
|
|
370
|
-
value: [],
|
|
371
376
|
method: 'get',
|
|
372
377
|
url: '/user',
|
|
373
378
|
})
|
|
374
379
|
|
|
375
380
|
const [roles, getRoles, { loading: isRolesLoading, downloadProgress: rolesDownloadProgress }] = useAxle({
|
|
376
|
-
value: [],
|
|
377
381
|
method: 'get',
|
|
378
382
|
url: '/role',
|
|
379
383
|
})
|
|
@@ -406,3 +410,141 @@ function sendAllRequest() {
|
|
|
406
410
|
<button @click="sendAllRequest">Send All Request</button>
|
|
407
411
|
</template>
|
|
408
412
|
```
|
|
413
|
+
|
|
414
|
+
### API Definition Enhancement
|
|
415
|
+
|
|
416
|
+
`createApi` is supported since `v0.9.0`, which is used to define APIs.
|
|
417
|
+
|
|
418
|
+
#### Define APIs
|
|
419
|
+
|
|
420
|
+
```ts
|
|
421
|
+
import { createAxle } from '@varlet/axle'
|
|
422
|
+
import { createUseAxle } from '@varlet/axle/use'
|
|
423
|
+
import { createApi } from '@varlet/axle/api'
|
|
424
|
+
|
|
425
|
+
const axle = createAxle({
|
|
426
|
+
baseURL: '/api',
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
const useAxle = createUseAxle({
|
|
430
|
+
axle,
|
|
431
|
+
})
|
|
432
|
+
|
|
433
|
+
const api = createApi(axle, useAxle)
|
|
434
|
+
|
|
435
|
+
export const apiGetUsers = api<Response<User[]>>('/user', 'get')
|
|
436
|
+
|
|
437
|
+
export const apiGetUser = api<Response<User>>('/user/:id', 'get')
|
|
438
|
+
|
|
439
|
+
export const apiCreateUser = api<Response<User>, CreateUser>('/user', 'post')
|
|
440
|
+
|
|
441
|
+
export const apiUpdateUser = api<Response<User>, UpdateUser>('/user/:id', 'put')
|
|
442
|
+
|
|
443
|
+
export const apiDeleteUser = api<Response<User>>('/user/:id', 'delete')
|
|
444
|
+
|
|
445
|
+
export type Response<T> = {
|
|
446
|
+
data: T
|
|
447
|
+
code: number
|
|
448
|
+
message: string
|
|
449
|
+
success: boolean
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export interface User {
|
|
453
|
+
id: string
|
|
454
|
+
name: string
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export interface CreateUser {
|
|
458
|
+
name: string
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export interface UpdateUser {
|
|
462
|
+
name: string
|
|
463
|
+
}
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
#### Invoke APIs
|
|
467
|
+
|
|
468
|
+
```ts
|
|
469
|
+
const route = useRoute()
|
|
470
|
+
|
|
471
|
+
const [users, getUsers] = apiGetUsers.use<Response<User[]>>(/** same as useAxle and extends pathParams **/)
|
|
472
|
+
|
|
473
|
+
const [user, getUser] = apiGetUser.use<Response<User>>({
|
|
474
|
+
pathParams: () => ({ id: route.params.id }),
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
async function handleCreate(params: CreateUser) {
|
|
478
|
+
const { success } = await apiCreateUser.load(params)
|
|
479
|
+
|
|
480
|
+
if (success) {
|
|
481
|
+
getUsers()
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
async function handleUpdate(params: UpdateUser, id: string) {
|
|
486
|
+
const { success } = await apiUpdateUser.load(params, { id })
|
|
487
|
+
|
|
488
|
+
if (success) {
|
|
489
|
+
getUsers()
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
async function handleDelete(id: string) {
|
|
494
|
+
const { success } = await apiDeleteUser.load({}, { id })
|
|
495
|
+
|
|
496
|
+
if (success) {
|
|
497
|
+
getUsers()
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
### Runner Enhancement
|
|
503
|
+
|
|
504
|
+
Since `v0.10.0`, the `runner` will include all the extra properties, so we can further simplify the work.
|
|
505
|
+
|
|
506
|
+
before:
|
|
507
|
+
|
|
508
|
+
```html
|
|
509
|
+
<script setup>
|
|
510
|
+
const [users, getUsers, { loading: isUsersLoading }] = useAxle({
|
|
511
|
+
method: 'get',
|
|
512
|
+
url: '/user',
|
|
513
|
+
})
|
|
514
|
+
|
|
515
|
+
const [posts, getPosts, { loading: isPostsLoading }] = useAxle({
|
|
516
|
+
method: 'get',
|
|
517
|
+
url: '/post',
|
|
518
|
+
})
|
|
519
|
+
</script>
|
|
520
|
+
|
|
521
|
+
<template>
|
|
522
|
+
<span>{{ isUsersLoading ? 'loading...' : users }}</span>
|
|
523
|
+
<span>{{ isPostsLoading ? 'loading...' : posts }}</span>
|
|
524
|
+
<button @click="getUsers">Send Request</button>
|
|
525
|
+
<button @click="getPosts">Send Request</button>
|
|
526
|
+
</template>
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
after:
|
|
530
|
+
|
|
531
|
+
```html
|
|
532
|
+
<script setup>
|
|
533
|
+
const [users, getUsers] = useAxle({
|
|
534
|
+
method: 'get',
|
|
535
|
+
url: '/user',
|
|
536
|
+
})
|
|
537
|
+
|
|
538
|
+
const [posts, getPosts] = useAxle({
|
|
539
|
+
method: 'get',
|
|
540
|
+
url: '/post',
|
|
541
|
+
})
|
|
542
|
+
</script>
|
|
543
|
+
|
|
544
|
+
<template>
|
|
545
|
+
<span>{{ getUsers.loading ? 'loading...' : users }}</span>
|
|
546
|
+
<span>{{ getPosts.loading ? 'loading...' : posts }}</span>
|
|
547
|
+
<button @click="getUsers">Send Request</button>
|
|
548
|
+
<button @click="getPosts">Send Request</button>
|
|
549
|
+
</template>
|
|
550
|
+
```
|
package/README.zh-CN.md
CHANGED
|
@@ -295,21 +295,27 @@ const axle = createAxle(/** @see https://axios-http.com **/)
|
|
|
295
295
|
|
|
296
296
|
const useAxle = createUseAxle({
|
|
297
297
|
axle,
|
|
298
|
-
// 可选项: useAxle 的默认 immediate
|
|
299
|
-
immediate:
|
|
298
|
+
// 可选项: useAxle 的默认 immediate, 默认值: true
|
|
299
|
+
immediate: false,
|
|
300
300
|
// 可选项: useAxle 的默认 onTransform
|
|
301
301
|
onTransform: (response) => response,
|
|
302
302
|
})
|
|
303
303
|
|
|
304
|
-
const [
|
|
304
|
+
const [
|
|
305
|
+
users,
|
|
306
|
+
// 请求触发器
|
|
307
|
+
getUsers,
|
|
308
|
+
// 附加属性
|
|
309
|
+
{ loading, error, uploadProgress, downloadProgress, abort }
|
|
310
|
+
] = useAxle({
|
|
305
311
|
// 请求初始化数据
|
|
306
312
|
value: [],
|
|
307
313
|
// 请求方法
|
|
308
314
|
method: 'get',
|
|
309
315
|
// 请求地址, 可以是 getter 函数
|
|
310
316
|
url: '/user',
|
|
311
|
-
// 是否立即发送请求, 默认值:
|
|
312
|
-
immediate:
|
|
317
|
+
// 是否立即发送请求, 默认值: true
|
|
318
|
+
immediate: false,
|
|
313
319
|
// 请求前是否需要重置 value, 默认值: false
|
|
314
320
|
resetValue: true,
|
|
315
321
|
// 重置 value 是否对 value 进行拷贝
|
|
@@ -367,13 +373,11 @@ const axle = createAxle(/** @see https://axios-http.com **/)
|
|
|
367
373
|
const useAxle = createUseAxle({ axle })
|
|
368
374
|
|
|
369
375
|
const [users, getUsers, { loading: isUsersLoading, downloadProgress: usersDownloadProgress }] = useAxle({
|
|
370
|
-
value: [],
|
|
371
376
|
method: 'get',
|
|
372
377
|
url: '/user',
|
|
373
378
|
})
|
|
374
379
|
|
|
375
380
|
const [roles, getRoles, { loading: isRolesLoading, downloadProgress: rolesDownloadProgress }] = useAxle({
|
|
376
|
-
value: [],
|
|
377
381
|
method: 'get',
|
|
378
382
|
url: '/role',
|
|
379
383
|
})
|
|
@@ -406,3 +410,141 @@ function sendAllRequest() {
|
|
|
406
410
|
<button @click="sendAllRequest">发送全部请求</button>
|
|
407
411
|
</template>
|
|
408
412
|
```
|
|
413
|
+
|
|
414
|
+
### API 定义增强
|
|
415
|
+
|
|
416
|
+
从 `0.9.0` 开始支持 `createApi`,以增强 API 定义能力。
|
|
417
|
+
|
|
418
|
+
#### 定义 API
|
|
419
|
+
|
|
420
|
+
```ts
|
|
421
|
+
import { createAxle } from '@varlet/axle'
|
|
422
|
+
import { createUseAxle } from '@varlet/axle/use'
|
|
423
|
+
import { createApi } from '@varlet/axle/api'
|
|
424
|
+
|
|
425
|
+
const axle = createAxle({
|
|
426
|
+
baseURL: '/api',
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
const useAxle = createUseAxle({
|
|
430
|
+
axle,
|
|
431
|
+
})
|
|
432
|
+
|
|
433
|
+
const api = createApi(axle, useAxle)
|
|
434
|
+
|
|
435
|
+
export const apiGetUsers = api<Response<User[]>>('/user', 'get')
|
|
436
|
+
|
|
437
|
+
export const apiGetUser = api<Response<User>>('/user/:id', 'get')
|
|
438
|
+
|
|
439
|
+
export const apiCreateUser = api<Response<User>, CreateUser>('/user', 'post')
|
|
440
|
+
|
|
441
|
+
export const apiUpdateUser = api<Response<User>, UpdateUser>('/user/:id', 'put')
|
|
442
|
+
|
|
443
|
+
export const apiDeleteUser = api<Response<User>>('/user/:id', 'delete')
|
|
444
|
+
|
|
445
|
+
export type Response<T> = {
|
|
446
|
+
data: T
|
|
447
|
+
code: number
|
|
448
|
+
message: string
|
|
449
|
+
success: boolean
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export interface User {
|
|
453
|
+
id: string
|
|
454
|
+
name: string
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export interface CreateUser {
|
|
458
|
+
name: string
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export interface UpdateUser {
|
|
462
|
+
name: string
|
|
463
|
+
}
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
#### 调用 API
|
|
467
|
+
|
|
468
|
+
```ts
|
|
469
|
+
const route = useRoute()
|
|
470
|
+
|
|
471
|
+
const [users, getUsers] = apiGetUsers.use<Response<User[]>>(/** 和 useAxle 一致并且扩展了 pathParams **/)
|
|
472
|
+
|
|
473
|
+
const [user, getUser] = apiGetUser.use<Response<User>>({
|
|
474
|
+
pathParams: () => ({ id: route.params.id }),
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
async function handleCreate(params: CreateUser) {
|
|
478
|
+
const { success } = await apiCreateUser.load(params)
|
|
479
|
+
|
|
480
|
+
if (success) {
|
|
481
|
+
getUsers()
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
async function handleUpdate(params: UpdateUser, id: string) {
|
|
486
|
+
const { success } = await apiUpdateUser.load(params, { id })
|
|
487
|
+
|
|
488
|
+
if (success) {
|
|
489
|
+
getUsers()
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
async function handleDelete(id: string) {
|
|
494
|
+
const { success } = await apiDeleteUser.load({}, { id })
|
|
495
|
+
|
|
496
|
+
if (success) {
|
|
497
|
+
getUsers()
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
### 请求触发器增强
|
|
503
|
+
|
|
504
|
+
从 `v0.10.0` 开始, 请求触发器将包含附加属性中的全部属性。
|
|
505
|
+
|
|
506
|
+
增强前:
|
|
507
|
+
|
|
508
|
+
```html
|
|
509
|
+
<script setup>
|
|
510
|
+
const [users, getUsers, { loading: isUsersLoading }] = useAxle({
|
|
511
|
+
method: 'get',
|
|
512
|
+
url: '/user',
|
|
513
|
+
})
|
|
514
|
+
|
|
515
|
+
const [posts, getPosts, { loading: isPostsLoading }] = useAxle({
|
|
516
|
+
method: 'get',
|
|
517
|
+
url: '/post',
|
|
518
|
+
})
|
|
519
|
+
</script>
|
|
520
|
+
|
|
521
|
+
<template>
|
|
522
|
+
<span>{{ isUsersLoading ? 'loading...' : users }}</span>
|
|
523
|
+
<span>{{ isPostsLoading ? 'loading...' : posts }}</span>
|
|
524
|
+
<button @click="getUsers">Send Request</button>
|
|
525
|
+
<button @click="getPosts">Send Request</button>
|
|
526
|
+
</template>
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
增强后:
|
|
530
|
+
|
|
531
|
+
```html
|
|
532
|
+
<script setup>
|
|
533
|
+
const [users, getUsers] = useAxle({
|
|
534
|
+
method: 'get',
|
|
535
|
+
url: '/user',
|
|
536
|
+
})
|
|
537
|
+
|
|
538
|
+
const [posts, getPosts] = useAxle({
|
|
539
|
+
method: 'get',
|
|
540
|
+
url: '/post',
|
|
541
|
+
})
|
|
542
|
+
</script>
|
|
543
|
+
|
|
544
|
+
<template>
|
|
545
|
+
<span>{{ getUsers.loading ? 'loading...' : users }}</span>
|
|
546
|
+
<span>{{ getPosts.loading ? 'loading...' : posts }}</span>
|
|
547
|
+
<button @click="getUsers">Send Request</button>
|
|
548
|
+
<button @click="getPosts">Send Request</button>
|
|
549
|
+
</template>
|
|
550
|
+
```
|
package/lib/index.cjs
CHANGED
|
@@ -47,11 +47,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
47
47
|
// src/index.ts
|
|
48
48
|
var src_exports = {};
|
|
49
49
|
__export(src_exports, {
|
|
50
|
+
AxiosError: () => import_axios2.AxiosError,
|
|
50
51
|
createAxle: () => createAxle,
|
|
51
52
|
createFetchRunner: () => createFetchRunner,
|
|
52
53
|
createMatcher: () => createMatcher,
|
|
53
54
|
createModifyRunner: () => createModifyRunner,
|
|
54
55
|
download: () => download,
|
|
56
|
+
isAxiosError: () => import_axios2.isAxiosError,
|
|
55
57
|
matchPattern: () => matchPattern,
|
|
56
58
|
requestHeadersInterceptor: () => requestHeadersInterceptor,
|
|
57
59
|
requestMd5Interceptor: () => requestMd5Interceptor,
|
|
@@ -86,6 +88,7 @@ function objectToFormData(object) {
|
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
// src/instance.ts
|
|
91
|
+
var import_axios2 = require("axios");
|
|
89
92
|
function createFetchRunner(service, method, responseType) {
|
|
90
93
|
return function(url, params, config) {
|
|
91
94
|
return service[method](url, __spreadValues({
|
|
@@ -207,7 +210,7 @@ function createMatcher(include, exclude) {
|
|
|
207
210
|
|
|
208
211
|
// src/helper.ts
|
|
209
212
|
var import_rattail = require("rattail");
|
|
210
|
-
var
|
|
213
|
+
var import_axios3 = require("axios");
|
|
211
214
|
function withResponse(promise) {
|
|
212
215
|
return new Promise((resolve, reject) => {
|
|
213
216
|
promise.then((response) => {
|
|
@@ -216,7 +219,7 @@ function withResponse(promise) {
|
|
|
216
219
|
errorResponse: void 0
|
|
217
220
|
});
|
|
218
221
|
}).catch((error) => {
|
|
219
|
-
if ((0,
|
|
222
|
+
if ((0, import_axios3.isAxiosError)(error)) {
|
|
220
223
|
resolve({
|
|
221
224
|
response: void 0,
|
|
222
225
|
errorResponse: error.response
|
|
@@ -264,7 +267,7 @@ function requestHeadersInterceptor(options = {}) {
|
|
|
264
267
|
}
|
|
265
268
|
|
|
266
269
|
// src/interceptors/requestMockInterceptor.ts
|
|
267
|
-
var
|
|
270
|
+
var import_axios4 = require("axios");
|
|
268
271
|
var import_minimatch2 = require("minimatch");
|
|
269
272
|
function settle(response, resolve, reject, delay = 0) {
|
|
270
273
|
if (delay > 0) {
|
|
@@ -276,7 +279,7 @@ function settle(response, resolve, reject, delay = 0) {
|
|
|
276
279
|
if (!response.config.validateStatus || response.config.validateStatus(response.status)) {
|
|
277
280
|
resolve(response);
|
|
278
281
|
} else {
|
|
279
|
-
reject(new
|
|
282
|
+
reject(new import_axios4.AxiosError(`Request failed with status code ${response.status}`, void 0, response.config, response));
|
|
280
283
|
}
|
|
281
284
|
}
|
|
282
285
|
function requestMockInterceptor(options = {}) {
|
|
@@ -361,14 +364,14 @@ function responseTimeoutInterceptor(options = {}) {
|
|
|
361
364
|
}
|
|
362
365
|
|
|
363
366
|
// src/interceptors/responseRetryInterceptor.ts
|
|
364
|
-
var
|
|
367
|
+
var import_axios5 = __toESM(require("axios"), 1);
|
|
365
368
|
function responseRetryInterceptor(options) {
|
|
366
369
|
return {
|
|
367
370
|
onFulfilled: (response) => response,
|
|
368
371
|
async onRejected(error) {
|
|
369
372
|
var _a, _b, _c;
|
|
370
373
|
const matcher = createMatcher(options.include, options.exclude);
|
|
371
|
-
if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "", (_c = error == null ? void 0 : error.response) == null ? void 0 : _c.status) || (0,
|
|
374
|
+
if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "", (_c = error == null ? void 0 : error.response) == null ? void 0 : _c.status) || (0, import_axios5.isCancel)(error)) {
|
|
372
375
|
return Promise.reject(error);
|
|
373
376
|
}
|
|
374
377
|
const { count = 1 } = options;
|
|
@@ -376,7 +379,7 @@ function responseRetryInterceptor(options) {
|
|
|
376
379
|
async function retry() {
|
|
377
380
|
try {
|
|
378
381
|
retryCount++;
|
|
379
|
-
return await
|
|
382
|
+
return await import_axios5.default.create()(error.config);
|
|
380
383
|
} catch (error2) {
|
|
381
384
|
if (retryCount === count) {
|
|
382
385
|
return Promise.reject(error2);
|
|
@@ -487,11 +490,13 @@ function requestMd5Interceptor(options = {}) {
|
|
|
487
490
|
}
|
|
488
491
|
// Annotate the CommonJS export names for ESM import in node:
|
|
489
492
|
0 && (module.exports = {
|
|
493
|
+
AxiosError,
|
|
490
494
|
createAxle,
|
|
491
495
|
createFetchRunner,
|
|
492
496
|
createMatcher,
|
|
493
497
|
createModifyRunner,
|
|
494
498
|
download,
|
|
499
|
+
isAxiosError,
|
|
495
500
|
matchPattern,
|
|
496
501
|
requestHeadersInterceptor,
|
|
497
502
|
requestMd5Interceptor,
|
package/lib/index.js
CHANGED
|
@@ -25,6 +25,7 @@ function objectToFormData(object) {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
// src/instance.ts
|
|
28
|
+
import { AxiosError, isAxiosError } from "axios";
|
|
28
29
|
function createFetchRunner(service, method, responseType) {
|
|
29
30
|
return function(url, params, config) {
|
|
30
31
|
return service[method](url, __spreadValues({
|
|
@@ -425,11 +426,13 @@ function requestMd5Interceptor(options = {}) {
|
|
|
425
426
|
};
|
|
426
427
|
}
|
|
427
428
|
export {
|
|
429
|
+
AxiosError,
|
|
428
430
|
createAxle,
|
|
429
431
|
createFetchRunner,
|
|
430
432
|
createMatcher,
|
|
431
433
|
createModifyRunner,
|
|
432
434
|
download,
|
|
435
|
+
isAxiosError,
|
|
433
436
|
matchPattern,
|
|
434
437
|
requestHeadersInterceptor,
|
|
435
438
|
requestMd5Interceptor,
|
package/lib/use.cjs
CHANGED
|
@@ -124,58 +124,67 @@ function createUseAxle(options) {
|
|
|
124
124
|
downloadProgress,
|
|
125
125
|
uploadProgress
|
|
126
126
|
};
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
127
|
+
const extra = {
|
|
128
|
+
uploadProgress,
|
|
129
|
+
downloadProgress,
|
|
130
|
+
loading,
|
|
131
|
+
error,
|
|
132
|
+
abort,
|
|
133
|
+
resetValue
|
|
133
134
|
};
|
|
134
|
-
|
|
135
|
-
|
|
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
|
-
|
|
135
|
+
let controller = new AbortController();
|
|
136
|
+
const run = Object.assign(
|
|
137
|
+
async (options3 = {}) => {
|
|
138
|
+
var _a, _b, _c, _d, _e;
|
|
139
|
+
if (controller.signal.aborted) {
|
|
140
|
+
controller = new AbortController();
|
|
141
|
+
}
|
|
142
|
+
const resetValueOption = (_b = (_a = options3.resetValue) != null ? _a : initialResetValue) != null ? _b : false;
|
|
143
|
+
if (resetValueOption === true) {
|
|
144
|
+
resetValue(options3);
|
|
145
|
+
}
|
|
146
|
+
uploadProgress.value = 0;
|
|
147
|
+
downloadProgress.value = 0;
|
|
148
|
+
const url = (_c = options3.url) != null ? _c : normalizeValueGetter(initialUrlOrGetter);
|
|
149
|
+
const params = (_d = options3.params) != null ? _d : normalizeValueGetter(initialParamsOrGetter);
|
|
150
|
+
const config = (_e = options3.config) != null ? _e : normalizeValueGetter(initialConfigOrGetter);
|
|
151
|
+
onBefore(refs);
|
|
152
|
+
loading.value = true;
|
|
153
|
+
try {
|
|
154
|
+
const response = await axle[method](url, params, __spreadValues({
|
|
155
|
+
signal: controller.signal,
|
|
156
|
+
onUploadProgress(event) {
|
|
157
|
+
var _a2;
|
|
158
|
+
uploadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
|
|
159
|
+
},
|
|
160
|
+
onDownloadProgress(event) {
|
|
161
|
+
var _a2;
|
|
162
|
+
downloadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
|
|
163
|
+
}
|
|
164
|
+
}, config));
|
|
165
|
+
value.value = await onTransform(response, refs);
|
|
166
|
+
error.value = void 0;
|
|
167
|
+
onSuccess(response, refs);
|
|
168
|
+
loading.value = false;
|
|
169
|
+
onAfter(refs);
|
|
170
|
+
return response;
|
|
171
|
+
} catch (responseError) {
|
|
172
|
+
error.value = responseError;
|
|
173
|
+
onError(responseError, refs);
|
|
174
|
+
loading.value = false;
|
|
175
|
+
onAfter(refs);
|
|
176
|
+
throw responseError;
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
uploadProgress,
|
|
181
|
+
downloadProgress,
|
|
182
|
+
loading,
|
|
183
|
+
error,
|
|
184
|
+
abort,
|
|
185
|
+
resetValue
|
|
174
186
|
}
|
|
175
|
-
|
|
176
|
-
const abort = () => {
|
|
177
|
-
controller.abort();
|
|
178
|
-
};
|
|
187
|
+
);
|
|
179
188
|
if (immediate) {
|
|
180
189
|
run({
|
|
181
190
|
url: normalizeValueGetter(initialUrlOrGetter),
|
|
@@ -183,18 +192,16 @@ function createUseAxle(options) {
|
|
|
183
192
|
config: normalizeValueGetter(initialConfigOrGetter)
|
|
184
193
|
});
|
|
185
194
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
];
|
|
195
|
+
function resetValue(options3 = {}) {
|
|
196
|
+
var _a, _b;
|
|
197
|
+
const cloneResetValue = (_b = (_a = options3.cloneResetValue) != null ? _a : initialCloneResetValue) != null ? _b : false;
|
|
198
|
+
const cloneFn = cloneResetValue === true ? (v) => v == null ? null : JSON.parse(JSON.stringify(v)) : (0, import_rattail.isFunction)(initialCloneResetValue) ? initialCloneResetValue : (v) => v;
|
|
199
|
+
value.value = cloneFn(initialValue);
|
|
200
|
+
}
|
|
201
|
+
function abort() {
|
|
202
|
+
controller.abort();
|
|
203
|
+
}
|
|
204
|
+
return [value, run, extra];
|
|
198
205
|
};
|
|
199
206
|
return useAxle;
|
|
200
207
|
}
|
package/lib/use.d.cts
CHANGED
|
@@ -15,7 +15,14 @@ interface RunOptions<V, P> {
|
|
|
15
15
|
resetValue?: boolean;
|
|
16
16
|
cloneResetValue?: boolean | ((value: V) => V);
|
|
17
17
|
}
|
|
18
|
-
type
|
|
18
|
+
type UseAxleExtra<V> = {
|
|
19
|
+
uploadProgress: Ref<number>;
|
|
20
|
+
downloadProgress: Ref<number>;
|
|
21
|
+
loading: Ref<boolean>;
|
|
22
|
+
error: Ref<Error | undefined>;
|
|
23
|
+
abort(): void;
|
|
24
|
+
resetValue(options?: ResetValueOptions<V>): void;
|
|
25
|
+
};
|
|
19
26
|
interface UseAxleRefs<V> {
|
|
20
27
|
value: Ref<V>;
|
|
21
28
|
loading: Ref<boolean>;
|
|
@@ -23,6 +30,9 @@ interface UseAxleRefs<V> {
|
|
|
23
30
|
uploadProgress: Ref<number>;
|
|
24
31
|
downloadProgress: Ref<number>;
|
|
25
32
|
}
|
|
33
|
+
type Run<V, R, P> = {
|
|
34
|
+
(options?: RunOptions<V, P>): Promise<R>;
|
|
35
|
+
} & UseAxleExtra<V>;
|
|
26
36
|
interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
|
|
27
37
|
url: string | (() => string);
|
|
28
38
|
method: RunnerMethod;
|
|
@@ -41,18 +51,7 @@ interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
|
|
|
41
51
|
interface ResetValueOptions<V> {
|
|
42
52
|
cloneResetValue?: boolean | ((value: V) => V);
|
|
43
53
|
}
|
|
44
|
-
type UseAxleInstance<V, R, P> = [
|
|
45
|
-
value: Ref<V>,
|
|
46
|
-
run: Run<V, R, P>,
|
|
47
|
-
extra: {
|
|
48
|
-
uploadProgress: Ref<number>;
|
|
49
|
-
downloadProgress: Ref<number>;
|
|
50
|
-
loading: Ref<boolean>;
|
|
51
|
-
error: Ref<Error | undefined>;
|
|
52
|
-
abort(): void;
|
|
53
|
-
resetValue(options?: ResetValueOptions<V>): void;
|
|
54
|
-
}
|
|
55
|
-
];
|
|
54
|
+
type UseAxleInstance<V, R, P> = [value: Ref<V>, run: Run<V, R, P>, extra: UseAxleExtra<V>];
|
|
56
55
|
interface CreateUseAxleOptions {
|
|
57
56
|
axle: AxleInstance;
|
|
58
57
|
immediate?: boolean;
|
|
@@ -62,4 +61,4 @@ type UseAxle = <V = any, R = any, P = Record<string, any>>(options: UseAxleOptio
|
|
|
62
61
|
declare function normalizeValueGetter<T>(valueGetter: T | (() => T)): T;
|
|
63
62
|
declare function createUseAxle(options: CreateUseAxleOptions): UseAxle;
|
|
64
63
|
|
|
65
|
-
export { CreateUseAxleOptions, ResetValueOptions, Run, RunOptions, UseAxle, UseAxleInstance, UseAxleOptions, UseAxleRefs, createUseAxle, normalizeValueGetter, useAverageProgress, useHasLoading, useValues };
|
|
64
|
+
export { CreateUseAxleOptions, ResetValueOptions, Run, RunOptions, UseAxle, UseAxleExtra, UseAxleInstance, UseAxleOptions, UseAxleRefs, createUseAxle, normalizeValueGetter, useAverageProgress, useHasLoading, useValues };
|
package/lib/use.d.ts
CHANGED
|
@@ -15,7 +15,14 @@ interface RunOptions<V, P> {
|
|
|
15
15
|
resetValue?: boolean;
|
|
16
16
|
cloneResetValue?: boolean | ((value: V) => V);
|
|
17
17
|
}
|
|
18
|
-
type
|
|
18
|
+
type UseAxleExtra<V> = {
|
|
19
|
+
uploadProgress: Ref<number>;
|
|
20
|
+
downloadProgress: Ref<number>;
|
|
21
|
+
loading: Ref<boolean>;
|
|
22
|
+
error: Ref<Error | undefined>;
|
|
23
|
+
abort(): void;
|
|
24
|
+
resetValue(options?: ResetValueOptions<V>): void;
|
|
25
|
+
};
|
|
19
26
|
interface UseAxleRefs<V> {
|
|
20
27
|
value: Ref<V>;
|
|
21
28
|
loading: Ref<boolean>;
|
|
@@ -23,6 +30,9 @@ interface UseAxleRefs<V> {
|
|
|
23
30
|
uploadProgress: Ref<number>;
|
|
24
31
|
downloadProgress: Ref<number>;
|
|
25
32
|
}
|
|
33
|
+
type Run<V, R, P> = {
|
|
34
|
+
(options?: RunOptions<V, P>): Promise<R>;
|
|
35
|
+
} & UseAxleExtra<V>;
|
|
26
36
|
interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
|
|
27
37
|
url: string | (() => string);
|
|
28
38
|
method: RunnerMethod;
|
|
@@ -41,18 +51,7 @@ interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
|
|
|
41
51
|
interface ResetValueOptions<V> {
|
|
42
52
|
cloneResetValue?: boolean | ((value: V) => V);
|
|
43
53
|
}
|
|
44
|
-
type UseAxleInstance<V, R, P> = [
|
|
45
|
-
value: Ref<V>,
|
|
46
|
-
run: Run<V, R, P>,
|
|
47
|
-
extra: {
|
|
48
|
-
uploadProgress: Ref<number>;
|
|
49
|
-
downloadProgress: Ref<number>;
|
|
50
|
-
loading: Ref<boolean>;
|
|
51
|
-
error: Ref<Error | undefined>;
|
|
52
|
-
abort(): void;
|
|
53
|
-
resetValue(options?: ResetValueOptions<V>): void;
|
|
54
|
-
}
|
|
55
|
-
];
|
|
54
|
+
type UseAxleInstance<V, R, P> = [value: Ref<V>, run: Run<V, R, P>, extra: UseAxleExtra<V>];
|
|
56
55
|
interface CreateUseAxleOptions {
|
|
57
56
|
axle: AxleInstance;
|
|
58
57
|
immediate?: boolean;
|
|
@@ -62,4 +61,4 @@ type UseAxle = <V = any, R = any, P = Record<string, any>>(options: UseAxleOptio
|
|
|
62
61
|
declare function normalizeValueGetter<T>(valueGetter: T | (() => T)): T;
|
|
63
62
|
declare function createUseAxle(options: CreateUseAxleOptions): UseAxle;
|
|
64
63
|
|
|
65
|
-
export { CreateUseAxleOptions, ResetValueOptions, Run, RunOptions, UseAxle, UseAxleInstance, UseAxleOptions, UseAxleRefs, createUseAxle, normalizeValueGetter, useAverageProgress, useHasLoading, useValues };
|
|
64
|
+
export { CreateUseAxleOptions, ResetValueOptions, Run, RunOptions, UseAxle, UseAxleExtra, UseAxleInstance, UseAxleOptions, UseAxleRefs, createUseAxle, normalizeValueGetter, useAverageProgress, useHasLoading, useValues };
|
package/lib/use.js
CHANGED
|
@@ -86,58 +86,67 @@ function createUseAxle(options) {
|
|
|
86
86
|
downloadProgress,
|
|
87
87
|
uploadProgress
|
|
88
88
|
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
89
|
+
const extra = {
|
|
90
|
+
uploadProgress,
|
|
91
|
+
downloadProgress,
|
|
92
|
+
loading,
|
|
93
|
+
error,
|
|
94
|
+
abort,
|
|
95
|
+
resetValue
|
|
95
96
|
};
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
97
|
+
let controller = new AbortController();
|
|
98
|
+
const run = Object.assign(
|
|
99
|
+
async (options3 = {}) => {
|
|
100
|
+
var _a, _b, _c, _d, _e;
|
|
101
|
+
if (controller.signal.aborted) {
|
|
102
|
+
controller = new AbortController();
|
|
103
|
+
}
|
|
104
|
+
const resetValueOption = (_b = (_a = options3.resetValue) != null ? _a : initialResetValue) != null ? _b : false;
|
|
105
|
+
if (resetValueOption === true) {
|
|
106
|
+
resetValue(options3);
|
|
107
|
+
}
|
|
108
|
+
uploadProgress.value = 0;
|
|
109
|
+
downloadProgress.value = 0;
|
|
110
|
+
const url = (_c = options3.url) != null ? _c : normalizeValueGetter(initialUrlOrGetter);
|
|
111
|
+
const params = (_d = options3.params) != null ? _d : normalizeValueGetter(initialParamsOrGetter);
|
|
112
|
+
const config = (_e = options3.config) != null ? _e : normalizeValueGetter(initialConfigOrGetter);
|
|
113
|
+
onBefore(refs);
|
|
114
|
+
loading.value = true;
|
|
115
|
+
try {
|
|
116
|
+
const response = await axle[method](url, params, __spreadValues({
|
|
117
|
+
signal: controller.signal,
|
|
118
|
+
onUploadProgress(event) {
|
|
119
|
+
var _a2;
|
|
120
|
+
uploadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
|
|
121
|
+
},
|
|
122
|
+
onDownloadProgress(event) {
|
|
123
|
+
var _a2;
|
|
124
|
+
downloadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
|
|
125
|
+
}
|
|
126
|
+
}, config));
|
|
127
|
+
value.value = await onTransform(response, refs);
|
|
128
|
+
error.value = void 0;
|
|
129
|
+
onSuccess(response, refs);
|
|
130
|
+
loading.value = false;
|
|
131
|
+
onAfter(refs);
|
|
132
|
+
return response;
|
|
133
|
+
} catch (responseError) {
|
|
134
|
+
error.value = responseError;
|
|
135
|
+
onError(responseError, refs);
|
|
136
|
+
loading.value = false;
|
|
137
|
+
onAfter(refs);
|
|
138
|
+
throw responseError;
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
uploadProgress,
|
|
143
|
+
downloadProgress,
|
|
144
|
+
loading,
|
|
145
|
+
error,
|
|
146
|
+
abort,
|
|
147
|
+
resetValue
|
|
136
148
|
}
|
|
137
|
-
|
|
138
|
-
const abort = () => {
|
|
139
|
-
controller.abort();
|
|
140
|
-
};
|
|
149
|
+
);
|
|
141
150
|
if (immediate) {
|
|
142
151
|
run({
|
|
143
152
|
url: normalizeValueGetter(initialUrlOrGetter),
|
|
@@ -145,18 +154,16 @@ function createUseAxle(options) {
|
|
|
145
154
|
config: normalizeValueGetter(initialConfigOrGetter)
|
|
146
155
|
});
|
|
147
156
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
];
|
|
157
|
+
function resetValue(options3 = {}) {
|
|
158
|
+
var _a, _b;
|
|
159
|
+
const cloneResetValue = (_b = (_a = options3.cloneResetValue) != null ? _a : initialCloneResetValue) != null ? _b : false;
|
|
160
|
+
const cloneFn = cloneResetValue === true ? (v) => v == null ? null : JSON.parse(JSON.stringify(v)) : isFunction(initialCloneResetValue) ? initialCloneResetValue : (v) => v;
|
|
161
|
+
value.value = cloneFn(initialValue);
|
|
162
|
+
}
|
|
163
|
+
function abort() {
|
|
164
|
+
controller.abort();
|
|
165
|
+
}
|
|
166
|
+
return [value, run, extra];
|
|
160
167
|
};
|
|
161
168
|
return useAxle;
|
|
162
169
|
}
|