@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 3.98 kB
Source Map (JSON)
{"version":3,"file":"hydrate-admin-copy-defaults.mjs","names":[],"sources":["../../../src/libs/i18n/hydrate-admin-copy-defaults.ts"],"sourcesContent":["import type { AdminCopyDescriptor } from \"./types.js\";\n\ntype AdminTranslationBundle = Record<string, string>;\n\n/**\n * Only hydrate response-shaped objects so builders, classes, and other runtime\n * instances keep their identity.\n */\nconst isPlainObject = (value: unknown): value is Record<string, unknown> => {\n\tif (value === null || typeof value !== \"object\") return false;\n\n\tconst prototype = Object.getPrototypeOf(value);\n\treturn prototype === Object.prototype || prototype === null;\n};\n\n/**\n * Detects admin copy descriptors by their transport shape before adding\n * fallback copy.\n */\nconst isAdminCopyDescriptor = (\n\tvalue: Record<string, unknown>,\n): value is AdminCopyDescriptor => {\n\treturn (\n\t\tvalue.type === \"lucid.copy\" &&\n\t\tvalue.scope === \"admin\" &&\n\t\ttypeof value.key === \"string\"\n\t);\n};\n\n/**\n * Recursively clones only the branches that need hydrated admin copy.\n */\nconst hydrateValue = (\n\tvalue: unknown,\n\tadminTranslations: AdminTranslationBundle,\n): unknown => {\n\tif (Array.isArray(value)) {\n\t\tlet changed = false;\n\t\tconst hydratedItems = value.map((item) => {\n\t\t\tconst hydratedItem = hydrateValue(item, adminTranslations);\n\t\t\tif (hydratedItem !== item) changed = true;\n\t\t\treturn hydratedItem;\n\t\t});\n\n\t\treturn changed ? hydratedItems : value;\n\t}\n\n\tif (!isPlainObject(value)) return value;\n\n\tif (isAdminCopyDescriptor(value)) {\n\t\tif (value.defaultMessage !== undefined) return value;\n\t\tif (!Object.hasOwn(adminTranslations, value.key)) return value;\n\n\t\tconst defaultMessage = adminTranslations[value.key];\n\t\tif (defaultMessage === undefined) return value;\n\n\t\treturn {\n\t\t\t...value,\n\t\t\tdefaultMessage,\n\t\t};\n\t}\n\n\tlet changed = false;\n\tconst hydratedEntries = Object.entries(value).map(([key, entryValue]) => {\n\t\tconst hydratedEntryValue = hydrateValue(entryValue, adminTranslations);\n\t\tif (hydratedEntryValue !== entryValue) changed = true;\n\t\treturn [key, hydratedEntryValue] as const;\n\t});\n\n\treturn changed ? Object.fromEntries(hydratedEntries) : value;\n};\n\n/**\n * Adds `defaultMessage` values to admin copy descriptors from an admin\n * translation bundle.\n *\n * Use this before returning descriptors across API or plugin boundaries so\n * consumers have stable fallback text while their own translations load. It\n * preserves explicit defaults and leaves missing keys untouched.\n *\n * @example\n * ```ts\n * import { copy, hydrateAdminCopyDefaults } from \"@lucidcms/core/plugin\";\n *\n * const details = hydrateAdminCopyDefaults(\n * { name: copy(\"admin:collections.blog.name\") },\n * { \"collections.blog.name\": \"Blog\" },\n * );\n * ```\n */\nexport const hydrateAdminCopyDefaults = <T>(\n\tvalue: T,\n\tadminTranslations?: AdminTranslationBundle,\n): T => {\n\tif (!adminTranslations) return value;\n\n\treturn hydrateValue(value, adminTranslations) as T;\n};\n"],"mappings":"AAQA,MAAM,EAAiB,GAAqD,CAC3E,GAAsB,OAAO,GAAU,WAAnC,EAA6C,MAAO,GAExD,IAAM,EAAY,OAAO,eAAe,CAAK,EAC7C,OAAO,IAAc,OAAO,WAAa,IAAc,IACxD,EAMM,EACL,GAGC,EAAM,OAAS,cACf,EAAM,QAAU,SAChB,OAAO,EAAM,KAAQ,SAOjB,GACL,EACA,IACa,CACb,GAAI,MAAM,QAAQ,CAAK,EAAG,CACzB,IAAI,EAAU,GACR,EAAgB,EAAM,IAAK,GAAS,CACzC,IAAM,EAAe,EAAa,EAAM,CAAiB,EAEzD,OADI,IAAiB,IAAM,EAAU,IAC9B,CACR,CAAC,EAED,OAAO,EAAU,EAAgB,CAClC,CAEA,GAAI,CAAC,EAAc,CAAK,EAAG,OAAO,EAElC,GAAI,EAAsB,CAAK,EAAG,CAEjC,GADI,EAAM,iBAAmB,IAAA,IACzB,CAAC,OAAO,OAAO,EAAmB,EAAM,GAAG,EAAG,OAAO,EAEzD,IAAM,EAAiB,EAAkB,EAAM,KAG/C,OAFI,IAAmB,IAAA,GAAkB,EAElC,CACN,GAAG,EACH,gBACD,CACD,CAEA,IAAI,EAAU,GACR,EAAkB,OAAO,QAAQ,CAAK,CAAC,CAAC,KAAK,CAAC,EAAK,KAAgB,CACxE,IAAM,EAAqB,EAAa,EAAY,CAAiB,EAErE,OADI,IAAuB,IAAY,EAAU,IAC1C,CAAC,EAAK,CAAkB,CAChC,CAAC,EAED,OAAO,EAAU,OAAO,YAAY,CAAe,EAAI,CACxD,EAoBa,GACZ,EACA,IAEK,EAEE,EAAa,EAAO,CAAiB,EAFb"}