@varlet/axle 0.0.15 → 0.0.16
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 +7 -4
- package/es/use.js +8 -7
- package/package.json +1 -1
package/es/use.d.ts
CHANGED
|
@@ -7,8 +7,10 @@ export interface CreateUseAxleOptions {
|
|
|
7
7
|
params?: any;
|
|
8
8
|
config?: AxleRequestConfig;
|
|
9
9
|
immediate?: boolean;
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
extra?: any;
|
|
11
|
+
onBefore?(): void;
|
|
12
|
+
onSuccess?(response: any, prev: any): any;
|
|
13
|
+
onError?(error: Error, prev: Error | undefined): Error;
|
|
12
14
|
}
|
|
13
15
|
export interface RunOptions<P> {
|
|
14
16
|
url?: string;
|
|
@@ -23,8 +25,9 @@ export interface UseAxleOptions<D, P, R> {
|
|
|
23
25
|
params?: P;
|
|
24
26
|
config?: AxleRequestConfig;
|
|
25
27
|
immediate?: boolean;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
onBefore?(): void;
|
|
29
|
+
onSuccess?(response: R, prev: D): D;
|
|
30
|
+
onError?(error: Error, prev: Error | undefined): Error;
|
|
28
31
|
}
|
|
29
32
|
export declare function createUseAxle(options?: CreateUseAxleOptions): <D = any, P = any, R = any>(options?: UseAxleOptions<D, P, R>) => [data: Ref<UnwrapRef<D>>, run: Run<D, P>, loading: Ref<UnwrapRef<boolean>>, extra: {
|
|
30
33
|
error: Ref<UnwrapRef<Error | undefined>>;
|
package/es/use.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { ref } from 'vue';
|
|
2
2
|
export function createUseAxle(options = {}) {
|
|
3
|
-
var _a, _b, _c;
|
|
3
|
+
var _a, _b, _c, _d;
|
|
4
4
|
const defaultRunner = options.runner;
|
|
5
5
|
const defaultUrl = options.url;
|
|
6
6
|
const defaultInitialData = options.data;
|
|
7
7
|
const defaultInitialParams = options.params;
|
|
8
8
|
const defaultInitialConfig = options.config;
|
|
9
9
|
const defaultImmediate = (_a = options.immediate) !== null && _a !== void 0 ? _a : false;
|
|
10
|
-
const
|
|
11
|
-
const
|
|
10
|
+
const defaultOnBefore = (_b = options.onBefore) !== null && _b !== void 0 ? _b : (() => { });
|
|
11
|
+
const defaultOnSuccess = (_c = options.onSuccess) !== null && _c !== void 0 ? _c : ((v) => v);
|
|
12
|
+
const defaultOnError = (_d = options.onError) !== null && _d !== void 0 ? _d : ((v) => v);
|
|
12
13
|
const useAxle = (options = {}) => {
|
|
13
|
-
const { runner = defaultRunner, data: initialData = defaultInitialData, url = defaultUrl, params: initialParams = defaultInitialParams, config: initialConfig = defaultInitialConfig, immediate = defaultImmediate,
|
|
14
|
+
const { runner = defaultRunner, data: initialData = defaultInitialData, url = defaultUrl, params: initialParams = defaultInitialParams, config: initialConfig = defaultInitialConfig, immediate = defaultImmediate, onBefore = defaultOnBefore, onSuccess = defaultOnSuccess, onError = defaultOnError, } = options;
|
|
14
15
|
const initialUrl = url;
|
|
15
16
|
const data = ref(initialData);
|
|
16
17
|
const loading = ref(false);
|
|
@@ -24,17 +25,17 @@ export function createUseAxle(options = {}) {
|
|
|
24
25
|
if (!url) {
|
|
25
26
|
throw new Error('[Axle]: Cannot found valid url, so options.url is required');
|
|
26
27
|
}
|
|
28
|
+
onBefore();
|
|
27
29
|
loading.value = true;
|
|
28
30
|
return runner(url, options.params, options.config)
|
|
29
31
|
.then((response) => {
|
|
30
|
-
data.value =
|
|
32
|
+
data.value = onSuccess(response, data.value);
|
|
31
33
|
error.value = undefined;
|
|
32
34
|
loading.value = false;
|
|
33
35
|
return data.value;
|
|
34
36
|
})
|
|
35
37
|
.catch((responseError) => {
|
|
36
|
-
|
|
37
|
-
error.value = errorTransformer(responseError, error.value);
|
|
38
|
+
error.value = onError(responseError, error.value);
|
|
38
39
|
loading.value = false;
|
|
39
40
|
throw responseError;
|
|
40
41
|
});
|