diff --git a/src/index.ts b/src/index.ts index 81ebb0ce..ac42a304 100644 --- a/src/index.ts +++ b/src/index.ts @@ -207,6 +207,7 @@ export type { export type { LinkToMediaField, FilledLinkToMediaField, + SingleLinkToMediaField, } from "./types/value/linkToMedia" export type { diff --git a/src/lib/resolveMigrationDocumentData.ts b/src/lib/resolveMigrationDocumentData.ts index dded02c9..67983bc1 100644 --- a/src/lib/resolveMigrationDocumentData.ts +++ b/src/lib/resolveMigrationDocumentData.ts @@ -11,7 +11,7 @@ import type { } from "../types/migration/ContentRelationship" import { PrismicMigrationDocument } from "../types/migration/Document" import type { FilledImageFieldImage } from "../types/value/image" -import type { LinkField } from "../types/value/link" +import type { SingleLinkField } from "../types/value/link" import { LinkType } from "../types/value/link" import type { RTImageNode } from "../types/value/richText" import { RichTextNodeType } from "../types/value/richText" @@ -78,7 +78,14 @@ export const resolveMigrationImage = ( image instanceof PrismicMigrationAsset ? { id: image } : image const asset = migration._assets.get(master.config.id)?.asset - const maybeInitialField = master.originalField + let maybeInitialField + if (Array.isArray(master.originalField)) { + maybeInitialField = isFilled.link(master.originalField[0]) + ? master.originalField[0] + : undefined + } else { + maybeInitialField = master.originalField + } if (asset) { const parameters = (maybeInitialField?.url || asset.url).split("?")[1] @@ -135,7 +142,7 @@ export const resolveMigrationRTImageNode = async ( const linkTo = (await resolveMigrationDocumentData( rtImageNode.linkTo, migration, - )) as LinkField + )) as SingleLinkField return { ...image, diff --git a/src/types/migration/Asset.ts b/src/types/migration/Asset.ts index 386eb0ce..02808a04 100644 --- a/src/types/migration/Asset.ts +++ b/src/types/migration/Asset.ts @@ -1,7 +1,10 @@ import type { Asset } from "../api/asset/asset" import type { FilledImageFieldImage } from "../value/image" import type { EmptyLinkField } from "../value/link" -import type { LinkToMediaField } from "../value/linkToMedia" +import type { + LinkToMediaField, + SingleLinkToMediaField, +} from "../value/linkToMedia" import { type RTImageNode } from "../value/richText" import type { InjectMigrationSpecificTypes } from "./Document" @@ -72,10 +75,10 @@ export type MigrationImage = * A link to media field in a migration. */ export type MigrationLinkToMedia = Pick< - LinkToMediaField<"filled">, + SingleLinkToMediaField<"filled">, "link_type" > & - Partial, "text">> & { + Partial, "text">> & { /** * A reference to the migration asset used to resolve the link to media * field's value. @@ -88,7 +91,7 @@ export type MigrationLinkToMedia = Pick< * with the migration API. */ export type MigrationLinkToMediaField = - | Pick, "link_type" | "id" | "text"> + | Pick, "link_type" | "id" | "text"> | EmptyLinkField<"Media"> /** diff --git a/test/writeClient-migrate-patch-linkToMedia.test.ts b/test/writeClient-migrate-patch-linkToMedia.test.ts index 341cc23f..5c85e404 100644 --- a/test/writeClient-migrate-patch-linkToMedia.test.ts +++ b/test/writeClient-migrate-patch-linkToMedia.test.ts @@ -4,6 +4,7 @@ import type { InjectMigrationSpecificTypes, LinkToMediaField, RichTextField, + SingleLinkToMediaField, } from "../src" import { LinkType, RichTextNodeType } from "../src" import type { Asset } from "../src/types/api/asset/asset" @@ -12,7 +13,7 @@ import type { MigrationLinkToMedia } from "../src/types/migration/Asset" const assetToLinkToMedia = ( asset: Asset, text?: string, -): LinkToMediaField<"filled"> => { +): SingleLinkToMediaField<"filled"> => { return { id: asset.id, link_type: LinkType.Media,