Skip to content

Commit

Permalink
refactor: per review
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Sep 18, 2024
1 parent 78fa415 commit 9bab813
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 275 deletions.
7 changes: 4 additions & 3 deletions src/Migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
if (input.isBroken) {
return {
link_type: LinkType.Document,
id: "__broken__",
// ID needs to be 16 characters long to be considered valid by the API
id: "_____broken_____",
isBroken: true,
// TODO: Remove when link text PR is merged
// @ts-expect-error - Future-proofing for link text
Expand All @@ -412,7 +413,7 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {

return {
link_type: LinkType.Document,
id: () => this.#getByOriginalID(input.id),
id: () => this.getByOriginalID(input.id),
// TODO: Remove when link text PR is merged
// @ts-expect-error - Future-proofing for link text
text: input.text,
Expand Down Expand Up @@ -507,7 +508,7 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
* @returns The migration document instance for the original ID, if a matching
* document is found.
*/
#getByOriginalID<TType extends TDocuments["type"]>(
getByOriginalID<TType extends TDocuments["type"]>(
id: string,
):
| PrismicMigrationDocument<ExtractDocumentType<TDocuments, TType>>
Expand Down
7 changes: 6 additions & 1 deletion src/WriteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,15 @@ export class WriteClient<
masterLanguageDocumentID =
"id" in masterLanguageDocument ? masterLanguageDocument.id : undefined
} else if (doc.originalPrismicDocument) {
masterLanguageDocumentID =
const maybeOriginalID =
doc.originalPrismicDocument.alternate_languages.find(
({ lang }) => lang === masterLocale,
)?.id

if (maybeOriginalID) {
masterLanguageDocumentID =
migration.getByOriginalID(maybeOriginalID)?.document.id
}
}

const { id } = await this.createDocument(
Expand Down
14 changes: 4 additions & 10 deletions src/lib/resolveMigrationDocumentData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MigrationLinkToMediaField } from "../types/migration/Asset"
import {
type MigrationImage,
type MigrationLinkToMedia,
Expand All @@ -12,7 +13,6 @@ import { PrismicMigrationDocument } from "../types/migration/Document"
import type { FilledImageFieldImage } from "../types/value/image"
import type { LinkField } from "../types/value/link"
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"

Expand Down Expand Up @@ -156,24 +156,18 @@ export const resolveMigrationRTImageNode = async (
export const resolveMigrationLinkToMedia = (
linkToMedia: MigrationLinkToMedia,
migration: Migration,
): LinkToMediaField<"filled"> | undefined => {
): MigrationLinkToMediaField => {
const asset = migration._assets.get(linkToMedia.id.config.id)?.asset

if (asset) {
return {
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,
// TODO: Remove when link text PR is merged
// @ts-expect-error - Future-proofing for link text
text: linkToMedia.text,
}
}

return { link_type: LinkType.Media }
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/types/migration/Asset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 RTImageNode } from "../value/richText"

Expand All @@ -11,6 +12,11 @@ import type { InjectMigrationSpecificTypes } from "./Document"
export type MigrationAssetConfig = {
/**
* ID the assets is indexed with on the migration instance.
*
* @remarks
* This property's value is not necessarily the same as the as the one in the
* `file` property. It is mainly used for deduplication within a `Migration`
* instance.
*/
id: string | URL | File | NonNullable<ConstructorParameters<File>[0]>[0]

Expand Down Expand Up @@ -84,6 +90,16 @@ export type MigrationLinkToMedia = Pick<
id: PrismicMigrationAsset
}

/**
* The minimum amount of information needed to represent a link to media field
* with the migration API.
*/
export type MigrationLinkToMediaField =
// TODO: Remove when link text PR is merged
// @ts-expect-error - Future-proofing for link text
| Pick<LinkToMediaField<"filled">, "link_type" | "id" | "text">
| EmptyLinkField<"Media">

/**
* A rich text image node in a migration.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`patches content relationship fields (from Prismic) > broken > group 1`]
"group": [
{
"field": {
"id": "__broken__",
"id": "_____broken_____",
"isBroken": true,
"link_type": "Document",
},
Expand All @@ -22,22 +22,22 @@ exports[`patches content relationship fields (from Prismic) > broken > shared sl
"items": [
{
"field": {
"id": "__broken__",
"id": "_____broken_____",
"isBroken": true,
"link_type": "Document",
},
},
],
"primary": {
"field": {
"id": "__broken__",
"id": "_____broken_____",
"isBroken": true,
"link_type": "Document",
},
"group": [
{
"field": {
"id": "__broken__",
"id": "_____broken_____",
"isBroken": true,
"link_type": "Document",
},
Expand All @@ -61,15 +61,15 @@ exports[`patches content relationship fields (from Prismic) > broken > slice 1`]
"items": [
{
"field": {
"id": "__broken__",
"id": "_____broken_____",
"isBroken": true,
"link_type": "Document",
},
},
],
"primary": {
"field": {
"id": "__broken__",
"id": "_____broken_____",
"isBroken": true,
"link_type": "Document",
},
Expand All @@ -84,7 +84,7 @@ exports[`patches content relationship fields (from Prismic) > broken > slice 1`]
exports[`patches content relationship fields (from Prismic) > broken > static zone 1`] = `
{
"field": {
"id": "__broken__",
"id": "_____broken_____",
"isBroken": true,
"link_type": "Document",
},
Expand Down
Loading

0 comments on commit 9bab813

Please sign in to comment.