@prismicio/next 0.2.0-alpha.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +3 -1
  2. package/dist/PrismicNextImage.cjs +86 -0
  3. package/dist/PrismicNextImage.cjs.map +1 -0
  4. package/dist/PrismicNextImage.d.ts +52 -0
  5. package/dist/PrismicNextImage.js +69 -0
  6. package/dist/PrismicNextImage.js.map +1 -0
  7. package/dist/PrismicPreview.cjs +83 -0
  8. package/dist/PrismicPreview.cjs.map +1 -0
  9. package/dist/PrismicPreview.d.ts +42 -0
  10. package/dist/PrismicPreview.js +66 -0
  11. package/dist/PrismicPreview.js.map +1 -0
  12. package/dist/enableAutoPreviews.cjs +17 -0
  13. package/dist/enableAutoPreviews.cjs.map +1 -0
  14. package/dist/enableAutoPreviews.d.ts +38 -0
  15. package/dist/enableAutoPreviews.js +17 -0
  16. package/dist/enableAutoPreviews.js.map +1 -0
  17. package/dist/exitPreview.cjs +8 -0
  18. package/dist/exitPreview.cjs.map +1 -0
  19. package/dist/exitPreview.d.ts +30 -0
  20. package/dist/exitPreview.js +8 -0
  21. package/dist/exitPreview.js.map +1 -0
  22. package/dist/imgixLoader.cjs +17 -0
  23. package/dist/imgixLoader.cjs.map +1 -0
  24. package/dist/imgixLoader.d.ts +9 -0
  25. package/dist/imgixLoader.js +17 -0
  26. package/dist/imgixLoader.js.map +1 -0
  27. package/dist/index.cjs +17 -182
  28. package/dist/index.cjs.map +1 -0
  29. package/dist/index.d.ts +8 -204
  30. package/dist/index.js +17 -153
  31. package/dist/index.js.map +1 -0
  32. package/dist/lib/__PRODUCTION__.cjs +5 -0
  33. package/dist/lib/__PRODUCTION__.cjs.map +1 -0
  34. package/dist/lib/__PRODUCTION__.d.ts +7 -0
  35. package/dist/lib/__PRODUCTION__.js +5 -0
  36. package/dist/lib/__PRODUCTION__.js.map +1 -0
  37. package/dist/lib/devMsg.cjs +8 -0
  38. package/dist/lib/devMsg.cjs.map +1 -0
  39. package/dist/lib/devMsg.d.ts +16 -0
  40. package/dist/lib/devMsg.js +8 -0
  41. package/dist/lib/devMsg.js.map +1 -0
  42. package/dist/lib/getPreviewCookieRepositoryName.cjs +7 -0
  43. package/dist/lib/getPreviewCookieRepositoryName.cjs.map +1 -0
  44. package/dist/lib/getPreviewCookieRepositoryName.d.ts +9 -0
  45. package/dist/lib/getPreviewCookieRepositoryName.js +7 -0
  46. package/dist/lib/getPreviewCookieRepositoryName.js.map +1 -0
  47. package/dist/lib/getPrismicPreviewCookie.cjs +38 -0
  48. package/dist/lib/getPrismicPreviewCookie.cjs.map +1 -0
  49. package/dist/lib/getPrismicPreviewCookie.d.ts +9 -0
  50. package/dist/lib/getPrismicPreviewCookie.js +21 -0
  51. package/dist/lib/getPrismicPreviewCookie.js.map +1 -0
  52. package/dist/package.json.cjs +5 -0
  53. package/dist/package.json.cjs.map +1 -0
  54. package/dist/package.json.js +5 -0
  55. package/dist/package.json.js.map +1 -0
  56. package/dist/redirectToPreviewURL.cjs +22 -0
  57. package/dist/redirectToPreviewURL.cjs.map +1 -0
  58. package/dist/redirectToPreviewURL.d.ts +59 -0
  59. package/dist/redirectToPreviewURL.js +22 -0
  60. package/dist/redirectToPreviewURL.js.map +1 -0
  61. package/dist/setPreviewData.cjs +28 -0
  62. package/dist/setPreviewData.cjs.map +1 -0
  63. package/dist/setPreviewData.d.ts +29 -0
  64. package/dist/setPreviewData.js +11 -0
  65. package/dist/setPreviewData.js.map +1 -0
  66. package/dist/types.d.ts +21 -0
  67. package/package.json +40 -35
  68. package/src/PrismicNextImage.tsx +141 -0
  69. package/src/PrismicPreview.tsx +80 -93
  70. package/src/enableAutoPreviews.ts +4 -2
  71. package/src/exitPreview.ts +15 -7
  72. package/src/imgixLoader.ts +25 -0
  73. package/src/index.ts +16 -11
  74. package/src/lib/__PRODUCTION__.ts +7 -0
  75. package/src/lib/devMsg.ts +20 -0
  76. package/src/lib/getPreviewCookieRepositoryName.ts +14 -0
  77. package/src/lib/getPrismicPreviewCookie.ts +33 -0
  78. package/src/redirectToPreviewURL.ts +11 -6
  79. package/src/setPreviewData.ts +7 -2
  80. package/src/types.ts +3 -2
  81. package/src/lib/extractPreviewRefRepositoryName.ts +0 -60
  82. package/src/lib/getCookie.ts +0 -51
