UNPKG

@prismicio/client

Version:

The official JavaScript + TypeScript client library for Prismic

284 lines (282 loc) 9.92 kB
const require_richText = require('./types/value/richText.cjs'); const require_link = require('./types/value/link.cjs'); const require_Asset = require('./types/migration/Asset.cjs'); const require_Document = require('./types/migration/Document.cjs'); const require_isValue = require('./lib/isValue.cjs'); const require_getOptionalLinkProperties = require('./lib/getOptionalLinkProperties.cjs'); const require_validateAssetMetadata = require('./lib/validateAssetMetadata.cjs'); //#region src/Migration.ts /** * A helper that allows preparing your migration to Prismic. * * @typeParam TDocuments - Document types that are registered for the Prismic * repository. Query methods will automatically be typed based on this type. */ var Migration = class { /** * Assets registered in the migration. * * @internal */ _assets = /* @__PURE__ */ new Map(); /** * Documents registered in the migration. * * @internal */ _documents = []; /** * Registers an asset to be created in the migration from a file, an asset * object, or an image or link to media field. * * @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 * the migration is executed through the `writeClient.migrate()` method. * * @returns A migration asset field instance. */ createAsset(fileOrAssetOrField, filename, { notes, credits, alt, tags } = {}) { let config; let maybeInitialField; if (typeof fileOrAssetOrField === "object" && "url" in fileOrAssetOrField) if ("dimensions" in fileOrAssetOrField || "link_type" in fileOrAssetOrField) { const url = fileOrAssetOrField.url.split("?")[0]; const filename$1 = "name" in fileOrAssetOrField ? fileOrAssetOrField.name : url.split("/").pop().split("_").pop(); const credits$1 = "copyright" in fileOrAssetOrField && fileOrAssetOrField.copyright ? fileOrAssetOrField.copyright : void 0; const alt$1 = "alt" in fileOrAssetOrField && fileOrAssetOrField.alt ? fileOrAssetOrField.alt : void 0; if ("dimensions" in fileOrAssetOrField) maybeInitialField = fileOrAssetOrField; config = { id: fileOrAssetOrField.id, file: url, filename: filename$1, notes: void 0, credits: credits$1, alt: alt$1, tags: void 0 }; } else { var _fileOrAssetOrField$t; config = { id: fileOrAssetOrField.id, file: fileOrAssetOrField.url, filename: fileOrAssetOrField.filename, notes: fileOrAssetOrField.notes, credits: fileOrAssetOrField.credits, alt: fileOrAssetOrField.alt, tags: (_fileOrAssetOrField$t = fileOrAssetOrField.tags) === null || _fileOrAssetOrField$t === void 0 ? void 0 : _fileOrAssetOrField$t.map(({ name }) => name) }; } else config = { id: fileOrAssetOrField, file: fileOrAssetOrField, filename, notes, credits, alt, tags }; require_validateAssetMetadata.validateAssetMetadata(config); const migrationAsset = new require_Asset.PrismicMigrationAsset(config, maybeInitialField); const maybeAsset = this._assets.get(config.id); if (maybeAsset) { maybeAsset.config.notes = maybeAsset.config.notes || config.notes; maybeAsset.config.credits = maybeAsset.config.credits || config.credits; maybeAsset.config.alt = maybeAsset.config.alt || config.alt; maybeAsset.config.tags = Array.from(new Set([...maybeAsset.config.tags || [], ...config.tags || []])); } else this._assets.set(config.id, migrationAsset); return migrationAsset; } /** * Registers a document to be created in the migration. * * @remarks * 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. * * @typeParam TType - Type of the Prismic document to create. * * @param document - The document to create. * @param title - The title of the document to create which will be displayed * in the editor. * @param params - Document master language document ID. * * @returns A migration document instance. */ createDocument(document, title, params) { const doc = new require_Document.PrismicMigrationDocument(document, title, params); this._documents.push(doc); return doc; } /** * Registers an existing document to be updated in the migration. * * @remarks * 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 title - The title of the document to update which will be displayed * in the editor. * * @returns A migration document instance. */ updateDocument(document, title) { const doc = new require_Document.PrismicMigrationDocument(document, title); this._documents.push(doc); return doc; } /** * 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 * registers it in your migration. The document will be created when the * migration is executed through the `writeClient.migrate()` method. * * @param document - The document from Prismic to create. * @param title - The title of the document to create which will be displayed * in the editor. * * @returns A migration document instance. */ createDocumentFromPrismic(document, title) { const doc = new require_Document.PrismicMigrationDocument(this.#migratePrismicDocumentData({ type: document.type, lang: document.lang, uid: document.uid, tags: document.tags, data: document.data }), title, { originalPrismicDocument: document }); this._documents.push(doc); return doc; } /** * Queries a document from the migration instance with a specific UID and * custom type. * * @example * * ```ts * const contentRelationship = migration.createContentRelationship(() => * migration.getByUID("blog_post", "my-first-post"), * ) * ``` * * @typeParam TType - Type of the Prismic document returned. * * @param type - The API ID of the document's custom type. * @param uid - The UID of the document. * * @returns The migration document instance with a UID matching the `uid` * parameter, if a matching document is found. */ getByUID(type, uid) { return this._documents.find((doc) => doc.document.type === type && doc.document.uid === uid); } /** * Queries a singleton document from the migration instance for a specific * custom type. * * @example * * ```ts * const contentRelationship = migration.createContentRelationship(() => * migration.getSingle("settings"), * ) * ``` * * @typeParam TType - Type of the Prismic document returned. * * @param type - The API ID of the singleton custom type. * * @returns The migration document instance for the custom type, if a matching * document is found. */ getSingle(type) { return this._documents.find((doc) => doc.document.type === type); } /** * Migrates a Prismic document data from another repository so that it can be * created through the current repository's Migration API. * * @param input - The Prismic document data to migrate. * * @returns The migrated Prismic document data. */ #migratePrismicDocumentData(input) { if (require_isValue.filledContentRelationship(input)) { const optionalLinkProperties = require_getOptionalLinkProperties.getOptionalLinkProperties(input); if (input.isBroken) return { ...optionalLinkProperties, link_type: require_link.LinkType.Document, id: "_____broken_____", isBroken: true }; return { ...optionalLinkProperties, link_type: require_link.LinkType.Document, id: () => this._getByOriginalID(input.id) }; } if (require_isValue.filledLinkToMedia(input)) return { ...require_getOptionalLinkProperties.getOptionalLinkProperties(input), link_type: require_link.LinkType.Media, id: this.createAsset(input) }; if (require_isValue.rtImageNode(input)) { const rtImageNode$1 = { type: require_richText.RichTextNodeType.image, id: this.createAsset(input) }; if (input.linkTo) rtImageNode$1.linkTo = this.#migratePrismicDocumentData(input.linkTo); return rtImageNode$1; } if (require_isValue.filledImage(input)) { const image = { id: this.createAsset(input) }; const { id: _id, url: _url, dimensions: _dimensions, edit: _edit, alt: _alt, copyright: _copyright,...thumbnails } = input; for (const name in thumbnails) if (require_isValue.filledImage(thumbnails[name])) image[name] = this.createAsset(thumbnails[name]); return image; } if (Array.isArray(input)) return input.map((element) => this.#migratePrismicDocumentData(element)); if (input && typeof input === "object") { const res = {}; for (const key in input) res[key] = this.#migratePrismicDocumentData(input[key]); return res; } return input; } /** * Queries a document from the migration instance for a specific original ID. * * @example * * ```ts * const contentRelationship = migration.createContentRelationship(() => * migration._getByOriginalID("YhdrDxIAACgAcp_b"), * ) * ``` * * @typeParam TType - Type of the Prismic document returned. * * @param id - The original ID of the Prismic document. * * @returns The migration document instance for the original ID, if a matching * document is found. * * @internal */ _getByOriginalID(id) { return this._documents.find((doc) => { var _doc$originalPrismicD; return ((_doc$originalPrismicD = doc.originalPrismicDocument) === null || _doc$originalPrismicD === void 0 ? void 0 : _doc$originalPrismicD.id) === id; }); } }; //#endregion exports.Migration = Migration; //# sourceMappingURL=Migration.cjs.map