intor 2.2.10 → 2.2.12

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.
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var React10 = require('react');
3
+ var React6 = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
- var merge = require('lodash.merge');
6
5
  var logry = require('logry');
7
6
  var Keyv = require('keyv');
7
+ var merge = require('lodash.merge');
8
8
  var intorTranslator = require('intor-translator');
9
9
 
10
10
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -27,24 +27,95 @@ function _interopNamespace(e) {
27
27
  return Object.freeze(n);
28
28
  }
29
29
 
30
- var React10__namespace = /*#__PURE__*/_interopNamespace(React10);
31
- var merge__default = /*#__PURE__*/_interopDefault(merge);
30
+ var React6__namespace = /*#__PURE__*/_interopNamespace(React6);
32
31
  var Keyv__default = /*#__PURE__*/_interopDefault(Keyv);
32
+ var merge__default = /*#__PURE__*/_interopDefault(merge);
33
33
 
34
34
  // src/client/react/contexts/intor-provider/intor-provider.tsx
35
- var ConfigContext = React10__namespace.createContext(void 0);
35
+ var ConfigContext = React6__namespace.createContext(void 0);
36
36
  function ConfigProvider({
37
37
  value: { config, pathname },
38
38
  children
39
39
  }) {
40
- const value = React10__namespace.useMemo(() => ({ config, pathname }), [config, pathname]);
40
+ const value = React6__namespace.useMemo(() => ({ config, pathname }), [config, pathname]);
41
41
  return /* @__PURE__ */ jsxRuntime.jsx(ConfigContext.Provider, { value, children });
42
42
  }
43
43
  function useConfig() {
44
- const context = React10__namespace.useContext(ConfigContext);
44
+ const context = React6__namespace.useContext(ConfigContext);
45
45
  if (!context) throw new Error("useConfig must be used within ConfigProvider");
46
46
  return context;
47
47
  }
