@data-fair/lib-vue 1.5.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 +7 -5
  2. package/fetch.js +14 -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
- export declare function useFetch<T>(url: string | Ref<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>;
8
+ export declare function useFetch<T>(url: string | Ref<string> | (() => string), options?: UseFetchOptions): {
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
@@ -4,6 +4,7 @@ import { computed, isRef, watch, ref } from 'vue'
4
4
  import { useUiNotif } from '@data-fair/lib-vue/ui-notif.js'
5
5
  export function useFetch (url, options = {}) {
6
6
  const { sendUiNotif } = useUiNotif()
7
+ if (typeof url === 'function') { url = computed(url) }
7
8
  const fullUrl = computed(() => {
8
9
  let fullUrl = isRef(url) ? url.value : url
9
10
  const query = isRef(options.query) ? options.query.value : options.query
@@ -12,25 +13,31 @@ export function useFetch (url, options = {}) {
12
13
  })
13
14
  const data = ref(null)
14
15
  const loading = ref(false)
16
+ const initialized = ref(false)
17
+ const error = ref(null)
15
18
  let abortController
16
19
  const refresh = async () => {
20
+ initialized.value = true
21
+ error.value = null
17
22
  if (abortController) { abortController.abort() }
18
23
  loading.value = true
19
24
  abortController = new AbortController()
20
25
  try {
21
26
  data.value = await ofetch(fullUrl.value, { signal: abortController.signal })
22
- } catch (error) {
23
- 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
+ }
24
34
  }
25
35
  loading.value = false
26
- }
27
- const initialFetch = async () => {
28
- if (loading.value || data.value) { return }
29
- await refresh()
36
+ return data.value
30
37
  }
31
38
  if (options.watch !== false) {
32
39
  watch(fullUrl, refresh, { immediate: true })
33
40
  }
34
- return { data, loading, refresh, initialFetch }
41
+ return { initialized, data, loading, refresh, error }
35
42
  }
36
43
  export default useFetch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.5.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": [