Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getToolbarSrc() helper #281

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/getToolbarSrc.ts
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,
);
}
};
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export { isRepositoryName } from "./isRepositoryName";
export { isRepositoryEndpoint } from "./isRepositoryEndpoint";
export { buildQueryURL } from "./buildQueryURL";

// Toolbar helpers.
export { getToolbarSrc } from "./getToolbarSrc";

// Query filters API.
/**
* @deprecated Renamed to `filter`
Expand Down
22 changes: 22 additions & 0 deletions test/getToolbarSrc.test.ts
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);
});