Skip to content

Commit

Permalink
fix: allow not updating document's title
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Sep 18, 2024
1 parent 9bab813 commit 30de0d1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
45 changes: 26 additions & 19 deletions src/Migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
*
* @remarks
* This method does not create the asset in Prismic media library right away.
* Instead it registers it in your migration. The asset will be created when
* Instead, it registers it in your migration. The asset will be created when
* the migration is executed through the `writeClient.migrate()` method.
*
* @param asset - An asset object from Prismic Asset API.
*
* @returns A migration asset field instance.
*
* @internal
*/
createAsset(asset: Asset): PrismicMigrationAsset

Expand All @@ -78,13 +80,15 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
*
* @remarks
* This method does not create the asset in Prismic media library right away.
* Instead it registers it in your migration. The asset will be created when
* Instead, it registers it in your migration. The asset will be created when
* the migration is executed through the `writeClient.migrate()` method.
*
* @param imageOrLinkToMediaField - An image or link to media field from
* Prismic Document API.
*
* @returns A migration asset field instance.
*
* @internal
*/
createAsset(
imageOrLinkToMediaField: FilledImageFieldImage | FilledLinkToMediaField,
Expand All @@ -95,7 +99,7 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
*
* @remarks
* This method does not create the asset in Prismic media library right away.
* Instead it registers it in your migration. The asset will be created when
* Instead, it registers it in your migration. The asset will be created when
* the migration is executed through the `writeClient.migrate()` method.
*
* @param file - The URL or content of the file to be created.
Expand All @@ -121,7 +125,7 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
*
* @remarks
* This method does not create the asset in Prismic media library right away.
* Instead it registers it in your migration. The asset will be created when
* Instead, it registers it in your migration. The asset will be created when
* the migration is executed through the `writeClient.migrate()` method.
*
* @returns A migration asset field instance.
Expand Down Expand Up @@ -227,7 +231,7 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
* Registers a document to be created in the migration.
*
* @remarks
* This method does not create the document in Prismic right away. Instead it
* This method does not create the document in Prismic right away. Instead, it
* registers it in your migration. The document will be created when the
* migration is executed through the `writeClient.migrate()` method.
*
Expand All @@ -236,20 +240,20 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
* @param document - The document to create.
* @param title - The title of the document to create which will be displayed
* in the editor.
* @param options - Document master language document ID.
* @param params - Document master language document ID.
*
* @returns A migration document instance.
*/
createDocument<TType extends TDocuments["type"]>(
document: ExtractDocumentType<PendingPrismicDocument<TDocuments>, TType>,
title: string,
options?: {
params?: {
masterLanguageDocument?: MigrationContentRelationship
},
): PrismicMigrationDocument<ExtractDocumentType<TDocuments, TType>> {
const doc = new PrismicMigrationDocument<
ExtractDocumentType<TDocuments, TType>
>(document, title, options)
>(document, title, params)

this._documents.push(doc)

Expand All @@ -260,21 +264,22 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
* Registers an existing document to be updated in the migration.
*
* @remarks
* This method does not update the document in Prismic right away. Instead it
* This method does not update the document in Prismic right away. Instead, it
* registers it in your migration. The document will be updated when the
* migration is executed through the `writeClient.migrate()` method.
*
* @typeParam TType - Type of Prismic documents to update.
*
* @param document - The document to update.
* @param documentTitle - The title of the document to update which will be
* displayed in the editor.
* @param title - The title of the document to update which will be displayed
* in the editor.
*
* @returns A migration document instance.
*/
updateDocument<TType extends TDocuments["type"]>(
document: ExtractDocumentType<ExistingPrismicDocument<TDocuments>, TType>,
title: string,
// Title is optional for existing documents as we might not want to update it.
title?: string,
): PrismicMigrationDocument<ExtractDocumentType<TDocuments, TType>> {
const doc = new PrismicMigrationDocument<
ExtractDocumentType<TDocuments, TType>
Expand All @@ -286,17 +291,17 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
}

/**
* Registers a document to be created in the migration.
* Registers a document from another Prismic repository to be created in the
* migration.
*
* @remarks
* This method does not create the document in Prismic right away. Instead it
* This method does not create the document in Prismic right away. Instead, it
* registers it in your migration. The document will be created when the
* migration is executed through the `writeClient.migrate()` method.
*
* @param document - The document to create.
* @param document - The document from Prismic to create.
* @param title - The title of the document to create which will be displayed
* in the editor.
* @param options - Document master language document ID.
*
* @returns A migration document instance.
*/
Expand Down Expand Up @@ -413,7 +418,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 @@ -497,7 +502,7 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
*
* ```ts
* const contentRelationship = migration.createContentRelationship(() =>
* migration.getByOriginalID("YhdrDxIAACgAcp_b"),
* migration._getByOriginalID("YhdrDxIAACgAcp_b"),
* )
* ```
*
Expand All @@ -507,8 +512,10 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
*
* @returns The migration document instance for the original ID, if a matching
* document is found.
*
* @internal
*/
getByOriginalID<TType extends TDocuments["type"]>(
_getByOriginalID<TType extends TDocuments["type"]>(
id: string,
):
| PrismicMigrationDocument<ExtractDocumentType<TDocuments, TType>>
Expand Down
4 changes: 2 additions & 2 deletions src/WriteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,14 @@ export class WriteClient<

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

const { id } = await this.createDocument(
// We'll upload docuements data later on.
{ ...doc.document, data: {} },
doc.title,
doc.title!,
{
masterLanguageDocumentID,
...fetchParams,
Expand Down
12 changes: 6 additions & 6 deletions src/types/migration/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class PrismicMigrationDocument<
/**
* The name of the document displayed in the editor.
*/
title: string
title?: string

/**
* The link to the master language document to relate the document to if any.
Expand All @@ -126,22 +126,22 @@ export class PrismicMigrationDocument<
*
* @param document - The document to be sent to the Migration API.
* @param title - The name of the document displayed in the editor.
* @param options - Parameters to create/update the document with on the
* @param params - Parameters to create/update the document with on the
* Migration API.
*
* @returns A Prismic migration document instance.
*/
constructor(
document: MigrationDocument<TDocument>,
title: string,
options?: {
title?: string,
params?: {
masterLanguageDocument?: MigrationContentRelationship
originalPrismicDocument?: ExistingPrismicDocument<PrismicDocument>
},
) {
this.document = document
this.title = title
this.masterLanguageDocument = options?.masterLanguageDocument
this.originalPrismicDocument = options?.originalPrismicDocument
this.masterLanguageDocument = params?.masterLanguageDocument
this.originalPrismicDocument = params?.originalPrismicDocument
}
}

0 comments on commit 30de0d1

Please sign in to comment.