@data-fair/lib-vue 1.11.0 → 1.13.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/async-action.d.ts +14 -0
- package/async-action.js +35 -0
- package/package.json +1 -1
- package/session.js +5 -5
- package/ui-notif.d.ts +2 -0
- package/ui-notif.js +3 -3
|
@@ -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/session.js
CHANGED
|
@@ -14,13 +14,12 @@ function jwtDecodeAlive (jwt) {
|
|
|
14
14
|
if (!decoded) { return }
|
|
15
15
|
const now = Math.ceil(Date.now().valueOf() / 1000)
|
|
16
16
|
if (typeof decoded.exp !== 'undefined' && decoded.exp < now) {
|
|
17
|
-
|
|
17
|
+
// token expired
|
|
18
18
|
return
|
|
19
19
|
}
|
|
20
20
|
if (typeof decoded.nbf !== 'undefined' && decoded.nbf > now) {
|
|
21
21
|
console.warn(`token not yet valid: ${decoded.nbf}>${now}, ${JSON.stringify(decoded)}`)
|
|
22
|
-
// do not return
|
|
23
|
-
// return null
|
|
22
|
+
// do not return here, this is probably a false flag due to a slightly mismatched clock
|
|
24
23
|
}
|
|
25
24
|
return decoded
|
|
26
25
|
}
|
|
@@ -216,7 +215,7 @@ export async function getSession (initOptions) {
|
|
|
216
215
|
await customFetch(`${options.directoryUrl}/api/auth/keepalive`, { method: 'POST' })
|
|
217
216
|
} catch (err) {
|
|
218
217
|
if (err instanceof FetchError && err.statusCode === 401) {
|
|
219
|
-
|
|
218
|
+
console.warn('session was expired or deleted server side')
|
|
220
219
|
} else {
|
|
221
220
|
throw err
|
|
222
221
|
}
|
|
@@ -233,7 +232,8 @@ export async function getSession (initOptions) {
|
|
|
233
232
|
// also run an auto-refresh loop
|
|
234
233
|
if (!ssr && !inIframe) {
|
|
235
234
|
const lastKeepalive = window.localStorage.getItem('sd-keepalive' + options.sitePath)
|
|
236
|
-
|
|
235
|
+
// check cookies.get('id_token') not state.user so that we do a keepalive on expired id tokens
|
|
236
|
+
if (cookies.get('id_token') && (!lastKeepalive || (new Date().getTime() - Number(lastKeepalive)) > 10000)) {
|
|
237
237
|
await keepalive()
|
|
238
238
|
}
|
|
239
239
|
const refreshLoopDelay = 10 * 60 * 1000 // 10 minutes
|
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 {
|