@varlet/axle 0.0.20 → 0.0.22

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 +10 -5
  2. package/es/use.js +11 -6
  3. package/package.json +1 -1
package/es/use.d.ts CHANGED
@@ -14,15 +14,20 @@ export interface UseAxleOptions<D = any, R = any, P = Record<string, any>> {
14
14
  config?: AxleRequestConfig;
15
15
  immediate?: boolean;
16
16
  onBefore?(): void;
17
- onSuccess?(response: UnwrapRef<R>, prev: UnwrapRef<D>): UnwrapRef<D>;
17
+ onAfter?(): void;
18
+ onTransform?(response: UnwrapRef<R>, prev: UnwrapRef<D>): UnwrapRef<D>;
19
+ onSuccess?(response: UnwrapRef<R>): void;
18
20
  onError?(error: Error, prev: Error | undefined): Error;
19
21
  }
20
- export type UseAxleReturn<D = any, R = any, P = Record<string, any>> = [
21
- data: Ref<UnwrapRef<D | R | undefined | null>>,
22
+ export type UseAxleReturn<D, P> = [
23
+ data: Ref<UnwrapRef<D>>,
22
24
  run: Run<D, P>,
23
- loading: Ref<UnwrapRef<boolean>>,
24
25
  extra: {
26
+ loading: Ref<UnwrapRef<boolean>>;
25
27
  error: Ref<UnwrapRef<Error | undefined>>;
26
28
  }
27
29
  ];
28
- export declare function createUseAxle(): <D = any, R = any, P = Record<string, any>>(options: UseAxleOptions<D, R, P>) => UseAxleReturn<D, R, P>;
30
+ export interface CreateUseAxleOptions {
31
+ onTransform?(response: any, prev: any): any;
32
+ }
33
+ export declare function createUseAxle(options?: CreateUseAxleOptions): <D = any, R = any, P = Record<string, any>>(options: UseAxleOptions<D, R, P>) => UseAxleReturn<D, P>;
package/es/use.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { ref } from 'vue';
2
- export function createUseAxle() {
2
+ export function createUseAxle(options = {}) {
3
+ const { onTransform: defaultOnTransform } = options;
3
4
  const useAxle = (options) => {
4
- const { url, runner, immediate, data: initialData, params: initialParams, config: initialConfig, onBefore = () => { }, onSuccess = (response) => response, onError = (error) => error, } = options;
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 = (error) => error, } = options;
5
7
  const initialUrl = url;
6
8
  const data = ref(initialData);
7
9
  const loading = ref(false);
@@ -13,15 +15,18 @@ export function createUseAxle() {
13
15
  loading.value = true;
14
16
  return runner(url, options.params, options.config)
15
17
  .then((response) => {
16
- data.value = onSuccess(response, data.value);
18
+ data.value = onTransform(response, data.value);
17
19
  error.value = undefined;
18
- loading.value = false;
20
+ onSuccess(response);
19
21
  return data.value;
20
22
  })
21
23
  .catch((responseError) => {
22
24
  error.value = onError(responseError, error.value);
23
- loading.value = false;
24
25
  throw responseError;
26
+ })
27
+ .finally(() => {
28
+ loading.value = false;
29
+ onAfter();
25
30
  });
26
31
  };
27
32
  if (immediate) {
@@ -34,8 +39,8 @@ export function createUseAxle() {
34
39
  return [
35
40
  data,
36
41
  run,
37
- loading,
38
42
  {
43
+ loading,
39
44
  error,
40
45
  },
41
46
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "A useful axios helper",
5
5
  "type": "module",
6
6
  "main": "es/index.js",