48
+
49
+ // src/config/constants/cache.constants.ts
50
+ var DEFAULT_CACHE_OPTIONS = {
51
+ enabled: process.env.NODE_ENV === "production",
52
+ ttl: 60 * 60 * 1e3
53
+ // 1 hour
54
+ };
55
+
56
+ // src/server/shared/logger/global-logger-pool.ts
57
+ function getGlobalLoggerPool() {
58
+ if (!globalThis.__INTOR_LOGGER_POOL__) {
59
+ globalThis.__INTOR_LOGGER_POOL__ = /* @__PURE__ */ new Map();
60
+ }
61
+ return globalThis.__INTOR_LOGGER_POOL__;
62
+ }
63
+
64
+ // src/server/shared/logger/get-logger.ts
65
+ var DEFAULT_FORMATTER_CONFIG = {
66
+ node: { meta: { compact: true }, lineBreaksAfter: 1 }
67
+ };
68
+ function getLogger({
69
+ id = "default",
70
+ formatterConfig,
71
+ preset,
72
+ ...options
73
+ }) {
74
+ const pool = getGlobalLoggerPool();
75
+ let logger = pool.get(id);
76
+ const useDefault = !formatterConfig && !preset;
77
+ if (!logger) {
78
+ logger = logry.logry({
79
+ id,
80
+ formatterConfig: useDefault ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
81
+ preset,
82
+ ...options
83
+ });
84
+ pool.set(id, logger);
85
+ if (pool.size > 1e3) {
86
+ const keys = [...pool.keys()];
87
+ for (const key of keys.slice(0, 200)) pool.delete(key);
88
+ }
89
+ }
90
+ return logger;
91
+ }
92
+
93
+ // src/server/messages/shared/utils/is-valid-messages.ts
94
+ function isPlainObject(value) {
95
+ return typeof value === "object" && value !== null && !Array.isArray(value);
96
+ }
97
+ function isValidMessages(value) {
98
+ if (!isPlainObject(value)) return false;
99
+ const stack = [value];
100
+ while (stack.length > 0) {
101
+ const current = stack.pop();
102
+ for (const v of Object.values(current)) {
103
+ if (typeof v === "string") continue;
104
+ if (isPlainObject(v)) {
105
+ stack.push(v);
106
+ } else {
107
+ return false;
108
+ }
109
+ }
110
+ }
111
+ return true;
112
+ }
113
+ function getGlobalMessagesPool() {
114
+ if (!globalThis.__INTOR_MESSAGES_POOL__) {
115
+ globalThis.__INTOR_MESSAGES_POOL__ = new Keyv__default.default();
116
+ }
117
+ return globalThis.__INTOR_MESSAGES_POOL__;
118
+ }
48
119
  var mergeMessages = (staticMessages = {}, loadedMessages = {}) => {
49
120
  if (!loadedMessages) return { ...staticMessages };
50
121
  return merge__default.default({}, staticMessages, loadedMessages);
@@ -151,97 +222,6 @@ var standardizePathname = ({
151
222
  return normalizePathname(standardizedPathname);
152
223
  };
153
224
 
154
- // src/client/react/contexts/load-import-messages.ts
155
- async function loadImportMessages({
156
- config,
157
- locale,
158
- importLoader,
159
- setLoadedMessages,
160
- setIsLoadingMessages
161
- }) {
162
- if (!importLoader) return;
163
- try {
164
- setIsLoadingMessages(true);
165
- const importMessages = await importLoader(locale);
166
- setLoadedMessages(mergeMessages(config.messages, importMessages));
167
- } catch (error) {
168
- console.error(`Failed to load messages for ${locale}`, error);
169
- } finally {
170
- setIsLoadingMessages(false);
171
- }
172
- }
173
-
174
- // src/config/constants/cache.constants.ts
175
- var DEFAULT_CACHE_OPTIONS = {
176
- enabled: process.env.NODE_ENV === "production",
177
- ttl: 60 * 60 * 1e3
178
- // 1 hour
179
- };
180
-
181
- // src/server/shared/logger/global-logger-pool.ts
182
- function getGlobalLoggerPool() {
183
- if (!globalThis.__INTOR_LOGGER_POOL__) {
184
- globalThis.__INTOR_LOGGER_POOL__ = /* @__PURE__ */ new Map();
185
- }
186
- return globalThis.__INTOR_LOGGER_POOL__;
187
- }
188
-
189
- // src/server/shared/logger/get-logger.ts
190
- var DEFAULT_FORMATTER_CONFIG = {
191
- node: { meta: { compact: true }, lineBreaksAfter: 1 }
192
- };
193
- function getLogger({
194
- id = "default",
195
- formatterConfig,
196
- preset,
197
- ...options
198
- }) {
199
- const pool = getGlobalLoggerPool();
200
- let logger = pool.get(id);
201
- const useDefault = !formatterConfig && !preset;
202
- if (!logger) {
203
- logger = logry.logry({
204
- id,
205
- formatterConfig: useDefault ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
206
- preset,
207
- ...options
208
- });
209
- pool.set(id, logger);
210
- if (pool.size > 1e3) {
211
- const keys = [...pool.keys()];
212
- for (const key of keys.slice(0, 200)) pool.delete(key);
213
- }
214
- }
215
- return logger;
216
- }
217
-
218
- // src/server/messages/shared/utils/is-valid-messages.ts
219
- function isPlainObject(value) {
220
- return typeof value === "object" && value !== null && !Array.isArray(value);
221
- }
222
- function isValidMessages(value) {
223
- if (!isPlainObject(value)) return false;
224
- const stack = [value];
225
- while (stack.length > 0) {
226
- const current = stack.pop();
227
- for (const v of Object.values(current)) {
228
- if (typeof v === "string") continue;
229
- if (isPlainObject(v)) {
230
- stack.push(v);
231
- } else {
232
- return false;
233
- }
234
- }
235
- }
236
- return true;
237
- }
238
- function getGlobalMessagesPool() {
239
- if (!globalThis.__INTOR_MESSAGES_POOL__) {
240
- globalThis.__INTOR_MESSAGES_POOL__ = new Keyv__default.default();
241
- }
242
- return globalThis.__INTOR_MESSAGES_POOL__;
243
- }
244
-
245
225
  // src/server/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.ts
246
226
  var fetchLocaleMessages = async ({
247
227
  remoteUrl,
@@ -379,11 +359,11 @@ var useRefetchMessages = ({
379
359
  setIsLoadingMessages
380
360
  }) => {
381
361
  const { messages: staticMessages } = config;
382
- const namespaces = React10__namespace.useMemo(() => {
362
+ const namespaces = React6__namespace.useMemo(() => {
383
363
  if (!config.loader) return [];
384
364
  return resolveNamespaces({ config, pathname });
385
365
  }, [config, pathname]);
386
- const refetchMessages = React10__namespace.useCallback(
366
+ const refetchMessages = React6__namespace.useCallback(
387
367
  async (newLocale) => {
388
368
  if (config.loader?.type === "remote") {
389
369
  setIsLoadingMessages(true);
@@ -416,21 +396,21 @@ var useRefetchMessages = ({
416
396
  );
417
397
  return { refetchMessages };
418
398
  };
419
- var MessagesContext = React10__namespace.createContext(void 0);
399
+ var MessagesContext = React6__namespace.createContext(void 0);
420
400
  function MessagesProvider({
421
401
  value: { messages = {} },
422
402
  children
423
403
  }) {
424
404
  const { config, pathname } = useConfig();
425
- const [loadedMessages, setLoadedMessages] = React10__namespace.useState(null);
426
- const [isLoadingMessages, setIsLoadingMessages] = React10__namespace.useState(false);
405
+ const [loadedMessages, setLoadedMessages] = React6__namespace.useState(null);
406
+ const [isLoadingMessages, setIsLoadingMessages] = React6__namespace.useState(false);
427
407
  const { refetchMessages } = useRefetchMessages({
428
408
  config,
429
409
  pathname,
430
410
  setLoadedMessages,
431
411
  setIsLoadingMessages
432
412
  });
433
- const value = React10__namespace.useMemo(
413
+ const value = React6__namespace.useMemo(
434
414
  () => ({
435
415
  messages: loadedMessages || messages,
436
416
  isLoading: isLoadingMessages,
@@ -443,7 +423,7 @@ function MessagesProvider({
443
423
  return /* @__PURE__ */ jsxRuntime.jsx(MessagesContext.Provider, { value, children });
444
424
  }
445
425
  function useMessages() {
446
- const context = React10__namespace.useContext(MessagesContext);
426
+ const context = React6__namespace.useContext(MessagesContext);
447
427
  if (!context)
448
428
  throw new Error("useMessages must be used within a MessagesProvider");
449
429
  return context;
@@ -456,15 +436,14 @@ var useInitLazyLoad = ({
456
436
  }) => {
457
437
  const { refetchMessages } = useMessages();
458
438
  const lazyLoad = !!loaderOptions?.lazyLoad;
459
- const isFirstLoadedRef = React10__namespace.useRef(false);
460
- React10__namespace.useEffect(() => {
439
+ const isFirstLoadedRef = React6__namespace.useRef(false);
440
+ React6__namespace.useEffect(() => {
461
441
  if (lazyLoad && !isFirstLoadedRef.current) {
462
442
  void refetchMessages(currentLocale);
463
443
  isFirstLoadedRef.current = true;
464
444
  }
465
445
  }, [lazyLoad, currentLocale, refetchMessages, isFirstLoadedRef]);
466
446
  };
467
- var LocaleContext = React10__namespace.createContext(void 0);
468
447
 
469
448
  // src/shared/utils/client/build-cookie-string.ts
470
449
  var buildCookieString = (cookie, locale) => {
@@ -499,6 +478,25 @@ var setLocaleCookieBrowser = ({
499
478
  document.cookie = cookieString;
500
479
  };
501
480
 
481
+ // src/client/react/contexts/locale/utils/use-init-locale-cookie.ts
482
+ var useInitLocaleCookie = ({
483
+ config,
484
+ locale
485
+ }) => {
486
+ React6__namespace.useEffect(() => {
487
+ if (typeof document === "undefined") return;
488
+ const { cookie, routing } = config;
489
+ const { firstVisit } = routing;
490
+ const cookies = document.cookie.split(";").map((c) => c.trim());
491
+ const isCookieExists = cookies.some((c) => c.startsWith(`${cookie.name}=`));
492
+ if (isCookieExists) return;
493
+ if (!firstVisit.redirect) return;
494
+ if (cookie.disabled || !cookie.autoSetCookie) return;
495
+ setLocaleCookieBrowser({ cookie, locale });
496
+ }, []);
497
+ };
498
+ var LocaleContext = React6__namespace.createContext(void 0);
499
+
502
500
  // src/client/react/contexts/locale/utils/change-locale.ts
503
501
  var changeLocale = ({
504
502
  currentLocale,
@@ -524,15 +522,16 @@ var changeLocale = ({
524
522
  }
525
523
  };
526
524
  function LocaleProvider({
527
- value: { initialLocale, importLoader },
525
+ value: { initialLocale, onLocaleChange },
528
526
  children
529
527
  }) {
530
528
  const { config } = useConfig();
531
- const { refetchMessages, setLoadedMessages, setIsLoadingMessages } = useMessages();
529
+ const { refetchMessages } = useMessages();
532
530
  const { loader: loaderOptions, cookie } = config;
533
- const [currentLocale, setCurrentLocale] = React10__namespace.useState(initialLocale);
531
+ const [currentLocale, setCurrentLocale] = React6__namespace.useState(initialLocale);
534
532
  useInitLazyLoad({ loaderOptions, currentLocale });
535
- const setLocale = React10__namespace.useCallback(
533
+ useInitLocaleCookie({ config, locale: initialLocale });
534
+ const setLocale = React6__namespace.useCallback(
536
535
  async (newLocale) => {
537
536
  changeLocale({
538
537
  currentLocale,
@@ -542,28 +541,11 @@ function LocaleProvider({
542
541
  setLocale: setCurrentLocale,
543
542
  refetchMessages
544
543
  });
545
- await loadImportMessages({
546
- config,
547
- locale: newLocale,
548
- importLoader,
549
- setLoadedMessages,
550
- setIsLoadingMessages
551
- });
544
+ onLocaleChange?.(newLocale);
552
545
  },
553
- [
554
- currentLocale,
555
- loaderOptions,
556
- cookie,
557
- refetchMessages,
558
- importLoader,
559
- setLoadedMessages,
560
- setIsLoadingMessages
561
- ]
546
+ [currentLocale, loaderOptions, cookie, refetchMessages, onLocaleChange]
562
547
  );
563
- React10__namespace.useEffect(() => {
564
- void setLocale(initialLocale);
565
- }, []);
566
- const value = React10__namespace.useMemo(
548
+ const value = React6__namespace.useMemo(
567
549
  () => ({
568
550
  locale: currentLocale,
569
551
  setLocale
@@ -573,12 +555,12 @@ function LocaleProvider({
573
555
  return /* @__PURE__ */ jsxRuntime.jsx(LocaleContext.Provider, { value, children });
574
556
  }
575
557
  function useLocale() {
576
- const context = React10__namespace.useContext(LocaleContext);
558
+ const context = React6__namespace.useContext(LocaleContext);
577
559
  if (!context)
578
560
  throw new Error("useLocale must be used within a LocaleProvider");
579
561
  return context;
580
562
  }
581
- var TranslateHandlersContext = React10__namespace.createContext(void 0);
563
+ var TranslateHandlersContext = React6__namespace.createContext(void 0);
582
564
  var TranslateHandlersProvider = ({
583
565
  children,
584
566
  handlers
@@ -587,19 +569,19 @@ var TranslateHandlersProvider = ({
587
569
  return /* @__PURE__ */ jsxRuntime.jsx(TranslateHandlersContext.Provider, { value, children });
588
570
  };
589
571
  function useTranslateHandlers() {
590
- const context = React10__namespace.useContext(TranslateHandlersContext);
572
+ const context = React6__namespace.useContext(TranslateHandlersContext);
591
573
  return context;
592
574
  }
593
575
  var useInitLoadingState = (config) => {
594
576
  const lazyLoad = !!config.loader?.lazyLoad;
595
- const [isCsr, setIsCsr] = React10__namespace.useState(false);
596
- React10__namespace.useEffect(() => {
577
+ const [isCsr, setIsCsr] = React6__namespace.useState(false);
578
+ React6__namespace.useEffect(() => {
597
579
  setIsCsr(true);
598
580
  }, []);
599
581
  const isBeforeCSRLoading = lazyLoad && !isCsr;
600
582
  return isBeforeCSRLoading;
601
583
  };
602
- var TranslatorContext = React10__namespace.createContext(void 0);
584
+ var TranslatorContext = React6__namespace.createContext(void 0);
603
585
  var EMPTY_OBJECT = Object.freeze({});
604
586
  function TranslatorProvider({ children }) {
605
587
  const { config } = useConfig();
@@ -608,7 +590,7 @@ function TranslatorProvider({ children }) {
608
590
  const translatorHandlers = useTranslateHandlers();
609
591
  const { fallbackLocales, translator: translatorOptions } = config;
610
592
  const isBeforeCSRLoading = useInitLoadingState(config);
611
- const value = React10__namespace.useMemo(() => {
593
+ const value = React6__namespace.useMemo(() => {
612
594
  const translator = new intorTranslator.Translator({
613
595
  messages: messages || EMPTY_OBJECT,
614
596
  locale,
@@ -632,7 +614,7 @@ function TranslatorProvider({ children }) {
632
614
  return /* @__PURE__ */ jsxRuntime.jsx(TranslatorContext.Provider, { value, children });
633
615
  }
634
616
  function useTranslator() {
635
- const context = React10__namespace.useContext(TranslatorContext);
617
+ const context = React6__namespace.useContext(TranslatorContext);
636
618
  if (!context)
637
619
  throw new Error(
638
620
  "useTranslator must be used within IntorTranslatorProvider"
@@ -645,11 +627,11 @@ var IntorProvider = ({
645
627
  pathname = "",
646
628
  initialLocale,
647
629
  messages = config.messages,
648
- importLoader
630
+ onLocaleChange
649
631
  },
650
632
  children
651
633
  }) => {
652
- return /* @__PURE__ */ jsxRuntime.jsx(ConfigProvider, { value: { config, pathname }, children: /* @__PURE__ */ jsxRuntime.jsx(MessagesProvider, { value: { messages }, children: /* @__PURE__ */ jsxRuntime.jsx(LocaleProvider, { value: { initialLocale, importLoader }, children: /* @__PURE__ */ jsxRuntime.jsx(TranslatorProvider, { children }) }) }) });
634
+ return /* @__PURE__ */ jsxRuntime.jsx(ConfigProvider, { value: { config, pathname }, children: /* @__PURE__ */ jsxRuntime.jsx(MessagesProvider, { value: { messages }, children: /* @__PURE__ */ jsxRuntime.jsx(LocaleProvider, { value: { initialLocale, onLocaleChange }, children: /* @__PURE__ */ jsxRuntime.jsx(TranslatorProvider, { children }) }) }) });
653
635
  };
654
636
 
655
637
  // src/client/react/hooks/use-translator.ts
@@ -125,12 +125,12 @@ interface IntorProviderProps {
125
125
  initialLocale: Locale;
126
126
  pathname?: string;
127
127
  messages?: Readonly<LocaleMessages>;
128
- importLoader?: (locale: string) => Promise<LocaleMessages>;
128
+ onLocaleChange?: (newLocale: string) => Promise<void> | void;
129
129
  };
130
130
  children: React.ReactNode;
131
131
  }
132
132
 
133
- declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages, importLoader, }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
133
+ declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages, onLocaleChange, }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
134
134
 
135
135
  type TranslateHandlersProviderProps = {
136
136
  children: React.ReactNode;
@@ -125,12 +125,12 @@ interface IntorProviderProps {
125
125
  initialLocale: Locale;
126
126
  pathname?: string;
127
127
  messages?: Readonly<LocaleMessages>;
128
- importLoader?: (locale: string) => Promise<LocaleMessages>;
128
+ onLocaleChange?: (newLocale: string) => Promise<void> | void;
129
129
  };
130
130
  children: React.ReactNode;
131
131
  }
132
132
 
133
- declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages, importLoader, }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
133
+ declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages, onLocaleChange, }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
134
134
 
135
135
  type TranslateHandlersProviderProps = {
136
136
  children: React.ReactNode;