@data-fair/lib-vue 1.6.0 → 1.7.0

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/fetch.d.ts +6 -4
  2. package/fetch.js +13 -7
  3. package/package.json +1 -1
package/fetch.d.ts CHANGED
@@ -3,11 +3,13 @@ import type { Ref } from 'vue';
3
3
  type UseFetchOptions = {
4
4
  query?: QueryObject | Ref<QueryObject>;
5
5
  watch?: Boolean;
6
+ notifError?: Boolean;
6
7
  };
7
8
  export declare function useFetch<T>(url: string | Ref<string> | (() => string), options?: UseFetchOptions): {
8
- data: [T | null] extends [Ref<any, any>] ? import("@vue/shared").IfAny<Ref<any, any> & T, Ref<Ref<any, any> & T, Ref<any, any> & T>, Ref<any, any> & T> : Ref<import("vue").UnwrapRef<T> | null, T | import("vue").UnwrapRef<T> | null>;
9
- loading: Ref<boolean, boolean>;
10
- refresh: () => Promise<void>;
11
- initialFetch: () => Promise<void>;
9
+ data: Ref<T | null>;
10
+ loading: Ref<boolean>;
11
+ initialized: Ref<boolean>;
12
+ error: Ref<any>;
13
+ refresh: (() => Promise<T | null>);
12
14
  };
13
15
  export default useFetch;
package/fetch.js CHANGED
@@ -13,25 +13,31 @@ export function useFetch (url, options = {}) {
13
13
  })
14
14
  const data = ref(null)
15
15
  const loading = ref(false)
16
+ const initialized = ref(false)
17
+ const error = ref(null)
16
18
  let abortController
17
19
  const refresh = async () => {
20
+ initialized.value = true
21
+ error.value = null
18
22
  if (abortController) { abortController.abort() }
19
23
  loading.value = true
20
24
  abortController = new AbortController()
21
25
  try {
22
26
  data.value = await ofetch(fullUrl.value, { signal: abortController.signal })
23
- } catch (error) {
24
- if (error.name !== 'AbortError') { sendUiNotif({ msg: '', error }) }
27
+ } catch (err) {
28
+ if (err.name !== 'AbortError') {
29
+ error.value = err
30
+ if (options.notifError !== false) {
31
+ sendUiNotif({ msg: '', error: err })
32
+ }
33
+ }
25
34
  }
26
35
  loading.value = false
27
- }
28
- const initialFetch = async () => {
29
- if (loading.value || data.value) { return }
30
- await refresh()
36
+ return data.value
31
37
  }
32
38
  if (options.watch !== false) {
33
39
  watch(fullUrl, refresh, { immediate: true })
34
40
  }
35
- return { data, loading, refresh, initialFetch }
41
+ return { initialized, data, loading, refresh, error }
36
42
  }
37
43
  export default useFetch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Composables and other utilities for Vue applications in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [