@uipkge/nuxt 0.1.6 → 0.1.9

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
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.0.0"
6
6
  },
7
- "version": "0.1.6",
7
+ "version": "0.1.9",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.2",
10
10
  "unbuild": "unknown"
@@ -4,10 +4,10 @@ export function useI18n(options) {
4
4
  const i18n = _useI18n({ useScope: "global", ...options });
5
5
  const nuxtApp = useNuxtApp();
6
6
  const originalT = i18n.t.bind(i18n);
7
- const patchedT = (key, defaultValue, ...rest) => {
8
- const result = originalT(key, defaultValue, ...rest);
7
+ const patchedT = (key, defaultValue, namedParams) => {
8
+ const result = namedParams ? originalT(key, namedParams) : originalT(key);
9
9
  const sync = nuxtApp.$i18nowSync;
10
- if (sync && !sync.existingKeys.has(key) && typeof defaultValue === "string") {
10
+ if (sync && !sync.existingKeys.has(key) && defaultValue !== void 0) {
11
11
  sync.syncKey(key, defaultValue);
12
12
  }
13
13
  return result;
@@ -1,5 +1,58 @@
1
1
  import { defineNuxtPlugin, useRuntimeConfig } from "#app";
2
- import { createI18nowSyncer } from "@uipkge/core";
2
+ function createI18nowSyncer(options) {
3
+ const { endpoint, 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({ keys })
18
+ }).then((res) => {
19
+ if (!res.ok) {
20
+ if (debug) {
21
+ res.json().then((data) => console.warn(`[i18now] Sync failed (${res.status}):`, 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
57
  const { projectId, cdnUrl, environment, syncIn, locale: configLocale } = useRuntimeConfig().public.i18now;
5
58
  if (!syncIn.includes(process.env.NODE_ENV ?? "production")) return;
@@ -35,9 +88,9 @@ export default defineNuxtPlugin(async (nuxtApp) => {
35
88
  await loadLocale(locale);
36
89
  const originalGlobalT = nuxtApp.vueApp.config.globalProperties.$t;
37
90
  if (originalGlobalT) {
38
- nuxtApp.vueApp.config.globalProperties.$t = function(key, defaultValue, ...rest) {
39
- const result = originalGlobalT.call(this, key, defaultValue, ...rest);
40
- if (!syncer.existingKeys.has(key) && typeof defaultValue === "string") {
91
+ nuxtApp.vueApp.config.globalProperties.$t = function(key, defaultValue, namedParams) {
92
+ const result = namedParams ? originalGlobalT.call(this, key, namedParams) : originalGlobalT.call(this, key);
93
+ if (!syncer.existingKeys.has(key) && defaultValue !== void 0) {
41
94
  syncer.syncKey(key, defaultValue);
42
95
  }
43
96
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uipkge/nuxt",
3
- "version": "0.1.6",
3
+ "version": "0.1.9",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {