@varlet/axle 0.9.5 → 0.10.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.
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: true,
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 [users, getUsers, { loading, error, uploadProgress, downloadProgress, abort, resetValue }] = useAxle({
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 false
312
- immediate: true,
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.value ? 'loading...' : users }}</span>
546
+ <span>{{ getPosts.loading.value ? '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: true,
298
+ // 可选项: useAxle 的默认 immediate, 默认值: true
299
+ immediate: false,
300
300
  // 可选项: useAxle 的默认 onTransform
301
301
  onTransform: (response) => response,
302
302
  })
303
303
 
304
- const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abort }] = useAxle({
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
- // 是否立即发送请求, 默认值: false
312
- immediate: true,
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.value ? 'loading...' : users }}</span>
546
+ <span>{{ getPosts.loading.value ? 'loading...' : posts }}</span>
547
+ <button @click="getUsers">Send Request</button>
548
+ <button @click="getPosts">Send Request</button>
549
+ </template>
550
+ ```
package/lib/use.cjs CHANGED
@@ -124,58 +124,67 @@ function createUseAxle(options) {
124
124
  downloadProgress,
125
125
  uploadProgress
126
126
  };
127
- let controller = new AbortController();
128
- const resetValue = (options3 = {}) => {
129
- var _a, _b;
130
- const cloneResetValue = (_b = (_a = options3.cloneResetValue) != null ? _a : initialCloneResetValue) != null ? _b : false;
131
- const cloneFn = cloneResetValue === true ? (v) => v == null ? null : JSON.parse(JSON.stringify(v)) : (0, import_rattail.isFunction)(initialCloneResetValue) ? initialCloneResetValue : (v) => v;
132
- value.value = cloneFn(initialValue);
127
+ const extra = {
128
+ uploadProgress,
129
+ downloadProgress,
130
+ loading,
131
+ error,
132
+ abort,
133
+ resetValue
133
134
  };
134
- const run = async (options3 = {}) => {
135
- var _a, _b, _c, _d, _e;
136
- if (controller.signal.aborted) {
137
- controller = new AbortController();
138
- }
139
- const resetValueOption = (_b = (_a = options3.resetValue) != null ? _a : initialResetValue) != null ? _b : false;
140
- if (resetValueOption === true) {
141
- resetValue(options3);
142
- }
143
- uploadProgress.value = 0;
144
- downloadProgress.value = 0;
145
- const url = (_c = options3.url) != null ? _c : normalizeValueGetter(initialUrlOrGetter);
146
- const params = (_d = options3.params) != null ? _d : normalizeValueGetter(initialParamsOrGetter);
147
- const config = (_e = options3.config) != null ? _e : normalizeValueGetter(initialConfigOrGetter);
148
- onBefore(refs);
149
- loading.value = true;
150
- try {
151
- const response = await axle[method](url, params, __spreadValues({
152
- signal: controller.signal,
153
- onUploadProgress(event) {
154
- var _a2;
155
- uploadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
156
- },
157
- onDownloadProgress(event) {
158
- var _a2;
159
- downloadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
160
- }
161
- }, config));
162
- value.value = await onTransform(response, refs);
163
- error.value = void 0;
164
- onSuccess(response, refs);
165
- loading.value = false;
166
- onAfter(refs);
167
- return response;
168
- } catch (responseError) {
169
- error.value = responseError;
170
- onError(responseError, refs);
171
- loading.value = false;
172
- onAfter(refs);
173
- throw responseError;
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
- return [
187
- value,
188
- run,
189
- {
190
- loading,
191
- error,
192
- uploadProgress,
193
- downloadProgress,
194
- abort,
195
- resetValue
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 Run<V, R, P> = (options?: RunOptions<V, P>) => Promise<R>;
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 Run<V, R, P> = (options?: RunOptions<V, P>) => Promise<R>;
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
- let controller = new AbortController();
90
- const resetValue = (options3 = {}) => {
91
- var _a, _b;
92
- const cloneResetValue = (_b = (_a = options3.cloneResetValue) != null ? _a : initialCloneResetValue) != null ? _b : false;
93
- const cloneFn = cloneResetValue === true ? (v) => v == null ? null : JSON.parse(JSON.stringify(v)) : isFunction(initialCloneResetValue) ? initialCloneResetValue : (v) => v;
94
- value.value = cloneFn(initialValue);
89
+ const extra = {
90
+ uploadProgress,
91
+ downloadProgress,
92
+ loading,
93
+ error,
94
+ abort,
95
+ resetValue
95
96
  };
96
- const run = async (options3 = {}) => {
97
- var _a, _b, _c, _d, _e;
98
- if (controller.signal.aborted) {
99
- controller = new AbortController();
100
- }
101
- const resetValueOption = (_b = (_a = options3.resetValue) != null ? _a : initialResetValue) != null ? _b : false;
102
- if (resetValueOption === true) {
103
- resetValue(options3);
104
- }
105
- uploadProgress.value = 0;
106
- downloadProgress.value = 0;
107
- const url = (_c = options3.url) != null ? _c : normalizeValueGetter(initialUrlOrGetter);
108
- const params = (_d = options3.params) != null ? _d : normalizeValueGetter(initialParamsOrGetter);
109
- const config = (_e = options3.config) != null ? _e : normalizeValueGetter(initialConfigOrGetter);
110
- onBefore(refs);
111
- loading.value = true;
112
- try {
113
- const response = await axle[method](url, params, __spreadValues({
114
- signal: controller.signal,
115
- onUploadProgress(event) {
116
- var _a2;
117
- uploadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
118
- },
119
- onDownloadProgress(event) {
120
- var _a2;
121
- downloadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
122
- }
123
- }, config));
124
- value.value = await onTransform(response, refs);
125
- error.value = void 0;
126
- onSuccess(response, refs);
127
- loading.value = false;
128
- onAfter(refs);
129
- return response;
130
- } catch (responseError) {
131
- error.value = responseError;
132
- onError(responseError, refs);
133
- loading.value = false;
134
- onAfter(refs);
135
- throw responseError;
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
- return [
149
- value,
150
- run,
151
- {
152
- loading,
153
- error,
154
- uploadProgress,
155
- downloadProgress,
156
- abort,
157
- resetValue
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.9.5",
3
+ "version": "0.10.1",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "type": "module",
6
6
  "main": "lib/index.cjs",