@uipkge/nuxt 0.1.8 → 0.1.10
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 +2 -9
- package/dist/runtime/plugin.client.js +57 -3
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineNuxtModule, createResolver, addPlugin,
|
|
1
|
+
import { defineNuxtModule, createResolver, addPlugin, addImports } from '@nuxt/kit';
|
|
2
2
|
|
|
3
3
|
const module$1 = defineNuxtModule({
|
|
4
4
|
meta: {
|
|
@@ -29,11 +29,9 @@ const module$1 = defineNuxtModule({
|
|
|
29
29
|
`[i18now] DANGER: syncIn includes "${dangerousEnvs.join('", "')}" \u2014 key sync is enabled in a production-like environment. This will expose your API key to users and flood i18now with production traffic. syncIn should only contain "development".`
|
|
30
30
|
);
|
|
31
31
|
}
|
|
32
|
-
nuxt.options.runtimeConfig.i18now = {
|
|
33
|
-
apiKey: options.apiKey
|
|
34
|
-
};
|
|
35
32
|
nuxt.options.runtimeConfig.public.i18now = {
|
|
36
33
|
projectId: options.projectId,
|
|
34
|
+
apiKey: options.apiKey,
|
|
37
35
|
host: options.host,
|
|
38
36
|
cdnUrl: options.cdnUrl,
|
|
39
37
|
environment: options.environment,
|
|
@@ -45,11 +43,6 @@ const module$1 = defineNuxtModule({
|
|
|
45
43
|
mode: "server"
|
|
46
44
|
});
|
|
47
45
|
if (nuxt.options.dev) {
|
|
48
|
-
addServerHandler({
|
|
49
|
-
route: "/api/_i18now/sync",
|
|
50
|
-
handler: resolver.resolve("./runtime/server/sync"),
|
|
51
|
-
method: "post"
|
|
52
|
-
});
|
|
53
46
|
addPlugin({
|
|
54
47
|
src: resolver.resolve("./runtime/plugin.client"),
|
|
55
48
|
mode: "client"
|
|
@@ -1,7 +1,60 @@
|
|
|
1
1
|
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
2
|
-
|
|
2
|
+
function createI18nowSyncer(options) {
|
|
3
|
+
const { endpoint, apiKey, debug = false, maxTracked = 2e3, maxRetries = 3 } = options;
|
|
4
|
+
const existingKeys = /* @__PURE__ */ new Set();
|
|
5
|
+
const synced = /* @__PURE__ */ new Set();
|
|
6
|
+
const retries = /* @__PURE__ */ new Map();
|
|
7
|
+
const pending = /* @__PURE__ */ new Map();
|
|
8
|
+
let flushTimer = null;
|
|
9
|
+
function flush() {
|
|
10
|
+
flushTimer = null;
|
|
11
|
+
if (pending.size === 0) return;
|
|
12
|
+
const keys = Array.from(pending.entries()).map(([key, value]) => ({ key, value }));
|
|
13
|
+
pending.clear();
|
|
14
|
+
fetch(endpoint, {
|
|
15
|
+
method: "POST",
|
|
16
|
+
headers: { "Content-Type": "application/json" },
|
|
17
|
+
body: JSON.stringify({ apiKey, keys })
|
|
18
|
+
}).then((res) => {
|
|
19
|
+
if (!res.ok) {
|
|
20
|
+
if (debug) {
|
|
21
|
+
res.json().then((data) => console.warn(`[i18now] Sync failed (${res.status}):`, data?.error ?? data?.statusMessage ?? data)).catch(() => console.warn(`[i18now] Sync failed with status ${res.status}`));
|
|
22
|
+
}
|
|
23
|
+
throw Object.assign(new Error(`${res.status}`), { status: res.status });
|
|
24
|
+
}
|
|
25
|
+
}).catch((err) => {
|
|
26
|
+
const status = err.status ?? 0;
|
|
27
|
+
const retryable = status === 0 || status >= 500;
|
|
28
|
+
for (const { key } of keys) {
|
|
29
|
+
if (!retryable) {
|
|
30
|
+
retries.delete(key);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const attempts = (retries.get(key) ?? 0) + 1;
|
|
34
|
+
if (attempts < maxRetries) {
|
|
35
|
+
retries.set(key, attempts);
|
|
36
|
+
synced.delete(key);
|
|
37
|
+
} else {
|
|
38
|
+
retries.delete(key);
|
|
39
|
+
if (debug) console.warn(`[i18now] Gave up syncing "${key}" after ${maxRetries} attempts.`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function syncKey(key, value) {
|
|
45
|
+
if (synced.has(key) || synced.size >= maxTracked) return;
|
|
46
|
+
synced.add(key);
|
|
47
|
+
pending.set(key, value);
|
|
48
|
+
if (!flushTimer) flushTimer = setTimeout(flush, 0);
|
|
49
|
+
}
|
|
50
|
+
function setExistingKeys(keys) {
|
|
51
|
+
existingKeys.clear();
|
|
52
|
+
for (const key of keys) existingKeys.add(key);
|
|
53
|
+
}
|
|
54
|
+
return { syncKey, existingKeys, setExistingKeys };
|
|
55
|
+
}
|
|
3
56
|
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
4
|
-
const { projectId, cdnUrl, environment, syncIn, locale: configLocale } = useRuntimeConfig().public.i18now;
|
|
57
|
+
const { projectId, apiKey, host, cdnUrl, environment, syncIn, locale: configLocale } = useRuntimeConfig().public.i18now;
|
|
5
58
|
if (!syncIn.includes(process.env.NODE_ENV ?? "production")) return;
|
|
6
59
|
if (!projectId) {
|
|
7
60
|
console.warn("[i18now] projectId is not set \u2014 key sync disabled.");
|
|
@@ -9,7 +62,8 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
9
62
|
}
|
|
10
63
|
const i18nGlobal = nuxtApp.$i18n ?? nuxtApp.vueApp.config.globalProperties.$i18n;
|
|
11
64
|
const syncer = createI18nowSyncer({
|
|
12
|
-
endpoint:
|
|
65
|
+
endpoint: `${host}/api/v1/projects/${projectId}/sync`,
|
|
66
|
+
apiKey,
|
|
13
67
|
debug: import.meta.dev
|
|
14
68
|
});
|
|
15
69
|
const locale = i18nGlobal?.locale?.value ?? i18nGlobal?.locale ?? configLocale;
|