From 45dd0019c146aae1450e2f79d04ade57055859ca Mon Sep 17 00:00:00 2001 From: lihbr Date: Wed, 11 Sep 2024 22:52:47 +0200 Subject: [PATCH] refactor: use classes to detect migration field --- src/Client.ts | 2 +- src/Migration.ts | 213 +-- src/WriteClient.ts | 129 +- src/index.ts | 21 +- src/lib/isMigrationField.ts | 180 --- src/lib/isValue.ts | 215 +++ src/lib/patchMigrationDocumentData.ts | 447 ------ src/lib/prepareMigrationRecord.ts | 120 ++ src/lib/toField.ts | 106 -- src/types/migration/ContentRelationship.ts | 55 + src/types/migration/Field.ts | 48 + src/types/migration/asset.ts | 189 ++- src/types/migration/document.ts | 210 ++- src/types/migration/fields.ts | 57 - src/types/migration/richText.ts | 19 - ...te-patch-contentRelationship.test.ts.snap} | 723 +--------- ...iteClient-migrate-patch-image.test.ts.snap | 944 ++++--------- ...ent-migrate-patch-linkToMedia.test.ts.snap | 245 ++-- ...ent-migrate-patch-rtImageNode.test.ts.snap | 1253 +++++++++++++++++ test/__testutils__/mockPrismicAssetAPI.ts | 4 +- .../testMigrationFieldPatching.ts | 12 +- test/migration-createDocument.test.ts | 27 +- test/migration-getByUID.test.ts | 4 +- test/migration-getSingle.test.ts | 4 +- test/types/migration-document.types.ts | 44 +- test/types/migration.types.ts | 81 +- test/writeClient-migrate-documents.test.ts | 111 +- ...migrate-patch-contentRelationship.test.ts} | 39 +- test/writeClient-migrate-patch-image.test.ts | 34 +- ...teClient-migrate-patch-linkToMedia.test.ts | 17 +- ...teClient-migrate-patch-rtImageNode.test.ts | 43 + test/writeClient-migrate.test.ts | 6 +- 32 files changed, 2845 insertions(+), 2757 deletions(-) delete mode 100644 src/lib/isMigrationField.ts create mode 100644 src/lib/isValue.ts delete mode 100644 src/lib/patchMigrationDocumentData.ts create mode 100644 src/lib/prepareMigrationRecord.ts delete mode 100644 src/lib/toField.ts create mode 100644 src/types/migration/ContentRelationship.ts create mode 100644 src/types/migration/Field.ts delete mode 100644 src/types/migration/fields.ts delete mode 100644 src/types/migration/richText.ts rename test/__snapshots__/{writeClient-migrate-patch-link.test.ts.snap => writeClient-migrate-patch-contentRelationship.test.ts.snap} (54%) create mode 100644 test/__snapshots__/writeClient-migrate-patch-rtImageNode.test.ts.snap rename test/{writeClient-migrate-patch-link.test.ts => writeClient-migrate-patch-contentRelationship.test.ts} (57%) create mode 100644 test/writeClient-migrate-patch-rtImageNode.test.ts diff --git a/src/Client.ts b/src/Client.ts index a9d4e778..22fcd565 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -1738,7 +1738,7 @@ export class Client< throw new RepositoryNotFoundError( `Prismic repository not found. Check that "${this.documentAPIEndpoint}" is pointing to the correct repository.`, url, - undefined, + url.startsWith(this.documentAPIEndpoint) ? undefined : res.text, ) } diff --git a/src/Migration.ts b/src/Migration.ts index 35b989b1..10faaf3e 100644 --- a/src/Migration.ts +++ b/src/Migration.ts @@ -1,119 +1,33 @@ -import * as is from "./lib/isMigrationField" +import { prepareMigrationRecord } from "./lib/prepareMigrationRecord" import { validateAssetMetadata } from "./lib/validateAssetMetadata" import type { Asset } from "./types/api/asset/asset" -import type { MigrationAsset } from "./types/migration/asset" +import type { MigrationAssetConfig } from "./types/migration/Asset" +import { MigrationImage } from "./types/migration/Asset" +import { MigrationDocument } from "./types/migration/Document" import type { - FieldsToMigrationFields, PrismicMigrationDocument, PrismicMigrationDocumentParams, -} from "./types/migration/document" -import { - type ImageMigrationField, - type LinkToMediaMigrationField, - MigrationFieldType, -} from "./types/migration/fields" +} from "./types/migration/Document" import type { PrismicDocument } from "./types/value/document" -import type { GroupField } from "./types/value/group" import type { FilledImageFieldImage } from "./types/value/image" -import { LinkType } from "./types/value/link" import type { FilledLinkToMediaField } from "./types/value/linkToMedia" -import { RichTextNodeType } from "./types/value/richText" -import type { SliceZone } from "./types/value/sliceZone" -import type { AnyRegularField } from "./types/value/types" - -import * as isFilled from "./helpers/isFilled" - -/** - * Discovers assets in a record of Prismic fields. - * - * @param record - Record of Prismic fields to loook for assets in. - * @param onAsset - Callback that is called for each asset found. - */ -const discoverAssets = ( - record: FieldsToMigrationFields< - Record - >, - onAsset: (asset: FilledImageFieldImage | FilledLinkToMediaField) => void, -) => { - for (const field of Object.values(record)) { - if (is.sliceZone(field)) { - for (const slice of field) { - discoverAssets(slice.primary, onAsset) - for (const item of slice.items) { - discoverAssets(item, onAsset) - } - } - } else if (is.richText(field)) { - for (const node of field) { - if ("type" in node) { - if (node.type === RichTextNodeType.image) { - onAsset(node) - if ( - node.linkTo && - "link_type" in node.linkTo && - node.linkTo.link_type === LinkType.Media - ) { - onAsset(node.linkTo) - } - } else if (node.type !== RichTextNodeType.embed) { - for (const span of node.spans) { - if ( - span.type === "hyperlink" && - span.data && - "link_type" in span.data && - span.data.link_type === LinkType.Media - ) { - onAsset(span.data) - } - } - } - } - } - } else if (is.group(field)) { - for (const item of field) { - discoverAssets(item, onAsset) - } - } else if ( - is.image(field) && - field && - "dimensions" in field && - isFilled.image(field) - ) { - onAsset(field) - } else if ( - is.link(field) && - field && - "link_type" in field && - field.link_type === LinkType.Media && - isFilled.linkToMedia(field) - ) { - onAsset(field) - } - } -} /** * Extracts one or more Prismic document types that match a given Prismic * document type. If no matches are found, no extraction is performed and the * union of all provided Prismic document types are returned. * - * @typeParam TMigrationDocuments - Prismic migration document types from which - * to extract. - * @typeParam TType - Type(s) to match `TMigrationDocuments` against. + * @typeParam TDocuments - Prismic document types from which to extract. + * @typeParam TDocumentType - Type(s) to match `TDocuments` against. */ -type ExtractMigrationDocumentType< - TMigrationDocuments extends PrismicMigrationDocument, - TType extends TMigrationDocuments["type"], +type ExtractDocumentType< + TDocuments extends PrismicDocument | PrismicMigrationDocument, + TDocumentType extends TDocuments["type"], > = - Extract extends never - ? TMigrationDocuments - : Extract - -type CreateAssetReturnType = ImageMigrationField & { - image: ImageMigrationField - linkToMedia: LinkToMediaMigrationField -} + Extract extends never + ? TDocuments + : Extract /** * The symbol used to index documents that are singletons. @@ -134,37 +48,37 @@ export class Migration< /** * @internal */ - _documents: { - document: TMigrationDocuments - params: PrismicMigrationDocumentParams - }[] = [] - #indexedDocuments: Record> = {} + _assets: Map = new Map() /** * @internal */ - _assets: Map = new Map() + _documents: MigrationDocument[] = [] + #indexedDocuments: Record< + string, + Record> + > = {} createAsset( asset: Asset | FilledImageFieldImage | FilledLinkToMediaField, - ): CreateAssetReturnType + ): MigrationImage createAsset( - file: MigrationAsset["file"], - filename: MigrationAsset["filename"], + file: MigrationAssetConfig["file"], + filename: MigrationAssetConfig["filename"], params?: { notes?: string credits?: string alt?: string tags?: string[] }, - ): CreateAssetReturnType + ): MigrationImage createAsset( fileOrAsset: - | MigrationAsset["file"] + | MigrationAssetConfig["file"] | Asset | FilledImageFieldImage | FilledLinkToMediaField, - filename?: MigrationAsset["filename"], + filename?: MigrationAssetConfig["filename"], { notes, credits, @@ -176,8 +90,9 @@ export class Migration< alt?: string tags?: string[] } = {}, - ): CreateAssetReturnType { - let asset: MigrationAsset + ): MigrationImage { + let asset: MigrationAssetConfig + let maybeInitialField: FilledImageFieldImage | undefined if (typeof fileOrAsset === "object" && "url" in fileOrAsset) { if ("dimensions" in fileOrAsset || "link_type" in fileOrAsset) { const url = fileOrAsset.url.split("?")[0] @@ -192,6 +107,10 @@ export class Migration< const alt = "alt" in fileOrAsset && fileOrAsset.alt ? fileOrAsset.alt : undefined + if ("dimensions" in fileOrAsset) { + maybeInitialField = fileOrAsset + } + asset = { id: fileOrAsset.id, file: url, @@ -243,64 +162,54 @@ export class Migration< this._assets.set(asset.id, asset) } - return { - migrationType: MigrationFieldType.Image, - ...asset, - image: { - migrationType: MigrationFieldType.Image, - ...asset, - }, - linkToMedia: { - migrationType: MigrationFieldType.LinkToMedia, - ...asset, - }, - } + return new MigrationImage(this._assets.get(asset.id)!, maybeInitialField) } createDocument( - document: ExtractMigrationDocumentType, + document: ExtractDocumentType, documentTitle: PrismicMigrationDocumentParams["documentTitle"], params: Omit = {}, - ): ExtractMigrationDocumentType { - this._documents.push({ - document, - params: { documentTitle, ...params }, - }) + ): MigrationDocument> { + const { record: data, dependencies } = prepareMigrationRecord( + document.data, + this.createAsset.bind(this), + ) + + const migrationDocument = new MigrationDocument( + { ...document, data }, + { + documentTitle, + ...params, + }, + dependencies, + ) + + this._documents.push(migrationDocument) // Index document if (!(document.type in this.#indexedDocuments)) { this.#indexedDocuments[document.type] = {} } this.#indexedDocuments[document.type][document.uid || SINGLE_INDEX] = - document - - // Find other assets in document - discoverAssets(document.data, this.createAsset.bind(this)) + migrationDocument - return document + return migrationDocument } - getByUID< - TType extends TMigrationDocuments["type"], - TMigrationDocument extends Extract< - TMigrationDocuments, - { type: TType } - > = Extract, - >(documentType: TType, uid: string): TMigrationDocument | undefined { + getByUID( + documentType: TType, + uid: string, + ): MigrationDocument> | undefined { return this.#indexedDocuments[documentType]?.[uid] as - | TMigrationDocument + | MigrationDocument | undefined } - getSingle< - TType extends TMigrationDocuments["type"], - TMigrationDocument extends Extract< - TMigrationDocuments, - { type: TType } - > = Extract, - >(documentType: TType): TMigrationDocument | undefined | undefined { + getSingle( + documentType: TType, + ): MigrationDocument> | undefined { return this.#indexedDocuments[documentType]?.[SINGLE_INDEX] as - | TMigrationDocument + | MigrationDocument | undefined } } diff --git a/src/WriteClient.ts b/src/WriteClient.ts index be5934e8..2dc6ac16 100644 --- a/src/WriteClient.ts +++ b/src/WriteClient.ts @@ -1,7 +1,5 @@ import { devMsg } from "./lib/devMsg" import { pLimit } from "./lib/pLimit" -import type { AssetMap, DocumentMap } from "./lib/patchMigrationDocumentData" -import { patchMigrationDocumentData } from "./lib/patchMigrationDocumentData" import type { Asset, @@ -24,11 +22,14 @@ import { type PostDocumentResult, type PutDocumentParams, } from "./types/api/migration/document" -import type { MigrationAsset } from "./types/migration/asset" +import type { AssetMap, MigrationAssetConfig } from "./types/migration/Asset" +import { MigrationContentRelationship } from "./types/migration/ContentRelationship" import type { + DocumentMap, + MigrationDocument, PrismicMigrationDocument, PrismicMigrationDocumentParams, -} from "./types/migration/document" +} from "./types/migration/Document" import type { PrismicDocument } from "./types/value/document" import { PrismicError } from "./errors/PrismicError" @@ -93,13 +94,13 @@ type MigrateReporterEventMap = { current: number remaining: number total: number - asset: MigrationAsset + asset: MigrationAssetConfig } "assets:creating": { current: number remaining: number total: number - asset: MigrationAsset + asset: MigrationAssetConfig } "assets:created": { created: number @@ -521,89 +522,77 @@ export class WriteClient< documents.set(document.id, document) } - const sortedDocuments: { - document: PrismicMigrationDocument - params: PrismicMigrationDocumentParams - }[] = [] + const sortedMigrationDocuments: MigrationDocument[] = [] // We create an array with non-master locale documents last because // we need their master locale document to be created first. - for (const { document, params } of migration._documents) { - if (document.lang === masterLocale) { - sortedDocuments.unshift({ document, params }) + for (const migrationDocument of migration._documents) { + if (migrationDocument.document.lang === masterLocale) { + sortedMigrationDocuments.unshift(migrationDocument) } else { - sortedDocuments.push({ document, params }) + sortedMigrationDocuments.push(migrationDocument) } } let i = 0 let created = 0 - for (const { document, params } of sortedDocuments) { - if (document.id && documents.has(document.id)) { + for (const migrationDocument of sortedMigrationDocuments) { + if ( + migrationDocument.document.id && + documents.has(migrationDocument.document.id) + ) { reporter?.({ type: "documents:skipping", data: { reason: "already exists", current: ++i, - remaining: sortedDocuments.length - i, - total: sortedDocuments.length, - document, - documentParams: params, + remaining: sortedMigrationDocuments.length - i, + total: sortedMigrationDocuments.length, + document: migrationDocument.document, + documentParams: migrationDocument.params, }, }) // Index the migration document - documents.set(document, documents.get(document.id)!) + documents.set( + migrationDocument, + documents.get(migrationDocument.document.id)!, + ) } else { created++ reporter?.({ type: "documents:creating", data: { current: ++i, - remaining: sortedDocuments.length - i, - total: sortedDocuments.length, - document, - documentParams: params, + remaining: sortedMigrationDocuments.length - i, + total: sortedMigrationDocuments.length, + document: migrationDocument.document, + documentParams: migrationDocument.params, }, }) // Resolve master language document ID for non-master locale documents let masterLanguageDocumentID: string | undefined - if (document.lang !== masterLocale) { - if (params.masterLanguageDocument) { - if (typeof params.masterLanguageDocument === "function") { - const masterLanguageDocument = - await params.masterLanguageDocument() - - if (masterLanguageDocument) { - // `masterLanguageDocument` is an existing document - if (masterLanguageDocument.id) { - masterLanguageDocumentID = documents.get( - masterLanguageDocument.id, - )?.id - } - - // `masterLanguageDocument` is a new document - if (!masterLanguageDocumentID) { - masterLanguageDocumentID = documents.get( - masterLanguageDocument, - )?.id - } - } - } else { - masterLanguageDocumentID = params.masterLanguageDocument.id - } - } else if (document.alternate_languages) { - masterLanguageDocumentID = document.alternate_languages.find( - ({ lang }) => lang === masterLocale, - )?.id + if (migrationDocument.document.lang !== masterLocale) { + if (migrationDocument.params.masterLanguageDocument) { + const link = new MigrationContentRelationship( + migrationDocument.params.masterLanguageDocument, + ) + + await link._prepare({ documents }) + masterLanguageDocumentID = link._field?.id + } else if (migrationDocument.document.alternate_languages) { + masterLanguageDocumentID = + migrationDocument.document.alternate_languages.find( + ({ lang }) => lang === masterLocale, + )?.id } } const { id } = await this.createDocument( // We'll upload docuements data later on. - { ...document, data: {} }, - params.documentTitle, + { ...migrationDocument.document, data: {} }, + migrationDocument.params.documentTitle, { masterLanguageDocumentID, ...fetchParams, @@ -611,10 +600,13 @@ export class WriteClient< ) // Index old ID for Prismic to Prismic migration - if (document.id) { - documents.set(document.id, { ...document, id }) + if (migrationDocument.document.id) { + documents.set(migrationDocument.document.id, { + ...migrationDocument.document, + id, + }) } - documents.set(document, { ...document, id }) + documents.set(migrationDocument, { ...migrationDocument.document, id }) } } @@ -650,30 +642,31 @@ export class WriteClient< }: { reporter?: (event: MigrateReporterEvents) => void } & FetchParams = {}, ): Promise { let i = 0 - for (const { document, params } of migration._documents) { + for (const migrationDocument of migration._documents) { reporter?.({ type: "documents:updating", data: { current: ++i, remaining: migration._documents.length - i, total: migration._documents.length, - document, - documentParams: params, + document: migrationDocument.document, + documentParams: migrationDocument.params, }, }) - const { id, uid } = documents.get(document)! - const data = await patchMigrationDocumentData( - document.data, - assets, - documents, - ) + const { id, uid } = documents.get(migrationDocument)! + await migrationDocument._prepare({ assets, documents }) await this.updateDocument( id, // We need to forward again document name and tags to update them // in case the document already existed during the previous step. - { documentTitle: params.documentTitle, uid, tags: document.tags, data }, + { + documentTitle: migrationDocument.params.documentTitle, + uid, + tags: migrationDocument.document.tags, + data: migrationDocument.document.data, + }, fetchParams, ) } diff --git a/src/index.ts b/src/index.ts index 45768631..8c1dfaf3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -326,24 +326,19 @@ export type { } from "./types/model/types" // Migrations - Types representing Prismic Migration API content values. -export { MigrationFieldType } from "./types/migration/fields" - export type { + MigrationDocument, PrismicMigrationDocument, - RichTextFieldToMigrationField, -} from "./types/migration/document" + RichTextFieldWithMigrationField, +} from "./types/migration/Document" export type { - ImageMigrationField, - LinkToMediaMigrationField, - ContentRelationshipMigrationField, - LinkMigrationField, -} from "./types/migration/fields" + MigrationImage, + MigrationLinkToMedia, + MigrationRTImageNode, +} from "./types/migration/Asset" -export type { - RTImageMigrationNode, - RTLinkMigrationNode, -} from "./types/migration/richText" +export type { MigrationContentRelationship } from "./types/migration/ContentRelationship" // API - Types representing Prismic Rest API V2 responses. export type { Query } from "./types/api/query" diff --git a/src/lib/isMigrationField.ts b/src/lib/isMigrationField.ts deleted file mode 100644 index 320f1fa6..00000000 --- a/src/lib/isMigrationField.ts +++ /dev/null @@ -1,180 +0,0 @@ -import type { - FieldToMigrationField, - GroupFieldToMigrationField, - RichTextFieldToMigrationField, - SliceZoneToMigrationField, -} from "../types/migration/document" -import type { - ImageMigrationField, - LinkMigrationField, -} from "../types/migration/fields" -import { MigrationFieldType } from "../types/migration/fields" -import type { GroupField } from "../types/value/group" -import type { ImageField } from "../types/value/image" -import type { LinkField } from "../types/value/link" -import { LinkType } from "../types/value/link" -import { type RichTextField, RichTextNodeType } from "../types/value/richText" -import type { SliceZone } from "../types/value/sliceZone" -import type { AnyRegularField } from "../types/value/types" - -/** - * Checks if a field is a slice zone. - * - * @param field - Field to check. - * - * @returns `true` if `field` is a slice zone migration field, `false` - * otherwise. - * - * @internal - * This is not an official helper function and it's only designed to work in the - * case of migration fields. - */ -export const sliceZone = ( - field: FieldToMigrationField, -): field is SliceZoneToMigrationField => { - return ( - Array.isArray(field) && - field.every((item) => "slice_type" in item && "id" in item) - ) -} - -/** - * Checks if a field is a rich text field. - * - * @param field - Field to check. - * - * @returns `true` if `field` is a rich text migration field, `false` otherwise. - * - * @internal - * This is not an official helper function and it's only designed to work in the - * case of migration fields. - */ -export const richText = ( - field: FieldToMigrationField, -): field is RichTextFieldToMigrationField => { - return ( - Array.isArray(field) && - field.every((item) => { - if ( - "migrationType" in item && - item.migrationType === MigrationFieldType.Image - ) { - return true - } else if ("type" in item && typeof item.type === "string") { - switch (item.type) { - case RichTextNodeType.heading1: - case RichTextNodeType.heading2: - case RichTextNodeType.heading3: - case RichTextNodeType.heading4: - case RichTextNodeType.heading5: - case RichTextNodeType.heading6: - case RichTextNodeType.paragraph: - case RichTextNodeType.preformatted: - case RichTextNodeType.listItem: - case RichTextNodeType.oListItem: - return "spans" in item && Array.isArray(item.spans) - - case RichTextNodeType.image: - return "dimensions" in item - - case RichTextNodeType.embed: - return "oembed" in item - } - } - - return false - }) - ) -} - -/** - * Checks if a field is a group field. - * - * @param field - Field to check. - * - * @returns `true` if `field` is a group migration field, `false` otherwise. - * - * @internal - * This is not an official helper function and it's only designed to work in the - * case of migration fields. - */ -export const group = ( - field: FieldToMigrationField, -): field is GroupFieldToMigrationField => { - return !sliceZone(field) && !richText(field) && Array.isArray(field) -} - -/** - * Checks if a field is a link field. - * - * @param field - Field to check. - * - * @returns `true` if `field` is a link migration field, `false` otherwise. - * - * @internal - * This is not an official helper function and it's only designed to work in the - * case of migration fields. - */ -export const link = ( - field: FieldToMigrationField, -): field is LinkMigrationField | LinkField => { - if (typeof field === "function") { - // Lazy content relationship field - return true - } else if (field && typeof field === "object" && !("version" in field)) { - if ( - "migrationType" in field && - field.migrationType === MigrationFieldType.LinkToMedia - ) { - // Migration link to media field - return true - } else if ( - "type" in field && - "lang" in field && - typeof field.lang === "string" - ) { - // Content relationship field declared using another repository document - return true - } else if ( - "link_type" in field && - (field.link_type === LinkType.Document || - field.link_type === LinkType.Media || - field.link_type === LinkType.Web) - ) { - // Regular link field - return true - } - } - - return false -} - -/** - * Checks if a field is an image field. - * - * @param field - Field to check. - * - * @returns `true` if `field` is an image migration field, `false` otherwise. - * - * @internal - * This is not an official helper function and it's only designed to work in the - * case of migration fields. - */ -export const image = ( - field: FieldToMigrationField, -): field is ImageMigrationField | ImageField => { - if (field && typeof field === "object" && !("version" in field)) { - if ( - "migrationType" in field && - field.migrationType === MigrationFieldType.Image - ) { - // Migration image field - return true - } else if ("id" in field && "url" in field && "dimensions" in field) { - // Regular image field - return true - } - } - - return false -} diff --git a/src/lib/isValue.ts b/src/lib/isValue.ts new file mode 100644 index 00000000..71f563f0 --- /dev/null +++ b/src/lib/isValue.ts @@ -0,0 +1,215 @@ +import type { + FieldWithMigrationField, + RichTextBlockNodeWithMigrationField, +} from "../types/migration/Document" +import type { FilledContentRelationshipField } from "../types/value/contentRelationship" +import type { PrismicDocument } from "../types/value/document" +import type { ImageField } from "../types/value/image" +import { LinkType } from "../types/value/link" +import type { FilledLinkToMediaField } from "../types/value/linkToMedia" +import { type RTImageNode, RichTextNodeType } from "../types/value/richText" + +/** + * Checks if a value is a link to media field. + * + * @param value - Value to check. + * + * @returns `true` if `value` is a link to media field, `false` otherwise. + * + * @internal + * This is not an official helper function and it's only designed to work with internal processes. + */ +export const linkToMedia = ( + value: + | PrismicDocument + | FieldWithMigrationField + | RichTextBlockNodeWithMigrationField + | unknown, +): value is FilledLinkToMediaField => { + if (value && typeof value === "object" && !("version" in value)) { + if ( + "link_type" in value && + value.link_type === LinkType.Media && + "id" in value && + "name" in value && + "kind" in value && + "url" in value && + "size" in value + ) { + return true + } + } + + return false +} + +/** + * Checks if a value is like an image field. + * + * @param value - Value to check. + * + * @returns `true` if `value` is like an image field, `false` otherwise. + * + * @internal + * This is not an official helper function and it's only designed to work with internal processes. + */ +const imageLike = ( + value: + | PrismicDocument + | FieldWithMigrationField + | RichTextBlockNodeWithMigrationField + | unknown, +): value is ImageField | RTImageNode => { + if ( + value && + typeof value === "object" && + (!("version" in value) || typeof value.version === "object") + ) { + if ( + "id" in value && + "url" in value && + typeof value.url === "string" && + "dimensions" in value && + "edit" in value && + "alt" in value && + "copyright" in value + ) { + return true + } + } + + return false +} + +/** + * Checks if a value is an image field. + * + * @param value - Value to check. + * + * @returns `true` if `value` is an image field, `false` otherwise. + * + * @internal + * This is not an official helper function and it's only designed to work with internal processes. + */ +export const image = ( + value: + | PrismicDocument + | FieldWithMigrationField + | RichTextBlockNodeWithMigrationField + | unknown, +): value is ImageField => { + if ( + imageLike(value) && + (!("type" in value) || value.type !== RichTextNodeType.image) + ) { + value + + return true + } + + return false +} + +/** + * Checks if a value is a rich text image node. + * + * @param value - Value to check. + * + * @returns `true` if `value` is a rich text image node, `false` otherwise. + * + * @internal + * This is not an official helper function and it's only designed to work with internal processes. + */ +export const rtImageNode = ( + value: + | PrismicDocument + | FieldWithMigrationField + | RichTextBlockNodeWithMigrationField + | unknown, +): value is RTImageNode => { + if ( + imageLike(value) && + "type" in value && + value.type === RichTextNodeType.image + ) { + value + + return true + } + + return false +} + +/** + * Checks if a value is a content relationship field. + * + * @param value - Value to check. + * + * @returns `true` if `value` is a content relationship, `false` otherwise. + * + * @internal + * This is not an official helper function and it's only designed to work with internal processes. + */ +export const contentRelationship = ( + value: + | PrismicDocument + | FieldWithMigrationField + | RichTextBlockNodeWithMigrationField + | unknown, +): value is FilledContentRelationshipField => { + if (value && typeof value === "object" && !("version" in value)) { + if ( + "link_type" in value && + value.link_type === LinkType.Document && + "id" in value && + "type" in value && + "tags" in value && + "lang" in value + ) { + return true + } + } + + return false +} + +/** + * Checks if a value is a Prismic document. + * + * @param value - Value to check. + * + * @returns `true` if `value` is a content relationship, `false` otherwise. + * + * @internal + * This is not an official helper function and it's only designed to work with internal processes. + */ +export const document = ( + value: + | PrismicDocument + | FieldWithMigrationField + | RichTextBlockNodeWithMigrationField + | unknown, +): value is PrismicDocument => { + if (value && typeof value === "object" && !("version" in value)) { + if ( + "id" in value && + "uid" in value && + "url" in value && + "type" in value && + typeof value.type === "string" && + "href" in value && + "tags" in value && + "first_publication_date" in value && + "last_publication_date" in value && + "slugs" in value && + "linked_documents" in value && + "lang" in value && + "alternate_languages" in value && + "data" in value + ) { + return true + } + } + + return false +} diff --git a/src/lib/patchMigrationDocumentData.ts b/src/lib/patchMigrationDocumentData.ts deleted file mode 100644 index d99b59b7..00000000 --- a/src/lib/patchMigrationDocumentData.ts +++ /dev/null @@ -1,447 +0,0 @@ -import type { Asset } from "../types/api/asset/asset" -import type { MigrationAsset } from "../types/migration/asset" -import type { - FieldsToMigrationFields, - GroupFieldToMigrationField, - PrismicMigrationDocument, - RichTextFieldToMigrationField, - SliceZoneToMigrationField, -} from "../types/migration/document" -import type { - ImageMigrationField, - LinkMigrationField, -} from "../types/migration/fields" -import { MigrationFieldType } from "../types/migration/fields" -import type { PrismicDocument } from "../types/value/document" -import type { GroupField, NestedGroupField } from "../types/value/group" -import type { FilledImageFieldImage, ImageField } from "../types/value/image" -import { LinkType } from "../types/value/link" -import type { LinkField } from "../types/value/link" -import type { RTInlineNode } from "../types/value/richText" -import { type RichTextField, RichTextNodeType } from "../types/value/richText" -import type { SharedSlice } from "../types/value/sharedSlice" -import type { Slice } from "../types/value/slice" -import type { SliceZone } from "../types/value/sliceZone" -import type { AnyRegularField } from "../types/value/types" - -import * as isFilled from "../helpers/isFilled" - -import * as is from "./isMigrationField" -import * as to from "./toField" - -/** - * A map of asset IDs to asset used to resolve assets when patching migration - * Prismic documents. - * - * @internal - */ -export type AssetMap = Map - -/** - * A map of document IDs, documents, and migraiton documents to content - * relationship field used to resolve content relationships when patching - * migration Prismic documents. - * - * @typeParam TDocuments - Type of Prismic documents in the repository. - * - * @internal - */ -export type DocumentMap = - Map< - | string - | TDocuments - | PrismicMigrationDocument - | PrismicMigrationDocument, - | PrismicDocument - | (Omit, "id"> & { id: string }) - > - -/** - * Inherits query parameters from an original URL to a new URL. - * - * @param url - The new URL. - * @param original - The original URL to inherit query parameters from. - * - * @returns The new URL with query parameters inherited from the original URL. - */ -const inheritQueryParams = (url: string, original: string): string => { - const queryParams = original.split("?")[1] || "" - - return `${url.split("?")[0]}${queryParams ? `?${queryParams}` : ""}` -} - -/** - * Patches references in a slice zone field. - * - * @typeParam TDocuments - Type of Prismic documents in the repository. - * - * @param sliceZone - The slice zone to patch. - * @param assets - A map of assets available in the Prismic repository. - * @param documents - A map of documents available in the Prismic repository. - * - * @returns The patched slice zone. - */ -const patchSliceZone = async < - TDocuments extends PrismicDocument = PrismicDocument, ->( - sliceZone: SliceZoneToMigrationField, - assets: AssetMap, - documents: DocumentMap, -): Promise => { - const result = [] as unknown as SliceZone - - for (const slice of sliceZone) { - const { primary, items, ...properties } = slice - const patchedPrimary = await patchRecord( - // We cast to `Slice["primary"]` which is stricter than `SharedSlice["primary"]` - // otherwise TypeScript gets confused while creating the patched slice below. - primary as Slice["primary"], - assets, - documents, - ) - const patchedItems = await patchGroup(items, assets, documents) - - result.push({ - ...properties, - primary: patchedPrimary, - items: patchedItems, - }) - } - - return result -} - -/** - * Patches references in a rich text field. - * - * @typeParam TDocuments - Type of Prismic documents in the repository. - * - * @param richText - The rich text field to patch. - * @param assets - A map of assets available in the Prismic repository. - * @param documents - A map of documents available in the Prismic repository. - * - * @returns The patched rich text field. - */ -const patchRichText = async < - TDocuments extends PrismicDocument = PrismicDocument, ->( - richText: RichTextFieldToMigrationField, - assets: AssetMap, - documents: DocumentMap, -): Promise => { - const result = [] as unknown as RichTextField<"filled"> - - for (const node of richText) { - if ("type" in node && typeof node.type === "string") { - if (node.type === RichTextNodeType.embed) { - result.push(node) - } else if (node.type === RichTextNodeType.image) { - const image = patchImage(node, assets) - - if (isFilled.image(image)) { - const linkTo = await patchLink(node.linkTo, assets, documents) - - result.push({ - ...image, - type: RichTextNodeType.image, - linkTo: isFilled.link(linkTo) ? linkTo : undefined, - }) - } - } else { - const { spans, ...properties } = node - - const patchedSpans: RTInlineNode[] = [] - - for (const span of spans) { - if (span.type === RichTextNodeType.hyperlink) { - const data = await patchLink(span.data, assets, documents) - - if (isFilled.link(data)) { - patchedSpans.push({ ...span, data }) - } - } else { - patchedSpans.push(span) - } - } - - result.push({ - ...properties, - spans: patchedSpans, - }) - } - } else { - // Migration image node - const asset = assets.get(node.id) - - const linkTo = await patchLink(node.linkTo, assets, documents) - - if (asset) { - result.push({ - ...to.rtImageNode(asset), - linkTo: isFilled.link(linkTo) ? linkTo : undefined, - }) - } - } - } - - return result -} - -/** - * Patches references in a group field. - * - * @typeParam TDocuments - Type of Prismic documents in the repository. - * - * @param group - The group field to patch. - * @param assets - A map of assets available in the Prismic repository. - * @param documents - A map of documents available in the Prismic repository. - * - * @returns The patched group field. - */ -const patchGroup = async < - TMigrationGroup extends GroupFieldToMigrationField< - GroupField | Slice["items"] | SharedSlice["items"] - >, - TDocuments extends PrismicDocument = PrismicDocument, ->( - group: TMigrationGroup, - assets: AssetMap, - documents: DocumentMap, -): Promise< - TMigrationGroup extends GroupFieldToMigrationField - ? TGroup - : never -> => { - const result = [] as unknown as GroupField< - Record, - "filled" - > - - for (const item of group) { - const patched = await patchRecord(item, assets, documents) - - result.push(patched) - } - - return result as TMigrationGroup extends GroupFieldToMigrationField< - infer TGroup - > - ? TGroup - : never -} - -/** - * Patches references in a link field. - * - * @typeParam TDocuments - Type of Prismic documents in the repository. - * - * @param link - The link field to patch. - * @param assets - A map of assets available in the Prismic repository. - * @param documents - A map of documents available in the Prismic repository. - * - * @returns The patched link field. - */ -const patchLink = async ( - link: LinkMigrationField | LinkField, - assets: AssetMap, - documents: DocumentMap, -): Promise => { - if (link) { - if ("migrationType" in link) { - // Migration link field - const asset = assets.get(link.id) - if (asset) { - return to.linkToMediaField(asset) - } - } else if ("link_type" in link) { - switch (link.link_type) { - case LinkType.Document: - // Existing content relationship - if (isFilled.contentRelationship(link)) { - const id = documents.get(link.id)?.id - if (id) { - return { ...link, id } - } else { - return { ...link, isBroken: true } - } - } - case LinkType.Media: - // Existing link to media - if (isFilled.linkToMedia(link)) { - const id = assets.get(link.id)?.id - if (id) { - return { ...link, id } - } - break - } - case LinkType.Web: - default: - return link - } - } else { - const resolved = typeof link === "function" ? await link() : link - - if (resolved) { - // Link might be represented by a document or a migration document... - let maybeRelationship = resolved.id - ? documents.get(resolved.id) - : undefined - - // ...or by a migration document - if (!maybeRelationship) { - maybeRelationship = documents.get(resolved) - } - - if (maybeRelationship) { - return to.contentRelationship(maybeRelationship) - } - } - } - } - - return { - link_type: LinkType.Any, - } -} - -/** - * Patches references in an image field. - * - * @param image - The image field to patch. - * @param assets - A map of assets available in the Prismic repository. - * - * @returns The patched image field. - */ -const patchImage = ( - image: ImageMigrationField | ImageField, - assets: AssetMap, -): ImageField => { - if (image) { - if ( - "migrationType" in image && - image.migrationType === MigrationFieldType.Image - ) { - // Migration image field - const asset = assets.get(image.id) - if (asset) { - return to.imageField(asset) - } - } else if ( - "dimensions" in image && - image.dimensions && - isFilled.image(image) - ) { - // Regular image field - const { - id, - url, - dimensions, - edit, - alt, - copyright: _, - ...thumbnails - } = image - const asset = assets.get(id) - - if (asset) { - const result = { - id: asset.id, - url: inheritQueryParams(asset.url, url), - dimensions, - edit, - alt: alt || null, - copyright: asset.credits || null, - } as ImageField - - if (Object.keys(thumbnails).length > 0) { - for (const name in thumbnails) { - const maybeThumbnail = ( - thumbnails as Record - )[name] - - if (is.image(maybeThumbnail)) { - const { url, dimensions, edit, alt } = ( - thumbnails as Record - )[name] - - result[name] = { - id: asset.id, - url: inheritQueryParams(asset.url, url), - dimensions, - edit, - alt: alt || null, - copyright: asset.credits || null, - } - } - } - } - - return result - } - } - } - - return {} -} - -/** - * Patches references in a record of Prismic field. - * - * @typeParam TFields - Type of the record of Prismic fields. - * @typeParam TDocuments - Type of Prismic documents in the repository. - * - * @param record - The link field to patch. - * @param assets - A map of assets available in the Prismic repository. - * @param documents - A map of documents available in the Prismic repository. - * - * @returns The patched record. - */ -const patchRecord = async < - TFields extends Record, - TDocuments extends PrismicDocument = PrismicDocument, ->( - record: FieldsToMigrationFields, - assets: AssetMap, - documents: DocumentMap, -): Promise => { - const result: Record = {} - - for (const [key, field] of Object.entries(record)) { - if (is.sliceZone(field)) { - result[key] = await patchSliceZone(field, assets, documents) - } else if (is.richText(field)) { - result[key] = await patchRichText(field, assets, documents) - } else if (is.group(field)) { - result[key] = await patchGroup(field, assets, documents) - } else if (is.link(field)) { - result[key] = await patchLink(field, assets, documents) - } else if (is.image(field)) { - result[key] = patchImage(field, assets) - } else { - result[key] = field - } - } - - return result as TFields -} - -/** - * Patches references in a document's data. - * - * @typeParam TDocuments - Type of Prismic documents in the repository. - * - * @param data - The document data to patch. - * @param assets - A map of assets available in the Prismic repository. - * @param documents - A map of documents available in the Prismic repository. - * - * @returns The patched document data. - */ -export const patchMigrationDocumentData = async < - TDocuments extends PrismicDocument = PrismicDocument, ->( - data: PrismicMigrationDocument["data"], - assets: AssetMap, - documents: DocumentMap, -): Promise => { - return patchRecord(data, assets, documents) -} diff --git a/src/lib/prepareMigrationRecord.ts b/src/lib/prepareMigrationRecord.ts new file mode 100644 index 00000000..26ea459e --- /dev/null +++ b/src/lib/prepareMigrationRecord.ts @@ -0,0 +1,120 @@ +import type { + MigrationImage, + MigrationLinkToMedia, +} from "../types/migration/Asset" +import type { UnresolvedMigrationContentRelationshipConfig } from "../types/migration/ContentRelationship" +import { MigrationContentRelationship } from "../types/migration/ContentRelationship" +import { MigrationDocument } from "../types/migration/Document" +import { MigrationField } from "../types/migration/Field" +import type { FilledImageFieldImage } from "../types/value/image" +import type { FilledLinkToWebField } from "../types/value/link" +import type { FilledLinkToMediaField } from "../types/value/linkToMedia" + +import * as is from "./isValue" + +/** + * Replaces existings assets and links in a record of Prismic fields get all + * dependencies to them. + * + * @typeParam TRecord - Record of values to work with. + * + * @param record - Record of Prismic fields to work with. + * @param onAsset - Callback that is called for each asset found. + * + * @returns An object containing the record with replaced assets and links and a + * list of dependencies found and/or created. + */ +export const prepareMigrationRecord = >( + record: TRecord, + onAsset: ( + asset: FilledImageFieldImage | FilledLinkToMediaField, + ) => MigrationImage, +): { record: TRecord; dependencies: MigrationField[] } => { + const result = {} as Record + const dependencies: MigrationField[] = [] + + for (const key in record) { + const field: unknown = record[key] + + if (field instanceof MigrationField) { + dependencies.push(field) + result[key] = field + } else if (is.linkToMedia(field)) { + const linkToMedia = onAsset(field).asLinkToMedia() + + dependencies.push(linkToMedia) + result[key] = linkToMedia + } else if (is.rtImageNode(field)) { + const rtImageNode = onAsset(field).asRTImageNode() + + if (field.linkTo) { + // Node `linkTo` dependency is tracked internally + rtImageNode.linkTo = prepareMigrationRecord( + { linkTo: field.linkTo }, + onAsset, + ).record.linkTo as + | MigrationContentRelationship + | MigrationLinkToMedia + | FilledLinkToWebField + } + + dependencies.push(rtImageNode) + result[key] = rtImageNode + } else if (is.image(field)) { + const image = onAsset(field).asImage() + + const { + id: _id, + url: _url, + dimensions: _dimensions, + edit: _edit, + alt: _alt, + copyright: _copyright, + ...thumbnails + } = field + + for (const name in thumbnails) { + if (is.image(thumbnails[name])) { + image.addThumbnail(name, onAsset(thumbnails[name]).asImage()) + } + } + + dependencies.push(image) + result[key] = image + } else if ( + is.contentRelationship(field) || + is.document(field) || + field instanceof MigrationDocument || + typeof field === "function" + ) { + const contentRelationship = new MigrationContentRelationship( + field as UnresolvedMigrationContentRelationshipConfig, + ) + + dependencies.push(contentRelationship) + result[key] = contentRelationship + } else if (Array.isArray(field)) { + const array = [] + + for (const item of field) { + const { record, dependencies: itemDependencies } = + prepareMigrationRecord({ item }, onAsset) + + array.push(record.item) + dependencies.push(...itemDependencies) + } + + result[key] = array + } else if (field && typeof field === "object") { + const { record, dependencies: fieldDependencies } = + prepareMigrationRecord({ ...field }, onAsset) + + dependencies.push(...fieldDependencies) + result[key] = record + } else { + result[key] = field + } + } + + return { record: result as TRecord, dependencies } +} diff --git a/src/lib/toField.ts b/src/lib/toField.ts deleted file mode 100644 index 84035a43..00000000 --- a/src/lib/toField.ts +++ /dev/null @@ -1,106 +0,0 @@ -import type { Asset } from "../types/api/asset/asset" -import type { PrismicMigrationDocument } from "../types/migration/document" -import type { FilledContentRelationshipField } from "../types/value/contentRelationship" -import type { PrismicDocument } from "../types/value/document" -import type { ImageField } from "../types/value/image" -import { LinkType } from "../types/value/link" -import type { LinkToMediaField } from "../types/value/linkToMedia" -import type { RTImageNode } from "../types/value/richText" -import { RichTextNodeType } from "../types/value/richText" - -/** - * Converts an asset to an image field. - * - * @param asset - Asset to convert. - * - * @returns Equivalent image field. - */ -export const imageField = ({ - id, - url, - width, - height, - alt, - credits, -}: Asset): ImageField => { - return { - id, - url, - dimensions: { width: width!, height: height! }, - edit: { x: 0, y: 0, zoom: 0, background: "transparent" }, - alt: alt || null, - copyright: credits || null, - } -} - -/** - * Converts an asset to a link to media field. - * - * @param asset - Asset to convert. - * - * @returns Equivalent link to media field. - */ -export const linkToMediaField = ({ - id, - filename, - kind, - url, - size, - width, - height, -}: Asset): LinkToMediaField<"filled"> => { - return { - id, - link_type: LinkType.Media, - name: filename, - kind, - url, - size: `${size}`, - width: width ? `${width}` : undefined, - height: height ? `${height}` : undefined, - } -} - -/** - * Converts an asset to an RT image node. - * - * @param asset - Asset to convert. - * - * @returns Equivalent rich text image node. - */ -export const rtImageNode = (asset: Asset): RTImageNode => { - return { - ...imageField(asset), - type: RichTextNodeType.image, - } -} - -/** - * Converts a document to a content relationship field. - * - * @typeParam TDocuments - Type of Prismic documents in the repository. - * - * @param document - Document to convert. - * - * @returns Equivalent content relationship. - */ -export const contentRelationship = < - TDocuments extends PrismicDocument = PrismicDocument, ->( - document: - | TDocuments - | (Omit & { id: string }), -): FilledContentRelationshipField => { - return { - link_type: LinkType.Document, - id: document.id, - uid: document.uid || undefined, - type: document.type, - tags: document.tags || [], - lang: document.lang, - url: undefined, - slug: undefined, - isBroken: false, - data: undefined, - } -} diff --git a/src/types/migration/ContentRelationship.ts b/src/types/migration/ContentRelationship.ts new file mode 100644 index 00000000..f9c0eff3 --- /dev/null +++ b/src/types/migration/ContentRelationship.ts @@ -0,0 +1,55 @@ +import type { FilledContentRelationshipField } from "../value/contentRelationship" +import type { PrismicDocument } from "../value/document" +import { LinkType } from "../value/link" + +import type { DocumentMap, MigrationDocument } from "./Document" +import { MigrationField } from "./Field" + +type MigrationContentRelationshipConfig = + | PrismicDocument + | MigrationDocument + | FilledContentRelationshipField + | undefined + +export type UnresolvedMigrationContentRelationshipConfig = + | MigrationContentRelationshipConfig + | (() => + | Promise + | MigrationContentRelationshipConfig) + +export class MigrationContentRelationship extends MigrationField { + unresolvedConfig: UnresolvedMigrationContentRelationshipConfig + + constructor( + unresolvedConfig: UnresolvedMigrationContentRelationshipConfig, + initialField?: FilledContentRelationshipField, + ) { + super(initialField) + + this.unresolvedConfig = unresolvedConfig + } + + async _prepare({ documents }: { documents: DocumentMap }): Promise { + const config = + typeof this.unresolvedConfig === "function" + ? await this.unresolvedConfig() + : this.unresolvedConfig + + if (config) { + const document = + "id" in config ? documents.get(config.id) : documents.get(config) + + if (document) { + this._field = { + link_type: LinkType.Document, + id: document.id, + uid: document.uid || undefined, + type: document.type, + tags: document.tags || [], + lang: document.lang, + isBroken: false, + } + } + } + } +} diff --git a/src/types/migration/Field.ts b/src/types/migration/Field.ts new file mode 100644 index 00000000..b70926a8 --- /dev/null +++ b/src/types/migration/Field.ts @@ -0,0 +1,48 @@ +import type { AnyRegularField } from "../value/types" + +import type { RTBlockNode, RTInlineNode } from "../value/richText" + +import type { AssetMap } from "./Asset" +import type { DocumentMap } from "./Document" + +interface Preparable { + /** + * @internal + */ + _prepare(args: { + assets: AssetMap + documents: DocumentMap + }): Promise | void +} + +export abstract class MigrationField< + TField extends AnyRegularField | RTBlockNode | RTInlineNode = + | AnyRegularField + | RTBlockNode + | RTInlineNode, + TInitialField = TField, +> implements Preparable +{ + /** + * @internal + */ + _field: TField | undefined + + /** + * @internal + */ + _initialField: TInitialField | undefined + + constructor(initialField?: TInitialField) { + this._initialField = initialField + } + + toJSON(): TField | undefined { + return this._field + } + + abstract _prepare(args: { + assets: AssetMap + documents: DocumentMap + }): Promise | void +} diff --git a/src/types/migration/asset.ts b/src/types/migration/asset.ts index f71ddd82..1bf3d934 100644 --- a/src/types/migration/asset.ts +++ b/src/types/migration/asset.ts @@ -1,7 +1,61 @@ +import type { Asset } from "../api/asset/asset" +import type { FilledImageFieldImage, ImageField } from "../value/image" +import { type FilledLinkToWebField, LinkType } from "../value/link" +import type { LinkToMediaField } from "../value/linkToMedia" +import { type RTImageNode, RichTextNodeType } from "../value/richText" + +import type { MigrationContentRelationship } from "./ContentRelationship" +import type { DocumentMap } from "./Document" +import { MigrationField } from "./Field" + +/** + * Converts an asset to an image field. + * + * @param asset - Asset to convert. + * @param maybeInitialField - Initial image field if available, used to preserve + * edits. + * + * @returns Equivalent image field. + */ +const assetToImage = ( + asset: Asset, + maybeInitialField?: + | FilledImageFieldImage + | LinkToMediaField<"filled"> + | RTImageNode, +): FilledImageFieldImage => { + const parameters = (maybeInitialField?.url || asset.url).split("?")[1] + const url = `${asset.url.split("?")[0]}${parameters ? `?${parameters}` : ""}` + const dimensions: FilledImageFieldImage["dimensions"] = { + width: asset.width!, + height: asset.height!, + } + const edit: FilledImageFieldImage["edit"] = + maybeInitialField && "edit" in maybeInitialField + ? maybeInitialField?.edit + : { x: 0, y: 0, zoom: 1, background: "transparent" } + + const alt = + (maybeInitialField && "alt" in maybeInitialField + ? maybeInitialField.alt + : undefined) || + asset.alt || + null + + return { + id: asset.id, + url, + dimensions, + edit, + alt: alt, + copyright: asset.credits || null, + } +} + /** * An asset to be uploaded to Prismic media library. */ -export type MigrationAsset = { +export type MigrationAssetConfig = { /** * ID of the asset used to reference it in Prismic documents. * @@ -43,3 +97,136 @@ export type MigrationAsset = { */ tags?: string[] } + +export abstract class MigrationAsset< + TField extends + | FilledImageFieldImage + | LinkToMediaField<"filled"> + | RTImageNode = + | FilledImageFieldImage + | LinkToMediaField<"filled"> + | RTImageNode, +> extends MigrationField< + TField, + FilledImageFieldImage | LinkToMediaField<"filled"> | RTImageNode +> { + config: MigrationAssetConfig + + constructor( + config: MigrationAssetConfig, + initialField?: + | FilledImageFieldImage + | LinkToMediaField<"filled"> + | RTImageNode, + ) { + super(initialField) + + this.config = config + } + + asImage(): MigrationImage { + return new MigrationImage(this.config, this._initialField) + } + + asLinkToMedia(): MigrationLinkToMedia { + return new MigrationLinkToMedia(this.config, this._initialField) + } + + asRTImageNode(): MigrationRTImageNode { + return new MigrationRTImageNode(this.config, this._initialField) + } +} + +export class MigrationImage extends MigrationAsset { + #thumbnails: Record = {} + + addThumbnail(name: string, thumbnail: MigrationImage): void { + this.#thumbnails[name] = thumbnail + } + + async _prepare({ + assets, + documents, + }: { + assets: AssetMap + documents: DocumentMap + }): Promise { + const asset = assets.get(this.config.id) + + if (asset) { + this._field = assetToImage(asset, this._initialField) + + for (const name in this.#thumbnails) { + await this.#thumbnails[name]._prepare({ assets, documents }) + + const thumbnail = this.#thumbnails[name]._field + if (thumbnail) { + ;(this._field as ImageField)[name] = thumbnail + } + } + } + } +} + +export class MigrationLinkToMedia extends MigrationAsset< + LinkToMediaField<"filled"> +> { + _prepare({ assets }: { assets: AssetMap }): void { + const asset = assets.get(this.config.id) + + if (asset) { + this._field = { + id: asset.id, + link_type: LinkType.Media, + name: asset.filename, + kind: asset.kind, + url: asset.url, + size: `${asset.size}`, + height: + typeof asset.height === "number" ? `${asset.height}` : undefined, + width: typeof asset.width === "number" ? `${asset.width}` : undefined, + } + } + } +} + +export class MigrationRTImageNode extends MigrationAsset { + linkTo: + | MigrationLinkToMedia + | MigrationContentRelationship + | FilledLinkToWebField + | undefined + + async _prepare({ + assets, + documents, + }: { + assets: AssetMap + documents: DocumentMap + }): Promise { + const asset = assets.get(this.config.id) + + if (this.linkTo instanceof MigrationField) { + await this.linkTo._prepare({ assets, documents }) + } + + if (asset) { + this._field = { + ...assetToImage(asset, this._initialField), + type: RichTextNodeType.image, + linkTo: + this.linkTo instanceof MigrationField + ? this.linkTo._field + : this.linkTo, + } + } + } +} + +/** + * A map of asset IDs to asset used to resolve assets when patching migration + * Prismic documents. + * + * @internal + */ +export type AssetMap = Map diff --git a/src/types/migration/document.ts b/src/types/migration/document.ts index 4d030122..aa381c77 100644 --- a/src/types/migration/document.ts +++ b/src/types/migration/document.ts @@ -1,13 +1,15 @@ import type { AnyRegularField } from "../value/types" +import type { FilledContentRelationshipField } from "../value/contentRelationship" import type { PrismicDocument } from "../value/document" import type { GroupField } from "../value/group" import type { ImageField } from "../value/image" -import type { LinkField } from "../value/link" +import type { FilledLinkToMediaField } from "../value/linkToMedia" import type { RTBlockNode, RTImageNode, RTInlineNode, + RTLinkNode, RTTextNode, RichTextField, } from "../value/richText" @@ -15,114 +17,147 @@ import type { SharedSlice } from "../value/sharedSlice" import type { Slice } from "../value/slice" import type { SliceZone } from "../value/sliceZone" -import type { ImageMigrationField, LinkMigrationField } from "./fields" -import type { RTImageMigrationNode, RTLinkMigrationNode } from "./richText" +import type { + AssetMap, + MigrationImage, + MigrationLinkToMedia, + MigrationRTImageNode, +} from "./Asset" +import type { + MigrationContentRelationship, + UnresolvedMigrationContentRelationshipConfig, +} from "./ContentRelationship" +import type { MigrationField } from "./Field" /** - * A utility type that converts a rich text text node to a rich text text - * migration node. + * A utility type that extends Rich text field node's spans with their migration + * node equivalent. * * @typeParam TRTNode - Rich text text node type to convert. */ -type RichTextTextNodeToMigrationField = Omit< - TRTNode, - "spans" -> & { - spans: (RTInlineNode | RTLinkMigrationNode)[] +type RichTextTextNodeWithMigrationField< + TRTNode extends RTTextNode = RTTextNode, +> = Omit & { + spans: ( + | RTInlineNode + | (Omit & { + data: + | MigrationLinkToMedia + | MigrationContentRelationship + | UnresolvedMigrationContentRelationshipConfig + }) + )[] } /** - * A utility type that converts a rich text block node to a rich text block - * migration node. + * A utility type that extends a Rich text field node with their migration node + * equivalent. * * @typeParam TRTNode - Rich text block node type to convert. */ -type RichTextBlockNodeToMigrationField = - TRTNode extends RTImageNode - ? - | RTImageMigrationNode - | (Omit & { - linkTo?: RTImageNode["linkTo"] | LinkMigrationField - }) - : TRTNode extends RTTextNode - ? RichTextTextNodeToMigrationField - : TRTNode +export type RichTextBlockNodeWithMigrationField< + TRTNode extends RTBlockNode = RTBlockNode, +> = TRTNode extends RTImageNode + ? RTImageNode | MigrationRTImageNode + : TRTNode extends RTTextNode + ? RichTextTextNodeWithMigrationField + : TRTNode /** - * A utility type that converts a rich text field to a rich text migration - * field. + * A utility type that extends a Rich text field's nodes with their migration + * node equivalent. * * @typeParam TField - Rich text field type to convert. */ -export type RichTextFieldToMigrationField = { - [Index in keyof TField]: RichTextBlockNodeToMigrationField +export type RichTextFieldWithMigrationField< + TField extends RichTextField = RichTextField, +> = { + [Index in keyof TField]: RichTextBlockNodeWithMigrationField } /** - * A utility type that converts a regular field to a regular migration field. + * A utility type that extends a regular field with their migration field + * equivalent. * * @typeParam TField - Regular field type to convert. */ -type RegularFieldToMigrationField = +type RegularFieldWithMigrationField< + TField extends AnyRegularField = AnyRegularField, +> = | (TField extends ImageField - ? ImageMigrationField - : TField extends LinkField - ? LinkMigrationField - : TField extends RichTextField - ? RichTextFieldToMigrationField - : never) + ? MigrationImage | undefined + : TField extends FilledLinkToMediaField + ? MigrationLinkToMedia | undefined + : TField extends FilledContentRelationshipField + ? + | MigrationContentRelationship + | UnresolvedMigrationContentRelationshipConfig + | undefined + : TField extends RichTextField + ? RichTextFieldWithMigrationField + : never) | TField /** - * A utility type that converts a group field to a group migration field. + * A utility type that extends a group's fields with their migration fields + * equivalent. * * @typeParam TField - Group field type to convert. */ -export type GroupFieldToMigrationField< - TField extends GroupField | Slice["items"] | SharedSlice["items"], -> = FieldsToMigrationFields[] - -type SliceToMigrationField = Omit< - TField, - "primary" | "items" -> & { - primary: FieldsToMigrationFields - items: GroupFieldToMigrationField +type GroupFieldWithMigrationField< + TField extends GroupField | Slice["items"] | SharedSlice["items"] = + | GroupField + | Slice["items"] + | SharedSlice["items"], +> = FieldsWithMigrationFields[] + +type SliceWithMigrationField< + TField extends Slice | SharedSlice = Slice | SharedSlice, +> = Omit & { + primary: FieldsWithMigrationFields + items: GroupFieldWithMigrationField } /** - * A utility type that converts a field to a migration field. + * A utility type that extends a SliceZone's slices fields with their migration + * fields equivalent. * * @typeParam TField - Field type to convert. */ -export type SliceZoneToMigrationField = - SliceToMigrationField[] +type SliceZoneWithMigrationField = + SliceWithMigrationField[] /** - * A utility type that converts a field to a migration field. + * A utility type that extends any field with their migration field equivalent. * * @typeParam TField - Field type to convert. */ -export type FieldToMigrationField< - TField extends AnyRegularField | GroupField | SliceZone, +export type FieldWithMigrationField< + TField extends AnyRegularField | GroupField | SliceZone = + | AnyRegularField + | GroupField + | SliceZone, > = TField extends AnyRegularField - ? RegularFieldToMigrationField + ? RegularFieldWithMigrationField : TField extends GroupField - ? GroupFieldToMigrationField + ? GroupFieldWithMigrationField : TField extends SliceZone - ? SliceZoneToMigrationField + ? SliceZoneWithMigrationField : never /** - * A utility type that converts a record of fields to a record of migration - * fields. + * A utility type that extends a record of fields with their migration fields + * equivalent. * * @typeParam TFields - Type of the record of Prismic fields. */ -export type FieldsToMigrationFields< - TFields extends Record, +export type FieldsWithMigrationFields< + TFields extends Record< + string, + AnyRegularField | GroupField | SliceZone + > = Record, > = { - [Key in keyof TFields]: FieldToMigrationField + [Key in keyof TFields]: FieldWithMigrationField } /** @@ -191,7 +226,7 @@ export type PrismicMigrationDocument< /** * Data contained in the document. */ - data: FieldsToMigrationFields + data: FieldsWithMigrationFields }> : never @@ -213,11 +248,52 @@ export type PrismicMigrationDocumentParams = { // it creates a circular reference to itself which makes TypeScript unhappy. // (but I think it's weird and it doesn't make sense :thinking:) masterLanguageDocument?: - | PrismicDocument - | PrismicMigrationDocument - | (() => - | Promise - | PrismicDocument - | PrismicMigrationDocument - | undefined) + | UnresolvedMigrationContentRelationshipConfig + | undefined +} + +export class MigrationDocument< + TDocument extends PrismicDocument = PrismicDocument, +> { + document: PrismicMigrationDocument + params: PrismicMigrationDocumentParams + dependencies: MigrationField[] + + constructor( + document: PrismicMigrationDocument, + params: PrismicMigrationDocumentParams, + dependencies: MigrationField[] = [], + ) { + this.document = document + this.params = params + this.dependencies = dependencies + } + + /** + * @internal + */ + async _prepare(args: { + assets: AssetMap + documents: DocumentMap + }): Promise { + for (const dependency of this.dependencies) { + await dependency._prepare(args) + } + } } + +/** + * A map of document IDs, documents, and migraiton documents to content + * relationship field used to resolve content relationships when patching + * migration Prismic documents. + * + * @typeParam TDocuments - Type of Prismic documents in the repository. + * + * @internal + */ +export type DocumentMap = + Map< + string | MigrationDocument | MigrationDocument, + | PrismicDocument + | (Omit, "id"> & { id: string }) + > diff --git a/src/types/migration/fields.ts b/src/types/migration/fields.ts deleted file mode 100644 index 6b83c53b..00000000 --- a/src/types/migration/fields.ts +++ /dev/null @@ -1,57 +0,0 @@ -// eslint-disable-next-line @typescript-eslint/no-unused-vars -import type { ContentRelationshipField } from "../value/contentRelationship" -import type { PrismicDocument } from "../value/document" -// eslint-disable-next-line @typescript-eslint/no-unused-vars -import type { ImageField } from "../value/image" -// eslint-disable-next-line @typescript-eslint/no-unused-vars -import type { LinkField } from "../value/link" -// eslint-disable-next-line @typescript-eslint/no-unused-vars -import type { LinkToMediaField } from "../value/linkToMedia" - -import type { MigrationAsset } from "./asset" -import type { PrismicMigrationDocument } from "./document" - -export const MigrationFieldType = { - Image: "image", - LinkToMedia: "linkToMedia", -} as const - -/** - * An alternate version of the {@link ImageField} for use with the Migration API. - */ -export type ImageMigrationField = - | (MigrationAsset & { - migrationType: typeof MigrationFieldType.Image - }) - | undefined - -/** - * An alternate version of the {@link LinkToMediaField} for use with the - * Migration API. - */ -export type LinkToMediaMigrationField = - | (MigrationAsset & { - migrationType: typeof MigrationFieldType.LinkToMedia - }) - | undefined - -/** - * An alternate version of the {@link ContentRelationshipField} for use with the - * Migration API. - */ -export type ContentRelationshipMigrationField = - | PrismicDocument - | PrismicMigrationDocument - | (() => - | Promise - | PrismicDocument - | PrismicMigrationDocument - | undefined) - | undefined - -/** - * An alternate version of the {@link LinkField} for use with the Migration API. - */ -export type LinkMigrationField = - | LinkToMediaMigrationField - | ContentRelationshipMigrationField diff --git a/src/types/migration/richText.ts b/src/types/migration/richText.ts deleted file mode 100644 index 9196ee88..00000000 --- a/src/types/migration/richText.ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { RTImageNode, RTLinkNode } from "../value/richText" - -import type { ImageMigrationField, LinkMigrationField } from "./fields" - -/** - * An alternate version of {@link RTLinkNode} that supports - * {@link LinkMigrationField} for use with the Migration API. - */ -export type RTLinkMigrationNode = Omit & { - data: LinkMigrationField -} - -/** - * An alternate version of {@link RTImageNode} that supports - * {@link ImageMigrationField} for use with the Migration API. - */ -export type RTImageMigrationNode = ImageMigrationField & { - linkTo?: RTImageNode["linkTo"] | LinkMigrationField -} diff --git a/test/__snapshots__/writeClient-migrate-patch-link.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-contentRelationship.test.ts.snap similarity index 54% rename from test/__snapshots__/writeClient-migrate-patch-link.test.ts.snap rename to test/__snapshots__/writeClient-migrate-patch-contentRelationship.test.ts.snap index 114003d9..739bcda2 100644 --- a/test/__snapshots__/writeClient-migrate-patch-link.test.ts.snap +++ b/test/__snapshots__/writeClient-migrate-patch-contentRelationship.test.ts.snap @@ -3,19 +3,7 @@ exports[`patches link fields > brokenLink > group 1`] = ` { "group": [ - { - "field": { - "id": "adcdf90", - "isBroken": true, - "lang": "nulla", - "link_type": "Document", - "tags": [ - "Convallis Posuere", - ], - "type": "vitae", - "url": "https://ac.example", - }, - }, + {}, ], } `; @@ -26,42 +14,11 @@ exports[`patches link fields > brokenLink > shared slice 1`] = ` { "id": "2ff58c741ba", "items": [ - { - "field": { - "id": "9be9130", - "isBroken": true, - "lang": "faucibus", - "link_type": "Document", - "tags": [ - "Ultrices", - ], - "type": "egestas_integer", - "url": "https://scelerisque.example", - }, - }, + {}, ], "primary": { - "field": { - "id": "06ad9e3", - "isBroken": true, - "lang": "fringilla", - "link_type": "Document", - "tags": [], - "type": "amet", - "url": "https://nec.example", - }, "group": [ - { - "field": { - "id": "bd0255b", - "isBroken": true, - "lang": "neque", - "link_type": "Document", - "tags": [], - "type": "lacus", - "url": "https://cras.example", - }, - }, + {}, ], }, "slice_label": null, @@ -79,29 +36,9 @@ exports[`patches link fields > brokenLink > slice 1`] = ` { "id": "e93cc3e4f28", "items": [ - { - "field": { - "id": "5ec2f4d", - "isBroken": true, - "lang": "enim,", - "link_type": "Document", - "tags": [], - "type": "felis_donec", - "url": "https://sem.example", - }, - }, + {}, ], - "primary": { - "field": { - "id": "31a1bd2", - "isBroken": true, - "lang": "ac", - "link_type": "Document", - "tags": [], - "type": "enim_praesent", - "url": "https://sit.example", - }, - }, + "primary": {}, "slice_label": "Pellentesque", "slice_type": "nulla_posuere", }, @@ -109,19 +46,7 @@ exports[`patches link fields > brokenLink > slice 1`] = ` } `; -exports[`patches link fields > brokenLink > static zone 1`] = ` -{ - "field": { - "id": "2adafa9", - "isBroken": true, - "lang": "elementum", - "link_type": "Document", - "tags": [], - "type": "aenean", - "url": "https://volutpat.example", - }, -} -`; +exports[`patches link fields > brokenLink > static zone 1`] = `{}`; exports[`patches link fields > existing > group 1`] = ` { @@ -743,13 +668,13 @@ exports[`patches link fields > otherRepositoryContentRelationship > group 1`] = "field": { "id": "id-migration", "isBroken": false, - "lang": "nulla", + "lang": "a", "link_type": "Document", "tags": [ - "Convallis Posuere", + "Odio Eu", ], - "type": "vitae", - "url": "https://ac.example", + "type": "amet", + "uid": "Non sodales neque", }, }, ], @@ -766,13 +691,11 @@ exports[`patches link fields > otherRepositoryContentRelationship > shared slice "field": { "id": "id-migration", "isBroken": false, - "lang": "faucibus", + "lang": "gravida", "link_type": "Document", - "tags": [ - "Ultrices", - ], - "type": "egestas_integer", - "url": "https://scelerisque.example", + "tags": [], + "type": "leo", + "uid": "Blandit volutpat maecenas", }, }, ], @@ -780,22 +703,22 @@ exports[`patches link fields > otherRepositoryContentRelationship > shared slice "field": { "id": "id-migration", "isBroken": false, - "lang": "fringilla", + "lang": "gravida", "link_type": "Document", "tags": [], - "type": "amet", - "url": "https://nec.example", + "type": "leo", + "uid": "Blandit volutpat maecenas", }, "group": [ { "field": { "id": "id-migration", "isBroken": false, - "lang": "neque", + "lang": "gravida", "link_type": "Document", "tags": [], - "type": "lacus", - "url": "https://cras.example", + "type": "leo", + "uid": "Blandit volutpat maecenas", }, }, ], @@ -819,11 +742,13 @@ exports[`patches link fields > otherRepositoryContentRelationship > slice 1`] = "field": { "id": "id-migration", "isBroken": false, - "lang": "enim,", + "lang": "ultrices", "link_type": "Document", - "tags": [], - "type": "felis_donec", - "url": "https://sem.example", + "tags": [ + "Ipsum Consequat", + ], + "type": "eget", + "uid": "Amet dictum sit", }, }, ], @@ -831,11 +756,13 @@ exports[`patches link fields > otherRepositoryContentRelationship > slice 1`] = "field": { "id": "id-migration", "isBroken": false, - "lang": "ac", + "lang": "ultrices", "link_type": "Document", - "tags": [], - "type": "enim_praesent", - "url": "https://sit.example", + "tags": [ + "Ipsum Consequat", + ], + "type": "eget", + "uid": "Amet dictum sit", }, }, "slice_label": "Pellentesque", @@ -850,11 +777,11 @@ exports[`patches link fields > otherRepositoryContentRelationship > static zone "field": { "id": "id-migration", "isBroken": false, - "lang": "elementum", + "lang": "nisi,", "link_type": "Document", "tags": [], - "type": "aenean", - "url": "https://volutpat.example", + "type": "eros", + "uid": "Scelerisque mauris pellentesque", }, } `; @@ -1106,583 +1033,3 @@ exports[`patches link fields > richTextLinkNode > static zone 1`] = ` ], } `; - -exports[`patches link fields > richTextNewImageNodeLink > group 1`] = ` -{ - "group": [ - { - "field": [ - { - "spans": [], - "text": "Nulla Aliquet Porttitor Lacus Luctus", - "type": "heading2", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "cdfdd322ca9", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "laoreet", - "link_type": "Document", - "tags": [], - "type": "orci", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c?w=6000&h=4000&fit=crop", - }, - ], - }, - ], -} -`; - -exports[`patches link fields > richTextNewImageNodeLink > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Eu Mi", - "type": "heading3", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "tortor,", - "link_type": "Document", - "tags": [ - "Risus In", - ], - "type": "dui", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", - "type": "heading3", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "tortor,", - "link_type": "Document", - "tags": [ - "Risus In", - ], - "type": "dui", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - "group": [ - { - "field": [ - { - "spans": [], - "text": "Arcu", - "type": "heading1", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "tortor,", - "link_type": "Document", - "tags": [ - "Risus In", - ], - "type": "dui", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches link fields > richTextNewImageNodeLink > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Habitasse platea dictumst quisque sagittis purus sit", - "type": "o-list-item", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "7963dc361cc", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "sapien", - "link_type": "Document", - "tags": [ - "Aenean", - ], - "type": "amet", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Ac Orci Phasellus Egestas Tellus", - "type": "heading5", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "7963dc361cc", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "sapien", - "link_type": "Document", - "tags": [ - "Aenean", - ], - "type": "amet", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches link fields > richTextNewImageNodeLink > static zone 1`] = ` -{ - "field": [ - { - "spans": [], - "text": "Elementum Integer", - "type": "heading6", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "0bbad670dad", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "eget", - "link_type": "Document", - "tags": [], - "type": "phasellus", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - }, - ], -} -`; - -exports[`patches link fields > richTextOtherReposiotryImageNodeLink > group 1`] = ` -{ - "group": [ - { - "field": [ - { - "spans": [], - "text": "Nulla Aliquet Porttitor Lacus Luctus", - "type": "heading2", - }, - { - "alt": "Donec enim diam vulputate ut pharetra sit amet aliquam id diam", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#7cfc26", - "x": 2977, - "y": -1163, - "zoom": 1.0472585898934068, - }, - "id": "e8d0985c099", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "laoreet", - "link_type": "Document", - "tags": [], - "type": "orci", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b", - }, - ], - }, - ], -} -`; - -exports[`patches link fields > richTextOtherReposiotryImageNodeLink > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Facilisis", - "type": "heading3", - }, - { - "alt": "Nullam vehicula ipsum a arcu cursus vitae congue mauris rhoncus aenean vel elit scelerisque", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#37ae3f", - "x": -1505, - "y": 902, - "zoom": 1.8328975606320652, - }, - "id": "3fc0dfa9fe9", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "tortor,", - "link_type": "Document", - "tags": [ - "Risus In", - ], - "type": "dui", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", - "type": "heading3", - }, - { - "alt": "Proin libero nunc consequat interdum varius", - "copyright": null, - "dimensions": { - "height": 3036, - "width": 4554, - }, - "edit": { - "background": "#904f4f", - "x": 1462, - "y": 1324, - "zoom": 1.504938844941775, - }, - "id": "3fc0dfa9fe9", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "tortor,", - "link_type": "Document", - "tags": [ - "Risus In", - ], - "type": "dui", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - "group": [ - { - "field": [ - { - "spans": [], - "text": "Egestas Integer Eget Aliquet Nibh", - "type": "heading5", - }, - { - "alt": "Arcu cursus vitae congue mauris", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#5f7a9b", - "x": -119, - "y": -2667, - "zoom": 1.9681315715350518, - }, - "id": "3fc0dfa9fe9", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "tortor,", - "link_type": "Document", - "tags": [ - "Risus In", - ], - "type": "dui", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches link fields > richTextOtherReposiotryImageNodeLink > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Amet", - "type": "heading5", - }, - { - "alt": "Auctor neque vitae tempus quam", - "copyright": null, - "dimensions": { - "height": 1440, - "width": 2560, - }, - "edit": { - "background": "#5bc5aa", - "x": -1072, - "y": -281, - "zoom": 1.3767766101231744, - }, - "id": "f70ca27104d", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "sapien", - "link_type": "Document", - "tags": [ - "Aenean", - ], - "type": "amet", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Ac Orci Phasellus Egestas Tellus", - "type": "heading5", - }, - { - "alt": "Urna id volutpat lacus laoreet non curabitur gravida arcu ac tortor", - "copyright": null, - "dimensions": { - "height": 3036, - "width": 4554, - }, - "edit": { - "background": "#4860cb", - "x": 280, - "y": -379, - "zoom": 1.2389796902982004, - }, - "id": "f70ca27104d", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "sapien", - "link_type": "Document", - "tags": [ - "Aenean", - ], - "type": "amet", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", - }, - ], - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches link fields > richTextOtherReposiotryImageNodeLink > static zone 1`] = ` -{ - "field": [ - { - "spans": [], - "text": "Elementum Integer", - "type": "heading6", - }, - { - "alt": "Interdum velit euismod in pellentesque", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#ab1d17", - "x": 3605, - "y": 860, - "zoom": 1.9465488211593005, - }, - "id": "04a95cc61c3", - "linkTo": { - "id": "id-existing", - "isBroken": false, - "lang": "eget", - "link_type": "Document", - "tags": [], - "type": "phasellus", - }, - "type": "image", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5", - }, - ], -} -`; diff --git a/test/__snapshots__/writeClient-migrate-patch-image.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-image.test.ts.snap index daa3e7f0..1a477d27 100644 --- a/test/__snapshots__/writeClient-migrate-patch-image.test.ts.snap +++ b/test/__snapshots__/writeClient-migrate-patch-image.test.ts.snap @@ -15,7 +15,7 @@ exports[`patches image fields > existing > group 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "id-existing", "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", @@ -43,7 +43,7 @@ exports[`patches image fields > existing > shared slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "id-existing", "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", @@ -62,7 +62,7 @@ exports[`patches image fields > existing > shared slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "id-existing", "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", @@ -80,7 +80,7 @@ exports[`patches image fields > existing > shared slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "id-existing", "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", @@ -115,7 +115,7 @@ exports[`patches image fields > existing > slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "id-existing", "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", @@ -134,7 +134,7 @@ exports[`patches image fields > existing > slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "id-existing", "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", @@ -160,7 +160,7 @@ exports[`patches image fields > existing > static zone 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "id-existing", "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?w=4335&h=6502&fit=crop", @@ -183,7 +183,7 @@ exports[`patches image fields > new > group 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "ef95d5daa4d", "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=2560&h=1705&fit=crop", @@ -211,7 +211,7 @@ exports[`patches image fields > new > shared slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "f2c3f8bfc90", "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", @@ -230,7 +230,7 @@ exports[`patches image fields > new > shared slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "f2c3f8bfc90", "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", @@ -248,7 +248,7 @@ exports[`patches image fields > new > shared slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "f2c3f8bfc90", "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", @@ -283,7 +283,7 @@ exports[`patches image fields > new > slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "45306297c5e", "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", @@ -302,7 +302,7 @@ exports[`patches image fields > new > slice 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "45306297c5e", "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", @@ -328,7 +328,7 @@ exports[`patches image fields > new > static zone 1`] = ` "background": "transparent", "x": 0, "y": 0, - "zoom": 0, + "zoom": 1, }, "id": "c5c95f8d3ac", "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", @@ -344,8 +344,8 @@ exports[`patches image fields > otherRepository > group 1`] = ` "alt": "Odio ut enim blandit volutpat maecenas volutpat blandit", "copyright": null, "dimensions": { - "height": 1705, - "width": 2560, + "height": 1, + "width": 1, }, "edit": { "background": "#fdb338", @@ -372,8 +372,8 @@ exports[`patches image fields > otherRepository > shared slice 1`] = ` "alt": "Vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum", "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#61adcf", @@ -391,8 +391,8 @@ exports[`patches image fields > otherRepository > shared slice 1`] = ` "alt": "Ut consequat semper viverra nam libero justo laoreet", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#8efd5a", @@ -409,8 +409,8 @@ exports[`patches image fields > otherRepository > shared slice 1`] = ` "alt": "Porttitor massa id neque aliquam vestibulum morbi blandit", "copyright": null, "dimensions": { - "height": 4392, - "width": 7372, + "height": 1, + "width": 1, }, "edit": { "background": "#bc3178", @@ -444,8 +444,8 @@ exports[`patches image fields > otherRepository > slice 1`] = ` "alt": "Vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis", "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#c361cc", @@ -463,8 +463,8 @@ exports[`patches image fields > otherRepository > slice 1`] = ` "alt": "Nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc consequat interdum varius sit", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#55e3be", @@ -489,8 +489,8 @@ exports[`patches image fields > otherRepository > static zone 1`] = ` "alt": "Vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#97acc6", @@ -508,7 +508,13 @@ exports[`patches image fields > otherRepositoryEmpty > group 1`] = ` { "group": [ { - "field": {}, + "field": { + "alt": null, + "copyright": null, + "dimensions": null, + "id": null, + "url": null, + }, }, ], } @@ -521,14 +527,32 @@ exports[`patches image fields > otherRepositoryEmpty > shared slice 1`] = ` "id": "2ff58c741ba", "items": [ { - "field": {}, + "field": { + "alt": null, + "copyright": null, + "dimensions": null, + "id": null, + "url": null, + }, }, ], "primary": { - "field": {}, + "field": { + "alt": null, + "copyright": null, + "dimensions": null, + "id": null, + "url": null, + }, "group": [ { - "field": {}, + "field": { + "alt": null, + "copyright": null, + "dimensions": null, + "id": null, + "url": null, + }, }, ], }, @@ -548,11 +572,23 @@ exports[`patches image fields > otherRepositoryEmpty > slice 1`] = ` "id": "e93cc3e4f28", "items": [ { - "field": {}, + "field": { + "alt": null, + "copyright": null, + "dimensions": null, + "id": null, + "url": null, + }, }, ], "primary": { - "field": {}, + "field": { + "alt": null, + "copyright": null, + "dimensions": null, + "id": null, + "url": null, + }, }, "slice_label": "Pellentesque", "slice_type": "nulla_posuere", @@ -563,7 +599,13 @@ exports[`patches image fields > otherRepositoryEmpty > slice 1`] = ` exports[`patches image fields > otherRepositoryEmpty > static zone 1`] = ` { - "field": {}, + "field": { + "alt": null, + "copyright": null, + "dimensions": null, + "id": null, + "url": null, + }, } `; @@ -575,8 +617,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > group 1`] = ` "alt": "Odio ut enim blandit volutpat maecenas volutpat blandit", "copyright": null, "dimensions": { - "height": 1705, - "width": 2560, + "height": 1, + "width": 1, }, "edit": { "background": "#fdb338", @@ -589,8 +631,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > group 1`] = ` "alt": "Odio ut enim blandit volutpat maecenas volutpat blandit", "copyright": null, "dimensions": { - "height": 1705, - "width": 2560, + "height": 1, + "width": 1, }, "edit": { "background": "#fdb338", @@ -619,8 +661,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > shared slice 1`] "alt": "Vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum", "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#61adcf", @@ -633,8 +675,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > shared slice 1`] "alt": "Vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum", "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#61adcf", @@ -654,8 +696,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > shared slice 1`] "alt": "Ut consequat semper viverra nam libero justo laoreet", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#8efd5a", @@ -668,8 +710,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > shared slice 1`] "alt": "Ut consequat semper viverra nam libero justo laoreet", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#8efd5a", @@ -688,8 +730,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > shared slice 1`] "alt": "Porttitor massa id neque aliquam vestibulum morbi blandit", "copyright": null, "dimensions": { - "height": 4392, - "width": 7372, + "height": 1, + "width": 1, }, "edit": { "background": "#bc3178", @@ -702,8 +744,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > shared slice 1`] "alt": "Porttitor massa id neque aliquam vestibulum morbi blandit", "copyright": null, "dimensions": { - "height": 4392, - "width": 7372, + "height": 1, + "width": 1, }, "edit": { "background": "#bc3178", @@ -739,8 +781,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > slice 1`] = ` "alt": "Vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis", "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#c361cc", @@ -753,8 +795,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > slice 1`] = ` "alt": "Vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis", "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#c361cc", @@ -774,8 +816,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > slice 1`] = ` "alt": "Nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc consequat interdum varius sit", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#55e3be", @@ -788,8 +830,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > slice 1`] = ` "alt": "Nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc consequat interdum varius sit", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#55e3be", @@ -816,8 +858,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > static zone 1`] "alt": "Vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#97acc6", @@ -830,8 +872,8 @@ exports[`patches image fields > otherRepositoryWithThumbnails > static zone 1`] "alt": "Vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed", "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#97acc6", @@ -855,8 +897,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > group 1`] = "alt": null, "copyright": null, "dimensions": { - "height": 1705, - "width": 2560, + "height": 1, + "width": 1, }, "edit": { "background": "#fdb338", @@ -869,8 +911,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > group 1`] = "alt": null, "copyright": null, "dimensions": { - "height": 1705, - "width": 2560, + "height": 1, + "width": 1, }, "edit": { "background": "#fdb338", @@ -899,8 +941,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > shared slic "alt": null, "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#61adcf", @@ -913,8 +955,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > shared slic "alt": null, "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#61adcf", @@ -934,8 +976,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > shared slic "alt": null, "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#8efd5a", @@ -948,8 +990,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > shared slic "alt": null, "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#8efd5a", @@ -968,8 +1010,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > shared slic "alt": null, "copyright": null, "dimensions": { - "height": 4392, - "width": 7372, + "height": 1, + "width": 1, }, "edit": { "background": "#bc3178", @@ -982,8 +1024,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > shared slic "alt": null, "copyright": null, "dimensions": { - "height": 4392, - "width": 7372, + "height": 1, + "width": 1, }, "edit": { "background": "#bc3178", @@ -1019,8 +1061,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > slice 1`] = "alt": null, "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#c361cc", @@ -1033,8 +1075,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > slice 1`] = "alt": null, "copyright": null, "dimensions": { - "height": 4000, - "width": 6000, + "height": 1, + "width": 1, }, "edit": { "background": "#c361cc", @@ -1054,8 +1096,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > slice 1`] = "alt": null, "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#55e3be", @@ -1068,8 +1110,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > slice 1`] = "alt": null, "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#55e3be", @@ -1096,8 +1138,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > static zone "alt": null, "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#97acc6", @@ -1110,8 +1152,8 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > static zone "alt": null, "copyright": null, "dimensions": { - "height": 2832, - "width": 4240, + "height": 1, + "width": 1, }, "edit": { "background": "#97acc6", @@ -1127,123 +1169,155 @@ exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > static zone } `; -exports[`patches image fields > richTextExisting > group 1`] = ` +exports[`patches image fields > otherRepositoryWithTypeThumbnail > group 1`] = ` { "group": [ { - "field": [ - { - "spans": [], - "text": "Nulla Aliquet Porttitor Lacus Luctus", - "type": "heading2", + "field": { + "alt": "Odio ut enim blandit volutpat maecenas volutpat blandit", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, }, - { - "alt": null, + "edit": { + "background": "#fdb338", + "x": 349, + "y": 115, + "zoom": 1.5951464943576479, + }, + "id": "4dcf07cfc26", + "type": { + "alt": "Odio ut enim blandit volutpat maecenas volutpat blandit", "copyright": null, "dimensions": { "height": 1, "width": 1, }, "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, + "background": "#fdb338", + "x": 349, + "y": 115, + "zoom": 1.5951464943576479, }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", + "id": "4dcf07cfc26", + "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?some=other&query=params", }, - ], + "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?some=query", + }, }, ], } `; -exports[`patches image fields > richTextExisting > shared slice 1`] = ` +exports[`patches image fields > otherRepositoryWithTypeThumbnail > shared slice 1`] = ` { "slices": [ { "id": "2ff58c741ba", "items": [ { - "field": [ - { - "spans": [], - "text": "Eu Mi", - "type": "heading3", + "field": { + "alt": "Vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, }, - { - "alt": null, + "edit": { + "background": "#61adcf", + "x": -1586, + "y": -2819, + "zoom": 1.9393723758262054, + }, + "id": "f5dbf0d9ed2", + "type": { + "alt": "Vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum", "copyright": null, "dimensions": { "height": 1, "width": 1, }, "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, + "background": "#61adcf", + "x": -1586, + "y": -2819, + "zoom": 1.9393723758262054, }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", + "id": "f5dbf0d9ed2", + "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", }, - ], + "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", + }, }, ], "primary": { - "field": [ - { - "spans": [], - "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", - "type": "heading3", + "field": { + "alt": "Ut consequat semper viverra nam libero justo laoreet", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, }, - { - "alt": null, + "edit": { + "background": "#8efd5a", + "x": 466, + "y": -571, + "zoom": 1.394663850868741, + }, + "id": "f5dbf0d9ed2", + "type": { + "alt": "Ut consequat semper viverra nam libero justo laoreet", "copyright": null, "dimensions": { "height": 1, "width": 1, }, "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, + "background": "#8efd5a", + "x": 466, + "y": -571, + "zoom": 1.394663850868741, }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", + "id": "f5dbf0d9ed2", + "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", }, - ], + "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", + }, "group": [ { - "field": [ - { - "spans": [], - "text": "Arcu", - "type": "heading1", + "field": { + "alt": "Porttitor massa id neque aliquam vestibulum morbi blandit", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, }, - { - "alt": null, + "edit": { + "background": "#bc3178", + "x": 1156, + "y": -2223, + "zoom": 1.602826201962965, + }, + "id": "f5dbf0d9ed2", + "type": { + "alt": "Porttitor massa id neque aliquam vestibulum morbi blandit", "copyright": null, "dimensions": { "height": 1, "width": 1, }, "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, + "background": "#bc3178", + "x": 1156, + "y": -2223, + "zoom": 1.602826201962965, }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", + "id": "f5dbf0d9ed2", + "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", }, - ], + "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", + }, }, ], }, @@ -1256,288 +1330,80 @@ exports[`patches image fields > richTextExisting > shared slice 1`] = ` } `; -exports[`patches image fields > richTextExisting > slice 1`] = ` +exports[`patches image fields > otherRepositoryWithTypeThumbnail > slice 1`] = ` { "slices": [ { "id": "e93cc3e4f28", "items": [ { - "field": [ - { - "spans": [], - "text": "Habitasse platea dictumst quisque sagittis purus sit", - "type": "o-list-item", + "field": { + "alt": "Vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, }, - { - "alt": null, + "edit": { + "background": "#c361cc", + "x": -751, + "y": -404, + "zoom": 1.6465649620272602, + }, + "id": "4f4a69ff72d", + "type": { + "alt": "Vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis", "copyright": null, "dimensions": { "height": 1, "width": 1, }, "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, + "background": "#c361cc", + "x": -751, + "y": -404, + "zoom": 1.6465649620272602, }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", + "id": "4f4a69ff72d", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=other&query=params", }, - ], + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=query", + }, }, ], "primary": { - "field": [ - { - "spans": [], - "text": "Ac Orci Phasellus Egestas Tellus", - "type": "heading5", + "field": { + "alt": "Nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc consequat interdum varius sit", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, }, - { - "alt": null, + "edit": { + "background": "#55e3be", + "x": -762, + "y": 991, + "zoom": 1.0207384987782544, + }, + "id": "4f4a69ff72d", + "type": { + "alt": "Nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc consequat interdum varius sit", "copyright": null, "dimensions": { "height": 1, "width": 1, }, "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, + "background": "#55e3be", + "x": -762, + "y": 991, + "zoom": 1.0207384987782544, }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", - }, - ], - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > richTextExisting > static zone 1`] = ` -{ - "field": [ - { - "spans": [], - "text": "Elementum Integer", - "type": "heading6", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?w=4335&h=6502&fit=crop", - }, - ], -} -`; - -exports[`patches image fields > richTextNew > group 1`] = ` -{ - "group": [ - { - "field": [ - { - "spans": [], - "text": "Nulla Aliquet Porttitor Lacus Luctus", - "type": "heading2", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, + "id": "4f4a69ff72d", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=other&query=params", }, - "id": "cdfdd322ca9", - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c?w=6000&h=4000&fit=crop", - }, - ], - }, - ], -} -`; - -exports[`patches image fields > richTextNew > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Eu Mi", - "type": "heading3", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=query", }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", - "type": "heading3", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - "group": [ - { - "field": [ - { - "spans": [], - "text": "Arcu", - "type": "heading1", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > richTextNew > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Habitasse platea dictumst quisque sagittis purus sit", - "type": "o-list-item", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "7963dc361cc", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Ac Orci Phasellus Egestas Tellus", - "type": "heading5", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "7963dc361cc", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], }, "slice_label": "Pellentesque", "slice_type": "nulla_posuere", @@ -1546,255 +1412,39 @@ exports[`patches image fields > richTextNew > slice 1`] = ` } `; -exports[`patches image fields > richTextNew > static zone 1`] = ` +exports[`patches image fields > otherRepositoryWithTypeThumbnail > static zone 1`] = ` { - "field": [ - { - "spans": [], - "text": "Elementum Integer", - "type": "heading6", + "field": { + "alt": "Vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, }, - { - "alt": null, + "edit": { + "background": "#97acc6", + "x": 1922, + "y": -967, + "zoom": 1.9765406541937358, + }, + "id": "9abffab1d17", + "type": { + "alt": "Vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed", "copyright": null, "dimensions": { "height": 1, "width": 1, }, "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "0bbad670dad", - "type": "image", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - }, - ], -} -`; - -exports[`patches image fields > richTextOtherRepository > group 1`] = ` -{ - "group": [ - { - "field": [ - { - "spans": [], - "text": "Nulla Aliquet Porttitor Lacus Luctus", - "type": "heading2", - }, - { - "alt": "Donec enim diam vulputate ut pharetra sit amet aliquam id diam", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#7cfc26", - "x": 2977, - "y": -1163, - "zoom": 1.0472585898934068, - }, - "id": "e8d0985c099", - "type": "image", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b", - }, - ], - }, - ], -} -`; - -exports[`patches image fields > richTextOtherRepository > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Facilisis", - "type": "heading3", - }, - { - "alt": "Nullam vehicula ipsum a arcu cursus vitae congue mauris rhoncus aenean vel elit scelerisque", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#37ae3f", - "x": -1505, - "y": 902, - "zoom": 1.8328975606320652, - }, - "id": "3fc0dfa9fe9", - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", - "type": "heading3", - }, - { - "alt": "Proin libero nunc consequat interdum varius", - "copyright": null, - "dimensions": { - "height": 3036, - "width": 4554, - }, - "edit": { - "background": "#904f4f", - "x": 1462, - "y": 1324, - "zoom": 1.504938844941775, - }, - "id": "3fc0dfa9fe9", - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - "group": [ - { - "field": [ - { - "spans": [], - "text": "Egestas Integer Eget Aliquet Nibh", - "type": "heading5", - }, - { - "alt": "Arcu cursus vitae congue mauris", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#5f7a9b", - "x": -119, - "y": -2667, - "zoom": 1.9681315715350518, - }, - "id": "3fc0dfa9fe9", - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > richTextOtherRepository > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Amet", - "type": "heading5", - }, - { - "alt": "Auctor neque vitae tempus quam", - "copyright": null, - "dimensions": { - "height": 1440, - "width": 2560, - }, - "edit": { - "background": "#5bc5aa", - "x": -1072, - "y": -281, - "zoom": 1.3767766101231744, - }, - "id": "f70ca27104d", - "type": "image", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Ac Orci Phasellus Egestas Tellus", - "type": "heading5", - }, - { - "alt": "Urna id volutpat lacus laoreet non curabitur gravida arcu ac tortor", - "copyright": null, - "dimensions": { - "height": 3036, - "width": 4554, - }, - "edit": { - "background": "#4860cb", - "x": 280, - "y": -379, - "zoom": 1.2389796902982004, - }, - "id": "f70ca27104d", - "type": "image", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", - }, - ], - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > richTextOtherRepository > static zone 1`] = ` -{ - "field": [ - { - "spans": [], - "text": "Elementum Integer", - "type": "heading6", - }, - { - "alt": "Interdum velit euismod in pellentesque", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#ab1d17", - "x": 3605, - "y": 860, - "zoom": 1.9465488211593005, + "background": "#97acc6", + "x": 1922, + "y": -967, + "zoom": 1.9765406541937358, }, - "id": "04a95cc61c3", - "type": "image", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5", + "id": "9abffab1d17", + "url": "https://images.unsplash.com/photo-1446329813274-7c9036bd9a1f?some=other&query=params", }, - ], + "url": "https://images.unsplash.com/photo-1446329813274-7c9036bd9a1f?some=query", + }, } `; diff --git a/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap index 7abdb040..2624fb3a 100644 --- a/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap +++ b/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap @@ -369,14 +369,14 @@ exports[`patches link to media fields > otherRepository > group 1`] = ` "group": [ { "field": { - "height": "1715", + "height": "1", "id": "fdb33894dcf", - "kind": "purus", + "kind": "image", "link_type": "Media", - "name": "nulla.example", - "size": "2039", - "url": "https://db0f6f37ebeb6ea09489124345af2a45.example.com/foo.png", - "width": "2091", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + "width": "1", }, }, ], @@ -391,39 +391,39 @@ exports[`patches link to media fields > otherRepository > shared slice 1`] = ` "items": [ { "field": { - "height": "2741", + "height": "1", "id": "ba97bfb16bf", - "kind": "quis", + "kind": "image", "link_type": "Media", - "name": "senectus.example", - "size": "844", - "url": "https://515f7ca1126821ccd3eb8b013a857528.example.com/foo.png", - "width": "749", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/reserve/HgZuGu3gSD6db21T3lxm_San%20Zenone.jpg?w=6373&h=4253&fit=crop", + "width": "1", }, }, ], "primary": { "field": { - "height": "1276", + "height": "1", "id": "ba97bfb16bf", - "kind": "ullamcorper", + "kind": "image", "link_type": "Media", - "name": "fringilla.example", - "size": "818", - "url": "https://515f7ca1126821ccd3eb8b013a857528.example.com/foo.png", - "width": "2024", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/reserve/HgZuGu3gSD6db21T3lxm_San%20Zenone.jpg?w=6373&h=4253&fit=crop", + "width": "1", }, "group": [ { "field": { - "height": "2964", + "height": "1", "id": "ba97bfb16bf", - "kind": "pellentesque", + "kind": "image", "link_type": "Media", - "name": "cras.example", - "size": "1564", - "url": "https://515f7ca1126821ccd3eb8b013a857528.example.com/foo.png", - "width": "599", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/reserve/HgZuGu3gSD6db21T3lxm_San%20Zenone.jpg?w=6373&h=4253&fit=crop", + "width": "1", }, }, ], @@ -445,27 +445,27 @@ exports[`patches link to media fields > otherRepository > slice 1`] = ` "items": [ { "field": { - "height": "1500", + "height": "1", "id": "a57963dc361", - "kind": "quam", + "kind": "image", "link_type": "Media", - "name": "integer.example", - "size": "1043", - "url": "https://6d52012dca4fc77aa554f25430aef501.example.com/foo.png", - "width": "737", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", + "width": "1", }, }, ], "primary": { "field": { - "height": "761", + "height": "1", "id": "a57963dc361", - "kind": "nibh", + "kind": "image", "link_type": "Media", - "name": "ac.example", - "size": "2757", - "url": "https://6d52012dca4fc77aa554f25430aef501.example.com/foo.png", - "width": "1300", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", + "width": "1", }, }, "slice_label": "Pellentesque", @@ -478,91 +478,14 @@ exports[`patches link to media fields > otherRepository > slice 1`] = ` exports[`patches link to media fields > otherRepository > static zone 1`] = ` { "field": { - "height": "740", + "height": "1", "id": "97acc6e9abf", - "kind": "suspendisse", + "kind": "image", "link_type": "Media", - "name": "elementum.example", - "size": "556", - "url": "https://7713b5e0c356963c79ccf86c6ff1710c.example.com/foo.png", - "width": "2883", - }, -} -`; - -exports[`patches link to media fields > otherRepositoryNotFoundID > group 1`] = ` -{ - "group": [ - { - "field": { - "link_type": "Any", - }, - }, - ], -} -`; - -exports[`patches link to media fields > otherRepositoryNotFoundID > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": { - "link_type": "Any", - }, - }, - ], - "primary": { - "field": { - "link_type": "Any", - }, - "group": [ - { - "field": { - "link_type": "Any", - }, - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches link to media fields > otherRepositoryNotFoundID > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": { - "link_type": "Any", - }, - }, - ], - "primary": { - "field": { - "link_type": "Any", - }, - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches link to media fields > otherRepositoryNotFoundID > static zone 1`] = ` -{ - "field": { - "link_type": "Any", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?w=4335&h=6502&fit=crop", + "width": "1", }, } `; @@ -1085,14 +1008,14 @@ exports[`patches link to media fields > richTextOtherRepository > group 1`] = ` }, { "data": { - "height": "1715", + "height": "1", "id": "fdb33894dcf", - "kind": "purus", + "kind": "image", "link_type": "Media", - "name": "nulla.example", - "size": "2039", - "url": "https://db0f6f37ebeb6ea09489124345af2a45.example.com/foo.png", - "width": "2091", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + "width": "1", }, "end": 5, "start": 0, @@ -1125,14 +1048,14 @@ exports[`patches link to media fields > richTextOtherRepository > shared slice 1 }, { "data": { - "height": "2741", + "height": "1", "id": "ba97bfb16bf", - "kind": "quis", + "kind": "image", "link_type": "Media", - "name": "senectus.example", - "size": "844", - "url": "https://515f7ca1126821ccd3eb8b013a857528.example.com/foo.png", - "width": "749", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/reserve/HgZuGu3gSD6db21T3lxm_San%20Zenone.jpg?w=6373&h=4253&fit=crop", + "width": "1", }, "end": 5, "start": 0, @@ -1156,14 +1079,14 @@ exports[`patches link to media fields > richTextOtherRepository > shared slice 1 }, { "data": { - "height": "1276", + "height": "1", "id": "ba97bfb16bf", - "kind": "ullamcorper", + "kind": "image", "link_type": "Media", - "name": "fringilla.example", - "size": "818", - "url": "https://515f7ca1126821ccd3eb8b013a857528.example.com/foo.png", - "width": "2024", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/reserve/HgZuGu3gSD6db21T3lxm_San%20Zenone.jpg?w=6373&h=4253&fit=crop", + "width": "1", }, "end": 5, "start": 0, @@ -1186,14 +1109,14 @@ exports[`patches link to media fields > richTextOtherRepository > shared slice 1 }, { "data": { - "height": "2964", + "height": "1", "id": "ba97bfb16bf", - "kind": "pellentesque", + "kind": "image", "link_type": "Media", - "name": "cras.example", - "size": "1564", - "url": "https://515f7ca1126821ccd3eb8b013a857528.example.com/foo.png", - "width": "599", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/reserve/HgZuGu3gSD6db21T3lxm_San%20Zenone.jpg?w=6373&h=4253&fit=crop", + "width": "1", }, "end": 5, "start": 0, @@ -1233,14 +1156,14 @@ exports[`patches link to media fields > richTextOtherRepository > slice 1`] = ` }, { "data": { - "height": "1500", + "height": "1", "id": "a57963dc361", - "kind": "quam", + "kind": "image", "link_type": "Media", - "name": "integer.example", - "size": "1043", - "url": "https://6d52012dca4fc77aa554f25430aef501.example.com/foo.png", - "width": "737", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", + "width": "1", }, "end": 5, "start": 0, @@ -1264,14 +1187,14 @@ exports[`patches link to media fields > richTextOtherRepository > slice 1`] = ` }, { "data": { - "height": "761", + "height": "1", "id": "a57963dc361", - "kind": "nibh", + "kind": "image", "link_type": "Media", - "name": "ac.example", - "size": "2757", - "url": "https://6d52012dca4fc77aa554f25430aef501.example.com/foo.png", - "width": "1300", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", + "width": "1", }, "end": 5, "start": 0, @@ -1302,14 +1225,14 @@ exports[`patches link to media fields > richTextOtherRepository > static zone 1` }, { "data": { - "height": "740", + "height": "1", "id": "97acc6e9abf", - "kind": "suspendisse", + "kind": "image", "link_type": "Media", - "name": "elementum.example", - "size": "556", - "url": "https://7713b5e0c356963c79ccf86c6ff1710c.example.com/foo.png", - "width": "2883", + "name": "default.jpg", + "size": "1", + "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?w=4335&h=6502&fit=crop", + "width": "1", }, "end": 5, "start": 0, diff --git a/test/__snapshots__/writeClient-migrate-patch-rtImageNode.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-rtImageNode.test.ts.snap new file mode 100644 index 00000000..28421765 --- /dev/null +++ b/test/__snapshots__/writeClient-migrate-patch-rtImageNode.test.ts.snap @@ -0,0 +1,1253 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`patches rich text image nodes > existing > group 1`] = ` +{ + "group": [ + { + "field": [ + { + "spans": [], + "text": "Nulla Aliquet Porttitor Lacus Luctus", + "type": "heading2", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "id-existing", + "type": "image", + "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", + }, + ], + }, + ], +} +`; + +exports[`patches rich text image nodes > existing > shared slice 1`] = ` +{ + "slices": [ + { + "id": "2ff58c741ba", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Eu Mi", + "type": "heading3", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "id-existing", + "type": "image", + "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", + "type": "heading3", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "id-existing", + "type": "image", + "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", + }, + ], + "group": [ + { + "field": [ + { + "spans": [], + "text": "Arcu", + "type": "heading1", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "id-existing", + "type": "image", + "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", + }, + ], + }, + ], + }, + "slice_label": null, + "slice_type": "at", + "variation": "tortor", + "version": "a79b9dd", + }, + ], +} +`; + +exports[`patches rich text image nodes > existing > slice 1`] = ` +{ + "slices": [ + { + "id": "e93cc3e4f28", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Habitasse platea dictumst quisque sagittis purus sit", + "type": "o-list-item", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "id-existing", + "type": "image", + "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Ac Orci Phasellus Egestas Tellus", + "type": "heading5", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "id-existing", + "type": "image", + "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", + }, + ], + }, + "slice_label": "Pellentesque", + "slice_type": "nulla_posuere", + }, + ], +} +`; + +exports[`patches rich text image nodes > existing > static zone 1`] = ` +{ + "field": [ + { + "spans": [], + "text": "Elementum Integer", + "type": "heading6", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "id-existing", + "type": "image", + "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?w=4335&h=6502&fit=crop", + }, + ], +} +`; + +exports[`patches rich text image nodes > new > group 1`] = ` +{ + "group": [ + { + "field": [ + { + "spans": [], + "text": "Nulla Aliquet Porttitor Lacus Luctus", + "type": "heading2", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "cdfdd322ca9", + "type": "image", + "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c?w=6000&h=4000&fit=crop", + }, + ], + }, + ], +} +`; + +exports[`patches rich text image nodes > new > shared slice 1`] = ` +{ + "slices": [ + { + "id": "2ff58c741ba", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Eu Mi", + "type": "heading3", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "bc31787853a", + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", + "type": "heading3", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "bc31787853a", + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + "group": [ + { + "field": [ + { + "spans": [], + "text": "Arcu", + "type": "heading1", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "bc31787853a", + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + }, + ], + }, + "slice_label": null, + "slice_type": "at", + "variation": "tortor", + "version": "a79b9dd", + }, + ], +} +`; + +exports[`patches rich text image nodes > new > slice 1`] = ` +{ + "slices": [ + { + "id": "e93cc3e4f28", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Habitasse platea dictumst quisque sagittis purus sit", + "type": "o-list-item", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "7963dc361cc", + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Ac Orci Phasellus Egestas Tellus", + "type": "heading5", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "7963dc361cc", + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + }, + "slice_label": "Pellentesque", + "slice_type": "nulla_posuere", + }, + ], +} +`; + +exports[`patches rich text image nodes > new > static zone 1`] = ` +{ + "field": [ + { + "spans": [], + "text": "Elementum Integer", + "type": "heading6", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "0bbad670dad", + "type": "image", + "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", + }, + ], +} +`; + +exports[`patches rich text image nodes > newLinkTo > group 1`] = ` +{ + "group": [ + { + "field": [ + { + "spans": [], + "text": "Nulla Aliquet Porttitor Lacus Luctus", + "type": "heading2", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "cdfdd322ca9", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "laoreet", + "link_type": "Document", + "tags": [], + "type": "orci", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c?w=6000&h=4000&fit=crop", + }, + ], + }, + ], +} +`; + +exports[`patches rich text image nodes > newLinkTo > shared slice 1`] = ` +{ + "slices": [ + { + "id": "2ff58c741ba", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Eu Mi", + "type": "heading3", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "bc31787853a", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "tortor,", + "link_type": "Document", + "tags": [ + "Risus In", + ], + "type": "dui", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", + "type": "heading3", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "bc31787853a", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "tortor,", + "link_type": "Document", + "tags": [ + "Risus In", + ], + "type": "dui", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + "group": [ + { + "field": [ + { + "spans": [], + "text": "Arcu", + "type": "heading1", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "bc31787853a", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "tortor,", + "link_type": "Document", + "tags": [ + "Risus In", + ], + "type": "dui", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + }, + ], + }, + "slice_label": null, + "slice_type": "at", + "variation": "tortor", + "version": "a79b9dd", + }, + ], +} +`; + +exports[`patches rich text image nodes > newLinkTo > slice 1`] = ` +{ + "slices": [ + { + "id": "e93cc3e4f28", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Habitasse platea dictumst quisque sagittis purus sit", + "type": "o-list-item", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "7963dc361cc", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "sapien", + "link_type": "Document", + "tags": [ + "Aenean", + ], + "type": "amet", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Ac Orci Phasellus Egestas Tellus", + "type": "heading5", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "7963dc361cc", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "sapien", + "link_type": "Document", + "tags": [ + "Aenean", + ], + "type": "amet", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", + }, + ], + }, + "slice_label": "Pellentesque", + "slice_type": "nulla_posuere", + }, + ], +} +`; + +exports[`patches rich text image nodes > newLinkTo > static zone 1`] = ` +{ + "field": [ + { + "spans": [], + "text": "Elementum Integer", + "type": "heading6", + }, + { + "alt": null, + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "transparent", + "x": 0, + "y": 0, + "zoom": 1, + }, + "id": "0bbad670dad", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "eget", + "link_type": "Document", + "tags": [], + "type": "phasellus", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", + }, + ], +} +`; + +exports[`patches rich text image nodes > otherRepository > group 1`] = ` +{ + "group": [ + { + "field": [ + { + "spans": [], + "text": "Nulla Aliquet Porttitor Lacus Luctus", + "type": "heading2", + }, + { + "alt": "Donec enim diam vulputate ut pharetra sit amet aliquam id diam", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#7cfc26", + "x": 2977, + "y": -1163, + "zoom": 1.0472585898934068, + }, + "id": "e8d0985c099", + "type": "image", + "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b", + }, + ], + }, + ], +} +`; + +exports[`patches rich text image nodes > otherRepository > shared slice 1`] = ` +{ + "slices": [ + { + "id": "2ff58c741ba", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Facilisis", + "type": "heading3", + }, + { + "alt": "Nullam vehicula ipsum a arcu cursus vitae congue mauris rhoncus aenean vel elit scelerisque", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#37ae3f", + "x": -1505, + "y": 902, + "zoom": 1.8328975606320652, + }, + "id": "3fc0dfa9fe9", + "type": "image", + "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", + "type": "heading3", + }, + { + "alt": "Proin libero nunc consequat interdum varius", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#904f4f", + "x": 1462, + "y": 1324, + "zoom": 1.504938844941775, + }, + "id": "3fc0dfa9fe9", + "type": "image", + "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", + }, + ], + "group": [ + { + "field": [ + { + "spans": [], + "text": "Egestas Integer Eget Aliquet Nibh", + "type": "heading5", + }, + { + "alt": "Arcu cursus vitae congue mauris", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#5f7a9b", + "x": -119, + "y": -2667, + "zoom": 1.9681315715350518, + }, + "id": "3fc0dfa9fe9", + "type": "image", + "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", + }, + ], + }, + ], + }, + "slice_label": null, + "slice_type": "at", + "variation": "tortor", + "version": "a79b9dd", + }, + ], +} +`; + +exports[`patches rich text image nodes > otherRepository > slice 1`] = ` +{ + "slices": [ + { + "id": "e93cc3e4f28", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Amet", + "type": "heading5", + }, + { + "alt": "Auctor neque vitae tempus quam", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#5bc5aa", + "x": -1072, + "y": -281, + "zoom": 1.3767766101231744, + }, + "id": "f70ca27104d", + "type": "image", + "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Ac Orci Phasellus Egestas Tellus", + "type": "heading5", + }, + { + "alt": "Urna id volutpat lacus laoreet non curabitur gravida arcu ac tortor", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#4860cb", + "x": 280, + "y": -379, + "zoom": 1.2389796902982004, + }, + "id": "f70ca27104d", + "type": "image", + "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", + }, + ], + }, + "slice_label": "Pellentesque", + "slice_type": "nulla_posuere", + }, + ], +} +`; + +exports[`patches rich text image nodes > otherRepository > static zone 1`] = ` +{ + "field": [ + { + "spans": [], + "text": "Elementum Integer", + "type": "heading6", + }, + { + "alt": "Interdum velit euismod in pellentesque", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#ab1d17", + "x": 3605, + "y": 860, + "zoom": 1.9465488211593005, + }, + "id": "04a95cc61c3", + "type": "image", + "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5", + }, + ], +} +`; + +exports[`patches rich text image nodes > otherRepositoryLinkTo > group 1`] = ` +{ + "group": [ + { + "field": [ + { + "spans": [], + "text": "Nulla Aliquet Porttitor Lacus Luctus", + "type": "heading2", + }, + { + "alt": "Donec enim diam vulputate ut pharetra sit amet aliquam id diam", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#7cfc26", + "x": 2977, + "y": -1163, + "zoom": 1.0472585898934068, + }, + "id": "e8d0985c099", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "laoreet", + "link_type": "Document", + "tags": [], + "type": "orci", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b", + }, + ], + }, + ], +} +`; + +exports[`patches rich text image nodes > otherRepositoryLinkTo > shared slice 1`] = ` +{ + "slices": [ + { + "id": "2ff58c741ba", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Facilisis", + "type": "heading3", + }, + { + "alt": "Nullam vehicula ipsum a arcu cursus vitae congue mauris rhoncus aenean vel elit scelerisque", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#37ae3f", + "x": -1505, + "y": 902, + "zoom": 1.8328975606320652, + }, + "id": "3fc0dfa9fe9", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "tortor,", + "link_type": "Document", + "tags": [ + "Risus In", + ], + "type": "dui", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", + "type": "heading3", + }, + { + "alt": "Proin libero nunc consequat interdum varius", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#904f4f", + "x": 1462, + "y": 1324, + "zoom": 1.504938844941775, + }, + "id": "3fc0dfa9fe9", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "tortor,", + "link_type": "Document", + "tags": [ + "Risus In", + ], + "type": "dui", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", + }, + ], + "group": [ + { + "field": [ + { + "spans": [], + "text": "Egestas Integer Eget Aliquet Nibh", + "type": "heading5", + }, + { + "alt": "Arcu cursus vitae congue mauris", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#5f7a9b", + "x": -119, + "y": -2667, + "zoom": 1.9681315715350518, + }, + "id": "3fc0dfa9fe9", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "tortor,", + "link_type": "Document", + "tags": [ + "Risus In", + ], + "type": "dui", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", + }, + ], + }, + ], + }, + "slice_label": null, + "slice_type": "at", + "variation": "tortor", + "version": "a79b9dd", + }, + ], +} +`; + +exports[`patches rich text image nodes > otherRepositoryLinkTo > slice 1`] = ` +{ + "slices": [ + { + "id": "e93cc3e4f28", + "items": [ + { + "field": [ + { + "spans": [], + "text": "Amet", + "type": "heading5", + }, + { + "alt": "Auctor neque vitae tempus quam", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#5bc5aa", + "x": -1072, + "y": -281, + "zoom": 1.3767766101231744, + }, + "id": "f70ca27104d", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "sapien", + "link_type": "Document", + "tags": [ + "Aenean", + ], + "type": "amet", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", + }, + ], + }, + ], + "primary": { + "field": [ + { + "spans": [], + "text": "Ac Orci Phasellus Egestas Tellus", + "type": "heading5", + }, + { + "alt": "Urna id volutpat lacus laoreet non curabitur gravida arcu ac tortor", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#4860cb", + "x": 280, + "y": -379, + "zoom": 1.2389796902982004, + }, + "id": "f70ca27104d", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "sapien", + "link_type": "Document", + "tags": [ + "Aenean", + ], + "type": "amet", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", + }, + ], + }, + "slice_label": "Pellentesque", + "slice_type": "nulla_posuere", + }, + ], +} +`; + +exports[`patches rich text image nodes > otherRepositoryLinkTo > static zone 1`] = ` +{ + "field": [ + { + "spans": [], + "text": "Elementum Integer", + "type": "heading6", + }, + { + "alt": "Interdum velit euismod in pellentesque", + "copyright": null, + "dimensions": { + "height": 1, + "width": 1, + }, + "edit": { + "background": "#ab1d17", + "x": 3605, + "y": 860, + "zoom": 1.9465488211593005, + }, + "id": "04a95cc61c3", + "linkTo": { + "id": "id-existing", + "isBroken": false, + "lang": "eget", + "link_type": "Document", + "tags": [], + "type": "phasellus", + }, + "type": "image", + "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5", + }, + ], +} +`; diff --git a/test/__testutils__/mockPrismicAssetAPI.ts b/test/__testutils__/mockPrismicAssetAPI.ts index 02187d2b..ab9ef6b1 100644 --- a/test/__testutils__/mockPrismicAssetAPI.ts +++ b/test/__testutils__/mockPrismicAssetAPI.ts @@ -108,7 +108,7 @@ export const mockPrismicAssetAPI = ( validateHeaders(req) - const index = Number.parseInt(req.url.searchParams.get("cursor") ?? "0") + const index = Number.parseInt(req.url.searchParams.get("cursor") || "0") const items: Asset[] = assetsDatabase[index] || [] let missing_ids: string[] | undefined @@ -143,7 +143,7 @@ export const mockPrismicAssetAPI = ( validateHeaders(req) const response: PostAssetResult = - args.newAssets?.shift() ?? mockAsset(args.ctx) + args.newAssets?.shift() || mockAsset(args.ctx) // Save the asset in DB assetsDatabase.push([response]) diff --git a/test/__testutils__/testMigrationFieldPatching.ts b/test/__testutils__/testMigrationFieldPatching.ts index be1a42a7..4a4c1a32 100644 --- a/test/__testutils__/testMigrationFieldPatching.ts +++ b/test/__testutils__/testMigrationFieldPatching.ts @@ -22,9 +22,7 @@ type GetDataArgs = { migration: prismic.Migration existingAssets: Asset[] existingDocuments: prismic.PrismicDocument[] - migrationDocuments: (Omit & { - uid: string - })[] + migrationDocuments: prismic.MigrationDocument[] mockedDomain: string } @@ -82,16 +80,20 @@ const internalTestMigrationFieldPatching = ( const migration = prismic.createMigration() + const migrationOtherDocument = migration.createDocument( + otherDocument, + "other", + ) + newDocument.data = args.getData({ ctx, migration, existingAssets: assetsDatabase.flat(), existingDocuments: queryResponse[0].results, - migrationDocuments: [otherDocument], + migrationDocuments: [migrationOtherDocument], mockedDomain, }) - migration.createDocument(otherDocument, "other") migration.createDocument(newDocument, "new") // We speed up the internal rate limiter to make these tests run faster (from 4500ms to nearly instant) diff --git a/test/migration-createDocument.test.ts b/test/migration-createDocument.test.ts index df7b98dc..960d2c0a 100644 --- a/test/migration-createDocument.test.ts +++ b/test/migration-createDocument.test.ts @@ -3,7 +3,8 @@ import { describe, expect, it } from "vitest" import type { MockFactory } from "@prismicio/mock" import * as prismic from "../src" -import type { MigrationAsset } from "../src/types/migration/asset" +import type { MigrationAssetConfig } from "../src/types/migration/Asset" +import { MigrationDocument } from "../src/types/migration/Document" it("creates a document", () => { const migration = prismic.createMigration() @@ -18,10 +19,9 @@ it("creates a document", () => { migration.createDocument(document, documentTitle) - expect(migration._documents[0]).toStrictEqual({ - document, - params: { documentTitle }, - }) + expect(migration._documents[0]).toStrictEqual( + new MigrationDocument(document, { documentTitle }), + ) }) it("creates a document from an existing Prismic document", (ctx) => { @@ -32,10 +32,9 @@ it("creates a document from an existing Prismic document", (ctx) => { migration.createDocument(document, documentTitle) - expect(migration._documents[0]).toStrictEqual({ - document, - params: { documentTitle }, - }) + expect(migration._documents[0]).toStrictEqual( + new MigrationDocument(document, { documentTitle }), + ) }) describe.each<{ @@ -46,7 +45,7 @@ describe.each<{ | prismic.FilledImageFieldImage | prismic.FilledLinkToMediaField | prismic.RichTextField<"filled"> - expected: MigrationAsset + expected: MigrationAssetConfig } }>([ { @@ -58,8 +57,8 @@ describe.each<{ id: image.id, field: image, expected: { - alt: image.alt ?? undefined, - credits: image.copyright ?? undefined, + alt: image.alt || undefined, + credits: image.copyright || undefined, file: image.url.split("?")[0], filename: image.url.split("/").pop()!.split("?")[0], id: image.id, @@ -104,8 +103,8 @@ describe.each<{ id: image.id, field: richText, expected: { - alt: image.alt ?? undefined, - credits: image.copyright ?? undefined, + alt: image.alt || undefined, + credits: image.copyright || undefined, file: image.url.split("?")[0], filename: image.url.split("/").pop()!.split("?")[0], id: image.id, diff --git a/test/migration-getByUID.test.ts b/test/migration-getByUID.test.ts index 0ac4d3ad..496e7b22 100644 --- a/test/migration-getByUID.test.ts +++ b/test/migration-getByUID.test.ts @@ -13,10 +13,10 @@ it("returns a document with a matching UID", () => { } const documentName = "documentName" - migration.createDocument(document, documentName) + const migrationDocument = migration.createDocument(document, documentName) expect(migration.getByUID(document.type, document.uid)).toStrictEqual( - document, + migrationDocument, ) }) diff --git a/test/migration-getSingle.test.ts b/test/migration-getSingle.test.ts index d2ff7359..4dbcc129 100644 --- a/test/migration-getSingle.test.ts +++ b/test/migration-getSingle.test.ts @@ -12,9 +12,9 @@ it("returns a document of a given singleton type", () => { } const documentName = "documentName" - migration.createDocument(document, documentName) + const migrationDocument = migration.createDocument(document, documentName) - expect(migration.getSingle(document.type)).toStrictEqual(document) + expect(migration.getSingle(document.type)).toStrictEqual(migrationDocument) }) it("returns `undefined` if a document is not found", () => { diff --git a/test/types/migration-document.types.ts b/test/types/migration-document.types.ts index 0d48cd56..14237c1a 100644 --- a/test/types/migration-document.types.ts +++ b/test/types/migration-document.types.ts @@ -100,8 +100,6 @@ expectType({ type Fields = { image: prismic.ImageField migrationImage: prismic.ImageField - link: prismic.LinkField - migrationLink: prismic.LinkField linkToMedia: prismic.LinkToMediaField migrationLinkToMedia: prismic.LinkToMediaField contentRelationship: prismic.ContentRelationshipField @@ -139,13 +137,11 @@ expectType({ lang: "", data: { image: {} as prismic.ImageField, - migrationImage: {} as prismic.ImageMigrationField, - link: {} as prismic.LinkField, - migrationLink: {} as prismic.LinkMigrationField, + migrationImage: {} as prismic.MigrationImage, linkToMedia: {} as prismic.LinkToMediaField, - migrationLinkToMedia: {} as prismic.LinkToMediaMigrationField, + migrationLinkToMedia: {} as prismic.MigrationLinkToMedia, contentRelationship: {} as prismic.ContentRelationshipField, - migrationContentRelationship: {} as prismic.ContentRelationshipField, + migrationContentRelationship: {} as prismic.MigrationContentRelationship, }, }) @@ -158,13 +154,12 @@ expectType({ group: [ { image: {} as prismic.ImageField, - migrationImage: {} as prismic.ImageMigrationField, - link: {} as prismic.LinkField, - migrationLink: {} as prismic.LinkMigrationField, + migrationImage: {} as prismic.MigrationImage, linkToMedia: {} as prismic.LinkToMediaField, - migrationLinkToMedia: {} as prismic.LinkToMediaMigrationField, + migrationLinkToMedia: {} as prismic.MigrationLinkToMedia, contentRelationship: {} as prismic.ContentRelationshipField, - migrationContentRelationship: {} as prismic.ContentRelationshipField, + migrationContentRelationship: + {} as prismic.MigrationContentRelationship, }, ], }, @@ -185,38 +180,33 @@ expectType({ version: "", primary: { image: {} as prismic.ImageField, - migrationImage: {} as prismic.ImageMigrationField, - link: {} as prismic.LinkField, - migrationLink: {} as prismic.LinkMigrationField, + migrationImage: {} as prismic.MigrationImage, linkToMedia: {} as prismic.LinkToMediaField, - migrationLinkToMedia: {} as prismic.LinkToMediaMigrationField, + migrationLinkToMedia: {} as prismic.MigrationLinkToMedia, contentRelationship: {} as prismic.ContentRelationshipField, - migrationContentRelationship: {} as prismic.ContentRelationshipField, + migrationContentRelationship: + {} as prismic.MigrationContentRelationship, group: [ { image: {} as prismic.ImageField, - migrationImage: {} as prismic.ImageMigrationField, - link: {} as prismic.LinkField, - migrationLink: {} as prismic.LinkMigrationField, + migrationImage: {} as prismic.MigrationImage, linkToMedia: {} as prismic.LinkToMediaField, - migrationLinkToMedia: {} as prismic.LinkToMediaMigrationField, + migrationLinkToMedia: {} as prismic.MigrationLinkToMedia, contentRelationship: {} as prismic.ContentRelationshipField, migrationContentRelationship: - {} as prismic.ContentRelationshipField, + {} as prismic.MigrationContentRelationship, }, ], }, items: [ { image: {} as prismic.ImageField, - migrationImage: {} as prismic.ImageMigrationField, - link: {} as prismic.LinkField, - migrationLink: {} as prismic.LinkMigrationField, + migrationImage: {} as prismic.MigrationImage, linkToMedia: {} as prismic.LinkToMediaField, - migrationLinkToMedia: {} as prismic.LinkToMediaMigrationField, + migrationLinkToMedia: {} as prismic.MigrationLinkToMedia, contentRelationship: {} as prismic.ContentRelationshipField, migrationContentRelationship: - {} as prismic.ContentRelationshipField, + {} as prismic.MigrationContentRelationship, }, ], }, diff --git a/test/types/migration.types.ts b/test/types/migration.types.ts index 91542ac2..f3c3bd2d 100644 --- a/test/types/migration.types.ts +++ b/test/types/migration.types.ts @@ -41,36 +41,85 @@ expectType>(false) // Default const defaultCreateAsset = defaultMigration.createAsset("url", "name") -expectType>(true) +expectType>(true) + +expectType< + TypeEqual< + ReturnType, + prismic.MigrationImage + > +>(true) expectType< - TypeEqual + TypeOf> >(true) + expectType< TypeEqual< - typeof defaultCreateAsset.linkToMedia, - prismic.LinkToMediaMigrationField + ReturnType, + prismic.MigrationLinkToMedia > >(true) expectType< - TypeOf + TypeOf< + prismic.MigrationLinkToMedia, + ReturnType + > +>(true) + +expectType< + TypeEqual< + ReturnType, + prismic.MigrationRTImageNode + > +>(true) +expectType< + TypeOf< + prismic.MigrationRTImageNode, + ReturnType + > >(true) // Documents const documentsCreateAsset = defaultMigration.createAsset("url", "name") -expectType>( - true, -) +expectType>(true) + expectType< - TypeEqual + TypeEqual< + ReturnType, + prismic.MigrationImage + > +>(true) +expectType< + TypeOf< + prismic.MigrationImage, + ReturnType + > +>(true) + +expectType< + TypeEqual< + ReturnType, + prismic.MigrationLinkToMedia + > >(true) +expectType< + TypeOf< + prismic.MigrationLinkToMedia, + ReturnType + > +>(true) + expectType< TypeEqual< - typeof documentsCreateAsset.linkToMedia, - prismic.LinkToMediaMigrationField + ReturnType, + prismic.MigrationRTImageNode > >(true) expectType< - TypeOf + TypeOf< + prismic.MigrationRTImageNode, + ReturnType + > >(true) /** @@ -87,9 +136,9 @@ const defaultCreateDocument = defaultMigration.createDocument( }, "", ) -expectType< - TypeEqual ->(true) +expectType>( + true, +) // Documents const documentsCreateDocument = documentsMigration.createDocument( @@ -104,6 +153,6 @@ const documentsCreateDocument = documentsMigration.createDocument( expectType< TypeEqual< typeof documentsCreateDocument, - prismic.PrismicMigrationDocument + prismic.MigrationDocument > >(true) diff --git a/test/writeClient-migrate-documents.test.ts b/test/writeClient-migrate-documents.test.ts index 28ce988d..fcb75ae7 100644 --- a/test/writeClient-migrate-documents.test.ts +++ b/test/writeClient-migrate-documents.test.ts @@ -8,7 +8,7 @@ import { mockPrismicMigrationAPI } from "./__testutils__/mockPrismicMigrationAPI import { mockPrismicRestAPIV2 } from "./__testutils__/mockPrismicRestAPIV2" import * as prismic from "../src" -import type { DocumentMap } from "../src/lib/patchMigrationDocumentData" +import type { DocumentMap } from "../src/types/migration/Document" // Skip test on Node 16 and 18 (File and FormData support) const isNode16 = process.version.startsWith("v16") @@ -137,7 +137,7 @@ it.concurrent("creates new documents", async (ctx) => { const migration = prismic.createMigration() - migration.createDocument(document, "foo") + const migrationDocument = migration.createDocument(document, "foo") let documents: DocumentMap | undefined const reporter = vi.fn<(event: prismic.MigrateReporterEvents) => void>( @@ -162,28 +162,36 @@ it.concurrent("creates new documents", async (ctx) => { }, }, }) - expect(documents?.get(document)?.id).toBe(newDocument.id) + expect(documents?.get(migrationDocument)?.id).toBe(newDocument.id) }) it.concurrent( - "creates new non-master locale document with direct reference", + "creates new non-master locale document with direct reference (existing document)", async (ctx) => { const client = createTestWriteClient({ ctx }) - const masterLanguageDocument = ctx.mock.value.document() + const queryResponse = createPagedQueryResponses({ + ctx, + pages: 1, + pageSize: 1, + }) + + const masterLanguageDocument = queryResponse[0].results[0] const document = ctx.mock.value.document() const newDocument = { id: "foo", masterLanguageDocumentID: masterLanguageDocument.id, } - mockPrismicRestAPIV2({ ctx }) + mockPrismicRestAPIV2({ ctx, queryResponse }) mockPrismicAssetAPI({ ctx, client }) mockPrismicMigrationAPI({ ctx, client, newDocuments: [newDocument] }) const migration = prismic.createMigration() - migration.createDocument(document, "foo", { masterLanguageDocument }) + const migrationDocument = migration.createDocument(document, "foo", { + masterLanguageDocument, + }) let documents: DocumentMap | undefined const reporter = vi.fn<(event: prismic.MigrateReporterEvents) => void>( @@ -209,7 +217,62 @@ it.concurrent( }, }, }) - ctx.expect(documents?.get(document)?.id).toBe(newDocument.id) + ctx.expect(documents?.get(migrationDocument)?.id).toBe(newDocument.id) + ctx.expect.assertions(3) + }, +) + +it.concurrent( + "creates new non-master locale document with direct reference (new document)", + async (ctx) => { + const client = createTestWriteClient({ ctx }) + + const masterLanguageDocument = + ctx.mock.value.document() as prismic.PrismicMigrationDocument + const document = + ctx.mock.value.document() as prismic.PrismicMigrationDocument + const newDocuments = [ + { + id: masterLanguageDocument.id!, + }, + { + id: document.id!, + masterLanguageDocumentID: masterLanguageDocument.id!, + }, + ] + + delete masterLanguageDocument.id + delete document.id + + mockPrismicRestAPIV2({ ctx }) + mockPrismicAssetAPI({ ctx, client }) + mockPrismicMigrationAPI({ ctx, client, newDocuments: [...newDocuments] }) + + const migration = prismic.createMigration() + + const masterLanguageMigrationDocument = migration.createDocument( + masterLanguageDocument, + "foo", + ) + const migrationDocument = migration.createDocument(document, "bar", { + masterLanguageDocument: masterLanguageMigrationDocument, + }) + + let documents: DocumentMap | undefined + const reporter = vi.fn<(event: prismic.MigrateReporterEvents) => void>( + (event) => { + if (event.type === "documents:created") { + documents = event.data.documents + } + }, + ) + + await client.migrate(migration, { reporter }) + + ctx + .expect(documents?.get(masterLanguageMigrationDocument)?.id) + .toBe(newDocuments[0].id) + ctx.expect(documents?.get(migrationDocument)?.id).toBe(newDocuments[1].id) ctx.expect.assertions(3) }, ) @@ -238,7 +301,7 @@ it.concurrent( const migration = prismic.createMigration() - migration.createDocument(document, "foo", { + const migrationDocument = migration.createDocument(document, "foo", { masterLanguageDocument: () => masterLanguageDocument, }) @@ -266,7 +329,7 @@ it.concurrent( }, }, }) - ctx.expect(documents?.get(document)?.id).toBe(newDocument.id) + ctx.expect(documents?.get(migrationDocument)?.id).toBe(newDocument.id) ctx.expect.assertions(3) }, ) @@ -299,9 +362,12 @@ it.concurrent( const migration = prismic.createMigration() - migration.createDocument(masterLanguageDocument, "foo") - migration.createDocument(document, "bar", { - masterLanguageDocument: () => masterLanguageDocument, + const masterLanguageMigrationDocument = migration.createDocument( + masterLanguageDocument, + "foo", + ) + const migrationDocument = migration.createDocument(document, "bar", { + masterLanguageDocument: () => masterLanguageMigrationDocument, }) let documents: DocumentMap | undefined @@ -316,9 +382,9 @@ it.concurrent( await client.migrate(migration, { reporter }) ctx - .expect(documents?.get(masterLanguageDocument)?.id) + .expect(documents?.get(masterLanguageMigrationDocument)?.id) .toBe(newDocuments[0].id) - ctx.expect(documents?.get(document)?.id).toBe(newDocuments[1].id) + ctx.expect(documents?.get(migrationDocument)?.id).toBe(newDocuments[1].id) ctx.expect.assertions(3) }, ) @@ -351,7 +417,7 @@ it.concurrent( const migration = prismic.createMigration() - migration.createDocument(document, "foo") + const migrationDocument = migration.createDocument(document, "foo") let documents: DocumentMap | undefined const reporter = vi.fn<(event: prismic.MigrateReporterEvents) => void>( @@ -376,7 +442,7 @@ it.concurrent( }, }, }) - ctx.expect(documents?.get(document)?.id).toBe(newDocument.id) + ctx.expect(documents?.get(migrationDocument)?.id).toBe(newDocument.id) ctx.expect.assertions(3) }, ) @@ -404,8 +470,11 @@ it.concurrent("creates master locale documents first", async (ctx) => { const migration = prismic.createMigration() - migration.createDocument(document, "bar") - migration.createDocument(masterLanguageDocument, "foo") + const migrationDocument = migration.createDocument(document, "bar") + const masterLanguageMigrationDocument = migration.createDocument( + masterLanguageDocument, + "foo", + ) let documents: DocumentMap | undefined const reporter = vi.fn<(event: prismic.MigrateReporterEvents) => void>( @@ -419,7 +488,7 @@ it.concurrent("creates master locale documents first", async (ctx) => { await client.migrate(migration, { reporter }) ctx - .expect(documents?.get(masterLanguageDocument)?.id) + .expect(documents?.get(masterLanguageMigrationDocument)?.id) .toBe(newDocuments[0].id) - ctx.expect(documents?.get(document)?.id).toBe(newDocuments[1].id) + ctx.expect(documents?.get(migrationDocument)?.id).toBe(newDocuments[1].id) }) diff --git a/test/writeClient-migrate-patch-link.test.ts b/test/writeClient-migrate-patch-contentRelationship.test.ts similarity index 57% rename from test/writeClient-migrate-patch-link.test.ts rename to test/writeClient-migrate-patch-contentRelationship.test.ts index 51cb64ef..5cd030b8 100644 --- a/test/writeClient-migrate-patch-link.test.ts +++ b/test/writeClient-migrate-patch-contentRelationship.test.ts @@ -5,7 +5,7 @@ import { RichTextNodeType } from "../src" testMigrationFieldPatching("patches link fields", { existing: ({ existingDocuments }) => existingDocuments[0], migration: ({ migrationDocuments }) => { - delete migrationDocuments[0].id + delete migrationDocuments[0].document.id return migrationDocuments[0] }, @@ -13,21 +13,27 @@ testMigrationFieldPatching("patches link fields", { return () => existingDocuments[0] }, lazyMigration: ({ migration, migrationDocuments }) => { - delete migrationDocuments[0].id + delete migrationDocuments[0].document.id return () => - migration.getByUID(migrationDocuments[0].type, migrationDocuments[0].uid) + migration.getByUID( + migrationDocuments[0].document.type, + migrationDocuments[0].document.uid!, + ) }, migrationNoTags: ({ migration, migrationDocuments }) => { - migrationDocuments[0].tags = undefined + migrationDocuments[0].document.tags = undefined return () => - migration.getByUID(migrationDocuments[0].type, migrationDocuments[0].uid) + migration.getByUID( + migrationDocuments[0].document.type, + migrationDocuments[0].document.uid!, + ) }, otherRepositoryContentRelationship: ({ ctx, migrationDocuments }) => { const contentRelationship = ctx.mock.value.link({ type: "Document" }) // `migrationDocuments` contains documents from "another repository" - contentRelationship.id = migrationDocuments[0].id! + contentRelationship.id = migrationDocuments[0].document.id! return contentRelationship }, @@ -47,25 +53,4 @@ testMigrationFieldPatching("patches link fields", { ], }, ], - richTextNewImageNodeLink: ({ ctx, migration, existingDocuments }) => [ - ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), - { - ...migration.createAsset("foo", "foo.png"), - linkTo: existingDocuments[0], - }, - ], - richTextOtherReposiotryImageNodeLink: ({ - ctx, - mockedDomain, - existingDocuments, - }) => [ - ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), - { - type: RichTextNodeType.image, - ...ctx.mock.value.image({ state: "filled" }), - id: "foo-id", - url: `${mockedDomain}/foo.png`, - linkTo: existingDocuments[0], - }, - ], }) diff --git a/test/writeClient-migrate-patch-image.test.ts b/test/writeClient-migrate-patch-image.test.ts index 39efa933..f0897927 100644 --- a/test/writeClient-migrate-patch-image.test.ts +++ b/test/writeClient-migrate-patch-image.test.ts @@ -1,7 +1,5 @@ import { testMigrationFieldPatching } from "./__testutils__/testMigrationFieldPatching" -import { RichTextNodeType } from "../src" - testMigrationFieldPatching("patches image fields", { new: ({ migration }) => migration.createAsset("foo", "foo.png"), existing: ({ migration, existingAssets }) => @@ -44,22 +42,20 @@ testMigrationFieldPatching("patches image fields", { }, } }, + otherRepositoryWithTypeThumbnail: ({ ctx, mockedDomain }) => { + const image = ctx.mock.value.image({ state: "filled" }) + const id = "foo-id" + + return { + ...image, + id, + url: `${mockedDomain}/foo.png?some=query`, + type: { + ...image, + id, + url: `${mockedDomain}/foo.png?some=other&query=params`, + }, + } + }, otherRepositoryEmpty: ({ ctx }) => ctx.mock.value.image({ state: "empty" }), - richTextNew: ({ ctx, migration }) => [ - ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), - migration.createAsset("foo", "foo.png"), - ], - richTextExisting: ({ ctx, migration, existingAssets }) => [ - ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), - migration.createAsset(existingAssets[0]), - ], - richTextOtherRepository: ({ ctx, mockedDomain }) => [ - ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), - { - type: RichTextNodeType.image, - ...ctx.mock.value.image({ state: "filled" }), - id: "foo-id", - url: `${mockedDomain}/foo.png`, - }, - ], }) diff --git a/test/writeClient-migrate-patch-linkToMedia.test.ts b/test/writeClient-migrate-patch-linkToMedia.test.ts index abe49fa0..8a19025c 100644 --- a/test/writeClient-migrate-patch-linkToMedia.test.ts +++ b/test/writeClient-migrate-patch-linkToMedia.test.ts @@ -4,9 +4,10 @@ import { RichTextNodeType } from "../src" import { AssetType } from "../src/types/api/asset/asset" testMigrationFieldPatching("patches link to media fields", { - new: ({ migration }) => migration.createAsset("foo", "foo.png").linkToMedia, + new: ({ migration }) => + migration.createAsset("foo", "foo.png").asLinkToMedia(), existing: ({ migration, existingAssets }) => - migration.createAsset(existingAssets[0]).linkToMedia, + migration.createAsset(existingAssets[0]).asLinkToMedia(), existingNonImage: ({ migration, existingAssets }) => { existingAssets[0].filename = "foo.pdf" existingAssets[0].extension = "pdf" @@ -14,7 +15,7 @@ testMigrationFieldPatching("patches link to media fields", { existingAssets[0].width = undefined existingAssets[0].height = undefined - return migration.createAsset(existingAssets[0]).linkToMedia + return migration.createAsset(existingAssets[0]).asLinkToMedia() }, otherRepository: ({ ctx, mockedDomain }) => { return { @@ -23,12 +24,6 @@ testMigrationFieldPatching("patches link to media fields", { url: `${mockedDomain}/foo.png`, } }, - otherRepositoryNotFoundID: ({ ctx }) => { - return { - ...ctx.mock.value.linkToMedia({ state: "empty" }), - id: null, - } - }, richTextNew: ({ migration }) => [ { type: RichTextNodeType.paragraph, @@ -39,7 +34,7 @@ testMigrationFieldPatching("patches link to media fields", { type: RichTextNodeType.hyperlink, start: 0, end: 5, - data: migration.createAsset("foo", "foo.png").linkToMedia, + data: migration.createAsset("foo", "foo.png").asLinkToMedia(), }, ], }, @@ -54,7 +49,7 @@ testMigrationFieldPatching("patches link to media fields", { type: RichTextNodeType.hyperlink, start: 0, end: 5, - data: migration.createAsset(existingAssets[0]).linkToMedia, + data: migration.createAsset(existingAssets[0]).asLinkToMedia(), }, ], }, diff --git a/test/writeClient-migrate-patch-rtImageNode.test.ts b/test/writeClient-migrate-patch-rtImageNode.test.ts new file mode 100644 index 00000000..4d69bc1e --- /dev/null +++ b/test/writeClient-migrate-patch-rtImageNode.test.ts @@ -0,0 +1,43 @@ +import { testMigrationFieldPatching } from "./__testutils__/testMigrationFieldPatching" + +import { RichTextNodeType } from "../src" +import { MigrationContentRelationship } from "../src/types/migration/ContentRelationship" + +testMigrationFieldPatching("patches rich text image nodes", { + new: ({ ctx, migration }) => [ + ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), + migration.createAsset("foo", "foo.png").asRTImageNode(), + ], + existing: ({ ctx, migration, existingAssets }) => [ + ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), + migration.createAsset(existingAssets[0]).asRTImageNode(), + ], + otherRepository: ({ ctx, mockedDomain }) => [ + ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), + { + type: RichTextNodeType.image, + ...ctx.mock.value.image({ state: "filled" }), + id: "foo-id", + url: `${mockedDomain}/foo.png`, + }, + ], + newLinkTo: ({ ctx, migration, existingDocuments }) => { + const rtImageNode = migration.createAsset("foo", "foo.png").asRTImageNode() + rtImageNode.linkTo = new MigrationContentRelationship(existingDocuments[0]) + + return [ + ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), + rtImageNode, + ] + }, + otherRepositoryLinkTo: ({ ctx, mockedDomain, existingDocuments }) => [ + ...ctx.mock.value.richText({ pattern: "short", state: "filled" }), + { + type: RichTextNodeType.image, + ...ctx.mock.value.image({ state: "filled" }), + id: "foo-id", + url: `${mockedDomain}/foo.png`, + linkTo: existingDocuments[0], + }, + ], +}) diff --git a/test/writeClient-migrate.test.ts b/test/writeClient-migrate.test.ts index db2a5137..3f6bb056 100644 --- a/test/writeClient-migrate.test.ts +++ b/test/writeClient-migrate.test.ts @@ -9,10 +9,8 @@ import { mockPrismicMigrationAPI } from "./__testutils__/mockPrismicMigrationAPI import { mockPrismicRestAPIV2 } from "./__testutils__/mockPrismicRestAPIV2" import * as prismic from "../src" -import type { - AssetMap, - DocumentMap, -} from "../src/lib/patchMigrationDocumentData" +import type { AssetMap } from "../src/types/migration/Asset" +import type { DocumentMap } from "../src/types/migration/Document" // Skip test on Node 16 and 18 (File and FormData support) const isNode16 = process.version.startsWith("v16")