Skip to content

Commit

Permalink
refactor: use random API key pool for migration API demo
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Sep 9, 2024
1 parent daac44f commit 7803461
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
1 change: 0 additions & 1 deletion messages/avoid-write-client-in-browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as prismic from "@prismicio/client";

const writeClient = prismic.createWriteClient("example-prismic-repo", {
writeToken: "xxx"
migrationAPIKey: "yyy"
})
```

Expand Down
53 changes: 51 additions & 2 deletions src/WriteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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}/`
Expand Down
1 change: 0 additions & 1 deletion test/__testutils__/createWriteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
14 changes: 10 additions & 4 deletions test/writeClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
Expand All @@ -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/")
Expand All @@ -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")
})

0 comments on commit 7803461

Please sign in to comment.