react-intlayer 2.0.1 → 2.0.3

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.
Files changed (73) hide show
  1. package/dist/cjs/client/ContentEditionLayout.cjs +10 -6
  2. package/dist/cjs/client/ContentEditionLayout.cjs.map +1 -1
  3. package/dist/cjs/client/IntlayerProvider.cjs +4 -3
  4. package/dist/cjs/client/IntlayerProvider.cjs.map +1 -1
  5. package/dist/cjs/client/IntlayerProvider.d.ts +1 -0
  6. package/dist/cjs/client/getBrowserLocale.cjs +7 -14
  7. package/dist/cjs/client/getBrowserLocale.cjs.map +1 -1
  8. package/dist/cjs/client/index.d.ts +1 -1
  9. package/dist/cjs/client/useContent.d.ts +1 -1
  10. package/dist/cjs/client/useIntlayer.d.ts +1 -0
  11. package/dist/cjs/client/useLocaleCookie.cjs.map +1 -1
  12. package/dist/cjs/index.cjs +0 -3
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs/index.d.ts +1 -1
  15. package/dist/cjs/processDictionary/index.cjs +2 -2
  16. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  17. package/dist/cjs/renderIntlayerEditor.cjs +55 -0
  18. package/dist/cjs/renderIntlayerEditor.cjs.map +1 -0
  19. package/dist/cjs/renderIntlayerEditor.d.ts +5 -0
  20. package/dist/cjs/server/IntlayerServerProvider.d.ts +5 -6
  21. package/dist/cjs/server/serverContext.cjs +1 -2
  22. package/dist/cjs/server/serverContext.cjs.map +1 -1
  23. package/dist/cjs/server/useIntlayer.d.ts +1 -0
  24. package/dist/cjs/useIntlayerBase.cjs +19 -38
  25. package/dist/cjs/useIntlayerBase.cjs.map +1 -1
  26. package/dist/cjs/useIntlayerBase.d.ts +24 -13
  27. package/dist/esm/{chunk-ZNCO4QRF.mjs → chunk-ZD7AOCMD.mjs} +1 -1
  28. package/dist/esm/client/ContentEditionLayout.mjs +10 -9
  29. package/dist/esm/client/ContentEditionLayout.mjs.map +1 -1
  30. package/dist/esm/client/IntlayerProvider.d.mts +1 -0
  31. package/dist/esm/client/IntlayerProvider.mjs +5 -4
  32. package/dist/esm/client/IntlayerProvider.mjs.map +1 -1
  33. package/dist/esm/client/getBrowserLocale.mjs +8 -15
  34. package/dist/esm/client/getBrowserLocale.mjs.map +1 -1
  35. package/dist/esm/client/index.d.mts +1 -1
  36. package/dist/esm/client/index.mjs +1 -1
  37. package/dist/esm/client/useContent.d.mts +1 -1
  38. package/dist/esm/client/useContent.mjs +1 -1
  39. package/dist/esm/client/useIntlayer.d.mts +1 -0
  40. package/dist/esm/client/useIntlayer.mjs +1 -1
  41. package/dist/esm/client/useLocale.mjs +1 -1
  42. package/dist/esm/client/useLocaleBase.mjs +1 -1
  43. package/dist/esm/client/useLocaleCookie.mjs +1 -1
  44. package/dist/esm/client/useLocaleCookie.mjs.map +1 -1
  45. package/dist/esm/client/useTraduction.mjs +1 -1
  46. package/dist/esm/craco/craco.config.mjs +1 -1
  47. package/dist/esm/craco/intlayerCracoPlugin.mjs +1 -1
  48. package/dist/esm/getEnumeration.mjs +1 -1
  49. package/dist/esm/getTranslation.mjs +1 -1
  50. package/dist/esm/index.d.mts +1 -1
  51. package/dist/esm/index.mjs +1 -3
  52. package/dist/esm/index.mjs.map +1 -1
  53. package/dist/esm/processDictionary/index.mjs +3 -3
  54. package/dist/esm/processDictionary/index.mjs.map +1 -1
  55. package/dist/esm/renderIntlayerEditor.d.mts +5 -0
  56. package/dist/esm/renderIntlayerEditor.mjs +31 -0
  57. package/dist/esm/renderIntlayerEditor.mjs.map +1 -0
  58. package/dist/esm/server/IntlayerServerProvider.d.mts +5 -6
  59. package/dist/esm/server/IntlayerServerProvider.mjs +1 -1
  60. package/dist/esm/server/getLocaleTranslation.mjs +1 -1
  61. package/dist/esm/server/index.mjs +1 -1
  62. package/dist/esm/server/serverContext.mjs +2 -3
  63. package/dist/esm/server/serverContext.mjs.map +1 -1
  64. package/dist/esm/server/useIntlayer.d.mts +1 -0
  65. package/dist/esm/server/useIntlayer.mjs +1 -1
  66. package/dist/esm/server/useTraduction.mjs +1 -1
  67. package/dist/esm/useIntlayerBase.d.mts +24 -13
  68. package/dist/esm/useIntlayerBase.mjs +20 -38
  69. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  70. package/dist/esm/vite/intlayerPlugin.mjs +1 -1
  71. package/package.json +18 -18
  72. package/src/index.ts +1 -1
  73. /package/dist/esm/{chunk-ZNCO4QRF.mjs.map → chunk-ZD7AOCMD.mjs.map} +0 -0
