@varlet/axle 0.0.24 → 0.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/es/use.d.ts +19 -10
  2. package/es/use.js +38 -26
  3. package/package.json +1 -1
package/es/use.d.ts CHANGED
@@ -4,24 +4,33 @@ export interface RunOptions<P> {
4
4
  url?: string;
5
5
  params?: P;
6
6
  config?: AxleRequestConfig;
7
+ _retryCount?: number;
8
+ }
9
+ export type Run<R, P> = (options?: RunOptions<P>) => Promise<R>;
10
+ export interface UseAxleRefs<D> {
11
+ data: Ref<D>;
12
+ loading: Ref<boolean>;
13
+ error: Ref<Error | undefined>;
14
+ uploadProgress: Ref<number>;
15
+ downloadProgress: Ref<number>;
7
16
  }
8
- export type Run<D, P> = (options?: RunOptions<P>) => Promise<D>;
9
17
  export interface UseAxleOptions<D = any, R = any, P = Record<string, any>> {
10
18
  url: string;
11
19
  runner: ReturnType<typeof createFetchHelper> | ReturnType<typeof createModifyHelper>;
12
20
  data: D;
13
21
  params?: P;
22
+ retry?: number;
14
23
  config?: AxleRequestConfig;
15
24
  immediate?: boolean;
16
- onBefore?(): void;
17
- onAfter?(): void;
18
- onTransform?(response: R, prev: D): D;
19
- onSuccess?(response: R): void;
20
- onError?(error: Error): void;
25
+ onBefore?(refs: UseAxleRefs<D>): void;
26
+ onAfter?(refs: UseAxleRefs<D>): void;
27
+ onTransform?(response: R, refs: UseAxleRefs<D>): D;
28
+ onSuccess?(response: R, refs: UseAxleRefs<D>): void;
29
+ onError?(error: Error, refs: UseAxleRefs<D>): void;
21
30
  }
22
- export type UseAxleReturn<D, P> = [
31
+ export type UseAxleInstance<D, R, P> = [
23
32
  data: Ref<D>,
24
- run: Run<D, P>,
33
+ run: Run<R, P>,
25
34
  extra: {
26
35
  uploadProgress: Ref<number>;
27
36
  downloadProgress: Ref<number>;
@@ -31,6 +40,6 @@ export type UseAxleReturn<D, P> = [
31
40
  }
32
41
  ];
33
42
  export interface CreateUseAxleOptions {
34
- onTransform?(response: any, prev: any): any;
43
+ onTransform?(response: any, refs: any): any;
35
44
  }
36
- export declare function createUseAxle(options?: CreateUseAxleOptions): <D = any, R = any, P = Record<string, any>>(options: UseAxleOptions<D, R, P>) => UseAxleReturn<D, P>;
45
+ export declare function createUseAxle(options?: CreateUseAxleOptions): <D = any, R = any, P = Record<string, any>>(options: UseAxleOptions<D, R, P>) => UseAxleInstance<D, R, P>;
package/es/use.js CHANGED
@@ -3,18 +3,23 @@ export function createUseAxle(options = {}) {
3
3
  const { onTransform: defaultOnTransform } = options;
4
4
  const useAxle = (options) => {
5
5
  var _a;
6
- const { url, runner, immediate, data: initialData, params: initialParams, config: initialConfig, onBefore = () => { }, onAfter = () => { }, onTransform = (_a = defaultOnTransform) !== null && _a !== void 0 ? _a : ((response) => response), onSuccess = () => { }, onError = () => { }, } = options;
6
+ const { url, runner, immediate, data: initialData, params: initialParams, config: initialConfig, retry = 0, onBefore = () => { }, onAfter = () => { }, onTransform = (_a = defaultOnTransform) !== null && _a !== void 0 ? _a : ((response) => response), onSuccess = () => { }, onError = () => { }, } = options;
7
7
  const initialUrl = url;
8
8
  const data = ref(initialData);
9
9
  const loading = ref(false);
10
10
  const error = ref();
11
11
  const downloadProgress = ref(0);
12
12
  const uploadProgress = ref(0);
13
- let controller = new AbortController();
14
- const abort = () => {
15
- controller.abort();
13
+ const shouldRetry = retry > 0;
14
+ const refs = {
15
+ data,
16
+ loading,
17
+ error,
18
+ downloadProgress,
19
+ uploadProgress
16
20
  };
17
- const run = (options = {}) => {
21
+ let controller = new AbortController();
22
+ const run = async (options = {}) => {
18
23
  var _a;
19
24
  if (controller.signal.aborted) {
20
25
  controller = new AbortController();
@@ -22,31 +27,38 @@ export function createUseAxle(options = {}) {
22
27
  uploadProgress.value = 0;
23
28
  downloadProgress.value = 0;
24
29
  const url = (_a = options.url) !== null && _a !== void 0 ? _a : initialUrl;
25
- onBefore();
30
+ onBefore(refs);
26
31
  loading.value = true;
27
- return runner(url, options.params, Object.assign({ signal: controller.signal, onUploadProgress(event) {
28
- var _a;
29
- uploadProgress.value = (_a = event.progress) !== null && _a !== void 0 ? _a : 0;
30
- },
31
- onDownloadProgress(event) {
32
- var _a;
33
- downloadProgress.value = (_a = event.progress) !== null && _a !== void 0 ? _a : 0;
34
- } }, options.config))
35
- .then((response) => {
36
- data.value = onTransform(response, data.value);
32
+ try {
33
+ const response = await runner(url, options.params, Object.assign({ signal: controller.signal, onUploadProgress(event) {
34
+ var _a;
35
+ uploadProgress.value = (_a = event.progress) !== null && _a !== void 0 ? _a : 0;
36
+ },
37
+ onDownloadProgress(event) {
38
+ var _a;
39
+ downloadProgress.value = (_a = event.progress) !== null && _a !== void 0 ? _a : 0;
40
+ } }, options.config));
41
+ data.value = onTransform(response, refs);
37
42
  error.value = undefined;
38
- onSuccess(response);
39
- return data.value;
40
- })
41
- .catch((responseError) => {
43
+ onSuccess(response, refs);
44
+ loading.value = false;
45
+ onAfter(refs);
46
+ return response;
47
+ }
48
+ catch (responseError) {
49
+ const currentRetryCount = options._retryCount == null ? 0 : options._retryCount;
50
+ if (shouldRetry && currentRetryCount < retry) {
51
+ return run(Object.assign(Object.assign({}, options), { _retryCount: currentRetryCount + 1 }));
52
+ }
42
53
  error.value = responseError;
43
- onError(responseError);
44
- throw responseError;
45
- })
46
- .finally(() => {
54
+ onError(responseError, refs);
47
55
  loading.value = false;
48
- onAfter();
49
- });
56
+ onAfter(refs);
57
+ throw responseError;
58
+ }
59
+ };
60
+ const abort = () => {
61
+ controller.abort();
50
62
  };
51
63
  if (immediate) {
52
64
  run({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "description": "A useful axios helper",
5
5
  "type": "module",
6
6
  "main": "es/index.js",