@@ -0,0 +1,7 @@
1
+ /**
2
+ * `true` if in the production environment, `false` otherwise.
3
+ *
4
+ * This boolean can be used to perform actions only in development environments,
5
+ * such as logging.
6
+ */
7
+ export declare const __PRODUCTION__: boolean;
@@ -0,0 +1,5 @@
1
+ const __PRODUCTION__ = process.env.NODE_ENV === "production";
2
+ export {
3
+ __PRODUCTION__
4
+ };
5
+ //# sourceMappingURL=__PRODUCTION__.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"__PRODUCTION__.js","sources":["../../src/lib/__PRODUCTION__.ts"],"sourcesContent":["/**\n * `true` if in the production environment, `false` otherwise.\n *\n * This boolean can be used to perform actions only in development environments,\n * such as logging.\n */\nexport const __PRODUCTION__ = process.env.NODE_ENV === \"production\";\n"],"names":[],"mappings":"AAMa,MAAA,iBAAiB,QAAQ,IAAI,aAAa;"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const _package = require("../package.json.cjs");
4
+ const devMsg = (slug) => {
5
+ return `https://prismic.dev/msg/next/v${_package.version}/${slug}`;
6
+ };
7
+ exports.devMsg = devMsg;
8
+ //# sourceMappingURL=devMsg.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"devMsg.cjs","sources":["../../src/lib/devMsg.ts"],"sourcesContent":["import { version } from \"../../package.json\";\n\n/**\n * Returns a `prismic.dev/msg` URL for a given message slug.\n *\n * @example\n *\n * ```ts\n * devMsg(\"missing-param\");\n * // => \"https://prismic.dev/msg/next/v1.2.3/missing-param.md\"\n * ```\n *\n * @param slug - Slug for the message. This corresponds to a Markdown file in\n * the Git repository's `/messages` directory.\n *\n * @returns The `prismic.dev/msg` URL for the given slug.\n */\nexport const devMsg = (slug: string) => {\n\treturn `https://prismic.dev/msg/next/v${version}/${slug}`;\n};\n"],"names":["version"],"mappings":";;;AAiBa,MAAA,SAAS,CAAC,SAAgB;AACtC,SAAO,iCAAiCA,SAAAA,WAAW;AACpD;;"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Returns a `prismic.dev/msg` URL for a given message slug.
3
+ *
4
+ * @example
5
+ *
6
+ * ```ts
7
+ * devMsg("missing-param");
8
+ * // => "https://prismic.dev/msg/next/v1.2.3/missing-param.md"
9
+ * ```
10
+ *
11
+ * @param slug - Slug for the message. This corresponds to a Markdown file in
12
+ * the Git repository's `/messages` directory.
13
+ *
14
+ * @returns The `prismic.dev/msg` URL for the given slug.
15
+ */
16
+ export declare const devMsg: (slug: string) => string;
@@ -0,0 +1,8 @@
1
+ import { version } from "../package.json.js";
2
+ const devMsg = (slug) => {
3
+ return `https://prismic.dev/msg/next/v${version}/${slug}`;
4
+ };
5
+ export {
6
+ devMsg
7
+ };
8
+ //# sourceMappingURL=devMsg.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"devMsg.js","sources":["../../src/lib/devMsg.ts"],"sourcesContent":["import { version } from \"../../package.json\";\n\n/**\n * Returns a `prismic.dev/msg` URL for a given message slug.\n *\n * @example\n *\n * ```ts\n * devMsg(\"missing-param\");\n * // => \"https://prismic.dev/msg/next/v1.2.3/missing-param.md\"\n * ```\n *\n * @param slug - Slug for the message. This corresponds to a Markdown file in\n * the Git repository's `/messages` directory.\n *\n * @returns The `prismic.dev/msg` URL for the given slug.\n */\nexport const devMsg = (slug: string) => {\n\treturn `https://prismic.dev/msg/next/v${version}/${slug}`;\n};\n"],"names":[],"mappings":";AAiBa,MAAA,SAAS,CAAC,SAAgB;AACtC,SAAO,iCAAiC,WAAW;AACpD;"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const getPreviewCookieRepositoryName = (previewCookie) => {
4
+ return (decodeURIComponent(previewCookie).match(/"(.+).prismic.io"/) || [])[1];
5
+ };
6
+ exports.getPreviewCookieRepositoryName = getPreviewCookieRepositoryName;
7
+ //# sourceMappingURL=getPreviewCookieRepositoryName.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPreviewCookieRepositoryName.cjs","sources":["../../src/lib/getPreviewCookieRepositoryName.ts"],"sourcesContent":["/**\n * Extracts preview reference repo name from stringified Prismic preview cookie\n *\n * @param previewCookie - The Prismic preview cookie.\n *\n * @returns The repository name for the Prismic preview cookie. If the cookie\n * represents an inactive preview session, `undefined` will be returned.\n */\nexport const getPreviewCookieRepositoryName = (\n\tpreviewCookie: string,\n): string | undefined => {\n\treturn (decodeURIComponent(previewCookie).match(/\"(.+).prismic.io\"/) ||\n\t\t[])[1];\n};\n"],"names":[],"mappings":";;AAQa,MAAA,iCAAiC,CAC7C,kBACuB;AACvB,UAAQ,mBAAmB,aAAa,EAAE,MAAM,mBAAmB,KAClE,CAAA,GAAI;AACN;;"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Extracts preview reference repo name from stringified Prismic preview cookie
3
+ *
4
+ * @param previewCookie - The Prismic preview cookie.
5
+ *
6
+ * @returns The repository name for the Prismic preview cookie. If the cookie
7
+ * represents an inactive preview session, `undefined` will be returned.
8
+ */
9
+ export declare const getPreviewCookieRepositoryName: (previewCookie: string) => string | undefined;
@@ -0,0 +1,7 @@
1
+ const getPreviewCookieRepositoryName = (previewCookie) => {
2
+ return (decodeURIComponent(previewCookie).match(/"(.+).prismic.io"/) || [])[1];
3
+ };
4
+ export {
5
+ getPreviewCookieRepositoryName
6
+ };
7
+ //# sourceMappingURL=getPreviewCookieRepositoryName.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPreviewCookieRepositoryName.js","sources":["../../src/lib/getPreviewCookieRepositoryName.ts"],"sourcesContent":["/**\n * Extracts preview reference repo name from stringified Prismic preview cookie\n *\n * @param previewCookie - The Prismic preview cookie.\n *\n * @returns The repository name for the Prismic preview cookie. If the cookie\n * represents an inactive preview session, `undefined` will be returned.\n */\nexport const getPreviewCookieRepositoryName = (\n\tpreviewCookie: string,\n): string | undefined => {\n\treturn (decodeURIComponent(previewCookie).match(/\"(.+).prismic.io\"/) ||\n\t\t[])[1];\n};\n"],"names":[],"mappings":"AAQa,MAAA,iCAAiC,CAC7C,kBACuB;AACvB,UAAQ,mBAAmB,aAAa,EAAE,MAAM,mBAAmB,KAClE,CAAA,GAAI;AACN;"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const prismic = require("@prismicio/client");
4
+ function _interopNamespaceDefault(e) {
5
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
6
+ if (e) {
7
+ for (const k in e) {
8
+ if (k !== "default") {
9
+ const d = Object.getOwnPropertyDescriptor(e, k);
10
+ Object.defineProperty(n, k, d.get ? d : {
11
+ enumerable: true,
12
+ get: () => e[k]
13
+ });
14
+ }
15
+ }
16
+ }
17
+ n.default = e;
18
+ return Object.freeze(n);
19
+ }
20
+ const prismic__namespace = /* @__PURE__ */ _interopNamespaceDefault(prismic);
21
+ const readValue = (value) => {
22
+ return value.replace(/%3B/g, ";");
23
+ };
24
+ const getPrismicPreviewCookie = (cookieJar) => {
25
+ const cookies = cookieJar.split("; ");
26
+ let value;
27
+ for (const cookie of cookies) {
28
+ const parts = cookie.split("=");
29
+ const name = readValue(parts[0]).replace(/%3D/g, "=");
30
+ if (name === prismic__namespace.cookie.preview) {
31
+ value = readValue(parts.slice(1).join("="));
32
+ continue;
33
+ }
34
+ }
35
+ return value;
36
+ };
37
+ exports.getPrismicPreviewCookie = getPrismicPreviewCookie;
38
+ //# sourceMappingURL=getPrismicPreviewCookie.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPrismicPreviewCookie.cjs","sources":["../../src/lib/getPrismicPreviewCookie.ts"],"sourcesContent":["import * as prismic from \"@prismicio/client\";\n\nconst readValue = (value: string): string => {\n\treturn value.replace(/%3B/g, \";\");\n};\n\n/**\n * Returns the value of a cookie from a given cookie store.\n *\n * @param cookieJar - The stringified cookie store from which to read the\n * cookie.\n *\n * @returns The value of the cookie, if it exists.\n */\nexport const getPrismicPreviewCookie = (\n\tcookieJar: string,\n): string | undefined => {\n\tconst cookies = cookieJar.split(\"; \");\n\n\tlet value: string | undefined;\n\n\tfor (const cookie of cookies) {\n\t\tconst parts = cookie.split(\"=\");\n\t\tconst name = readValue(parts[0]).replace(/%3D/g, \"=\");\n\n\t\tif (name === prismic.cookie.preview) {\n\t\t\tvalue = readValue(parts.slice(1).join(\"=\"));\n\t\t\tcontinue;\n\t\t}\n\t}\n\n\treturn value;\n};\n"],"names":["prismic"],"mappings":";;;;;;;;;;;;;;;;;;;;AAEA,MAAM,YAAY,CAAC,UAAyB;AACpC,SAAA,MAAM,QAAQ,QAAQ,GAAG;AACjC;AAUa,MAAA,0BAA0B,CACtC,cACuB;AACjB,QAAA,UAAU,UAAU,MAAM,IAAI;AAEhC,MAAA;AAEJ,aAAW,UAAU,SAAS;AACvB,UAAA,QAAQ,OAAO,MAAM,GAAG;AAC9B,UAAM,OAAO,UAAU,MAAM,EAAE,EAAE,QAAQ,QAAQ,GAAG;AAEhD,QAAA,SAASA,mBAAQ,OAAO,SAAS;AACpC,cAAQ,UAAU,MAAM,MAAM,CAAC,EAAE,KAAK,GAAG,CAAC;AAC1C;AAAA,IACA;AAAA,EACD;AAEM,SAAA;AACR;;"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Returns the value of a cookie from a given cookie store.
3
+ *
4
+ * @param cookieJar - The stringified cookie store from which to read the
5
+ * cookie.
6
+ *
7
+ * @returns The value of the cookie, if it exists.
8
+ */
9
+ export declare const getPrismicPreviewCookie: (cookieJar: string) => string | undefined;
@@ -0,0 +1,21 @@
1
+ import * as prismic from "@prismicio/client";
2
+ const readValue = (value) => {
3
+ return value.replace(/%3B/g, ";");
4
+ };
5
+ const getPrismicPreviewCookie = (cookieJar) => {
6
+ const cookies = cookieJar.split("; ");
7
+ let value;
8
+ for (const cookie of cookies) {
9
+ const parts = cookie.split("=");
10
+ const name = readValue(parts[0]).replace(/%3D/g, "=");
11
+ if (name === prismic.cookie.preview) {
12
+ value = readValue(parts.slice(1).join("="));
13
+ continue;
14
+ }
15
+ }
16
+ return value;
17
+ };
18
+ export {
19
+ getPrismicPreviewCookie
20
+ };
21
+ //# sourceMappingURL=getPrismicPreviewCookie.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPrismicPreviewCookie.js","sources":["../../src/lib/getPrismicPreviewCookie.ts"],"sourcesContent":["import * as prismic from \"@prismicio/client\";\n\nconst readValue = (value: string): string => {\n\treturn value.replace(/%3B/g, \";\");\n};\n\n/**\n * Returns the value of a cookie from a given cookie store.\n *\n * @param cookieJar - The stringified cookie store from which to read the\n * cookie.\n *\n * @returns The value of the cookie, if it exists.\n */\nexport const getPrismicPreviewCookie = (\n\tcookieJar: string,\n): string | undefined => {\n\tconst cookies = cookieJar.split(\"; \");\n\n\tlet value: string | undefined;\n\n\tfor (const cookie of cookies) {\n\t\tconst parts = cookie.split(\"=\");\n\t\tconst name = readValue(parts[0]).replace(/%3D/g, \"=\");\n\n\t\tif (name === prismic.cookie.preview) {\n\t\t\tvalue = readValue(parts.slice(1).join(\"=\"));\n\t\t\tcontinue;\n\t\t}\n\t}\n\n\treturn value;\n};\n"],"names":[],"mappings":";AAEA,MAAM,YAAY,CAAC,UAAyB;AACpC,SAAA,MAAM,QAAQ,QAAQ,GAAG;AACjC;AAUa,MAAA,0BAA0B,CACtC,cACuB;AACjB,QAAA,UAAU,UAAU,MAAM,IAAI;AAEhC,MAAA;AAEJ,aAAW,UAAU,SAAS;AACvB,UAAA,QAAQ,OAAO,MAAM,GAAG;AAC9B,UAAM,OAAO,UAAU,MAAM,EAAE,EAAE,QAAQ,QAAQ,GAAG;AAEhD,QAAA,SAAS,QAAQ,OAAO,SAAS;AACpC,cAAQ,UAAU,MAAM,MAAM,CAAC,EAAE,KAAK,GAAG,CAAC;AAC1C;AAAA,IACA;AAAA,EACD;AAEM,SAAA;AACR;"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const version = "0.2.0";
4
+ exports.version = version;
5
+ //# sourceMappingURL=package.json.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.json.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
@@ -0,0 +1,5 @@
1
+ const version = "0.2.0";
2
+ export {
3
+ version
4
+ };
5
+ //# sourceMappingURL=package.json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.json.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const isPrismicNextQuery = (query) => {
4
+ return typeof query.documentId === "string" && typeof query.token === "string";
5
+ };
6
+ async function redirectToPreviewURL(config) {
7
+ const defaultURL = config.defaultURL || "/";
8
+ const basePath = config.basePath || "";
9
+ if (isPrismicNextQuery(config.req.query)) {
10
+ const previewUrl = await config.client.resolvePreviewURL({
11
+ linkResolver: config.linkResolver,
12
+ defaultURL,
13
+ documentID: config.req.query.documentId,
14
+ previewToken: config.req.query.token
15
+ });
16
+ config.res.redirect(basePath + previewUrl);
17
+ return;
18
+ }
19
+ config.res.redirect(basePath + defaultURL);
20
+ }
21
+ exports.redirectToPreviewURL = redirectToPreviewURL;
22
+ //# sourceMappingURL=redirectToPreviewURL.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redirectToPreviewURL.cjs","sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import type { Client } from \"@prismicio/client\";\nimport type { NextApiRequest, NextApiResponse } from \"next\";\nimport type { LinkResolverFunction } from \"@prismicio/helpers\";\n\ntype PrismicNextQuery = {\n\tdocumentId: string;\n\ttoken: string;\n};\n\n/**\n * Determines if a query object from a Next.js API route request contains\n * Prismic preview data.\n *\n * @param query - Query object to check.\n *\n * @returns `true` if `query` contains Prismic preview data, `false` otherwise.\n */\nconst isPrismicNextQuery = (\n\tquery: NextApiRequest[\"query\"],\n): query is PrismicNextQuery => {\n\treturn (\n\t\ttypeof query.documentId === \"string\" && typeof query.token === \"string\"\n\t);\n};\n\n/**\n * Preview config for enabling previews with redirectToPreviewURL\n */\nexport type RedirectToPreviewURLConfig<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tTLinkResolverFunction extends LinkResolverFunction<any> = LinkResolverFunction,\n> = {\n\t/**\n\t * The `req` object from a Next.js API route. This is given as a parameter to\n\t * the API route.\n\t *\n\t * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}\n\t */\n\treq: {\n\t\tquery: NextApiRequest[\"query\"];\n\t};\n\n\t/**\n\t * The `res` object from a Next.js API route. This is given as a parameter to\n\t * the API route.\n\t *\n\t * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}\n\t */\n\tres: {\n\t\tredirect: NextApiResponse[\"redirect\"];\n\t};\n\n\t/**\n\t * The Prismic client configured for the preview session's repository.\n\t */\n\tclient: Client;\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: TLinkResolverFunction;\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed\n\t * document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the\n\t * `defaultURL` option must _not_ include it. Instead, provide the `basePath`\n\t * property using the `basePath` option.\n\t */\n\tdefaultURL?: string;\n\n\t/**\n\t * The `basePath` for the Next.js app as it is defined in `next.config.js`.\n\t * This option can be omitted if the app does not have a `basePath`.\n\t *\n\t * @remarks\n\t * The API route is unable to detect the app's `basePath` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string;\n};\n\n/**\n * Redirects a user to the URL of a previewed Prismic document from within a\n * Next.js API route.\n */\nexport async function redirectToPreviewURL<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tTLinkResolverFunction extends LinkResolverFunction<any>,\n>(config: RedirectToPreviewURLConfig<TLinkResolverFunction>): Promise<void> {\n\tconst defaultURL = config.defaultURL || \"/\";\n\tconst basePath = config.basePath || \"\";\n\n\tif (isPrismicNextQuery(config.req.query)) {\n\t\tconst previewUrl = await config.client.resolvePreviewURL({\n\t\t\tlinkResolver: config.linkResolver,\n\t\t\tdefaultURL,\n\t\t\tdocumentID: config.req.query.documentId,\n\t\t\tpreviewToken: config.req.query.token,\n\t\t});\n\n\t\tconfig.res.redirect(basePath + previewUrl);\n\n\t\treturn;\n\t}\n\n\tconfig.res.redirect(basePath + defaultURL);\n}\n"],"names":[],"mappings":";;AAiBA,MAAM,qBAAqB,CAC1B,UAC8B;AAC9B,SACC,OAAO,MAAM,eAAe,YAAY,OAAO,MAAM,UAAU;AAEjE;AAkEA,eAAsB,qBAGpB,QAAyD;AACpD,QAAA,aAAa,OAAO,cAAc;AAClC,QAAA,WAAW,OAAO,YAAY;AAEpC,MAAI,mBAAmB,OAAO,IAAI,KAAK,GAAG;AACzC,UAAM,aAAa,MAAM,OAAO,OAAO,kBAAkB;AAAA,MACxD,cAAc,OAAO;AAAA,MACrB;AAAA,MACA,YAAY,OAAO,IAAI,MAAM;AAAA,MAC7B,cAAc,OAAO,IAAI,MAAM;AAAA,IAAA,CAC/B;AAEM,WAAA,IAAI,SAAS,WAAW,UAAU;AAEzC;AAAA,EACA;AAEM,SAAA,IAAI,SAAS,WAAW,UAAU;AAC1C;;"}
@@ -0,0 +1,59 @@
1
+ import type { Client } from "@prismicio/client";
2
+ import type { NextApiRequest, NextApiResponse } from "next";
3
+ import type { LinkResolverFunction } from "@prismicio/helpers";
4
+ /**
5
+ * Preview config for enabling previews with redirectToPreviewURL
6
+ */
7
+ export declare type RedirectToPreviewURLConfig<TLinkResolverFunction extends LinkResolverFunction<any> = LinkResolverFunction> = {
8
+ /**
9
+ * The `req` object from a Next.js API route. This is given as a parameter to
10
+ * the API route.
11
+ *
12
+ * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
13
+ */
14
+ req: {
15
+ query: NextApiRequest["query"];
16
+ };
17
+ /**
18
+ * The `res` object from a Next.js API route. This is given as a parameter to
19
+ * the API route.
20
+ *
21
+ * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
22
+ */
23
+ res: {
24
+ redirect: NextApiResponse["redirect"];
25
+ };
26
+ /**
27
+ * The Prismic client configured for the preview session's repository.
28
+ */
29
+ client: Client;
30
+ /**
31
+ * A Link Resolver used to resolve the previewed document's URL.
32
+ *
33
+ * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
34
+ */
35
+ linkResolver?: TLinkResolverFunction;
36
+ /**
37
+ * The default redirect URL if a URL cannot be determined for the previewed
38
+ * document.
39
+ *
40
+ * **Note**: If you `next.config.js` file contains a `basePath`, the
41
+ * `defaultURL` option must _not_ include it. Instead, provide the `basePath`
42
+ * property using the `basePath` option.
43
+ */
44
+ defaultURL?: string;
45
+ /**
46
+ * The `basePath` for the Next.js app as it is defined in `next.config.js`.
47
+ * This option can be omitted if the app does not have a `basePath`.
48
+ *
49
+ * @remarks
50
+ * The API route is unable to detect the app's `basePath` automatically. It
51
+ * must be provided to `redirectToPreviewURL()` manually.
52
+ */
53
+ basePath?: string;
54
+ };
55
+ /**
56
+ * Redirects a user to the URL of a previewed Prismic document from within a
57
+ * Next.js API route.
58
+ */
59
+ export declare function redirectToPreviewURL<TLinkResolverFunction extends LinkResolverFunction<any>>(config: RedirectToPreviewURLConfig<TLinkResolverFunction>): Promise<void>;
@@ -0,0 +1,22 @@
1
+ const isPrismicNextQuery = (query) => {
2
+ return typeof query.documentId === "string" && typeof query.token === "string";
3
+ };
4
+ async function redirectToPreviewURL(config) {
5
+ const defaultURL = config.defaultURL || "/";
6
+ const basePath = config.basePath || "";
7
+ if (isPrismicNextQuery(config.req.query)) {
8
+ const previewUrl = await config.client.resolvePreviewURL({
9
+ linkResolver: config.linkResolver,
10
+ defaultURL,
11
+ documentID: config.req.query.documentId,
12
+ previewToken: config.req.query.token
13
+ });
14
+ config.res.redirect(basePath + previewUrl);
15
+ return;
16
+ }
17
+ config.res.redirect(basePath + defaultURL);
18
+ }
19
+ export {
20
+ redirectToPreviewURL
21
+ };
22
+ //# sourceMappingURL=redirectToPreviewURL.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redirectToPreviewURL.js","sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import type { Client } from \"@prismicio/client\";\nimport type { NextApiRequest, NextApiResponse } from \"next\";\nimport type { LinkResolverFunction } from \"@prismicio/helpers\";\n\ntype PrismicNextQuery = {\n\tdocumentId: string;\n\ttoken: string;\n};\n\n/**\n * Determines if a query object from a Next.js API route request contains\n * Prismic preview data.\n *\n * @param query - Query object to check.\n *\n * @returns `true` if `query` contains Prismic preview data, `false` otherwise.\n */\nconst isPrismicNextQuery = (\n\tquery: NextApiRequest[\"query\"],\n): query is PrismicNextQuery => {\n\treturn (\n\t\ttypeof query.documentId === \"string\" && typeof query.token === \"string\"\n\t);\n};\n\n/**\n * Preview config for enabling previews with redirectToPreviewURL\n */\nexport type RedirectToPreviewURLConfig<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tTLinkResolverFunction extends LinkResolverFunction<any> = LinkResolverFunction,\n> = {\n\t/**\n\t * The `req` object from a Next.js API route. This is given as a parameter to\n\t * the API route.\n\t *\n\t * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}\n\t */\n\treq: {\n\t\tquery: NextApiRequest[\"query\"];\n\t};\n\n\t/**\n\t * The `res` object from a Next.js API route. This is given as a parameter to\n\t * the API route.\n\t *\n\t * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}\n\t */\n\tres: {\n\t\tredirect: NextApiResponse[\"redirect\"];\n\t};\n\n\t/**\n\t * The Prismic client configured for the preview session's repository.\n\t */\n\tclient: Client;\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: TLinkResolverFunction;\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed\n\t * document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the\n\t * `defaultURL` option must _not_ include it. Instead, provide the `basePath`\n\t * property using the `basePath` option.\n\t */\n\tdefaultURL?: string;\n\n\t/**\n\t * The `basePath` for the Next.js app as it is defined in `next.config.js`.\n\t * This option can be omitted if the app does not have a `basePath`.\n\t *\n\t * @remarks\n\t * The API route is unable to detect the app's `basePath` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string;\n};\n\n/**\n * Redirects a user to the URL of a previewed Prismic document from within a\n * Next.js API route.\n */\nexport async function redirectToPreviewURL<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tTLinkResolverFunction extends LinkResolverFunction<any>,\n>(config: RedirectToPreviewURLConfig<TLinkResolverFunction>): Promise<void> {\n\tconst defaultURL = config.defaultURL || \"/\";\n\tconst basePath = config.basePath || \"\";\n\n\tif (isPrismicNextQuery(config.req.query)) {\n\t\tconst previewUrl = await config.client.resolvePreviewURL({\n\t\t\tlinkResolver: config.linkResolver,\n\t\t\tdefaultURL,\n\t\t\tdocumentID: config.req.query.documentId,\n\t\t\tpreviewToken: config.req.query.token,\n\t\t});\n\n\t\tconfig.res.redirect(basePath + previewUrl);\n\n\t\treturn;\n\t}\n\n\tconfig.res.redirect(basePath + defaultURL);\n}\n"],"names":[],"mappings":"AAiBA,MAAM,qBAAqB,CAC1B,UAC8B;AAC9B,SACC,OAAO,MAAM,eAAe,YAAY,OAAO,MAAM,UAAU;AAEjE;AAkEA,eAAsB,qBAGpB,QAAyD;AACpD,QAAA,aAAa,OAAO,cAAc;AAClC,QAAA,WAAW,OAAO,YAAY;AAEpC,MAAI,mBAAmB,OAAO,IAAI,KAAK,GAAG;AACzC,UAAM,aAAa,MAAM,OAAO,OAAO,kBAAkB;AAAA,MACxD,cAAc,OAAO;AAAA,MACrB;AAAA,MACA,YAAY,OAAO,IAAI,MAAM;AAAA,MAC7B,cAAc,OAAO,IAAI,MAAM;AAAA,IAAA,CAC/B;AAEM,WAAA,IAAI,SAAS,WAAW,UAAU;AAEzC;AAAA,EACA;AAEM,SAAA,IAAI,SAAS,WAAW,UAAU;AAC1C;"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const prismic = require("@prismicio/client");
4
+ function _interopNamespaceDefault(e) {
5
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
6
+ if (e) {
7
+ for (const k in e) {
8
+ if (k !== "default") {
9
+ const d = Object.getOwnPropertyDescriptor(e, k);
10
+ Object.defineProperty(n, k, d.get ? d : {
11
+ enumerable: true,
12
+ get: () => e[k]
13
+ });
14
+ }
15
+ }
16
+ }
17
+ n.default = e;
18
+ return Object.freeze(n);
19
+ }
20
+ const prismic__namespace = /* @__PURE__ */ _interopNamespaceDefault(prismic);
21
+ function setPreviewData({ req, res }) {
22
+ const ref = req.query.token || req.cookies[prismic__namespace.cookie.preview];
23
+ if (ref) {
24
+ res.setPreviewData({ ref });
25
+ }
26
+ }
27
+ exports.setPreviewData = setPreviewData;
28
+ //# sourceMappingURL=setPreviewData.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setPreviewData.cjs","sources":["../src/setPreviewData.ts"],"sourcesContent":["import { NextApiResponse, NextApiRequest } from \"next\";\nimport * as prismic from \"@prismicio/client\";\n\n/**\n * Configuration for `setPreviewData`.\n */\nexport type SetPreviewDataConfig = {\n\t/**\n\t * The `req` object from a Next.js API route. This is given as a parameter to\n\t * the API route.\n\t *\n\t * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}\n\t */\n\treq: {\n\t\tquery: NextApiRequest[\"query\"];\n\t\tcookies: NextApiRequest[\"cookies\"];\n\t};\n\n\t/**\n\t * The `res` object from a Next.js API route. This is given as a parameter to\n\t * the API route.\n\t *\n\t * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}\n\t */\n\tres: {\n\t\tsetPreviewData: NextApiResponse[\"setPreviewData\"];\n\t};\n};\n\n/**\n * Set Prismic preview data for Next.js's Preview Mode.\n */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst ref = req.query.token || req.cookies[prismic.cookie.preview];\n\n\tif (ref) {\n\t\tres.setPreviewData({ ref });\n\t}\n}\n"],"names":["prismic"],"mappings":";;;;;;;;;;;;;;;;;;;;SAgCgB,eAAe,EAAE,KAAK,OAA2B;AAChE,QAAM,MAAM,IAAI,MAAM,SAAS,IAAI,QAAQA,mBAAQ,OAAO;AAE1D,MAAI,KAAK;AACJ,QAAA,eAAe,EAAE,IAAA,CAAK;AAAA,EAC1B;AACF;;"}
@@ -0,0 +1,29 @@
1
+ import { NextApiResponse, NextApiRequest } from "next";
2
+ /**
3
+ * Configuration for `setPreviewData`.
4
+ */
5
+ export declare type SetPreviewDataConfig = {
6
+ /**
7
+ * The `req` object from a Next.js API route. This is given as a parameter to
8
+ * the API route.
9
+ *
10
+ * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
11
+ */
12
+ req: {
13
+ query: NextApiRequest["query"];
14
+ cookies: NextApiRequest["cookies"];
15
+ };
16
+ /**
17
+ * The `res` object from a Next.js API route. This is given as a parameter to
18
+ * the API route.
19
+ *
20
+ * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
21
+ */
22
+ res: {
23
+ setPreviewData: NextApiResponse["setPreviewData"];
24
+ };
25
+ };
26
+ /**
27
+ * Set Prismic preview data for Next.js's Preview Mode.
28
+ */
29
+ export declare function setPreviewData({ req, res }: SetPreviewDataConfig): void;
@@ -0,0 +1,11 @@
1
+ import * as prismic from "@prismicio/client";
2
+ function setPreviewData({ req, res }) {
3
+ const ref = req.query.token || req.cookies[prismic.cookie.preview];
4
+ if (ref) {
5
+ res.setPreviewData({ ref });
6
+ }
7
+ }
8
+ export {
9
+ setPreviewData
10
+ };
11
+ //# sourceMappingURL=setPreviewData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setPreviewData.js","sources":["../src/setPreviewData.ts"],"sourcesContent":["import { NextApiResponse, NextApiRequest } from \"next\";\nimport * as prismic from \"@prismicio/client\";\n\n/**\n * Configuration for `setPreviewData`.\n */\nexport type SetPreviewDataConfig = {\n\t/**\n\t * The `req` object from a Next.js API route. This is given as a parameter to\n\t * the API route.\n\t *\n\t * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}\n\t */\n\treq: {\n\t\tquery: NextApiRequest[\"query\"];\n\t\tcookies: NextApiRequest[\"cookies\"];\n\t};\n\n\t/**\n\t * The `res` object from a Next.js API route. This is given as a parameter to\n\t * the API route.\n\t *\n\t * @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}\n\t */\n\tres: {\n\t\tsetPreviewData: NextApiResponse[\"setPreviewData\"];\n\t};\n};\n\n/**\n * Set Prismic preview data for Next.js's Preview Mode.\n */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst ref = req.query.token || req.cookies[prismic.cookie.preview];\n\n\tif (ref) {\n\t\tres.setPreviewData({ ref });\n\t}\n}\n"],"names":[],"mappings":";SAgCgB,eAAe,EAAE,KAAK,OAA2B;AAChE,QAAM,MAAM,IAAI,MAAM,SAAS,IAAI,QAAQ,QAAQ,OAAO;AAE1D,MAAI,KAAK;AACJ,QAAA,eAAe,EAAE,IAAA,CAAK;AAAA,EAC1B;AACF;"}
@@ -0,0 +1,21 @@
1
+ import type { PreviewData, NextApiRequest } from "next";
2
+ import type { ClientConfig } from "@prismicio/client";
3
+ /**
4
+ * Configuration for creating a Prismic client with automatic preview support in
5
+ * Next.js apps.
6
+ */
7
+ export declare type CreateClientConfig = {
8
+ /**
9
+ * Preview data coming from Next.js context object. This context object comes
10
+ * from `getStaticProps` or `getServerSideProps`.
11
+ *
12
+ * Pass `previewData` when using outside a Next.js API endpoint.
13
+ */
14
+ previewData?: PreviewData;
15
+ /**
16
+ * A Next.js API endpoint request object.
17
+ *
18
+ * Pass a `req` object when using in a Next.js API endpoint.
19
+ */
20
+ req?: NextApiRequest;
21
+ } & ClientConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/next",
3
- "version": "0.2.0-alpha.0",
3
+ "version": "0.2.0",
4
4
  "description": "Helpers to integrate Prismic into Next.js apps",
