@data-fair/lib-vue 1.1.1 → 1.2.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.
package/fetch.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { QueryObject } from 'ufo';
2
+ import type { Ref } from 'vue';
3
+ type UseFetchOptions = {
4
+ query?: QueryObject | Ref<QueryObject>;
5
+ };
6
+ export declare function useFetch<T>(url: string | Ref<string>, options?: UseFetchOptions): {
7
+ 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>;
8
+ loading: Ref<boolean, boolean>;
9
+ refresh: () => Promise<void>;
10
+ };
11
+ export default useFetch;
package/fetch.js ADDED
@@ -0,0 +1,27 @@
1
+ import { ofetch } from 'ofetch'
2
+ import { withQuery } from 'ufo'
3
+ import { computed, isRef, watch, ref } from 'vue'
4
+ import { useUiNotif } from '@data-fair/lib-vue/ui-notif.js'
5
+ export function useFetch (url, options = {}) {
6
+ const { sendUiNotif } = useUiNotif()
7
+ const fullUrl = computed(() => {
8
+ let fullUrl = isRef(url) ? url.value : url
9
+ const query = isRef(options.query) ? options.query.value : options.query
10
+ if (query) { fullUrl = withQuery(fullUrl, query) }
11
+ return fullUrl
12
+ })
13
+ const data = ref(null)
14
+ const loading = ref(false)
15
+ const refresh = async () => {
16
+ loading.value = true
17
+ try {
18
+ data.value = await ofetch(fullUrl.value)
19
+ } catch (error) {
20
+ sendUiNotif({ msg: '', error })
21
+ }
22
+ loading.value = false
23
+ }
24
+ watch(fullUrl, refresh, { immediate: true })
25
+ return { data, loading, refresh }
26
+ }
27
+ export default useFetch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Composables and other utilities for Vue applications in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
package/ui-notif.d.ts CHANGED
@@ -36,7 +36,6 @@ export declare const getUiNotif: () => {
36
36
  errorMsg: string;
37
37
  } | null>;
38
38
  sendUiNotif: (partialNotif: PartialUiNotif) => void;
39
- withUiNotif: <F extends (...args: any[]) => Promise<any>>(fn: F, errorMsg: string, successNotif?: PartialUiNotif) => F;
40
39
  };
41
40
  export declare const uiNotifKey: unique symbol;
