diff --git a/messages/avoid-write-client-in-browser.md b/messages/avoid-write-client-in-browser.md index c20834ce..d88dbc9f 100644 --- a/messages/avoid-write-client-in-browser.md +++ b/messages/avoid-write-client-in-browser.md @@ -9,7 +9,6 @@ import * as prismic from "@prismicio/client"; const writeClient = prismic.createWriteClient("example-prismic-repo", { writeToken: "xxx" - migrationAPIKey: "yyy" }) ``` diff --git a/src/WriteClient.ts b/src/WriteClient.ts index a2e75851..3f42feba 100644 --- a/src/WriteClient.ts +++ b/src/WriteClient.ts @@ -224,6 +224,18 @@ const ASSET_CREDITS_MAX_LENGTH = 500 */ const ASSET_ALT_MAX_LENGTH = 500 +/** + * Prismic migration API demo keys. + */ +const MIGRATION_API_DEMO_KEYS = [ + "cSaZlfkQlF9C6CEAM2Del6MNX9WonlV86HPbeEJL", + "pZCexCajUQ4jriYwIGSxA1drZrFxDyFf1S0D1K0P", + "Yc0mfrkGDw8gaaGKTrzwC3QUZDajv6k73DA99vWN", + "ySzSEbVMAb5S1oSCQfbVG4mbh9Cb8wlF7BCvKI0L", + "g2DA3EKWvx8uxVYcNFrmT5nJpon1Vi9V4XcOibJD", + "CCNIlI0Vz41J66oFwsHUXaZa6NYFIY6z7aDF62Bc", +] + /** * Checks if a string is an asset tag ID. * @@ -297,12 +309,45 @@ export const validateAssetMetadata = ({ * Configuration for clients that determine how content is queried. */ export type WriteClientConfig = { + /** + * A Prismic write token that allows writing content to the repository. + */ writeToken: string - migrationAPIKey: string + /** + * An explicit Prismic migration API key that allows working with the + * migration API. If none is provided, the client will pick a random one to + * authenticate your requests. + * + * @remarks + * Those keys are the same for all Prismic users. They are only useful while + * the migration API is in beta to reduce load. It should be one of: + * + * - `cSaZlfkQlF9C6CEAM2Del6MNX9WonlV86HPbeEJL` + * - `pZCexCajUQ4jriYwIGSxA1drZrFxDyFf1S0D1K0P` + * - `Yc0mfrkGDw8gaaGKTrzwC3QUZDajv6k73DA99vWN` + * - `ySzSEbVMAb5S1oSCQfbVG4mbh9Cb8wlF7BCvKI0L` + * - `g2DA3EKWvx8uxVYcNFrmT5nJpon1Vi9V4XcOibJD` + * - `CCNIlI0Vz41J66oFwsHUXaZa6NYFIY6z7aDF62Bc` + */ + migrationAPIKey?: string + /** + * The Prismic asset API endpoint. + * + * @defaultValue `"https://asset-api.prismic.io/"` + * + * @see Prismic asset API technical references: {@link https://prismic.io/docs/asset-api-technical-reference} + */ assetAPIEndpoint?: string + /** + * The Prismic migration API endpoint. + * + * @defaultValue `"https://migration.prismic.io/"` + * + * @see Prismic migration API technical references: {@link https://prismic.io/docs/migration-api-technical-reference} + */ migrationAPIEndpoint?: string } & ClientConfig @@ -349,7 +394,11 @@ export class WriteClient< } this.writeToken = options.writeToken - this.migrationAPIKey = options.migrationAPIKey + this.migrationAPIKey = + options.migrationAPIKey || + MIGRATION_API_DEMO_KEYS[ + Math.floor(Math.random() * MIGRATION_API_DEMO_KEYS.length) + ] if (options.assetAPIEndpoint) { this.assetAPIEndpoint = `${options.assetAPIEndpoint}/` diff --git a/test/__testutils__/createWriteClient.ts b/test/__testutils__/createWriteClient.ts index 962d8ceb..32ebde3b 100644 --- a/test/__testutils__/createWriteClient.ts +++ b/test/__testutils__/createWriteClient.ts @@ -21,7 +21,6 @@ export const createTestWriteClient = ( return prismic.createWriteClient(repositoryName, { fetch, writeToken: "xxx", - migrationAPIKey: "yyy", // We create unique endpoints so we can run tests concurrently assetAPIEndpoint: `https://${repositoryName}.asset-api.prismic.io`, migrationAPIEndpoint: `https://${repositoryName}.migration.prismic.io`, diff --git a/test/writeClient.test.ts b/test/writeClient.test.ts index b2ba0466..55381e7c 100644 --- a/test/writeClient.test.ts +++ b/test/writeClient.test.ts @@ -6,7 +6,6 @@ it("`createWriteClient` creates a write client", () => { const client = prismic.createWriteClient("qwerty", { fetch: vi.fn(), writeToken: "xxx", - migrationAPIKey: "yyy", }) expect(client).toBeInstanceOf(prismic.WriteClient) @@ -23,7 +22,6 @@ it("constructor warns if running in a browser-like environment", () => { prismic.createWriteClient("qwerty", { fetch: vi.fn(), writeToken: "xxx", - migrationAPIKey: "yyy", }) expect(consoleWarnSpy).toHaveBeenCalledWith( expect.stringMatching(/avoid-write-client-in-browser/i), @@ -39,7 +37,6 @@ it("uses provided asset API endpoint and adds `/` suffix", () => { fetch: vi.fn(), assetAPIEndpoint: "https://example.com", writeToken: "xxx", - migrationAPIKey: "yyy", }) expect(client.assetAPIEndpoint).toBe("https://example.com/") @@ -50,8 +47,17 @@ it("uses provided migration API endpoint and adds `/` suffix", () => { fetch: vi.fn(), migrationAPIEndpoint: "https://example.com", writeToken: "xxx", - migrationAPIKey: "yyy", }) expect(client.migrationAPIEndpoint).toBe("https://example.com/") }) + +it("uses provided migration API key", () => { + const client = prismic.createWriteClient("qwerty", { + fetch: vi.fn(), + writeToken: "xxx", + migrationAPIKey: "yyy", + }) + + expect(client.migrationAPIKey).toBe("yyy") +})