cosey 0.11.9 → 0.11.10

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.
@@ -11,9 +11,10 @@ interface UseFetchStale<T> {
11
11
  isFetching: ShallowRef<boolean>;
12
12
  data: ShallowRef<T | undefined>;
13
13
  error: ShallowRef<any>;
14
+ promise: ShallowRef<Promise<T>>;
14
15
  }
15
16
  interface UseFetchResult<T, U> extends UseFetchStale<T> {
16
17
  execute: (params?: U | undefined) => Promise<void>;
17
18
  }
18
- export declare function useFetch<T = any, U = any>(fetcher: (params: U) => Promise<any> | any, options?: UseFetchOptions<T>): UseFetchResult<T, U>;
19
+ export declare function useFetch<T = any, U = any>(fetcher: (params: U) => Promise<T> | T, options?: UseFetchOptions<T>): UseFetchResult<T, U>;
19
20
  export {};
package/hooks/useFetch.js CHANGED
@@ -4,18 +4,23 @@ import { isNullish } from '../utils/is.js';
4
4
  const staleMap = /* @__PURE__ */ new Map();
5
5
  function useFetch(fetcher, options = {}) {
6
6
  const { immediate = true, initialData, stale, onSuccess, onError, onFinally } = options;
7
- const { isFetching, error, data } = !isNullish(stale) && staleMap.get(stale) || {
7
+ const { isFetching, error, data, promise } = !isNullish(stale) && staleMap.get(stale) || {
8
8
  isFetching: shallowRef(false),
9
9
  data: shallowRef(initialData),
10
- error: shallowRef()
10
+ error: shallowRef(),
11
+ promise: shallowRef(Promise.resolve())
11
12
  };
12
13
  if (!isNullish(stale) && !staleMap.get(stale)) {
13
14
  staleMap.set(stale, {
14
15
  isFetching,
15
16
  error,
16
- data
17
+ data,
18
+ promise
17
19
  });
18
20
  }
21
+ const promisedFetcher = async (params) => {
22
+ return await fetcher(params);
23
+ };
19
24
  async function execute(params) {
20
25
  if (isFetching.value) {
21
26
  return;
@@ -23,7 +28,8 @@ function useFetch(fetcher, options = {}) {
23
28
  try {
24
29
  error.value = void 0;
25
30
  isFetching.value = true;
26
- const res = await fetcher(params);
31
+ promise.value = promisedFetcher(params);
32
+ const res = await promise.value;
27
33
  data.value = res;
28
34
  onSuccess?.(res);
29
35
  } catch (err) {
@@ -45,7 +51,8 @@ function useFetch(fetcher, options = {}) {
45
51
  isFetching,
46
52
  error,
47
53
  data,
48
- execute
54
+ execute,
55
+ promise
49
56
  };
50
57
  }
51
58
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cosey",
3
- "version": "0.11.9",
3
+ "version": "0.11.10",
4
4
  "description": "基于 Vue3 + vite 的后台管理系统框架",
5
5
  "type": "module",
6
6
  "main": "index.js",