cosey 0.11.9 → 0.11.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.
@@ -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,24 @@ 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 { promise: firstPromise, resolve, reject } = Promise.withResolvers();
8
+ const { isFetching, error, data, promise } = !isNullish(stale) && staleMap.get(stale) || {
8
9
  isFetching: shallowRef(false),
9
10
  data: shallowRef(initialData),
10
- error: shallowRef()
11
+ error: shallowRef(),
12
+ promise: shallowRef(firstPromise)
11
13
  };
12
14
  if (!isNullish(stale) && !staleMap.get(stale)) {
13
15
  staleMap.set(stale, {
14
16
  isFetching,
15
17
  error,
16
- data
18
+ data,
19
+ promise
17
20
  });
18
21
  }
22
+ const promisedFetcher = async (params) => {
23
+ return await fetcher(params);
24
+ };
19
25
  async function execute(params) {
20
26
  if (isFetching.value) {
21
27
  return;
@@ -23,15 +29,18 @@ function useFetch(fetcher, options = {}) {
23
29
  try {
24
30
  error.value = void 0;
25
31
  isFetching.value = true;
26
- const res = await fetcher(params);
32
+ promise.value = promisedFetcher(params);
33
+ const res = await promise.value;
27
34
  data.value = res;
28
35
  onSuccess?.(res);
36
+ resolve(res);
29
37
  } catch (err) {
30
38
  error.value = err;
31
39
  onError?.(err);
32
40
  } finally {
33
41
  isFetching.value = false;
34
42
  onFinally?.();
43
+ reject();
35
44
  }
36
45
  }
37
46
  if (immediate) {
@@ -45,7 +54,8 @@ function useFetch(fetcher, options = {}) {
45
54
  isFetching,
46
55
  error,
47
56
  data,
48
- execute
57
+ execute,
58
+ promise
49
59
  };
50
60
  }
51
61
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cosey",
3
- "version": "0.11.9",
3
+ "version": "0.11.11",
4
4
  "description": "基于 Vue3 + vite 的后台管理系统框架",
5
5
  "type": "module",
6
6
  "main": "index.js",