@inzombieland/nuxt-common 1.18.0 → 1.18.2
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 +1 -1
- package/dist/module.mjs +1 -1
- package/dist/runtime/packages/core/get-user.mjs +2 -17
- package/dist/runtime/packages/core/get-visitor.d.ts +1 -8
- package/dist/runtime/packages/core/get-visitor.mjs +3 -18
- package/dist/runtime/packages/core/helpers/index.d.ts +1 -0
- package/dist/runtime/packages/core/helpers/index.mjs +14 -0
- package/dist/runtime/packages/core/index.d.ts +3 -3
- package/dist/runtime/packages/core/package.json +1 -1
- package/dist/runtime/packages/core/refresh-token.mjs +2 -17
- package/dist/runtime/packages/core/types.d.ts +6 -0
- package/package.json +1 -1
package/dist/module.json
CHANGED
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.18.
|
|
5
|
+
const version = "1.18.2";
|
|
6
6
|
|
|
7
7
|
const module = defineNuxtModule({
|
|
8
8
|
meta: {
|
|
@@ -2,23 +2,7 @@ import { useLocalStorage } from "@vueuse/core";
|
|
|
2
2
|
import { flush, getToken, setUser } from "./api-client.mjs";
|
|
3
3
|
import { useApiActions } from "./composables/use-api-actions.mjs";
|
|
4
4
|
import { useUser } from "./composables/use-user.mjs";
|
|
5
|
-
import { once } from "./helpers/index.mjs";
|
|
6
|
-
let gettingUserInProgress = false;
|
|
7
|
-
let gettingUserPromise = null;
|
|
8
|
-
function getUser(fetch, config) {
|
|
9
|
-
if (gettingUserInProgress && gettingUserPromise) {
|
|
10
|
-
return gettingUserPromise;
|
|
11
|
-
}
|
|
12
|
-
return gettingUserStart(fetch, config);
|
|
13
|
-
}
|
|
14
|
-
function gettingUserStart(fetch, config) {
|
|
15
|
-
gettingUserInProgress = true;
|
|
16
|
-
gettingUserPromise = getUserRequest(fetch, config).finally(() => {
|
|
17
|
-
gettingUserInProgress = false;
|
|
18
|
-
gettingUserPromise = null;
|
|
19
|
-
});
|
|
20
|
-
return gettingUserPromise;
|
|
21
|
-
}
|
|
5
|
+
import { createSingletonAsync, once } from "./helpers/index.mjs";
|
|
22
6
|
async function getUserRequest(fetch, config) {
|
|
23
7
|
const token = getToken();
|
|
24
8
|
const user = useUser();
|
|
@@ -50,6 +34,7 @@ async function getUserRequest(fetch, config) {
|
|
|
50
34
|
return { token: null, user: null };
|
|
51
35
|
}
|
|
52
36
|
}
|
|
37
|
+
const getUser = createSingletonAsync(getUserRequest);
|
|
53
38
|
export const createApiGetUser = once((fetch, config) => {
|
|
54
39
|
return () => getUser(fetch, config);
|
|
55
40
|
});
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import type { FetchConfig } from "./types";
|
|
2
|
-
export declare type Visitor = {
|
|
3
|
-
id?: string;
|
|
4
|
-
ip?: string;
|
|
5
|
-
device?: string;
|
|
6
|
-
appName?: string;
|
|
7
|
-
};
|
|
1
|
+
import type { FetchConfig, Visitor } from "./types";
|
|
8
2
|
export declare const getVisitorId: () => string | undefined;
|
|
9
3
|
export declare const getVisitor: () => Visitor | null;
|
|
10
|
-
export declare const getVisitorRequest: (config: FetchConfig) => Promise<void>;
|
|
11
4
|
export declare const createApiGetVisitorIdentifier: (config: FetchConfig) => () => Promise<void>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { hashComponents, load } from "@fingerprintjs/fingerprintjs";
|
|
2
2
|
import { ofetch } from "ofetch";
|
|
3
|
-
import { once } from "./helpers/index.mjs";
|
|
3
|
+
import { createSingletonAsync, once } from "./helpers/index.mjs";
|
|
4
4
|
let visitor = null;
|
|
5
5
|
export const getVisitorId = () => {
|
|
6
6
|
return visitor?.id;
|
|
@@ -11,23 +11,7 @@ export const getVisitor = () => {
|
|
|
11
11
|
const setVisitor = (newVisitor) => {
|
|
12
12
|
visitor = !visitor ? newVisitor : { ...visitor, ...newVisitor };
|
|
13
13
|
};
|
|
14
|
-
|
|
15
|
-
let gettingPromise = null;
|
|
16
|
-
function getVisitorIdentifier(config) {
|
|
17
|
-
if (gettingInProgress && gettingPromise) {
|
|
18
|
-
return gettingPromise;
|
|
19
|
-
}
|
|
20
|
-
return gettingVisitorStart(config);
|
|
21
|
-
}
|
|
22
|
-
function gettingVisitorStart(config) {
|
|
23
|
-
gettingInProgress = true;
|
|
24
|
-
gettingPromise = getVisitorRequest(config).finally(() => {
|
|
25
|
-
gettingInProgress = false;
|
|
26
|
-
gettingPromise = null;
|
|
27
|
-
});
|
|
28
|
-
return gettingPromise;
|
|
29
|
-
}
|
|
30
|
-
export const getVisitorRequest = async (config) => {
|
|
14
|
+
const getVisitorRequest = async (config) => {
|
|
31
15
|
if (typeof window === "undefined" || visitor) {
|
|
32
16
|
return;
|
|
33
17
|
}
|
|
@@ -43,6 +27,7 @@ export const getVisitorRequest = async (config) => {
|
|
|
43
27
|
const appName = config.appName || "unknown";
|
|
44
28
|
setVisitor({ id, ip, device, appName });
|
|
45
29
|
};
|
|
30
|
+
const getVisitorIdentifier = createSingletonAsync(getVisitorRequest);
|
|
46
31
|
export const createApiGetVisitorIdentifier = once((config) => {
|
|
47
32
|
return () => getVisitorIdentifier(config);
|
|
48
33
|
});
|
|
@@ -2,3 +2,4 @@ export { apiHelper } from "./api-helper";
|
|
|
2
2
|
export { dateHelper } from "./date-helper";
|
|
3
3
|
export { deviceHelper, type Device } from "./device-helper";
|
|
4
4
|
export declare function once<T extends (...args: any[]) => any>(fn: T): T;
|
|
5
|
+
export declare function createSingletonAsync<T, Args extends unknown[]>(fn: (...args: Args) => Promise<T>): (...args: Args) => Promise<T>;
|
|
@@ -13,3 +13,17 @@ export function once(fn) {
|
|
|
13
13
|
return result;
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
+
export function createSingletonAsync(fn) {
|
|
17
|
+
const inProgressMap = /* @__PURE__ */ new Map();
|
|
18
|
+
return (...args) => {
|
|
19
|
+
const key = JSON.stringify(args);
|
|
20
|
+
if (inProgressMap.has(key)) {
|
|
21
|
+
return inProgressMap.get(key);
|
|
22
|
+
}
|
|
23
|
+
const promise = fn(...args).finally(() => {
|
|
24
|
+
inProgressMap.delete(key);
|
|
25
|
+
});
|
|
26
|
+
inProgressMap.set(key, promise);
|
|
27
|
+
return promise;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { type Ref } from "vue";
|
|
2
|
-
import type { ActiveSession, Fetch, FetchConfig, User } from "./types";
|
|
3
|
-
export type { ActiveSession, Fetch, FetchConfig, User };
|
|
2
|
+
import type { ActiveSession, Fetch, FetchConfig, User, Visitor } from "./types";
|
|
3
|
+
export type { ActiveSession, Fetch, FetchConfig, User, Visitor };
|
|
4
4
|
export { default as AppProvider } from "./AppProvider.vue";
|
|
5
5
|
export { useUser } from "./composables/use-user";
|
|
6
6
|
export { useSubscribe } from "./composables/use-subscribe";
|
|
7
7
|
export { useActiveSessions } from "./composables/use-active-sessions";
|
|
8
8
|
export { useThemeMode } from "./composables/use-theme-mode";
|
|
9
9
|
export { useAppLocale } from "./composables/use-app-locale";
|
|
10
|
-
export { getVisitor, getVisitorId
|
|
10
|
+
export { getVisitor, getVisitorId } from "./get-visitor";
|
|
11
11
|
export declare function initApiFetch($fetch: Fetch, config: FetchConfig): {
|
|
12
12
|
fetch: Fetch;
|
|
13
13
|
getUser: () => Promise<import("./types").GetUserResponse>;
|
|
@@ -1,21 +1,5 @@
|
|
|
1
1
|
import { flush, setToken } from "./api-client.mjs";
|
|
2
|
-
import { once } from "./helpers/index.mjs";
|
|
3
|
-
let refreshingTokenInProgress = false;
|
|
4
|
-
let refreshingTokenPromise = null;
|
|
5
|
-
function refreshToken(fetch, config) {
|
|
6
|
-
if (refreshingTokenInProgress && refreshingTokenPromise) {
|
|
7
|
-
return refreshingTokenPromise;
|
|
8
|
-
}
|
|
9
|
-
return refreshingTokenStart(fetch, config);
|
|
10
|
-
}
|
|
11
|
-
function refreshingTokenStart(fetch, config) {
|
|
12
|
-
refreshingTokenInProgress = true;
|
|
13
|
-
refreshingTokenPromise = refreshTokenRequest(fetch, config).finally(() => {
|
|
14
|
-
refreshingTokenInProgress = false;
|
|
15
|
-
refreshingTokenPromise = null;
|
|
16
|
-
});
|
|
17
|
-
return refreshingTokenPromise;
|
|
18
|
-
}
|
|
2
|
+
import { createSingletonAsync, once } from "./helpers/index.mjs";
|
|
19
3
|
async function refreshTokenRequest(fetch, config) {
|
|
20
4
|
setToken(null);
|
|
21
5
|
try {
|
|
@@ -34,6 +18,7 @@ async function refreshTokenRequest(fetch, config) {
|
|
|
34
18
|
throw error;
|
|
35
19
|
}
|
|
36
20
|
}
|
|
21
|
+
const refreshToken = createSingletonAsync(refreshTokenRequest);
|
|
37
22
|
export const createApiRefreshToken = once((fetch, config) => {
|
|
38
23
|
return () => refreshToken(fetch, config);
|
|
39
24
|
});
|