Skip to content

Commit

Permalink
feat: support repeatable links in migration
Browse files Browse the repository at this point in the history
  • Loading branch information
levimykel committed Oct 30, 2024
1 parent a683942 commit f6fcf11
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export type {
export type {
LinkToMediaField,
FilledLinkToMediaField,
SingleLinkToMediaField,
} from "./types/value/linkToMedia"

export type {
Expand Down
13 changes: 10 additions & 3 deletions src/lib/resolveMigrationDocumentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
} from "../types/migration/ContentRelationship"
import { PrismicMigrationDocument } from "../types/migration/Document"
import type { FilledImageFieldImage } from "../types/value/image"
import type { LinkField } from "../types/value/link"
import type { SingleLinkField } from "../types/value/link"
import { LinkType } from "../types/value/link"
import type { RTImageNode } from "../types/value/richText"
import { RichTextNodeType } from "../types/value/richText"
Expand Down Expand Up @@ -78,7 +78,14 @@ export const resolveMigrationImage = (
image instanceof PrismicMigrationAsset ? { id: image } : image

const asset = migration._assets.get(master.config.id)?.asset
const maybeInitialField = master.originalField
let maybeInitialField
if (Array.isArray(master.originalField)) {
maybeInitialField = isFilled.link(master.originalField[0])
? master.originalField[0]
: undefined
} else {
maybeInitialField = master.originalField
}

if (asset) {
const parameters = (maybeInitialField?.url || asset.url).split("?")[1]
Expand Down Expand Up @@ -135,7 +142,7 @@ export const resolveMigrationRTImageNode = async (
const linkTo = (await resolveMigrationDocumentData(
rtImageNode.linkTo,
migration,
)) as LinkField
)) as SingleLinkField

return {
...image,
Expand Down
11 changes: 7 additions & 4 deletions src/types/migration/Asset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Asset } from "../api/asset/asset"
import type { FilledImageFieldImage } from "../value/image"
import type { EmptyLinkField } from "../value/link"
import type { LinkToMediaField } from "../value/linkToMedia"
import type {
LinkToMediaField,
SingleLinkToMediaField,
} from "../value/linkToMedia"
import { type RTImageNode } from "../value/richText"

import type { InjectMigrationSpecificTypes } from "./Document"
Expand Down Expand Up @@ -72,10 +75,10 @@ export type MigrationImage =
* A link to media field in a migration.
*/
export type MigrationLinkToMedia = Pick<
LinkToMediaField<"filled">,
SingleLinkToMediaField<"filled">,
"link_type"
> &
Partial<Pick<LinkToMediaField<"filled">, "text">> & {
Partial<Pick<SingleLinkToMediaField<"filled">, "text">> & {
/**
* A reference to the migration asset used to resolve the link to media
* field's value.
Expand All @@ -88,7 +91,7 @@ export type MigrationLinkToMedia = Pick<
* with the migration API.
*/
export type MigrationLinkToMediaField =
| Pick<LinkToMediaField<"filled">, "link_type" | "id" | "text">
| Pick<SingleLinkToMediaField<"filled">, "link_type" | "id" | "text">
| EmptyLinkField<"Media">

/**
Expand Down
3 changes: 2 additions & 1 deletion test/writeClient-migrate-patch-linkToMedia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
InjectMigrationSpecificTypes,
LinkToMediaField,
RichTextField,
SingleLinkToMediaField,
} from "../src"
import { LinkType, RichTextNodeType } from "../src"
import type { Asset } from "../src/types/api/asset/asset"
Expand All @@ -12,7 +13,7 @@ import type { MigrationLinkToMedia } from "../src/types/migration/Asset"
const assetToLinkToMedia = (
asset: Asset,
text?: string,
): LinkToMediaField<"filled"> => {
): SingleLinkToMediaField<"filled"> => {
return {
id: asset.id,
link_type: LinkType.Media,
Expand Down

0 comments on commit f6fcf11

Please sign in to comment.