@@ -1,4 +1,4 @@
1
- import "../chunk-ZNCO4QRF.mjs";
1
+ import "../chunk-ZD7AOCMD.mjs";
2
2
  import react from "react";
3
3
  const cacheFallback = () => () => ({ value: void 0 });
4
4
  const createServerContext = (defaultValue) => {
@@ -25,8 +25,7 @@ const getServerContext = ({
25
25
  _defaultValue
26
26
  }) => {
27
27
  const store = _storage();
28
- if (!store)
29
- return _defaultValue;
28
+ if (!store) return _defaultValue;
30
29
  return store.value;
31
30
  };
32
31
  const throwInClient = () => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/serverContext.ts"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer.Provider value={locale}>\n * {children}\n * </IntlayerServer.Provider>\n */\n\nimport react from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n return {\n Provider: ({ children, value }) => {\n getCache().value = value;\n return children;\n },\n Consumer: ({ children }) => {\n const store = getCache();\n return children(store ? store.value : defaultValue);\n },\n _storage: getCache,\n _defaultValue: defaultValue,\n };\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = {\n Provider: ({\n children,\n value,\n }: {\n children: React.ReactNode;\n value: T;\n }) => React.ReactNode;\n Consumer: ({\n children,\n }: {\n children: (context: T | undefined) => React.ReactNode;\n }) => React.ReactNode;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":";AAYA,OAAO,WAAW;AAIlB,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAI,iBAAuC;AAC5E,gBAAc;AAEd,QAAM,QAAQ,MAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,SAAO;AAAA,IACL,UAAU,CAAC,EAAE,UAAU,MAAM,MAAM;AACjC,eAAS,EAAE,QAAQ;AACnB,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,EAAE,SAAS,MAAM;AAC1B,YAAM,QAAQ,SAAS;AACvB,aAAO,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,IACpD;AAAA,IACA,UAAU;AAAA,IACV,eAAe;AAAA,EACjB;AACF;AASO,MAAM,mBAAmB,CAAI;AAAA,EAClC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC;AAAO,WAAO;AACnB,SAAO,MAAM;AACf;AAuBA,MAAM,gBAAgB,MAAoB;AAExC,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/serverContext.ts"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer.Provider value={locale}>\n * {children}\n * </IntlayerServer.Provider>\n */\n\nimport react from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n return {\n Provider: ({ children, value }) => {\n getCache().value = value;\n return children;\n },\n Consumer: ({ children }) => {\n const store = getCache();\n return children(store ? store.value : defaultValue);\n },\n _storage: getCache,\n _defaultValue: defaultValue,\n };\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = {\n Provider: ({\n children,\n value,\n }: {\n children: React.ReactNode;\n value: T;\n }) => React.ReactNode;\n Consumer: ({\n children,\n }: {\n children: (context: T | undefined) => React.ReactNode;\n }) => React.ReactNode;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":";AAYA,OAAO,WAAW;AAIlB,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAI,iBAAuC;AAC5E,gBAAc;AAEd,QAAM,QAAQ,MAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,SAAO;AAAA,IACL,UAAU,CAAC,EAAE,UAAU,MAAM,MAAM;AACjC,eAAS,EAAE,QAAQ;AACnB,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,EAAE,SAAS,MAAM;AAC1B,YAAM,QAAQ,SAAS;AACvB,aAAO,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,IACpD;AAAA,IACA,UAAU;AAAA,IACV,eAAe;AAAA,EACjB;AACF;AASO,MAAM,mBAAmB,CAAI;AAAA,EAClC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC,MAAO,QAAO;AACnB,SAAO,MAAM;AACf;AAuBA,MAAM,gBAAgB,MAAoB;AAExC,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACF;","names":[]}
@@ -1,5 +1,6 @@
1
1
  import { UseIntlayer } from '../useIntlayerBase.mjs';
2
2
  import '@intlayer/config';
3
+ import '@intlayer/core';
3
4
  import 'intlayer';
4
5
  import 'react';
5
6
 
@@ -1,4 +1,4 @@
1
- import "../chunk-ZNCO4QRF.mjs";
1
+ import "../chunk-ZD7AOCMD.mjs";
2
2
  import {
3
3
  useIntlayerBase
4
4
  } from '../useIntlayerBase.mjs';
@@ -1,4 +1,4 @@
1
- import "../chunk-ZNCO4QRF.mjs";
1
+ import "../chunk-ZD7AOCMD.mjs";
2
2
  import { getTranslation } from '../getTranslation.mjs';
3
3
  import { IntlayerServerContext } from './IntlayerServerProvider.mjs';
4
4
  import { getServerContext } from './serverContext.mjs';
@@ -1,4 +1,5 @@
1
1
  import { Locales } from '@intlayer/config';
2
+ import { NodeType } from '@intlayer/core';
2
3
  import { IntlayerDictionaryTypesConnector } from 'intlayer';
3
4
  import { ReactNode } from 'react';
4
5
 
@@ -20,12 +21,26 @@ type StringFallback<T> = T extends never ? string : T;
20
21
  * ```
21
22
  */
22
23
  type DictionaryKeys = StringFallback<keyof IntlayerDictionaryTypesConnector>;
23
- type IntlayerNode = ReactNode & {
24
- value: string;
24
+ type IntlayerNode<T = string> = ReactNode & {
25
+ value: T;
25
26
  };
26
- type DeepTransformContent<T> = T extends object ? {
27
- [K in keyof T]: DeepTransformContent<T[K]>;
28
- } : IntlayerNode;
27
+ type TransformNodeType<T, L extends Locales> = T extends {
28
+ [NodeType.Enumeration]: {
29
+ '1': any;
30
+ };
31
+ } ? (quantity: number) => DeepTransformContent<T[NodeType.Enumeration]['1'], L> : T extends {
32
+ [NodeType.Translation]: object;
33
+ } ? L extends keyof T[NodeType.Translation] ? DeepTransformContent<T[NodeType.Translation][L], L> : never : T;
34
+ type DeepTransformContent<T, L extends Locales> = T extends object ? T extends (infer U)[] ? DeepTransformContent<U, L>[] : T extends {
35
+ nodeType: NodeType | string;
36
+ } ? TransformNodeType<T, L> : T extends {
37
+ _owner: any;
38
+ key: any;
39
+ props: any;
40
+ ref: any;
41
+ } ? ReactNode : {
42
+ [K in keyof T]: DeepTransformContent<T[K], L>;
43
+ } : T extends undefined ? never : IntlayerNode<T>;
29
44
  /**
30
45
  * Excludes the 'id' and 'filePath' keys from the dictionary content,
31
46
  * as they are not part of the IntlayerDictionaryTypesConnector type.
@@ -35,25 +50,21 @@ type ExcludeIntlayerUtilsKeys<T> = Omit<T, 'id' | 'filePath'>;
35
50
  * Represents the data type returned by the useIntlayer hook,
36
51
  * excluding the 'id' and 'filePath' keys from the dictionary content.
37
52
  */
38
- type Data<T extends DictionaryKeys> = ExcludeIntlayerUtilsKeys<DeepTransformContent<IntlayerDictionaryTypesConnector[T]>>;
53
+ type Data<T extends DictionaryKeys, K extends Locales> = ExcludeIntlayerUtilsKeys<DeepTransformContent<IntlayerDictionaryTypesConnector[T], K>>;
39
54
  /**
40
55
  * Parcourt the object. If a object has a keyPath, render the intlayer editor if editor enabled.
41
56
  */
42
- declare const recursiveTransformContent: (obj: object) => object;
43
- type DeepStrinfifyContent<T> = T extends React.ComponentType<unknown> ? string : T extends object ? {
44
- [K in keyof T]: DeepStrinfifyContent<T[K]>;
45
- } : T;
46
- declare const recursiveStringifyContent: <T extends object>(obj: T) => DeepStrinfifyContent<T>;
57
+ declare const recursiveTransformContent: (value: any) => object;
47
58
  /**
48
59
  * Type definition for the useIntlayer hook, which takes a dictionary ID and an optional locale,
49
60
  * and returns the deeply transformed dictionary content.
50
61
  *
51
62
  */
52
- type UseIntlayer = <T extends DictionaryKeys>(id: T, locale?: Locales) => Data<T>;
63
+ type UseIntlayer = <T extends DictionaryKeys, L extends Locales>(id: T, locale?: L) => Data<T, L>;
53
64
  /**
54
65
  * Hook that picks one dictionary by its ID and returns the content,
55
66
  * deeply transformed according to the dictionary structure and metadata.
56
67
  */
57
68
  declare const useIntlayerBase: UseIntlayer;
58
69
 
59
- export { type DictionaryKeys, type IntlayerNode, type StringFallback, type UseIntlayer, recursiveStringifyContent, recursiveTransformContent, useIntlayerBase };
70
+ export { type DictionaryKeys, type IntlayerNode, type StringFallback, type UseIntlayer, recursiveTransformContent, useIntlayerBase };
@@ -1,45 +1,28 @@
1
- import "./chunk-ZNCO4QRF.mjs";
1
+ import "./chunk-ZD7AOCMD.mjs";
2
2
  import dictionaries from "@intlayer/dictionaries-entry";
3
- import { renderIntlayerEditor } from "intlayer-editor/client";
3
+ import { isValidElement } from "react";
4
4
  import { processDictionary } from './processDictionary/index.mjs';
5
- const recursiveTransformContent = (obj) => Object.entries(obj).reduce((acc, [key, value]) => {
6
- if (typeof value === "object" && typeof value.keyPath !== "undefined" && typeof value.dictionaryId !== "undefined" && typeof value.dictionaryPath !== "undefined") {
7
- return {
8
- ...acc,
9
- [key]: renderIntlayerEditor(value)
10
- };
5
+ import { renderIntlayerEditor } from './renderIntlayerEditor.mjs';
6
+ const recursiveTransformContent = (value) => {
7
+ if (typeof value === "function") {
8
+ return (props) => recursiveTransformContent(value(props));
9
+ } else if (typeof value === "object" && typeof value.keyPath !== "undefined" && typeof value.dictionaryId !== "undefined" && typeof value.dictionaryPath !== "undefined") {
10
+ return renderIntlayerEditor(value);
11
11
  } else if (typeof value === "object" && Array.isArray(value)) {
12
- return {
13
- ...acc,
14
- [key]: value.map(recursiveTransformContent)
15
- };
12
+ return value.map(recursiveTransformContent);
13
+ } else if (typeof value === "object" && isValidElement(value)) {
14
+ return value;
16
15
  } else if (typeof value === "object") {
17
- return {
18
- ...acc,
19
- [key]: recursiveTransformContent(value)
20
- };
16
+ return Object.entries(value).reduce(
17
+ (acc, [key, value2]) => ({
18
+ ...acc,
19
+ [key]: recursiveTransformContent(value2)
20
+ }),
21
+ {}
22
+ );
21
23
  }
22
- return acc;
23
- }, {});
24
- const recursiveStringifyContent = (obj) => Object.entries(obj).reduce((acc, [key, value]) => {
25
- if (typeof value === "object" && typeof value.value !== "undefined") {
26
- return {
27
- ...acc,
28
- [key]: value.value
29
- };
30
- } else if (typeof value === "object" && Array.isArray(value)) {
31
- return {
32
- ...acc,
33
- [key]: value.map(recursiveStringifyContent)
34
- };
35
- } else if (typeof value === "object") {
36
- return {
37
- ...acc,
38
- [key]: recursiveStringifyContent(value)
39
- };
40
- }
41
- return acc;
42
- }, {});
24
+ return value.value;
25
+ };
43
26
  const useIntlayerBase = (id, locale) => {
44
27
  const dictionary = dictionaries[id];
45
28
  const result = processDictionary(
@@ -52,7 +35,6 @@ const useIntlayerBase = (id, locale) => {
52
35
  return recursiveTransformContent(result);
53
36
  };
54
37
  export {
55
- recursiveStringifyContent,
56
38
  recursiveTransformContent,
57
39
  useIntlayerBase
58
40
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config';\n/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport type { Dictionary } from '@intlayer/core';\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport type { IntlayerDictionaryTypesConnector } from 'intlayer';\nimport { renderIntlayerEditor } from 'intlayer-editor/client';\nimport type { ReactNode } from 'react';\nimport { processDictionary } from './processDictionary/index';\n\n/**\n * Provides a fallback to string type if the generic type T is never,\n * otherwise returns T. This is useful for handling cases where no keys are found.\n * Example: StringFallback<never> -> string; StringFallback<'key'> -> 'key'\n */\nexport type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key\n\n/**\n * Represents the keys of the IntlayerDictionaryTypesConnector,\n * ensuring they are valid dictionary keys or fallback to string if none exist.\n *\n * Example:\n * ```ts\n * DictionaryKeys -> 'key1' | 'key2'\n * // or if IntlayerDictionaryTypesConnector is not defined,\n * DictionaryKeys -> string\n * ```\n */\nexport type DictionaryKeys = StringFallback<\n keyof IntlayerDictionaryTypesConnector\n>;\n\nexport type IntlayerNode = ReactNode & {\n value: string;\n};\n\ntype DeepTransformContent<T> = T extends object\n ? {\n [K in keyof T]: DeepTransformContent<T[K]>;\n }\n : IntlayerNode;\n\n/**\n * Excludes the 'id' and 'filePath' keys from the dictionary content,\n * as they are not part of the IntlayerDictionaryTypesConnector type.\n */\ntype ExcludeIntlayerUtilsKeys<T> = Omit<T, 'id' | 'filePath'>;\n\n/**\n * Represents the data type returned by the useIntlayer hook,\n * excluding the 'id' and 'filePath' keys from the dictionary content.\n */\ntype Data<T extends DictionaryKeys> = ExcludeIntlayerUtilsKeys<\n DeepTransformContent<IntlayerDictionaryTypesConnector[T]>\n>;\n\n/**\n * Parcourt the object. If a object has a keyPath, render the intlayer editor if editor enabled.\n */\nexport const recursiveTransformContent = (obj: object): object =>\n Object.entries(obj).reduce((acc, [key, value]) => {\n if (\n typeof value === 'object' &&\n typeof value.keyPath !== 'undefined' &&\n typeof value.dictionaryId !== 'undefined' &&\n typeof value.dictionaryPath !== 'undefined'\n ) {\n return {\n ...acc,\n [key]: renderIntlayerEditor(value),\n };\n } else if (typeof value === 'object' && Array.isArray(value)) {\n return {\n ...acc,\n [key]: value.map(recursiveTransformContent),\n };\n } else if (typeof value === 'object') {\n return {\n ...acc,\n [key]: recursiveTransformContent(value),\n };\n }\n\n return acc;\n }, {});\n\ntype DeepStrinfifyContent<T> =\n T extends React.ComponentType<unknown>\n ? string\n : T extends object\n ? {\n [K in keyof T]: DeepStrinfifyContent<T[K]>;\n }\n : T;\n\nexport const recursiveStringifyContent = <T extends object>(\n obj: T\n): DeepStrinfifyContent<T> =>\n Object.entries(obj).reduce((acc, [key, value]) => {\n if (typeof value === 'object' && typeof value.value !== 'undefined') {\n return {\n ...acc,\n [key]: value.value,\n };\n } else if (typeof value === 'object' && Array.isArray(value)) {\n return {\n ...acc,\n [key]: value.map(recursiveStringifyContent),\n };\n } else if (typeof value === 'object') {\n return {\n ...acc,\n [key]: recursiveStringifyContent(value),\n };\n }\n\n return acc;\n }, {}) as DeepStrinfifyContent<T>;\n\n/**\n * Type definition for the useIntlayer hook, which takes a dictionary ID and an optional locale,\n * and returns the deeply transformed dictionary content.\n *\n */\nexport type UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => Data<T>;\n\n/**\n * Hook that picks one dictionary by its ID and returns the content,\n * deeply transformed according to the dictionary structure and metadata.\n */\nexport const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const dictionary: Dictionary = dictionaries[id];\n\n const result = processDictionary(\n dictionary,\n id,\n dictionary.filePath,\n [],\n locale\n ) as object;\n\n return recursiveTransformContent(result) as Data<T>;\n};\n"],"mappings":";AAOA,OAAO,kBAAkB;AAEzB,SAAS,4BAA4B;AAErC,SAAS,yBAAyB;AAmD3B,MAAM,4BAA4B,CAAC,QACxC,OAAO,QAAQ,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM;AAChD,MACE,OAAO,UAAU,YACjB,OAAO,MAAM,YAAY,eACzB,OAAO,MAAM,iBAAiB,eAC9B,OAAO,MAAM,mBAAmB,aAChC;AACA,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,qBAAqB,KAAK;AAAA,IACnC;AAAA,EACF,WAAW,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;AAC5D,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,MAAM,IAAI,yBAAyB;AAAA,IAC5C;AAAA,EACF,WAAW,OAAO,UAAU,UAAU;AACpC,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,0BAA0B,KAAK;AAAA,IACxC;AAAA,EACF;AAEA,SAAO;AACT,GAAG,CAAC,CAAC;AAWA,MAAM,4BAA4B,CACvC,QAEA,OAAO,QAAQ,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM;AAChD,MAAI,OAAO,UAAU,YAAY,OAAO,MAAM,UAAU,aAAa;AACnE,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,MAAM;AAAA,IACf;AAAA,EACF,WAAW,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;AAC5D,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,MAAM,IAAI,yBAAyB;AAAA,IAC5C;AAAA,EACF,WAAW,OAAO,UAAU,UAAU;AACpC,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,0BAA0B,KAAK;AAAA,IACxC;AAAA,EACF;AAEA,SAAO;AACT,GAAG,CAAC,CAAC;AAgBA,MAAM,kBAA+B,CAC1C,IACA,WACG;AACH,QAAM,aAAyB,aAAa,EAAE;AAE9C,QAAM,SAAS;AAAA,IACb;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,CAAC;AAAA,IACD;AAAA,EACF;AAEA,SAAO,0BAA0B,MAAM;AACzC;","names":[]}
1
+ {"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { Locales } from '@intlayer/config';\n/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport type { Dictionary, NodeType } from '@intlayer/core';\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport type { IntlayerDictionaryTypesConnector } from 'intlayer';\nimport { isValidElement, type ReactNode } from 'react';\nimport { processDictionary } from './processDictionary/index';\nimport { renderIntlayerEditor } from './renderIntlayerEditor';\n\n/**\n * Provides a fallback to string type if the generic type T is never,\n * otherwise returns T. This is useful for handling cases where no keys are found.\n * Example: StringFallback<never> -> string; StringFallback<'key'> -> 'key'\n */\nexport type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key\n\n/**\n * Represents the keys of the IntlayerDictionaryTypesConnector,\n * ensuring they are valid dictionary keys or fallback to string if none exist.\n *\n * Example:\n * ```ts\n * DictionaryKeys -> 'key1' | 'key2'\n * // or if IntlayerDictionaryTypesConnector is not defined,\n * DictionaryKeys -> string\n * ```\n */\nexport type DictionaryKeys = StringFallback<\n keyof IntlayerDictionaryTypesConnector\n>;\n\nexport type IntlayerNode<T = string> = ReactNode & {\n value: T;\n};\n\ntype TransformNodeType<T, L extends Locales> = T extends {\n [NodeType.Enumeration]: { '1': any };\n}\n ? (quantity: number) => DeepTransformContent<T[NodeType.Enumeration]['1'], L>\n : T extends {\n [NodeType.Translation]: object;\n }\n ? L extends keyof T[NodeType.Translation]\n ? DeepTransformContent<T[NodeType.Translation][L], L> // DeepTransformContent<T[L], L>\n : never\n : T;\n\ntype DeepTransformContent<T, L extends Locales> = T extends object // Check if the property is an object\n ? T extends (infer U)[] // If it's an array, infer the type of array elements\n ? DeepTransformContent<U, L>[] // Apply DeepTransformContent recursively to each element of the array\n : T extends {\n nodeType: NodeType | string;\n }\n ? TransformNodeType<T, L>\n : T extends { _owner: any; key: any; props: any; ref: any }\n ? ReactNode\n : {\n [K in keyof T]: DeepTransformContent<T[K], L>;\n }\n : T extends undefined\n ? never\n : IntlayerNode<T>;\n\n/**\n * Excludes the 'id' and 'filePath' keys from the dictionary content,\n * as they are not part of the IntlayerDictionaryTypesConnector type.\n */\ntype ExcludeIntlayerUtilsKeys<T> = Omit<T, 'id' | 'filePath'>;\n\n/**\n * Represents the data type returned by the useIntlayer hook,\n * excluding the 'id' and 'filePath' keys from the dictionary content.\n */\ntype Data<\n T extends DictionaryKeys,\n K extends Locales,\n> = ExcludeIntlayerUtilsKeys<\n DeepTransformContent<IntlayerDictionaryTypesConnector[T], K>\n>;\n\n/**\n * Parcourt the object. If a object has a keyPath, render the intlayer editor if editor enabled.\n */\nexport const recursiveTransformContent = (value: any): object => {\n if (typeof value === 'function') {\n return (props: any) => recursiveTransformContent(value(props));\n } else if (\n typeof value === 'object' &&\n typeof value.keyPath !== 'undefined' &&\n typeof value.dictionaryId !== 'undefined' &&\n typeof value.dictionaryPath !== 'undefined'\n ) {\n return renderIntlayerEditor(value);\n } else if (typeof value === 'object' && Array.isArray(value)) {\n return value.map(recursiveTransformContent);\n } else if (typeof value === 'object' && isValidElement(value)) {\n return value;\n } else if (typeof value === 'object') {\n return Object.entries(value).reduce(\n (acc, [key, value]) => ({\n ...acc,\n [key]: recursiveTransformContent(value),\n }),\n {} as object\n );\n }\n\n return value.value;\n};\n\n/**\n * Type definition for the useIntlayer hook, which takes a dictionary ID and an optional locale,\n * and returns the deeply transformed dictionary content.\n *\n */\nexport type UseIntlayer = <T extends DictionaryKeys, L extends Locales>(\n id: T,\n locale?: L\n) => Data<T, L>;\n\n/**\n * Hook that picks one dictionary by its ID and returns the content,\n * deeply transformed according to the dictionary structure and metadata.\n */\nexport const useIntlayerBase: UseIntlayer = <\n T extends DictionaryKeys,\n L extends Locales,\n>(\n id: T,\n locale?: L\n) => {\n const dictionary: Dictionary = dictionaries[id as keyof typeof dictionaries];\n\n const result = processDictionary(\n dictionary,\n id,\n dictionary.filePath,\n [],\n locale\n ) as object;\n\n return recursiveTransformContent(result) as Data<T, L>;\n};\n"],"mappings":";AAQA,OAAO,kBAAkB;AAEzB,SAAS,sBAAsC;AAC/C,SAAS,yBAAyB;AAClC,SAAS,4BAA4B;AA4E9B,MAAM,4BAA4B,CAAC,UAAuB;AAC/D,MAAI,OAAO,UAAU,YAAY;AAC/B,WAAO,CAAC,UAAe,0BAA0B,MAAM,KAAK,CAAC;AAAA,EAC/D,WACE,OAAO,UAAU,YACjB,OAAO,MAAM,YAAY,eACzB,OAAO,MAAM,iBAAiB,eAC9B,OAAO,MAAM,mBAAmB,aAChC;AACA,WAAO,qBAAqB,KAAK;AAAA,EACnC,WAAW,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;AAC5D,WAAO,MAAM,IAAI,yBAAyB;AAAA,EAC5C,WAAW,OAAO,UAAU,YAAY,eAAe,KAAK,GAAG;AAC7D,WAAO;AAAA,EACT,WAAW,OAAO,UAAU,UAAU;AACpC,WAAO,OAAO,QAAQ,KAAK,EAAE;AAAA,MAC3B,CAAC,KAAK,CAAC,KAAKA,MAAK,OAAO;AAAA,QACtB,GAAG;AAAA,QACH,CAAC,GAAG,GAAG,0BAA0BA,MAAK;AAAA,MACxC;AAAA,MACA,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO,MAAM;AACf;AAgBO,MAAM,kBAA+B,CAI1C,IACA,WACG;AACH,QAAM,aAAyB,aAAa,EAA+B;AAE3E,QAAM,SAAS;AAAA,IACb;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,CAAC;AAAA,IACD;AAAA,EACF;AAEA,SAAO,0BAA0B,MAAM;AACzC;","names":["value"]}
@@ -1,4 +1,4 @@
1
- import "../chunk-ZNCO4QRF.mjs";
1
+ import "../chunk-ZD7AOCMD.mjs";
2
2
  import { join, relative, resolve } from "path";
3
3
  import { watch } from "@intlayer/chokidar";
4
4
  import { getConfiguration, formatEnvVariable } from "@intlayer/config";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-intlayer",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "private": false,
5
5
  "description": "Internationalization layer for React applications - Layer of abstraction between the business logic and the data access layer. Manage internationalization in a simple way, through TypeScript, JavaScript or JSON declaration file.",
6
6
  "keywords": [
@@ -73,33 +73,33 @@
73
73
  "cross-spawn": "^7.0.3",
74
74
  "js-cookie": "^3.0.5",
75
75
  "react-cookie": "^7.1.4",
76
- "vite": "^5.2.0",
77
- "webpack": "^5.91.0",
78
- "@intlayer/chokidar": "^2.0.1",
79
- "@intlayer/config": "^2.0.1",
80
- "@intlayer/core": "^2.0.1",
81
- "@intlayer/dictionaries-entry": "^2.0.1",
82
- "@intlayer/webpack": "^2.0.1",
83
- "intlayer": "^2.0.1"
76
+ "vite": "^5.3.1",
77
+ "webpack": "^5.92.1",
78
+ "@intlayer/chokidar": "^2.0.3",
79
+ "@intlayer/config": "^2.0.3",
80
+ "@intlayer/core": "^2.0.3",
81
+ "@intlayer/dictionaries-entry": "^2.0.3",
82
+ "@intlayer/webpack": "^2.0.3",
83
+ "intlayer": "^2.0.3"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@craco/types": "^7.1.0",
87
87
  "@types/cross-spawn": "^6.0.6",
88
88
  "@types/js-cookie": "^3.0.6",
89
- "@types/node": "^20.12.7",
90
- "@types/react": "^18.2.79",
91
- "react": "^18.2.0",
92
- "react-dom": "^18.2.0",
89
+ "@types/node": "^20.14.9",
90
+ "@types/react": "^18.3.3",
91
+ "react": "^18.3.1",
92
+ "react-dom": "^18.3.1",
93
93
  "rimraf": "5.0.5",
94
- "tsup": "^8.0.2",
95
- "typescript": "^5.4.5",
96
- "@utils/eslint-config": "^1.0.1",
97
- "@utils/ts-config": "^1.0.1"
94
+ "tsup": "^8.1.0",
95
+ "typescript": "^5.5.2",
96
+ "@utils/eslint-config": "^1.0.3",
97
+ "@utils/ts-config": "^1.0.3"
98
98
  },
99
99
  "peerDependencies": {
100
100
  "react": "^18.2.0",
101
101
  "react-dom": "^18.2.0",
102
- "intlayer-editor": "^2.1.0"
102
+ "intlayer-editor": "^2.1.2"
103
103
  },
104
104
  "peerDependenciesMeta": {
105
105
  "intlayer-editor": {
package/src/index.ts CHANGED
@@ -12,4 +12,4 @@ export {
12
12
  getBrowserLocale,
13
13
  useLocaleBase,
14
14
  } from './client/index';
15
- export { recursiveStringifyContent } from './useIntlayerBase';
15
+ export type { IntlayerNode } from './useIntlayerBase';