@data-fair/lib-vue 1.0.1 → 1.1.1

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.
@@ -0,0 +1,13 @@
1
+ // made for https://github.com/unplugin/unplugin-auto-import
2
+ export default [
3
+ 'vue',
4
+ 'vue-i18n',
5
+ 'vue-router',
6
+ {
7
+ '@data-fair/lib-vue/session.js': ['useSession'],
8
+ '@data-fair/lib-vue/reactive-search-params.js': ['useReactiveSearchParams'],
9
+ '@data-fair/lib-vue/locale-dayjs.js': ['useLocaleDayjs'],
10
+ '@data-fair/lib-vue/concept-filters.js': ['useConceptFilters'],
11
+ '@data-fair/lib-vue/ui-notif.js': ['useUiNotif']
12
+ }
13
+ ]
@@ -1,4 +1,4 @@
1
- export declare const locale: import("vue").Ref<string, string>, dayjs: (date?: string | number | import("dayjs").Dayjs | Date | null | undefined) => import("dayjs").Dayjs & {
1
+ export declare const locale: string, dayjs: (date?: string | number | import("dayjs").Dayjs | Date | null | undefined) => import("dayjs").Dayjs & {
2
2
  fromNow(withoutSuffix?: boolean): string;
3
3
  from(compared: import("dayjs").ConfigType, withoutSuffix?: boolean): string;
4
4
  toNow(withoutSuffix?: boolean): string;
@@ -4,4 +4,5 @@ import { getLocaleDayjs } from './locale-dayjs.js'
4
4
  if (import.meta.env?.SSR) {
5
5
  throw new Error('this module uses a module level singleton, it cannot be used in SSR mode')
6
6
  }
7
+ console.error('locale-dayjs-global is deprecated, please use create + use')
7
8
  export const { locale, dayjs } = getLocaleDayjs()
package/locale-dayjs.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Dayjs, ConfigType } from 'dayjs';
2
- import type { Ref, App } from 'vue';
2
+ import type { App } from 'vue';
3
3
  import dayjs from 'dayjs';
4
4
  import 'dayjs/locale/fr';
5
5
  import 'dayjs/locale/en';
@@ -9,12 +9,12 @@ type RelativeDayjs = Dayjs & {
9
9
  toNow(withoutSuffix?: boolean): string;
10
10
  to(compared: ConfigType, withoutSuffix?: boolean): string;
11
11
  };
12
- export declare function getLocaleDayjs(_locale?: Ref<string>): {
13
- locale: Ref<string, string>;
12
+ export declare function getLocaleDayjs(locale?: string): {
13
+ locale: string;
14
14
  dayjs: (date?: string | number | dayjs.Dayjs | Date | null | undefined) => RelativeDayjs;
15
15
  };
16
16
  export declare const localeDayjsKey: unique symbol;
17
- export declare function createLocaleDayjs(locale: Ref<string>): {
17
+ export declare function createLocaleDayjs(locale?: string): {
18
18
  install(app: App): void;
19
19
  };
20
20
  export declare function useLocaleDayjs(): ReturnType<typeof getLocaleDayjs>;
package/locale-dayjs.js CHANGED
@@ -1,4 +1,4 @@
1
- import { inject, ref } from 'vue'
1
+ import { inject } from 'vue'
2
2
  import dayjs from 'dayjs'
3
3
  import 'dayjs/locale/fr'
4
4
  import 'dayjs/locale/en'
@@ -8,12 +8,12 @@ dayjs.extend(localizedFormat)
8
8
  dayjs.extend(relativeTime)
9
9
  // main functionality, use through the createLocaleDayjs plugin and useLocaleDayjs composable
10
10
  // or as a global singleton through ./locale-dayjs-global.js
11
- export function getLocaleDayjs (_locale) {
12
- const locale = _locale ?? ref('fr')
11
+ export function getLocaleDayjs (locale) {
12
+ locale = locale ?? 'fr'
13
13
  return {
14
14
  locale,
15
15
  dayjs: (date) => {
16
- return dayjs(date).locale(locale.value)
16
+ return dayjs(date).locale(locale)
17
17
  }
18
18
  }
19
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "Composables and other utilities for Vue applications in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -4,7 +4,7 @@ export declare function getReactiveSearchParams(router?: Router): Record<string,
4
4
  export declare const reactiveSearchParamsKey: unique symbol;
5
5
  export declare function createReactiveSearchParams(router?: Router): {
6
6
  install(app: App): void;
7
- value: Record<string, string>;
7
+ state: Record<string, string>;
8
8
  };
9
9
  export declare function useReactiveSearchParams(): ReturnType<typeof getReactiveSearchParams>;
10
10
  export declare const useStringSearchParam: (key: string, options?: string | {
@@ -82,7 +82,7 @@ export function createReactiveSearchParams (router) {
82
82
  const reactiveSearchParams = getReactiveSearchParams(router)
83
83
  return {
84
84
  install (app) { app.provide(reactiveSearchParamsKey, reactiveSearchParams) },
85
- value: reactiveSearchParams
85
+ state: reactiveSearchParams
86
86
  }
87
87
  }
88
88
  export function useReactiveSearchParams () {
package/session.d.ts CHANGED
@@ -10,9 +10,9 @@ interface GenericCookies {
10
10
  remove: (key: string) => void;
11
11
  }
12
12
  export interface SessionOptions {
13
+ sitePath: string;
14
+ directoryUrl: string;
13
15
  route?: RouteLocation;
14
- sitePath?: string;
15
- directoryUrl?: string;
16
16
  logoutRedirectUrl?: string;
17
17
  req?: IncomingMessage;
18
18
  cookies?: GenericCookies;
@@ -36,11 +36,23 @@ export interface Session {
36
36
  export type SessionAuthenticated = Session & {
37
37
  state: SessionStateAuthenticated;
38
38
  };
39
- export declare function getSession(initOptions: SessionOptions): Promise<Session>;
39
+ export declare function getSession(initOptions: Partial<SessionOptions>): Promise<Session>;
40
40
  export declare const sessionKey: unique symbol;
41
- export declare function createSession(initOptions: SessionOptions): Promise<{
41
+ export declare function createSession(initOptions: Partial<SessionOptions>): Promise<{
42
42
  install(app: App): void;
43
- value: Session;
43
+ state: SessionState;
44
+ loginUrl: (redirect?: string, extraParams?: Record<string, string>, immediateRedirect?: true) => string;
45
+ login: (redirect?: string, extraParams?: Record<string, string>, immediateRedirect?: true) => void;
46
+ logout: (redirect?: string) => Promise<void>;
47
+ switchOrganization: (org: string | null, dep?: string) => void;
48
+ setAdminMode: (adminMode: boolean, redirect?: string) => Promise<void>;
49
+ asAdmin: (user: any | null) => Promise<void>;
50
+ cancelDeletion: () => Promise<void>;
51
+ keepalive: () => Promise<void>;
52
+ switchDark: (value: boolean) => void;
53
+ switchLang: (value: string) => void;
54
+ topLocation: Ref<Location | undefined>;
55
+ options: SessionOptions;
44
56
  }>;
45
57
  export declare function useSession(): Session;
46
58
  export declare function useSessionAuthenticated(errorBuilder?: () => any): SessionAuthenticated;
package/session.js CHANGED
@@ -238,8 +238,8 @@ export const sessionKey = Symbol('session')
238
238
  export async function createSession (initOptions) {
239
239
  const session = await getSession(initOptions)
240
240
  return {
241
+ ...session,
241
242
  install (app) { app.provide(sessionKey, session) },
242
- value: session
243
243
  }
244
244
  }
245
245
  export function useSession () {
@@ -1,5 +1,5 @@
1
1
  declare const _default: {
2
- current: import("vue").Ref<{
2
+ notification: import("vue").Ref<{
3
3
  type?: ("default" | "info" | "success" | "warning") | undefined;
4
4
  msg: string;
5
5
  } | {
@@ -16,6 +16,7 @@ declare const _default: {
16
16
  error: any;
17
17
  errorMsg: string;
18
18
  } | null>;
19
- send: (partialNotif: import("./ui-notif.js").PartialUiNotif) => void;
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;
20
21
  };
21
22
  export default _default;
@@ -4,4 +4,5 @@ import { getUiNotif } from './ui-notif.js'
4
4
  if (import.meta.env?.SSR) {
5
5
  throw new Error('this module uses a module level singleton, it cannot be used in SSR mode')
6
6
  }
7
+ console.error('ui-notif-global is deprecated, please use create + use')
7
8
  export default getUiNotif()
package/ui-notif.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import type { App } from 'vue';
2
2
  export type UiNotif = UiNotifBase | UiNotifError;
3
- export interface PartialUiNotif {
4
- type?: 'default' | 'info' | 'success' | 'warning' | 'error';
3
+ type NotifType = 'default' | 'info' | 'success' | 'warning' | 'error';
4
+ export type PartialUiNotif = string | {
5
+ type?: NotifType;
5
6
  msg: string;
6
7
  error?: any;
7
8
  errorMsg?: string;
8
- }
9
+ };
9
10
  interface UiNotifBase {
10
11
  type?: 'default' | 'info' | 'success' | 'warning';
11
12
  msg: string;
@@ -17,7 +18,7 @@ interface UiNotifError {
17
18
  errorMsg: string;
18
19
  }
19
20
  export declare const getUiNotif: () => {
20
- current: import("vue").Ref<{
21
+ notification: import("vue").Ref<{
21
22
  type?: ("default" | "info" | "success" | "warning") | undefined;
22
23
  msg: string;
23
24
  } | {
@@ -34,7 +35,8 @@ export declare const getUiNotif: () => {
34
35
  error: any;
35
36
  errorMsg: string;
36
37
  } | null>;
37
- send: (partialNotif: PartialUiNotif) => void;
38
+ sendUiNotif: (partialNotif: PartialUiNotif) => void;
39
+ withUiNotif: <F extends (...args: any[]) => Promise<any>>(fn: F, errorMsg: string, successNotif?: PartialUiNotif) => F;
38
40
  };
39
41
  export declare const uiNotifKey: unique symbol;
40
42
  export declare function createUiNotif(): {
package/ui-notif.js CHANGED
@@ -1,25 +1,54 @@
1
+ // simple composable to display store a UI notification
2
+ // this will be transmitted to frame parent if available (compatible with v-iframe uiNotification message type)
3
+ // or can be displayed locally by @data-fair/lib-vuetify/ui-notif.vue
1
4
  import { ref, inject } from 'vue'
2
5
  import inIframe from '@data-fair/lib-utils/in-iframe.js'
3
- function getFullNotif (notif) {
4
- if (typeof notif === 'string') { return { msg: notif, type: 'default' } }
6
+ function getErrorMsg (error) {
7
+ if (typeof error === 'string') { return error }
8
+ if (typeof error.data === 'string') { return error.data }
9
+ if (typeof error.response?.data === 'string') { return error.response.data }
10
+ if (typeof error.statusText === 'string') { return error.statusText }
11
+ if (typeof error.response?.statusText === 'string') { return error.response?.statusText }
12
+ if (error.message) { return error.message }
13
+ return 'unknown error'
14
+ }
15
+ function getFullNotif (notif, defaultType = 'default') {
16
+ if (typeof notif === 'string') { return { msg: notif, type: defaultType } }
5
17
  if (notif.error) {
6
- notif.type = 'error'
7
- notif.errorMsg = (notif.error.response && (notif.error.response.data || notif.error.response.status)) || notif.error.message || notif.error
18
+ console.log('ERROR', notif.error)
19
+ return {
20
+ ...notif,
21
+ type: 'error',
22
+ errorMsg: getErrorMsg(notif.error)
23
+ }
8
24
  }
9
- notif.type = notif.type || 'default'
10
- if (inIframe) {
11
- window.top?.postMessage({ vIframe: true, uiNotification: notif }, '*')
12
- } else {
13
- console.log('notification', notif)
25
+ return {
26
+ ...notif,
27
+ type: notif.type ?? defaultType
14
28
  }
15
- throw new Error('invalid UI notification')
16
29
  }
17
30
  export const getUiNotif = () => {
18
- const current = ref(null)
19
- const send = (partialNotif) => {
20
- current.value = getFullNotif(partialNotif)
31
+ const notification = ref(null)
32
+ const sendUiNotif = (partialNotif) => {
33
+ const notif = notification.value = getFullNotif(partialNotif)
34
+ if (inIframe) {
35
+ window.top?.postMessage({ vIframe: true, uiNotification: notif }, '*')
36
+ } else {
37
+ console.log('iframe notification', notif)
38
+ }
39
+ }
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
+ }
21
50
  }
22
- return { current, send }
51
+ return { notification, sendUiNotif, withUiNotif }
23
52
  }
24
53
  // uses pattern for SSR friendly plugin/composable, cf https://antfu.me/posts/composable-vue-vueday-2021#shared-state-ssr-friendly
25
54
  export const uiNotifKey = Symbol('uiNotif')
@@ -28,8 +57,8 @@ export function createUiNotif () {
28
57
  return { install (app) { app.provide(uiNotifKey, uiNotif) } }
29
58
  }
30
59
  export function useUiNotif () {
31
- const session = inject(uiNotifKey)
32
- if (!session) { throw new Error('useUiNotif requires using the plugin createUiNotif') }
33
- return session
60
+ const uiNotif = inject(uiNotifKey)
61
+ if (!uiNotif) { throw new Error('useUiNotif requires using the plugin createUiNotif') }
62
+ return uiNotif
34
63
  }
35
64
  export default useUiNotif
package/vite.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export declare const autoImports: (string | {
2
+ '@data-fair/lib-vue/session.js': string[];
3
+ '@data-fair/lib-vue/reactive-search-params.js': string[];
4
+ '@data-fair/lib-vue/locale-dayjs.js': string[];
5
+ '@data-fair/lib-vue/concept-filters.js': string[];
6
+ '@data-fair/lib-vue/ui-notif.js': string[];
7
+ })[];
package/vite.js ADDED
@@ -0,0 +1,13 @@
1
+ // made for https://github.com/unplugin/unplugin-auto-import
2
+ export const autoImports = [
3
+ 'vue',
4
+ 'vue-i18n',
5
+ 'vue-router',
6
+ {
7
+ '@data-fair/lib-vue/session.js': ['useSession'],
8
+ '@data-fair/lib-vue/reactive-search-params.js': ['useReactiveSearchParams'],
9
+ '@data-fair/lib-vue/locale-dayjs.js': ['useLocaleDayjs'],
10
+ '@data-fair/lib-vue/concept-filters.js': ['useConceptFilters'],
11
+ '@data-fair/lib-vue/ui-notif.js': ['useUiNotif']
12
+ }
13
+ ]
@@ -1 +0,0 @@
1
- export {};