@varlet/axle 0.0.23 → 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.
- package/es/use.d.ts +26 -14
- package/es/use.js +49 -16
- package/package.json +2 -2
package/es/use.d.ts
CHANGED
|
@@ -1,33 +1,45 @@
|
|
|
1
|
-
import { type Ref
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
2
|
import { type AxleRequestConfig, createFetchHelper, createModifyHelper } from './instance.js';
|
|
3
3
|
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<UnwrapRef<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:
|
|
19
|
-
onSuccess?(response:
|
|
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
|
|
23
|
-
data: Ref<
|
|
24
|
-
run: Run<
|
|
31
|
+
export type UseAxleInstance<D, R, P> = [
|
|
32
|
+
data: Ref<D>,
|
|
33
|
+
run: Run<R, P>,
|
|
25
34
|
extra: {
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
uploadProgress: Ref<number>;
|
|
36
|
+
downloadProgress: Ref<number>;
|
|
37
|
+
loading: Ref<boolean>;
|
|
38
|
+
error: Ref<Error | undefined>;
|
|
39
|
+
abort(): void;
|
|
28
40
|
}
|
|
29
41
|
];
|
|
30
42
|
export interface CreateUseAxleOptions {
|
|
31
|
-
onTransform?(response: any,
|
|
43
|
+
onTransform?(response: any, refs: any): any;
|
|
32
44
|
}
|
|
33
|
-
export declare function createUseAxle(options?: CreateUseAxleOptions): <D = any, R = any, P = Record<string, any>>(options: UseAxleOptions<D, R, 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,32 +3,62 @@ 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
|
-
const
|
|
11
|
+
const downloadProgress = ref(0);
|
|
12
|
+
const uploadProgress = ref(0);
|
|
13
|
+
const shouldRetry = retry > 0;
|
|
14
|
+
const refs = {
|
|
15
|
+
data,
|
|
16
|
+
loading,
|
|
17
|
+
error,
|
|
18
|
+
downloadProgress,
|
|
19
|
+
uploadProgress
|
|
20
|
+
};
|
|
21
|
+
let controller = new AbortController();
|
|
22
|
+
const run = async (options = {}) => {
|
|
12
23
|
var _a;
|
|
24
|
+
if (controller.signal.aborted) {
|
|
25
|
+
controller = new AbortController();
|
|
26
|
+
}
|
|
27
|
+
uploadProgress.value = 0;
|
|
28
|
+
downloadProgress.value = 0;
|
|
13
29
|
const url = (_a = options.url) !== null && _a !== void 0 ? _a : initialUrl;
|
|
14
|
-
onBefore();
|
|
30
|
+
onBefore(refs);
|
|
15
31
|
loading.value = true;
|
|
16
|
-
|
|
17
|
-
.
|
|
18
|
-
|
|
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);
|
|
19
42
|
error.value = undefined;
|
|
20
|
-
onSuccess(response);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
+
}
|
|
24
53
|
error.value = responseError;
|
|
25
|
-
onError(responseError);
|
|
26
|
-
throw responseError;
|
|
27
|
-
})
|
|
28
|
-
.finally(() => {
|
|
54
|
+
onError(responseError, refs);
|
|
29
55
|
loading.value = false;
|
|
30
|
-
onAfter();
|
|
31
|
-
|
|
56
|
+
onAfter(refs);
|
|
57
|
+
throw responseError;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const abort = () => {
|
|
61
|
+
controller.abort();
|
|
32
62
|
};
|
|
33
63
|
if (immediate) {
|
|
34
64
|
run({
|
|
@@ -43,6 +73,9 @@ export function createUseAxle(options = {}) {
|
|
|
43
73
|
{
|
|
44
74
|
loading,
|
|
45
75
|
error,
|
|
76
|
+
uploadProgress,
|
|
77
|
+
downloadProgress,
|
|
78
|
+
abort,
|
|
46
79
|
},
|
|
47
80
|
];
|
|
48
81
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@varlet/axle",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.25",
|
|
4
4
|
"description": "A useful axios helper",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "es/index.js",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@varlet/cli": "^2.11.8",
|
|
39
39
|
"rimraf": "^5.0.1",
|
|
40
40
|
"typescript": "^5.1.5",
|
|
41
|
-
"vue": "
|
|
41
|
+
"vue": "3.3.4"
|
|
42
42
|
},
|
|
43
43
|
"repository": {
|
|
44
44
|
"type": "git",
|