@data-fair/lib-vue 1.2.1 → 1.4.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 CHANGED
@@ -2,10 +2,12 @@ import type { QueryObject } from 'ufo';
2
2
  import type { Ref } from 'vue';
3
3
  type UseFetchOptions = {
4
4
  query?: QueryObject | Ref<QueryObject>;
5
+ watch?: Boolean;
5
6
  };
6
7
  export declare function useFetch<T>(url: string | Ref<string>, options?: UseFetchOptions): {
7
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>;
8
9
  loading: Ref<boolean, boolean>;
9
10
  refresh: () => Promise<void>;
11
+ initialFetch: () => Promise<void>;
10
12
  };
11
13
  export default useFetch;
package/fetch.js CHANGED
@@ -12,16 +12,25 @@ export function useFetch (url, options = {}) {
12
12
  })
13
13
  const data = ref(null)
14
14
  const loading = ref(false)
15
+ let abortController
15
16
  const refresh = async () => {
17
+ if (abortController) { abortController.abort() }
16
18
  loading.value = true
19
+ abortController = new AbortController()
17
20
  try {
18
- data.value = await ofetch(fullUrl.value)
21
+ data.value = await ofetch(fullUrl.value, { signal: abortController.signal })
19
22
  } catch (error) {
20
- sendUiNotif({ msg: '', error })
23
+ if (error.name !== 'AbortError') { sendUiNotif({ msg: '', error }) }
21
24
  }
22
25
  loading.value = false
23
26
  }
24
- watch(fullUrl, refresh, { immediate: true })
25
- return { data, loading, refresh }
27
+ const initialFetch = async () => {
28
+ if (loading.value || data.value) { return }
29
+ await refresh()
30
+ }
31
+ if (options.watch !== false) {
32
+ watch(fullUrl, refresh, { immediate: true })
33
+ }
34
+ return { data, loading, refresh, initialFetch }
26
35
  }
27
36
  export default useFetch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.2.1",
3
+ "version": "1.4.0",
4
4
  "description": "Composables and other utilities for Vue applications in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -105,7 +105,7 @@ export const useBooleanSearchParam = (key, options = {}) => {
105
105
  const defaultValue = typeof options === 'boolean' ? options : (options.default ?? false)
106
106
  const strings = (typeof options !== 'boolean' && options.strings) || ['1', '0']
107
107
  return computed({
108
- get: () => key in reactiveSearchParams ? reactiveSearchParams[key] === strings[0] : defaultValue,
108
+ get: () => key in reactiveSearchParams ? (reactiveSearchParams[key] === strings[0] || reactiveSearchParams[key] === 'true') : defaultValue,
109
109
  set: (value) => {
110
110
  if (value === defaultValue) { delete reactiveSearchParams[key] } else { reactiveSearchParams[key] = value ? strings[0] : strings[1] }
111
111
  }
package/session.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type IncomingMessage } from 'node:http';
2
- import { type Ref, App } from 'vue';
2
+ import { type Ref, type ComputedRef, type App } from 'vue';
3
3
  import { type RouteLocation } from 'vue-router';
4
4
  import { type fetch } from 'ofetch';
5
5
  import { type SessionState, type SessionStateAuthenticated } from '@data-fair/lib-common-types/session/index.js';
@@ -20,6 +20,12 @@ export interface SessionOptions {
20
20
  }
21
21
  export interface Session {
22
22
  state: SessionState;
23
+ user: ComputedRef<SessionState['user']>;
24
+ organization: ComputedRef<SessionState['organization']>;
25
+ account: ComputedRef<SessionState['account']>;
26
+ accountRole: ComputedRef<SessionState['accountRole']>;
27
+ lang: ComputedRef<SessionState['lang']>;
28
+ dark: ComputedRef<SessionState['dark']>;
23
29
  loginUrl: (redirect?: string, extraParams?: Record<string, string>, immediateRedirect?: true) => string;
24
30
  login: (redirect?: string, extraParams?: Record<string, string>, immediateRedirect?: true) => void;
25
31
  logout: (redirect?: string) => Promise<void>;
@@ -35,12 +41,21 @@ export interface Session {
35
41
  }
36
42
  export type SessionAuthenticated = Session & {
37
43
  state: SessionStateAuthenticated;
44
+ user: ComputedRef<SessionStateAuthenticated['user']>;
45
+ account: ComputedRef<SessionStateAuthenticated['account']>;
46
+ accountRole: ComputedRef<SessionStateAuthenticated['accountRole']>;
38
47
  };
39
48
  export declare function getSession(initOptions: Partial<SessionOptions>): Promise<Session>;
40
49
  export declare const sessionKey: unique symbol;
41
50
  export declare function createSession(initOptions: Partial<SessionOptions>): Promise<{
42
51
  install(app: App): void;
43
52
  state: SessionState;
53
+ user: ComputedRef<SessionState["user"]>;
54
+ organization: ComputedRef<SessionState["organization"]>;
55
+ account: ComputedRef<SessionState["account"]>;
56
+ accountRole: ComputedRef<SessionState["accountRole"]>;
57
+ lang: ComputedRef<SessionState["lang"]>;
58
+ dark: ComputedRef<SessionState["dark"]>;
44
59
  loginUrl: (redirect?: string, extraParams?: Record<string, string>, immediateRedirect?: true) => string;
45
60
  login: (redirect?: string, extraParams?: Record<string, string>, immediateRedirect?: true) => void;
46
61
  logout: (redirect?: string) => Promise<void>;
package/session.js CHANGED
@@ -218,6 +218,12 @@ export async function getSession (initOptions) {
218
218
  }
219
219
  const session = {
220
220
  state,
221
+ organization: computed(() => state.organization),
222
+ user: computed(() => state.user),
223
+ account: computed(() => state.account),
224
+ accountRole: computed(() => state.accountRole),
225
+ dark: computed(() => state.dark),
226
+ lang: computed(() => state.lang),
221
227
  loginUrl,
222
228
  login,
223
229
  logout,