@prismicio/client
Version:
The official JavaScript + TypeScript client library for Prismic
1 lines • 10.6 kB
Source Map (JSON)
{"version":3,"file":"resolveMigrationDocumentData.cjs","sources":["../../../src/lib/resolveMigrationDocumentData.ts"],"sourcesContent":["import type { MigrationLinkToMediaField } from \"../types/migration/Asset\"\nimport {\n\ttype MigrationImage,\n\ttype MigrationLinkToMedia,\n\ttype MigrationRTImageNode,\n\tPrismicMigrationAsset,\n} from \"../types/migration/Asset\"\nimport type {\n\tMigrationContentRelationship,\n\tMigrationContentRelationshipField,\n} from \"../types/migration/ContentRelationship\"\nimport { PrismicMigrationDocument } from \"../types/migration/Document\"\nimport type { MaybeLink } from \"../types/migration/Link\"\nimport type { FilledImageFieldImage } from \"../types/value/image\"\nimport type { LinkField, OptionalLinkProperties } from \"../types/value/link\"\nimport { LinkType } from \"../types/value/link\"\nimport type { RTImageNode } from \"../types/value/richText\"\nimport { RichTextNodeType } from \"../types/value/richText\"\n\nimport * as isFilled from \"../helpers/isFilled\"\nimport type { Migration } from \"../Migration\"\n\nimport * as isMigration from \"./isMigrationValue\"\nimport { getOptionalLinkProperties } from \"./getOptionalLinkProperties\"\n\n/**\n * Resolves a migration content relationship to a content relationship field.\n *\n * @param relation - Content relationship to resolve.\n *\n * @returns Resolved content relationship field.\n */\nexport async function resolveMigrationContentRelationship(\n\trelation: MaybeLink<MigrationContentRelationship>,\n): Promise<MigrationContentRelationshipField & OptionalLinkProperties> {\n\tif (typeof relation === \"function\") {\n\t\treturn resolveMigrationContentRelationship(await relation())\n\t}\n\n\tif (relation instanceof PrismicMigrationDocument) {\n\t\treturn relation.document.id\n\t\t\t? { link_type: LinkType.Document, id: relation.document.id }\n\t\t\t: { link_type: LinkType.Any }\n\t}\n\n\tconst optionalLinkProperties =\n\t\trelation && \"link_type\" in relation\n\t\t\t? getOptionalLinkProperties(relation)\n\t\t\t: undefined\n\n\tif (relation) {\n\t\tif (\n\t\t\tisMigration.contentRelationship(relation.id) ||\n\t\t\ttypeof relation.id !== \"string\"\n\t\t) {\n\t\t\treturn {\n\t\t\t\t...optionalLinkProperties,\n\t\t\t\t...(await resolveMigrationContentRelationship(relation.id)),\n\t\t\t}\n\t\t}\n\n\t\t// This is only called when resolveMigrationContentRelationship recursively\n\t\t// calls itself from the statement above and the resolved content relation\n\t\t// is a Prismic document value.\n\t\treturn {\n\t\t\t...optionalLinkProperties,\n\t\t\tlink_type: LinkType.Document,\n\t\t\tid: relation.id,\n\t\t}\n\t}\n\n\treturn {\n\t\t...optionalLinkProperties,\n\t\tlink_type: LinkType.Any,\n\t}\n}\n\n/**\n * Resolves a migration image to an image field.\n *\n * @param migrationAsset - Asset to resolve.\n * @param migration - Migration instance.\n * @param withThumbnails - Whether to include thumbnails.\n *\n * @returns Resolved image field.\n */\nexport const resolveMigrationImage = (\n\timage: MigrationImage,\n\tmigration: Migration,\n\twithThumbnails?: boolean,\n): FilledImageFieldImage | undefined => {\n\tconst { id: master, ...thumbnails } =\n\t\timage instanceof PrismicMigrationAsset ? { id: image } : image\n\n\tconst asset = migration._assets.get(master.config.id)?.asset\n\tconst maybeInitialField = master.originalField\n\n\tif (asset) {\n\t\tconst parameters = (maybeInitialField?.url || asset.url).split(\"?\")[1]\n\t\tconst url = `${asset.url.split(\"?\")[0]}${parameters ? `?${parameters}` : \"\"}`\n\t\tconst dimensions: FilledImageFieldImage[\"dimensions\"] = {\n\t\t\twidth: asset.width!,\n\t\t\theight: asset.height!,\n\t\t}\n\t\tconst edit: FilledImageFieldImage[\"edit\"] =\n\t\t\tmaybeInitialField && \"edit\" in maybeInitialField\n\t\t\t\t? maybeInitialField?.edit\n\t\t\t\t: { x: 0, y: 0, zoom: 1, background: \"transparent\" }\n\n\t\t// We give priority to the asset's specific alt text, then the image's general alt text\n\t\tconst alt = master.config.alt || asset.alt || null\n\n\t\tconst resolvedThumbnails: Record<string, FilledImageFieldImage> = {}\n\t\tif (withThumbnails) {\n\t\t\tfor (const [name, thumbnail] of Object.entries(thumbnails)) {\n\t\t\t\tconst resolvedThumbnail = resolveMigrationImage(thumbnail, migration)\n\t\t\t\tif (resolvedThumbnail) {\n\t\t\t\t\tresolvedThumbnails[name] = resolvedThumbnail\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tid: asset.id,\n\t\t\turl,\n\t\t\tdimensions,\n\t\t\tedit,\n\t\t\talt: alt,\n\t\t\tcopyright: asset.credits || null,\n\t\t\t...resolvedThumbnails,\n\t\t}\n\t}\n}\n\n/**\n * Resolves a migration rich text image node to a regular rich text image node.\n *\n * @param rtImageNode - Migration rich text image node to resolve.\n * @param migration - Migration instance.\n *\n * @returns Resolved rich text image node.\n */\nexport const resolveMigrationRTImageNode = async (\n\trtImageNode: MigrationRTImageNode,\n\tmigration: Migration,\n): Promise<RTImageNode | undefined> => {\n\tconst image = resolveMigrationImage(rtImageNode.id, migration)\n\n\tif (image) {\n\t\tconst linkTo = (await resolveMigrationDocumentData(\n\t\t\trtImageNode.linkTo,\n\t\t\tmigration,\n\t\t)) as LinkField\n\n\t\treturn {\n\t\t\t...image,\n\t\t\ttype: RichTextNodeType.image,\n\t\t\tlinkTo: isFilled.link(linkTo) ? linkTo : undefined,\n\t\t}\n\t}\n}\n\n/**\n * Resolves a migration link to media to a regular link to media field.\n *\n * @param linkToMedia - Migration link to media to resolve.\n * @param migration - Migration instance.\n *\n * @returns Resolved link to media field.\n */\nexport const resolveMigrationLinkToMedia = (\n\tlinkToMedia: MaybeLink<MigrationLinkToMedia>,\n\tmigration: Migration,\n): MigrationLinkToMediaField => {\n\tconst asset = migration._assets.get(linkToMedia.id.config.id)?.asset\n\tconst optionalLinkProperties = getOptionalLinkProperties(linkToMedia)\n\n\tif (asset) {\n\t\treturn {\n\t\t\t...optionalLinkProperties,\n\t\t\tid: asset.id,\n\t\t\tlink_type: LinkType.Media,\n\t\t}\n\t}\n\n\treturn {\n\t\t...optionalLinkProperties,\n\t\tlink_type: LinkType.Any,\n\t}\n}\n\n/**\n * Resolves a migration document data to actual data ready to be sent to the\n * Migration API.\n *\n * @param input - Migration link to media to resolve.\n * @param migration - Migration instance.\n *\n * @returns Resolved data.\n */\nexport async function resolveMigrationDocumentData(\n\tinput: unknown,\n\tmigration: Migration,\n): Promise<unknown> {\n\t// Migration fields\n\tif (isMigration.contentRelationship(input)) {\n\t\treturn resolveMigrationContentRelationship(input)\n\t}\n\n\tif (isMigration.image(input)) {\n\t\treturn resolveMigrationImage(input, migration, true)\n\t}\n\n\tif (isMigration.linkToMedia(input)) {\n\t\treturn resolveMigrationLinkToMedia(input, migration)\n\t}\n\n\tif (isMigration.rtImageNode(input)) {\n\t\treturn resolveMigrationRTImageNode(input, migration)\n\t}\n\n\tif (typeof input === \"function\") {\n\t\treturn await resolveMigrationDocumentData(await input(), migration)\n\t}\n\n\t// Object traversing\n\tif (Array.isArray(input)) {\n\t\tconst res = []\n\n\t\tfor (const element of input) {\n\t\t\tres.push(await resolveMigrationDocumentData(element, migration))\n\t\t}\n\n\t\treturn res.filter(Boolean)\n\t}\n\n\tif (input && typeof input === \"object\") {\n\t\tconst res: Record<PropertyKey, unknown> = {}\n\n\t\tfor (const key in input) {\n\t\t\tres[key] = await resolveMigrationDocumentData(\n\t\t\t\tinput[key as keyof typeof input],\n\t\t\t\tmigration,\n\t\t\t)\n\t\t}\n\n\t\treturn res\n\t}\n\n\t// Primitives\n\treturn input\n}\n"],"names":["PrismicMigrationDocument","LinkType","getOptionalLinkProperties","isMigration.contentRelationship","PrismicMigrationAsset","RichTextNodeType","isFilled.link","isMigration.image","isMigration.linkToMedia","isMigration.rtImageNode"],"mappings":";;;;;;;;;AAgCA,eAAsB,oCACrB,UAAiD;AAE7C,MAAA,OAAO,aAAa,YAAY;AAC5B,WAAA,oCAAoC,MAAM,UAAU;AAAA,EAAA;AAG5D,MAAI,oBAAoBA,SAAAA,0BAA0B;AACjD,WAAO,SAAS,SAAS,KACtB,EAAE,WAAWC,KAAS,SAAA,UAAU,IAAI,SAAS,SAAS,GAAI,IAC1D,EAA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