-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
getToolbarSrc()
helper (#281)
- Loading branch information
1 parent
d4720ad
commit f0e768c
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { PrismicError } from "./errors/PrismicError"; | ||
|
||
import { isRepositoryName } from "./isRepositoryName"; | ||
|
||
/** | ||
* Returns the URL for a Prismic repository's Prismic Toolbar script. Use the | ||
* URL to inject the script into your app. | ||
* | ||
* @example | ||
* | ||
* ```typescriptreact | ||
* // In Next.js apps, use `next/script` in your `app/layout.tsx` file. | ||
* | ||
* import Script from "next/script"; | ||
* import * as prismic from '@prismicio/client' | ||
* | ||
* export default function RootLayout({ | ||
* children, | ||
* }: { | ||
* children: React.ReactNode, | ||
* }) { | ||
* const toolbarSrc = prismic.getToolbarSrc("my-repo"); | ||
* | ||
* return ( | ||
* <html lang="en"> | ||
* <body>{children}</body> | ||
* <Script src={toolbarSrc} /> | ||
* </html> | ||
* ); | ||
* } | ||
* ``` | ||
* | ||
* @param repositoryName - The name of the Prismic repository. For example, | ||
* `"my-repo"` if the repository URL is `my-repo.prismic.io`. | ||
* | ||
* @returns The URL for the given Prismic repository's Prismic Toolbar script. | ||
*/ | ||
export const getToolbarSrc = <TRepositoryName extends string>( | ||
repositoryName: TRepositoryName, | ||
): `https://static.cdn.prismic.io/prismic.js?new=true&repo=${TRepositoryName}` => { | ||
if (isRepositoryName(repositoryName)) { | ||
return `https://static.cdn.prismic.io/prismic.js?new=true&repo=${repositoryName}` as const; | ||
} else { | ||
throw new PrismicError( | ||
`An invalid Prismic repository name was given: ${repositoryName}`, | ||
undefined, | ||
undefined, | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect, it } from "vitest"; | ||
|
||
import * as prismic from "../src"; | ||
|
||
it("returns a URL for the Prismic Toolbar script", () => { | ||
const endpoint = prismic.getToolbarSrc("qwerty"); | ||
|
||
expect(endpoint).toBe( | ||
"https://static.cdn.prismic.io/prismic.js?new=true&repo=qwerty", | ||
); | ||
}); | ||
|
||
it("throws if an invalid repository name is given", () => { | ||
expect(() => { | ||
prismic.getToolbarSrc("this is invalid"); | ||
}).toThrowError( | ||
/An invalid Prismic repository name was given: this is invalid/i, | ||
); | ||
expect(() => { | ||
prismic.getToolbarSrc("this is invalid"); | ||
}).toThrowError(prismic.PrismicError); | ||
}); |