@varlet/axle 0.0.9 → 0.0.11
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 +6 -6
- package/es/use.js +5 -5
- package/package.json +1 -1
package/es/use.d.ts
CHANGED
|
@@ -7,8 +7,8 @@ export interface CreateUseAxleOptions {
|
|
|
7
7
|
params?: any;
|
|
8
8
|
config?: AxleRequestConfig;
|
|
9
9
|
immediate?: boolean;
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
dataTransformer?(data: any, prevData: any): any;
|
|
11
|
+
errorTransformer?(errorResponse: Error): Error;
|
|
12
12
|
}
|
|
13
13
|
export interface RunOptions<P> {
|
|
14
14
|
url?: string;
|
|
@@ -17,16 +17,16 @@ export interface RunOptions<P> {
|
|
|
17
17
|
}
|
|
18
18
|
export type RunReturn<D> = Promise<[UnwrapRef<D>, undefined] | [undefined, Error]>;
|
|
19
19
|
export type Run<D, P> = (options?: RunOptions<P>) => RunReturn<D>;
|
|
20
|
-
export interface UseAxleOptions<D, P> {
|
|
20
|
+
export interface UseAxleOptions<D, P, R> {
|
|
21
21
|
runner?: ReturnType<typeof createFetchHelper> | ReturnType<typeof createModifyHelper>;
|
|
22
22
|
url?: string;
|
|
23
23
|
data?: D;
|
|
24
24
|
params?: P;
|
|
25
25
|
config?: AxleRequestConfig;
|
|
26
26
|
immediate?: boolean;
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
dataTransformer?(response: R, prevData: D): D;
|
|
28
|
+
errorTransformer?(errorResponse: Error): Error;
|
|
29
29
|
}
|
|
30
|
-
export declare function createUseAxle(options?: CreateUseAxleOptions): <D = any, P = any>(options?: UseAxleOptions<D, P>) => [data: Ref<UnwrapRef<D>>, run: Run<D, P>, loading: Ref<UnwrapRef<boolean>>, extra: {
|
|
30
|
+
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: {
|
|
31
31
|
error: Ref<UnwrapRef<Error | undefined>>;
|
|
32
32
|
}];
|
package/es/use.js
CHANGED
|
@@ -7,10 +7,10 @@ export function createUseAxle(options = {}) {
|
|
|
7
7
|
const defaultInitialParams = options.params;
|
|
8
8
|
const defaultInitialConfig = options.config;
|
|
9
9
|
const defaultImmediate = (_a = options.immediate) !== null && _a !== void 0 ? _a : true;
|
|
10
|
-
const
|
|
11
|
-
const
|
|
10
|
+
const defaultDataTransformer = (_b = options.dataTransformer) !== null && _b !== void 0 ? _b : ((v) => v);
|
|
11
|
+
const defaultErrorTransformer = (_c = options.errorTransformer) !== null && _c !== void 0 ? _c : ((v) => v);
|
|
12
12
|
const useAxle = (options = {}) => {
|
|
13
|
-
const { runner = defaultRunner, data: initialData = defaultInitialData, url = defaultUrl, params: initialParams = defaultInitialParams, config: initialConfig = defaultInitialConfig, immediate = defaultImmediate,
|
|
13
|
+
const { runner = defaultRunner, data: initialData = defaultInitialData, url = defaultUrl, params: initialParams = defaultInitialParams, config: initialConfig = defaultInitialConfig, immediate = defaultImmediate, dataTransformer = defaultDataTransformer, errorTransformer = defaultErrorTransformer } = options;
|
|
14
14
|
const initialUrl = url;
|
|
15
15
|
const data = ref(initialData);
|
|
16
16
|
const loading = ref(false);
|
|
@@ -27,14 +27,14 @@ export function createUseAxle(options = {}) {
|
|
|
27
27
|
loading.value = true;
|
|
28
28
|
return runner(url, options.params, options.config)
|
|
29
29
|
.then((response) => {
|
|
30
|
-
data.value =
|
|
30
|
+
data.value = dataTransformer(response, data.value);
|
|
31
31
|
error.value = undefined;
|
|
32
32
|
loading.value = false;
|
|
33
33
|
return [data.value, undefined];
|
|
34
34
|
})
|
|
35
35
|
.catch((responseError) => {
|
|
36
36
|
data.value = initialData;
|
|
37
|
-
error.value =
|
|
37
|
+
error.value = errorTransformer(responseError);
|
|
38
38
|
loading.value = false;
|
|
39
39
|
return [undefined, error.value];
|
|
40
40
|
});
|