@data-fair/lib-vue 1.12.0 → 1.13.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.
- package/async-action.d.ts +14 -0
- package/async-action.js +35 -0
- package/package.json +1 -1
- package/ui-notif.d.ts +2 -0
- package/ui-notif.js +3 -3
- package/vite.d.ts +1 -0
- package/vite.js +1 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
import { type PartialUiNotif, type UiNotif } from './ui-notif.js';
|
|
3
|
+
type AsyncActionOptions = {
|
|
4
|
+
error?: string;
|
|
5
|
+
success?: PartialUiNotif;
|
|
6
|
+
catch?: 'error' | 'success' | 'all';
|
|
7
|
+
};
|
|
8
|
+
export declare function asyncAction<F extends (...args: any[]) => Promise<any>>(fn: F, options?: AsyncActionOptions): {
|
|
9
|
+
execute: F;
|
|
10
|
+
notif: Ref<UiNotif | null>;
|
|
11
|
+
loading: Ref<boolean>;
|
|
12
|
+
error: Ref<string | null>;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
package/async-action.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// similar to withUiNotif but more powerful
|
|
2
|
+
import { ref } from 'vue'
|
|
3
|
+
import { useUiNotif, getFullNotif, getErrorMsg } from './ui-notif.js'
|
|
4
|
+
export function asyncAction (fn, options) {
|
|
5
|
+
const { sendUiNotif } = useUiNotif()
|
|
6
|
+
const notif = ref(null)
|
|
7
|
+
const loading = ref(false)
|
|
8
|
+
const error = ref(null)
|
|
9
|
+
const execute = async function (...args) {
|
|
10
|
+
loading.value = true
|
|
11
|
+
notif.value = null
|
|
12
|
+
error.value = null
|
|
13
|
+
try {
|
|
14
|
+
const result = await fn(...args)
|
|
15
|
+
if (options?.success) {
|
|
16
|
+
const successNotif = getFullNotif(options?.success, 'success')
|
|
17
|
+
notif.value = successNotif
|
|
18
|
+
if (options?.catch !== 'success' && options?.catch !== 'all') {
|
|
19
|
+
sendUiNotif(successNotif)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return result
|
|
23
|
+
} catch (err) {
|
|
24
|
+
const errorMsg = getErrorMsg(err)
|
|
25
|
+
error.value = errorMsg
|
|
26
|
+
const errorNotif = getFullNotif({ msg: options?.error ?? '', error: errorMsg })
|
|
27
|
+
notif.value = errorNotif
|
|
28
|
+
if (options?.catch !== 'error' && options?.catch !== 'all') {
|
|
29
|
+
sendUiNotif(errorNotif)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
loading.value = false
|
|
33
|
+
}
|
|
34
|
+
return { execute, notif, loading, error }
|
|
35
|
+
}
|
package/package.json
CHANGED
package/ui-notif.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ interface UiNotifError {
|
|
|
17
17
|
error: any;
|
|
18
18
|
errorMsg: string;
|
|
19
19
|
}
|
|
20
|
+
export declare function getErrorMsg(error: any): string;
|
|
21
|
+
export declare function getFullNotif(notif: PartialUiNotif, defaultType?: UiNotifBase['type']): UiNotif;
|
|
20
22
|
export declare const getUiNotif: () => {
|
|
21
23
|
notification: import("vue").Ref<{
|
|
22
24
|
type?: ("default" | "info" | "success" | "warning") | undefined;
|
package/ui-notif.js
CHANGED
|
@@ -3,16 +3,16 @@
|
|
|
3
3
|
// or can be displayed locally by @data-fair/lib-vuetify/ui-notif.vue
|
|
4
4
|
import { ref, inject } from 'vue'
|
|
5
5
|
import inIframe from '@data-fair/lib-utils/in-iframe.js'
|
|
6
|
-
function getErrorMsg (error) {
|
|
6
|
+
export function getErrorMsg (error) {
|
|
7
7
|
if (typeof error === 'string') { return error }
|
|
8
8
|
if (error.data && typeof error.data === 'string') { return error.data }
|
|
9
9
|
if (error.response?.data && typeof error.response.data === 'string') { return error.response.data }
|
|
10
10
|
if (typeof error.statusText === 'string') { return error.statusText }
|
|
11
11
|
if (typeof error.response?.statusText === 'string') { return error.response?.statusText }
|
|
12
12
|
if (error.message) { return error.message }
|
|
13
|
-
return 'erreur
|
|
13
|
+
return 'erreur inconnue'
|
|
14
14
|
}
|
|
15
|
-
function getFullNotif (notif, defaultType = 'default') {
|
|
15
|
+
export function getFullNotif (notif, defaultType = 'default') {
|
|
16
16
|
if (typeof notif === 'string') { return { msg: notif, type: defaultType } }
|
|
17
17
|
if (notif.error) {
|
|
18
18
|
return {
|
package/vite.d.ts
CHANGED
package/vite.js
CHANGED