5
5
  "keywords": [
6
6
  "typescript",
@@ -15,6 +15,7 @@
15
15
  },
16
16
  "license": "Apache-2.0",
17
17
  "author": "Prismic <contact@prismic.io> (https://prismic.io)",
18
+ "sideEffects": false,
18
19
  "exports": {
19
20
  ".": {
20
21
  "require": "./dist/index.cjs",
@@ -23,21 +24,21 @@
23
24
  "./package.json": "./package.json"
24
25
  },
25
26
  "main": "./dist/index.cjs",
26
- "module": "dist/index.js",
27
+ "module": "./dist/index.js",
27
28
  "types": "dist/index.d.ts",
28
29
  "files": [
29
30
  "dist",
30
31
  "src"
31
32
  ],
32
33
  "scripts": {
33
- "build": "siroc build",
34
- "dev": "siroc build --watch",
34
+ "build": "vite build",
35
+ "dev": "vite build --watch",
35
36
  "format": "prettier --write .",
36
37
  "lint": "eslint --ext .js,.ts .",
37
38
  "prepare": "npm run build",
38
39
  "release": "npm run test && standard-version && git push --follow-tags && npm run build && npm publish",
39
- "release:alpha": "npm run test && standard-version --release-as major --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
40
- "release:alpha:dry": "standard-version --release-as major --prerelease alpha --dry-run",
40
+ "release:alpha": "npm run test && standard-version --release-as patch --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
41
+ "release:alpha:dry": "standard-version --release-as patch --prerelease alpha --dry-run",
41
42
  "release:dry": "standard-version --dry-run",
42
43
  "size": "size-limit",
43
44
  "test": "npm run lint && npm run unit && npm run build && npm run size",
@@ -45,42 +46,46 @@
45
46
  "unit:watch": "vitest watch"
46
47
  },
47
48
  "dependencies": {
48
- "@prismicio/client": "^6.0.0",
49
- "@prismicio/react": "^2.2.0",
50
- "@prismicio/types": "^0.1.27"
49
+ "@prismicio/helpers": "^2.3.5",
50
+ "@prismicio/types": "^0.2.3",
51
+ "imgix-url-builder": "^0.0.3"
51
52
  },
52
53
  "devDependencies": {
53
- "@size-limit/preset-small-lib": "^7.0.8",
54
- "@types/react-test-renderer": "^17.0.1",
55
- "@typescript-eslint/eslint-plugin": "^5.17.0",
56
- "@typescript-eslint/parser": "^5.17.0",
57
- "c8": "^7.11.0",
58
- "eslint": "^8.12.0",
54
+ "@prismicio/client": "^6.7.1",
55
+ "@prismicio/mock": "^0.1.1",
56
+ "@size-limit/preset-small-lib": "^8.1.0",
57
+ "@types/react-test-renderer": "^18.0.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.40.1",
59
+ "@typescript-eslint/parser": "^5.40.1",
60
+ "@vitejs/plugin-react": "^2.1.0",
61
+ "@vitest/coverage-c8": "^0.24.3",
62
+ "eslint": "^8.25.0",
59
63
  "eslint-config-prettier": "^8.5.0",
60
- "eslint-plugin-prettier": "^4.0.0",
61
- "eslint-plugin-react": "^7.29.4",
62
- "eslint-plugin-react-hooks": "^4.4.0",
63
- "eslint-plugin-tsdoc": "^0.2.14",
64
- "happy-dom": "^2.55.0",
65
- "next": "^12.1.4",
66
- "nyc": "^15.1.0",
67
- "prettier": "^2.6.1",
68
- "prettier-plugin-jsdoc": "^0.3.35",
69
- "react": "^18.0.0",
70
- "react-test-renderer": "^18.0.0",
71
- "siroc": "^0.16.0",
72
- "size-limit": "^7.0.8",
73
- "standard-version": "^9.3.2",
74
- "typescript": "^4.6.3",
75
- "vitest": "^0.8.1"
64
+ "eslint-plugin-prettier": "^4.2.1",
65
+ "eslint-plugin-react": "^7.31.10",
66
+ "eslint-plugin-react-hooks": "^4.6.0",
67
+ "eslint-plugin-tsdoc": "^0.2.17",
68
+ "happy-dom": "^7.5.13",
69
+ "next": "^13.0.0",
70
+ "prettier": "^2.7.1",
71
+ "prettier-plugin-jsdoc": "^0.4.2",
72
+ "react": "^18.1.0",
73
+ "react-dom": "^18.2.0",
74
+ "react-test-renderer": "^18.2.0",
75
+ "size-limit": "^8.1.0",
76
+ "standard-version": "^9.5.0",
77
+ "typescript": "^4.8.4",
78
+ "vite": "^3.1.8",
79
+ "vite-plugin-sdk": "^0.0.3",
80
+ "vitest": "^0.24.3"
76
81
  },
77
82
  "peerDependencies": {
78
- "@prismicio/client": "^6.0.0",
79
- "next": "^11 || ^12",
80
- "react": "^17 || ^18"
83
+ "@prismicio/client": "^6",
84
+ "next": "^13",
85
+ "react": "^18"
81
86
  },
82
87
  "engines": {
83
- "node": ">=12.7.0"
88
+ "node": ">=14.15.0"
84
89
  },
85
90
  "publishConfig": {
86
91
  "access": "public"