@inzombieland/nuxt-common 1.16.23 → 1.16.25

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/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/nuxt-common",
3
- "version": "1.16.23",
3
+ "version": "1.16.25",
4
4
  "configKey": "nuxt-common",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.6.0",
package/dist/module.mjs CHANGED
@@ -2,7 +2,7 @@ import { fileURLToPath } from 'node:url';
2
2
  import { defineNuxtModule, createResolver, addServerHandler, addImportsDir, addPlugin, addComponent } from '@nuxt/kit';
3
3
 
4
4
  const name = "@inzombieland/nuxt-common";
5
- const version = "1.16.23";
5
+ const version = "1.16.25";
6
6
 
7
7
  const module = defineNuxtModule({
8
8
  meta: {
@@ -1,52 +1,19 @@
1
1
  <script setup lang="ts">
2
2
  import { StyleProvider, Themes } from "@varlet/ui"
3
3
  import { useLocalStorage } from "@vueuse/core"
4
- import { useI18n, useLocalePath } from "#i18n"
5
4
  import { useNuxtApp } from "#imports"
6
5
  import { onMounted, ref, watch } from "vue"
7
- import { useRouter } from "vue-router"
8
- import { getVisitorId } from "./api"
9
- import { useActiveSessions } from "./composables/use-active-sessions"
10
- import { useUserActions } from "./composables/use-user-actions"
11
- import { useSubscribe, useUser } from "./packages/api"
6
+ import { useUser } from "./packages/api"
12
7
  import bus from "./packages/api/bus"
13
8
  import type { ConfigProviderTheme } from "vant"
14
9
  import "@vant/touch-emulator"
15
10
 
16
11
  const theme = ref("")
17
12
  const appPreferencesTheme = useLocalStorage("app-preferences-theme", JSON.stringify({ value: "" }))
18
- const locale = useLocalStorage("app-preferences-locale", JSON.stringify({ value: "en" }))
19
13
  const user = useUser()
20
- const activeSessions = useActiveSessions()
21
- const { fetchActiveSessions, logout } = useUserActions()
22
- const localePath = useLocalePath()
23
- const router = useRouter()
24
14
  const appLoading = ref(Boolean(user.value))
25
- // @ts-ignore
26
- const { setLocale } = useI18n()
27
15
  const { $mobileApp } = useNuxtApp()
28
16
 
29
- useSubscribe("WS.SessionsUpdated", async (session: { status?: string; visitorId?: string; sessionId?: string }) => {
30
- if (user.value && activeSessions.value.length > 0) {
31
- activeSessions.value = [...activeSessions.value].map(activeSession => {
32
- if (activeSession.id === session?.sessionId) {
33
- activeSession.isOnline = session?.status === "Online"
34
- activeSession.signedOut = session?.status === "SignedOut"
35
- activeSession.dateTime = new Date()
36
- }
37
- return activeSession
38
- })
39
- }
40
- if (user.value && session?.status === "SignedOut" && session?.visitorId === getVisitorId()) {
41
- const data = await fetchActiveSessions()
42
- const isCurrentSession = data.sessions.some((s: any) => s.isCurrentSession && s.id === session?.sessionId)
43
- if (isCurrentSession) {
44
- await logout()
45
- await router.push({ path: localePath("/") })
46
- }
47
- }
48
- })
49
-
50
17
  onMounted(() => {
51
18
  watch(
52
19
  appPreferencesTheme,
@@ -82,20 +49,6 @@ onMounted(() => {
82
49
  { immediate: true }
83
50
  )
84
51
 
85
- watch(
86
- locale,
87
- locale => {
88
- try {
89
- const { value } = JSON.parse(locale)
90
- setLocale(value)
91
- } catch (error) {
92
- console.error(`Error parsing app preferences locale:`, error)
93
- setLocale("en")
94
- }
95
- },
96
- { immediate: true }
97
- )
98
-
99
52
  appLoading.value = false
100
53
  })
101
54
 
@@ -27,5 +27,8 @@ declare const fetch: Fetch, getUser: () => Promise<import("src/runtime/packages/
27
27
  key: string;
28
28
  }>;
29
29
  logout: () => Promise<void>;
30
+ fetchActiveSessions: () => Promise<{
31
+ sessions: import("src/runtime/packages/api/types").ActiveSession[];
32
+ }>;
30
33
  };
31
34
  export { fetch, getUser, useApiFetch, userActions };
@@ -0,0 +1,26 @@
1
+ import type { ActiveSession } from "../types";
2
+ export declare function useActiveSessions(): import("vue").Ref<{
3
+ id: string;
4
+ visitorId: string;
5
+ device: string;
6
+ appName: string;
7
+ location: string;
8
+ ipAddress: string;
9
+ isCurrentSession?: boolean | undefined;
10
+ isOnline?: boolean | undefined;
11
+ signedOut?: boolean | undefined;
12
+ dateTime: Date;
13
+ firstSignIn: Date;
14
+ }[], ActiveSession[] | {
15
+ id: string;
16
+ visitorId: string;
17
+ device: string;
18
+ appName: string;
19
+ location: string;
20
+ ipAddress: string;
21
+ isCurrentSession?: boolean | undefined;
22
+ isOnline?: boolean | undefined;
23
+ signedOut?: boolean | undefined;
24
+ dateTime: Date;
25
+ firstSignIn: Date;
26
+ }[]>;
@@ -0,0 +1,14 @@
1
+ import { ref } from "vue";
2
+ import { useUserActions } from "../index.mjs";
3
+ const activeSessions = ref([]);
4
+ export function useActiveSessions() {
5
+ const userActions = useUserActions();
6
+ const fetchActiveSessions = async () => {
7
+ if (activeSessions.value.length === 0) {
8
+ const data = await userActions?.value.fetchActiveSessions();
9
+ activeSessions.value = data?.sessions ?? [];
10
+ }
11
+ };
12
+ fetchActiveSessions();
13
+ return activeSessions;
14
+ }
@@ -1,3 +1,4 @@
1
+ import { type Ref } from "vue";
1
2
  import type { ActiveSession, Fetch, FetchConfig, User } from "./types";
2
3
  export type { ActiveSession, Fetch, FetchConfig, User };
3
4
  export { useUser } from "./composables/use-user";
@@ -16,5 +17,33 @@ export declare function initApiFetch($fetch: Fetch, config: FetchConfig): {
16
17
  key: string;
17
18
  }>;
18
19
  logout: () => Promise<void>;
20
+ fetchActiveSessions: () => Promise<{
21
+ sessions: ActiveSession[];
22
+ }>;
19
23
  };
20
24
  };
25
+ export declare const useUserActions: () => Ref<{
26
+ signIn: (body: {
27
+ username: string;
28
+ password: string;
29
+ rememberMe: boolean;
30
+ }) => Promise<import("./types").GetUserResponse | {
31
+ key: string;
32
+ }>;
33
+ logout: () => Promise<void>;
34
+ fetchActiveSessions: () => Promise<{
35
+ sessions: ActiveSession[];
36
+ }>;
37
+ }, {
38
+ signIn: (body: {
39
+ username: string;
40
+ password: string;
41
+ rememberMe: boolean;
42
+ }) => Promise<import("./types").GetUserResponse | {
43
+ key: string;
44
+ }>;
45
+ logout: () => Promise<void>;
46
+ fetchActiveSessions: () => Promise<{
47
+ sessions: ActiveSession[];
48
+ }>;
49
+ }> | undefined;
@@ -1,28 +1,26 @@
1
- import { Locale as VarletLocale } from "@varlet/ui";
2
1
  import { useLocalStorage } from "@vueuse/core";
3
- import { setNotifyDefaultOptions, Locale as VantLocale } from "vant";
4
- import enUS from "vant/es/locale/lang/en-US";
5
- import ruRU from "vant/es/locale/lang/ru-RU";
6
- import { watch } from "vue";
2
+ import { ref } from "vue";
7
3
  import { createApiFetch } from "./api-client.mjs";
8
4
  import bus from "./bus.mjs";
9
5
  import { newCometClient } from "./comet-client.mjs";
10
6
  import { useApiActions } from "./composables/use-api-actions.mjs";
11
7
  import { createApiGetUser } from "./get-user.mjs";
12
8
  import { createApiGetVisitorIdentifier } from "./get-visitor.mjs";
9
+ import { createInitApplication } from "./init-application.mjs";
13
10
  import { createApiRefreshToken } from "./refresh-token.mjs";
14
11
  import { createApiUserActions } from "./user-actions.mjs";
15
12
  export { useUser } from "./composables/use-user.mjs";
16
13
  export { useSubscribe } from "./composables/use-subscribe.mjs";
17
14
  export { getVisitor, getVisitorId } from "./get-visitor.mjs";
18
15
  const apiActions = useApiActions();
16
+ let userActions;
19
17
  export function initApiFetch($fetch, config) {
20
18
  const fetch = createApiFetch($fetch, config);
21
19
  const getUser = createApiGetUser(fetch, config);
22
20
  const getVisitorIdentifier = createApiGetVisitorIdentifier(config);
21
+ const initApplication = createInitApplication();
23
22
  const refreshToken = createApiRefreshToken(fetch, config);
24
- const useApiFetch = () => fetch;
25
- const userActions = createApiUserActions(fetch, config, getUser);
23
+ userActions = ref(createApiUserActions(fetch, config, getUser));
26
24
  apiActions.value = {
27
25
  initialized: true,
28
26
  getVisitorIdentifier,
@@ -30,8 +28,6 @@ export function initApiFetch($fetch, config) {
30
28
  };
31
29
  if (typeof window !== "undefined") {
32
30
  const loggedIn = useLocalStorage("lin", "no");
33
- const theme = useLocalStorage("app-preferences-theme", JSON.stringify({ value: "" }));
34
- const locale = useLocalStorage("app-preferences-locale", JSON.stringify({ value: "en" }));
35
31
  const cometServerURL = config.cometServerURL;
36
32
  let cometClient;
37
33
  bus.subscribe("user:onUpdated", (user) => {
@@ -46,48 +42,15 @@ export function initApiFetch($fetch, config) {
46
42
  cometClient.isStarted = false;
47
43
  }
48
44
  loggedIn.value = user ? "yes" : "no";
49
- try {
50
- const { value } = JSON.parse(theme.value);
51
- if (user && user.theme !== value) {
52
- theme.value = JSON.stringify({ value: user.theme });
53
- }
54
- } catch (error) {
55
- console.error(`Error parsing app preferences theme:`, error);
56
- }
57
- try {
58
- const { value } = JSON.parse(locale.value);
59
- if (user && user.locale !== value) {
60
- locale.value = JSON.stringify({ value: user.locale });
61
- }
62
- } catch (error) {
63
- console.error(`Error parsing app preferences locale:`, error);
64
- }
65
45
  });
66
- watch(
67
- locale,
68
- (locale2) => {
69
- try {
70
- const { value } = JSON.parse(locale2);
71
- if (value === "en") {
72
- VantLocale.use(locale2, enUS);
73
- VarletLocale.add("en-US", VarletLocale.enUS);
74
- }
75
- if (value === "ru") {
76
- VantLocale.use(locale2, ruRU);
77
- VarletLocale.add("en-US", VarletLocale.enUS);
78
- }
79
- } catch (error) {
80
- console.error(`Error parsing app preferences locale:`, error);
81
- VantLocale.use(locale2, enUS);
82
- VarletLocale.add("en-US", VarletLocale.enUS);
83
- }
84
- },
85
- { immediate: true }
86
- );
87
- setNotifyDefaultOptions({ position: "bottom" });
88
46
  bus.subscribe("app:beforeMount", () => {
89
47
  getVisitorIdentifier();
48
+ initApplication();
90
49
  });
91
50
  }
92
- return { fetch, getUser, useApiFetch, userActions };
51
+ const useApiFetch = () => fetch;
52
+ return { fetch, getUser, useApiFetch, userActions: userActions.value };
93
53
  }
54
+ export const useUserActions = () => {
55
+ return userActions;
56
+ };
@@ -0,0 +1 @@
1
+ export declare const createInitApplication: () => () => void;
@@ -0,0 +1,94 @@
1
+ import { Locale as VarletLocale } from "@varlet/ui";
2
+ import { useLocalStorage } from "@vueuse/core";
3
+ import { setNotifyDefaultOptions, Locale as VantLocale } from "vant";
4
+ import enUS from "vant/es/locale/lang/en-US";
5
+ import ruRU from "vant/es/locale/lang/ru-RU";
6
+ import { watch } from "vue";
7
+ import { useI18n } from "vue-i18n";
8
+ import { useRouter } from "vue-router";
9
+ import { useActiveSessions } from "./composables/use-active-sessions.mjs";
10
+ import { useSubscribe } from "./composables/use-subscribe.mjs";
11
+ import { useUser } from "./composables/use-user.mjs";
12
+ import { once } from "./helpers/index.mjs";
13
+ import { getVisitorId, useUserActions } from "./index.mjs";
14
+ function initApplication() {
15
+ const user = useUser();
16
+ const theme = useLocalStorage("app-preferences-theme", JSON.stringify({ value: "" }));
17
+ const locale = useLocalStorage("app-preferences-locale", JSON.stringify({ value: "en" }));
18
+ const i18n = useI18n();
19
+ watch(
20
+ user,
21
+ (user2) => {
22
+ try {
23
+ const { value } = JSON.parse(theme.value);
24
+ if (user2 && user2.theme !== value) {
25
+ theme.value = JSON.stringify({ value: user2.theme });
26
+ }
27
+ } catch (error) {
28
+ console.error(`Error parsing app preferences theme:`, error);
29
+ }
30
+ try {
31
+ const { value } = JSON.parse(locale.value);
32
+ if (user2 && user2.locale !== value) {
33
+ locale.value = JSON.stringify({ value: user2.locale });
34
+ }
35
+ } catch (error) {
36
+ console.error(`Error parsing app preferences locale:`, error);
37
+ }
38
+ },
39
+ { immediate: true }
40
+ );
41
+ watch(
42
+ locale,
43
+ (locale2) => {
44
+ try {
45
+ const { value } = JSON.parse(locale2);
46
+ if ("setLocale" in i18n && typeof i18n.setLocale === "function") {
47
+ i18n.setLocale(value);
48
+ }
49
+ if (value === "en") {
50
+ VantLocale.use(locale2, enUS);
51
+ VarletLocale.add("en-US", VarletLocale.enUS);
52
+ }
53
+ if (value === "ru") {
54
+ VantLocale.use(locale2, ruRU);
55
+ VarletLocale.add("en-US", VarletLocale.enUS);
56
+ }
57
+ } catch (error) {
58
+ console.error(`Error parsing app preferences locale:`, error);
59
+ }
60
+ },
61
+ { immediate: true }
62
+ );
63
+ setNotifyDefaultOptions({ position: "bottom" });
64
+ useSubscribe("WS.SessionsUpdated", async (session) => {
65
+ const user2 = useUser();
66
+ if (!user2.value) {
67
+ return;
68
+ }
69
+ const activeSessions = useActiveSessions();
70
+ if (activeSessions.value.length > 0) {
71
+ activeSessions.value = [...activeSessions.value].map((activeSession) => {
72
+ if (activeSession.id === session?.sessionId) {
73
+ activeSession.isOnline = session?.status === "Online";
74
+ activeSession.signedOut = session?.status === "SignedOut";
75
+ activeSession.dateTime = /* @__PURE__ */ new Date();
76
+ }
77
+ return activeSession;
78
+ });
79
+ }
80
+ if (session?.status === "SignedOut" && session?.visitorId === getVisitorId()) {
81
+ const userActions = useUserActions();
82
+ const data = await userActions?.value.fetchActiveSessions();
83
+ const isCurrentSession = data?.sessions.some((s) => s.isCurrentSession && s.id === session?.sessionId);
84
+ if (isCurrentSession) {
85
+ await userActions?.value.logout();
86
+ const router = useRouter();
87
+ await router.push({ path: "/" });
88
+ }
89
+ }
90
+ });
91
+ }
92
+ export const createInitApplication = once(() => {
93
+ return () => initApplication();
94
+ });
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/api",
3
- "version": "1.16.23",
3
+ "version": "1.16.25",
4
4
  "type": "module",
5
5
  "license": "ISC",
6
6
  "main": "./index.mjs",
@@ -17,7 +17,6 @@
17
17
  "ofetch": "^1.4.1",
18
18
  "rxjs": "^7.8.2",
19
19
  "vant": "^4.9.19",
20
- "vue": "^3.5.13",
21
20
  "zod": "^3.24.2"
22
21
  }
23
22
  }
@@ -1,4 +1,5 @@
1
- import type { Fetch, FetchConfig, GetUserResponse } from "./types";
1
+ import type { ActiveSession, Fetch, FetchConfig, GetUserResponse } from "./types";
2
+ export type UserActions = ReturnType<typeof createApiUserActions>;
2
3
  export declare const createApiUserActions: (fetch: Fetch, config: FetchConfig, getUser: () => Promise<GetUserResponse>) => {
3
4
  signIn: (body: {
4
5
  username: string;
@@ -8,4 +9,7 @@ export declare const createApiUserActions: (fetch: Fetch, config: FetchConfig, g
8
9
  key: string;
9
10
  }>;
10
11
  logout: () => Promise<void>;
12
+ fetchActiveSessions: () => Promise<{
13
+ sessions: ActiveSession[];
14
+ }>;
11
15
  };
@@ -29,6 +29,9 @@ export const createApiUserActions = once(
29
29
  } finally {
30
30
  flush();
31
31
  }
32
+ },
33
+ fetchActiveSessions: async () => {
34
+ return await fetch("/account/1/sessions");
32
35
  }
33
36
  };
34
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/nuxt-common",
3
- "version": "1.16.23",
3
+ "version": "1.16.25",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "exports": {