intor 2.2.9 → 2.2.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.
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var React6 = require('react');
3
+ var React10 = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
+ var merge = require('lodash.merge');
5
6
  var logry = require('logry');
6
7
  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,95 +27,24 @@ function _interopNamespace(e) {
27
27
  return Object.freeze(n);
28
28
  }
29
29
 
30
- var React6__namespace = /*#__PURE__*/_interopNamespace(React6);
31
- var Keyv__default = /*#__PURE__*/_interopDefault(Keyv);
30
+ var React10__namespace = /*#__PURE__*/_interopNamespace(React10);
32
31
  var merge__default = /*#__PURE__*/_interopDefault(merge);
32
+ var Keyv__default = /*#__PURE__*/_interopDefault(Keyv);
33
33
 
34
34
  // src/client/react/contexts/intor-provider/intor-provider.tsx
35
- var ConfigContext = React6__namespace.createContext(void 0);
35
+ var ConfigContext = React10__namespace.createContext(void 0);
36
36
  function ConfigProvider({
37
37
  value: { config, pathname },
38
38
  children
39
39
  }) {
40
- const value = React6__namespace.useMemo(() => ({ config, pathname }), [config, pathname]);
40
+ const value = React10__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 = React6__namespace.useContext(ConfigContext);
44
+ const context = React10__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
- }
119
48
  var mergeMessages = (staticMessages = {}, loadedMessages = {}) => {
120
49
  if (!loadedMessages) return { ...staticMessages };
121
50
  return merge__default.default({}, staticMessages, loadedMessages);
@@ -222,6 +151,97 @@ var standardizePathname = ({
222
151
  return normalizePathname(standardizedPathname);
223
152
  };
224
153
 
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
+
225
245
  // src/server/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.ts
226
246
  var fetchLocaleMessages = async ({
227
247
  remoteUrl,
@@ -359,11 +379,11 @@ var useRefetchMessages = ({
359
379
  setIsLoadingMessages
360
380
  }) => {
361
381
  const { messages: staticMessages } = config;
362
- const namespaces = React6__namespace.useMemo(() => {
382
+ const namespaces = React10__namespace.useMemo(() => {
363
383
  if (!config.loader) return [];
364
384
  return resolveNamespaces({ config, pathname });
365
385
  }, [config, pathname]);
366
- const refetchMessages = React6__namespace.useCallback(
386
+ const refetchMessages = React10__namespace.useCallback(
367
387
  async (newLocale) => {
368
388
  if (config.loader?.type === "remote") {
369
389
  setIsLoadingMessages(true);
@@ -396,21 +416,21 @@ var useRefetchMessages = ({
396
416
  );
397
417
  return { refetchMessages };
398
418
  };
399
- var MessagesContext = React6__namespace.createContext(void 0);
419
+ var MessagesContext = React10__namespace.createContext(void 0);
400
420
  function MessagesProvider({
401
421
  value: { messages = {} },
402
422
  children
403
423
  }) {
404
424
  const { config, pathname } = useConfig();
405
- const [loadedMessages, setLoadedMessages] = React6__namespace.useState(null);
406
- const [isLoadingMessages, setIsLoadingMessages] = React6__namespace.useState(false);
425
+ const [loadedMessages, setLoadedMessages] = React10__namespace.useState(null);
426
+ const [isLoadingMessages, setIsLoadingMessages] = React10__namespace.useState(false);
407
427
  const { refetchMessages } = useRefetchMessages({
408
428
  config,
409
429
  pathname,
410
430
  setLoadedMessages,
411
431
  setIsLoadingMessages
412
432
  });
413
- const value = React6__namespace.useMemo(
433
+ const value = React10__namespace.useMemo(
414
434
  () => ({
415
435
  messages: loadedMessages || messages,
416
436
  isLoading: isLoadingMessages,
@@ -423,7 +443,7 @@ function MessagesProvider({
423
443
  return /* @__PURE__ */ jsxRuntime.jsx(MessagesContext.Provider, { value, children });
424
444
  }
425
445
  function useMessages() {
426
- const context = React6__namespace.useContext(MessagesContext);
446
+ const context = React10__namespace.useContext(MessagesContext);
427
447
  if (!context)
428
448
  throw new Error("useMessages must be used within a MessagesProvider");
429
449
  return context;
@@ -436,14 +456,15 @@ var useInitLazyLoad = ({
436
456
  }) => {
437
457
  const { refetchMessages } = useMessages();
438
458
  const lazyLoad = !!loaderOptions?.lazyLoad;
439
- const isFirstLoadedRef = React6__namespace.useRef(false);
440
- React6__namespace.useEffect(() => {
459
+ const isFirstLoadedRef = React10__namespace.useRef(false);
460
+ React10__namespace.useEffect(() => {
441
461
  if (lazyLoad && !isFirstLoadedRef.current) {
442
462
  void refetchMessages(currentLocale);
443
463
  isFirstLoadedRef.current = true;
444
464
  }
445
465
  }, [lazyLoad, currentLocale, refetchMessages, isFirstLoadedRef]);
446
466
  };
467
+ var LocaleContext = React10__namespace.createContext(void 0);
447
468
 
448
469
  // src/shared/utils/client/build-cookie-string.ts
449
470
  var buildCookieString = (cookie, locale) => {
@@ -478,25 +499,6 @@ var setLocaleCookieBrowser = ({
478
499
  document.cookie = cookieString;
479
500
  };
480
501
 
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
-
500
502
  // src/client/react/contexts/locale/utils/change-locale.ts
501
503
  var changeLocale = ({
502
504
  currentLocale,
@@ -522,17 +524,16 @@ var changeLocale = ({
522
524
  }
523
525
  };
524
526
  function LocaleProvider({
525
- value: { initialLocale },
527
+ value: { initialLocale, importLoader },
526
528
  children
527
529
  }) {
528
530
  const { config } = useConfig();
529
- const { refetchMessages } = useMessages();
531
+ const { refetchMessages, setLoadedMessages, setIsLoadingMessages } = useMessages();
530
532
  const { loader: loaderOptions, cookie } = config;
531
- const [currentLocale, setCurrentLocale] = React6__namespace.useState(initialLocale);
533
+ const [currentLocale, setCurrentLocale] = React10__namespace.useState(initialLocale);
532
534
  useInitLazyLoad({ loaderOptions, currentLocale });
533
- useInitLocaleCookie({ config, locale: initialLocale });
534
- const setLocale = React6__namespace.useCallback(
535
- (newLocale) => {
535
+ const setLocale = React10__namespace.useCallback(
536
+ async (newLocale) => {
536
537
  changeLocale({
537
538
  currentLocale,
538
539
  newLocale,
@@ -541,10 +542,28 @@ function LocaleProvider({
541
542
  setLocale: setCurrentLocale,
542
543
  refetchMessages
543
544
  });
545
+ await loadImportMessages({
546
+ config,
547
+ locale: newLocale,
548
+ importLoader,
549
+ setLoadedMessages,
550
+ setIsLoadingMessages
551
+ });
544
552
  },
545
- [currentLocale, loaderOptions, cookie, refetchMessages]
553
+ [
554
+ currentLocale,
555
+ loaderOptions,
556
+ cookie,
557
+ refetchMessages,
558
+ importLoader,
559
+ setLoadedMessages,
560
+ setIsLoadingMessages
561
+ ]
546
562
  );
547
- const value = React6__namespace.useMemo(
563
+ React10__namespace.useEffect(() => {
564
+ void setLocale(initialLocale);
565
+ }, []);
566
+ const value = React10__namespace.useMemo(
548
567
  () => ({
549
568
  locale: currentLocale,
550
569
  setLocale
@@ -554,12 +573,12 @@ function LocaleProvider({
554
573
  return /* @__PURE__ */ jsxRuntime.jsx(LocaleContext.Provider, { value, children });
555
574
  }
556
575
  function useLocale() {
557
- const context = React6__namespace.useContext(LocaleContext);
576
+ const context = React10__namespace.useContext(LocaleContext);
558
577
  if (!context)
559
578
  throw new Error("useLocale must be used within a LocaleProvider");
560
579
  return context;
561
580
  }
562
- var TranslateHandlersContext = React6__namespace.createContext(void 0);
581
+ var TranslateHandlersContext = React10__namespace.createContext(void 0);
563
582
  var TranslateHandlersProvider = ({
564
583
  children,
565
584
  handlers
@@ -568,19 +587,19 @@ var TranslateHandlersProvider = ({
568
587
  return /* @__PURE__ */ jsxRuntime.jsx(TranslateHandlersContext.Provider, { value, children });
569
588
  };
570
589
  function useTranslateHandlers() {
571
- const context = React6__namespace.useContext(TranslateHandlersContext);
590
+ const context = React10__namespace.useContext(TranslateHandlersContext);
572
591
  return context;
573
592
  }
574
593
  var useInitLoadingState = (config) => {
575
594
  const lazyLoad = !!config.loader?.lazyLoad;
576
- const [isCsr, setIsCsr] = React6__namespace.useState(false);
577
- React6__namespace.useEffect(() => {
595
+ const [isCsr, setIsCsr] = React10__namespace.useState(false);
596
+ React10__namespace.useEffect(() => {
578
597
  setIsCsr(true);
579
598
  }, []);
580
599
  const isBeforeCSRLoading = lazyLoad && !isCsr;
581
600
  return isBeforeCSRLoading;
582
601
  };
583
- var TranslatorContext = React6__namespace.createContext(void 0);
602
+ var TranslatorContext = React10__namespace.createContext(void 0);
584
603
  var EMPTY_OBJECT = Object.freeze({});
585
604
  function TranslatorProvider({ children }) {
586
605
  const { config } = useConfig();
@@ -589,7 +608,7 @@ function TranslatorProvider({ children }) {
589
608
  const translatorHandlers = useTranslateHandlers();
590
609
  const { fallbackLocales, translator: translatorOptions } = config;
591
610
  const isBeforeCSRLoading = useInitLoadingState(config);
592
- const value = React6__namespace.useMemo(() => {
611
+ const value = React10__namespace.useMemo(() => {
593
612
  const translator = new intorTranslator.Translator({
594
613
  messages: messages || EMPTY_OBJECT,
595
614
  locale,
@@ -613,7 +632,7 @@ function TranslatorProvider({ children }) {
613
632
  return /* @__PURE__ */ jsxRuntime.jsx(TranslatorContext.Provider, { value, children });
614
633
  }
615
634
  function useTranslator() {
616
- const context = React6__namespace.useContext(TranslatorContext);
635
+ const context = React10__namespace.useContext(TranslatorContext);
617
636
  if (!context)
618
637
  throw new Error(
619
638
  "useTranslator must be used within IntorTranslatorProvider"
@@ -621,10 +640,16 @@ function useTranslator() {
621
640
  return context;
622
641
  }
623
642
  var IntorProvider = ({
624
- value: { config, pathname = "", initialLocale, messages = config.messages },
643
+ value: {
644
+ config,
645
+ pathname = "",
646
+ initialLocale,
647
+ messages = config.messages,
648
+ importLoader
649
+ },
625
650
  children
626
651
  }) => {
627
- return /* @__PURE__ */ jsxRuntime.jsx(ConfigProvider, { value: { config, pathname }, children: /* @__PURE__ */ jsxRuntime.jsx(MessagesProvider, { value: { messages }, children: /* @__PURE__ */ jsxRuntime.jsx(LocaleProvider, { value: { initialLocale }, children: /* @__PURE__ */ jsxRuntime.jsx(TranslatorProvider, { children }) }) }) });
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 }) }) }) });
628
653
  };
629
654
 
630
655
  // src/client/react/hooks/use-translator.ts
@@ -125,11 +125,12 @@ interface IntorProviderProps {
125
125
  initialLocale: Locale;
126
126
  pathname?: string;
127
127
  messages?: Readonly<LocaleMessages>;
128
+ importLoader?: (locale: string) => Promise<LocaleMessages>;
128
129
  };
129
130
  children: React.ReactNode;
130
131
  }
131
132
 
132
- declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
133
+ declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages, importLoader, }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
133
134
 
134
135
  type TranslateHandlersProviderProps = {
135
136
  children: React.ReactNode;
@@ -125,11 +125,12 @@ interface IntorProviderProps {
125
125
  initialLocale: Locale;
126
126
  pathname?: string;
127
127
  messages?: Readonly<LocaleMessages>;
128
+ importLoader?: (locale: string) => Promise<LocaleMessages>;
128
129
  };
129
130
  children: React.ReactNode;
130
131
  }
131
132
 
132
- declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
133
+ declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages, importLoader, }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
133
134
 
134
135
  type TranslateHandlersProviderProps = {
135
136
  children: React.ReactNode;