@data-fair/lib-vue 1.8.0 → 1.9.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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/session.d.ts +14 -3
  3. package/session.js +11 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.8.0",
3
+ "version": "1.9.1",
4
4
  "description": "Composables and other utilities for Vue applications in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
package/session.d.ts CHANGED
@@ -12,6 +12,7 @@ interface GenericCookies {
12
12
  export interface SessionOptions {
13
13
  sitePath: string;
14
14
  directoryUrl: string;
15
+ defaultLang: string;
15
16
  route?: RouteLocation;
16
17
  logoutRedirectUrl?: string;
17
18
  req?: IncomingMessage;
@@ -19,12 +20,22 @@ export interface SessionOptions {
19
20
  customFetch?: typeof fetch;
20
21
  siteInfo?: boolean;
21
22
  }
22
- export interface Theme {
23
- primaryColor: string;
23
+ export interface Colors {
24
+ background: string;
25
+ surface: string;
26
+ primary: string;
27
+ secondary: string;
28
+ accent: string;
29
+ error: string;
30
+ info: string;
31
+ success: string;
32
+ warning: string;
33
+ admin: string;
24
34
  }
25
35
  export interface SiteInfo {
26
- theme: Theme;
36
+ main?: boolean;
27
37
  logo?: string;
38
+ colors: Colors;
28
39
  }
29
40
  export interface Session {
30
41
  state: SessionState;
package/session.js CHANGED
@@ -3,6 +3,7 @@ import { ofetch } from 'ofetch'
3
3
  import { jwtDecode } from 'jwt-decode'
4
4
  import cookiesModule from 'universal-cookie'
5
5
  import Debug from 'debug'
6
+ import inIframe from '@data-fair/lib-utils/in-iframe.js'
6
7
  const Cookies = cookiesModule
7
8
  const debug = Debug('session')
8
9
  debug.log = console.log.bind(console)
@@ -36,7 +37,11 @@ const goTo = (url) => {
36
37
  }
37
38
  if (url) { topLocation.href = url } else { topLocation.reload() }
38
39
  }
39
- const defaultOptions = { directoryUrl: '/simple-directory', sitePath: '' }
40
+ const getSiteInfoStorage = (sitePath) => {
41
+ const siteInfoStorageStr = window.localStorage.getItem('sd-site-info' + sitePath)
42
+ return siteInfoStorageStr ? JSON.parse(siteInfoStorageStr) : null
43
+ }
44
+ const defaultOptions = { directoryUrl: '/simple-directory', sitePath: '', defaultLang: 'fr' }
40
45
  export async function getSession (initOptions) {
41
46
  const options = { ...defaultOptions, ...initOptions }
42
47
  const cookiesPath = options.sitePath + '/'
@@ -62,7 +67,7 @@ export async function getSession (initOptions) {
62
67
  const darkCookie = cookies.get('theme_dark')
63
68
  state.dark = darkCookie === '1' || darkCookie === 'true'
64
69
  const langCookie = cookies.get('i18n_lang')
65
- if (langCookie) { state.lang = langCookie } else { delete state.lang }
70
+ state.lang = langCookie ?? options.defaultLang
66
71
  const idToken = cookies.get('id_token')
67
72
  const user = jwtDecodeAlive(idToken)
68
73
  if (!user) {
@@ -109,7 +114,7 @@ export async function getSession (initOptions) {
109
114
  state.accountRole = 'admin'
110
115
  }
111
116
  if (!ssr) {
112
- const siteInfoStorage = getSiteInfoStorage()
117
+ const siteInfoStorage = getSiteInfoStorage(options.sitePath)
113
118
  site.value = siteInfoStorage ? siteInfoStorage.info : null
114
119
  }
115
120
  }
@@ -217,10 +222,6 @@ export async function getSession (initOptions) {
217
222
  await customFetch(`${options.directoryUrl}/api/auth/keepalive`, { method: 'POST' })
218
223
  readState()
219
224
  }
220
- const getSiteInfoStorage = () => {
221
- const siteInfoStorageStr = window.localStorage.getItem('sd-site-info' + options.sitePath)
222
- return siteInfoStorageStr ? JSON.parse(siteInfoStorageStr) : null
223
- }
224
225
  const setSiteInfoStorage = (siteInfo) => {
225
226
  const siteInfoStorage = { info: siteInfo, updatedAt: new Date().getTime() }
226
227
  window.localStorage.setItem('sd-site-info' + options.sitePath, JSON.stringify(siteInfoStorage))
@@ -235,13 +236,13 @@ export async function getSession (initOptions) {
235
236
  // immediately performs a keepalive, but only on top windows (not iframes or popups)
236
237
  // and only if it was not done very recently (maybe from a refreshed page next to this one)
237
238
  // also run an auto-refresh loop
238
- if (!ssr && window.top === window.self) {
239
+ if (!ssr && !inIframe) {
239
240
  const lastKeepalive = window.localStorage.getItem('sd-keepalive' + options.sitePath)
240
- if (!lastKeepalive || (new Date().getTime() - Number(lastKeepalive)) > 10000) {
241
+ if (state.user && (!lastKeepalive || (new Date().getTime() - Number(lastKeepalive)) > 10000)) {
241
242
  await keepalive()
242
243
  }
243
244
  if (options.siteInfo) {
244
- const lastSiteInfoStorage = getSiteInfoStorage()
245
+ const lastSiteInfoStorage = getSiteInfoStorage(options.sitePath)
245
246
  if (!lastSiteInfoStorage || (new Date().getTime() - Number(lastSiteInfoStorage.updatedAt)) > 10000) {
246
247
  await refreshSiteInfo()
247
248
  }