42
41
  export declare function createUiNotif(): {
@@ -44,3 +43,4 @@ export declare function createUiNotif(): {
44
43
  };
45
44
  export declare function useUiNotif(): ReturnType<typeof getUiNotif>;
46
45
  export default useUiNotif;
46
+ export declare function withUiNotif<F extends (...args: any[]) => Promise<any>>(fn: F, errorMsg?: string, successNotif?: PartialUiNotif): F;
package/ui-notif.js CHANGED
@@ -15,7 +15,6 @@ function getErrorMsg (error) {
15
15
  function getFullNotif (notif, defaultType = 'default') {
16
16
  if (typeof notif === 'string') { return { msg: notif, type: defaultType } }
17
17
  if (notif.error) {
18
- console.log('ERROR', notif.error)
19
18
  return {
20
19
  ...notif,
21
20
  type: 'error',
@@ -29,7 +28,7 @@ function getFullNotif (notif, defaultType = 'default') {
29
28
  }
30
29
  export const getUiNotif = () => {
31
30
  const notification = ref(null)
32
- const sendUiNotif = (partialNotif) => {
31
+ function sendUiNotif (partialNotif) {
33
32
  const notif = notification.value = getFullNotif(partialNotif)
34
33
  if (inIframe) {
35
34
  window.top?.postMessage({ vIframe: true, uiNotification: notif }, '*')
@@ -37,18 +36,7 @@ export const getUiNotif = () => {
37
36
  console.log('iframe notification', notif)
38
37
  }
39
38
  }
40
- function withUiNotif (fn, errorMsg, successNotif) {
41
- return async function (...args) {
42
- try {
43
- const result = await fn(...args)
44
- if (successNotif) { sendUiNotif(getFullNotif(successNotif, 'success')) }
45
- return result
46
- } catch (error) {
47
- sendUiNotif({ msg: errorMsg, error })
48
- }
49
- }
50
- }
51
- return { notification, sendUiNotif, withUiNotif }
39
+ return { notification, sendUiNotif }
52
40
  }
53
41
  // uses pattern for SSR friendly plugin/composable, cf https://antfu.me/posts/composable-vue-vueday-2021#shared-state-ssr-friendly
54
42
  export const uiNotifKey = Symbol('uiNotif')
@@ -62,3 +50,15 @@ export function useUiNotif () {
62
50
  return uiNotif
63
51
  }
64
52
  export default useUiNotif
53
+ export function withUiNotif (fn, errorMsg, successNotif) {
54
+ const { sendUiNotif } = useUiNotif()
55
+ return async function (...args) {
56
+ try {
57
+ const result = await fn(...args)
58
+ if (successNotif) { sendUiNotif(getFullNotif(successNotif, 'success')) }
59
+ return result
60
+ } catch (error) {
61
+ sendUiNotif({ msg: errorMsg ?? '', error })
62
+ }
63
+ }
64
+ }
package/vite.d.ts CHANGED
@@ -4,4 +4,6 @@ export declare const autoImports: (string | {
4
4
  '@data-fair/lib-vue/locale-dayjs.js': string[];
5
5
  '@data-fair/lib-vue/concept-filters.js': string[];
6
6
  '@data-fair/lib-vue/ui-notif.js': string[];
7
+ '@data-fair/lib-vue/fetch.js': string[];
8
+ '@data-fair/lib-vue/ws.js': string[];
7
9
  })[];
package/vite.js CHANGED
@@ -4,10 +4,12 @@ export const autoImports = [
4
4
  'vue-i18n',
5
5
  'vue-router',
6
6
  {
7
- '@data-fair/lib-vue/session.js': ['useSession'],
8
- '@data-fair/lib-vue/reactive-search-params.js': ['useReactiveSearchParams'],
7
+ '@data-fair/lib-vue/session.js': ['useSession', 'useSessionAuthenticated'],
8
+ '@data-fair/lib-vue/reactive-search-params.js': ['useReactiveSearchParams', 'useStringSearchParam', 'useBooleanSearchParam', 'useNumberSearchParam', 'useStringsArraySearchParam'],
9
9
  '@data-fair/lib-vue/locale-dayjs.js': ['useLocaleDayjs'],
10
10
  '@data-fair/lib-vue/concept-filters.js': ['useConceptFilters'],
11
- '@data-fair/lib-vue/ui-notif.js': ['useUiNotif']
11
+ '@data-fair/lib-vue/ui-notif.js': ['useUiNotif', 'withUiNotif'],
12
+ '@data-fair/lib-vue/fetch.js': ['useFetch'],
13
+ '@data-fair/lib-vue/ws.js': ['useWS'],
12
14
  }
13
15
  ]
@@ -1,22 +0,0 @@
1
- declare const _default: {
2
- notification: import("vue").Ref<{
3
- type?: ("default" | "info" | "success" | "warning") | undefined;
4
- msg: string;
5
- } | {
6
- type: "error";
7
- msg: string;
8
- error: any;
9
- errorMsg: string;
10
- } | null, import("./ui-notif.js").UiNotif | {
11
- type?: ("default" | "info" | "success" | "warning") | undefined;
12
- msg: string;
13
- } | {
14
- type: "error";
15
- msg: string;
16
- error: any;
17
- errorMsg: string;
18
- } | null>;
19
- sendUiNotif: (partialNotif: import("./ui-notif.js").PartialUiNotif) => void;
20
- withUiNotif: <F extends (...args: any[]) => Promise<any>>(fn: F, errorMsg: string, successNotif?: import("./ui-notif.js").PartialUiNotif) => F;
21
- };
22
- export default _default;