@prismicio/next 0.1.2 → 0.1.4
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.
- package/README.md +3 -1
- package/dist/PrismicNextImage.mjs +60 -0
- package/dist/PrismicPreview.mjs +87 -0
- package/dist/enableAutoPreviews.mjs +15 -0
- package/dist/exitPreview.mjs +6 -0
- package/dist/index.d.ts +137 -64
- package/dist/index.mjs +6 -0
- package/dist/lib/__PRODUCTION__.mjs +3 -0
- package/dist/lib/devMsg.mjs +7 -0
- package/dist/lib/getPreviewCookieRepositoryName.mjs +5 -0
- package/dist/lib/getPrismicPreviewCookie.mjs +20 -0
- package/dist/package.mjs +3 -0
- package/dist/redirectToPreviewURL.mjs +20 -0
- package/dist/setPreviewData.mjs +10 -0
- package/package.json +35 -41
- package/src/PrismicNextImage.tsx +126 -0
- package/src/PrismicPreview.tsx +92 -55
- package/src/enableAutoPreviews.ts +8 -8
- package/src/exitPreview.ts +23 -22
- package/src/index.ts +14 -5
- package/src/lib/__PRODUCTION__.ts +7 -0
- package/src/lib/devMsg.ts +20 -0
- package/src/lib/getPreviewCookieRepositoryName.ts +14 -0
- package/src/lib/getPrismicPreviewCookie.ts +33 -0
- package/src/redirectToPreviewURL.ts +79 -21
- package/src/types.ts +3 -49
- package/dist/index.cjs +0 -184
- package/dist/index.js +0 -153
- package/src/lib/extractPreviewRefRepositoryName.ts +0 -60
- package/src/lib/getCookie.ts +0 -58
package/src/types.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { PreviewData, NextApiRequest
|
|
2
|
-
import {
|
|
3
|
-
import { Client } from "@prismicio/client";
|
|
1
|
+
import type { PreviewData, NextApiRequest } from "next";
|
|
2
|
+
import type { ClientConfig } from "@prismicio/client";
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Configuration for creating a Prismic client with automatic preview support in
|
|
@@ -21,49 +20,4 @@ export type CreateClientConfig = {
|
|
|
21
20
|
* Pass a `req` object when using in a Next.js API endpoint.
|
|
22
21
|
*/
|
|
23
22
|
req?: NextApiRequest;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Preview config for enabling previews with redirectToPreviewURL
|
|
28
|
-
*/
|
|
29
|
-
export type PreviewConfig<
|
|
30
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
-
TLinkResolverFunction extends LinkResolverFunction<any> = LinkResolverFunction,
|
|
32
|
-
> = {
|
|
33
|
-
/**
|
|
34
|
-
* The `req` object from a Next.js API route. This is given as a parameter to
|
|
35
|
-
* the API route.
|
|
36
|
-
*
|
|
37
|
-
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
38
|
-
*/
|
|
39
|
-
req: {
|
|
40
|
-
query: NextApiRequest["query"];
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* The `res` object from a Next.js API route. This is given as a parameter to
|
|
45
|
-
* the API route.
|
|
46
|
-
*
|
|
47
|
-
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
48
|
-
*/
|
|
49
|
-
res: {
|
|
50
|
-
redirect: NextApiResponse["redirect"];
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* The Prismic client configured for the preview session's repository.
|
|
55
|
-
*/
|
|
56
|
-
client: Client;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* A Link Resolver used to resolve the previewed document's URL.
|
|
60
|
-
*
|
|
61
|
-
* @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
62
|
-
*/
|
|
63
|
-
linkResolver?: TLinkResolverFunction;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* The default redirect URL if a URL cannot be determined for the previewed document.
|
|
67
|
-
*/
|
|
68
|
-
defaultURL?: string;
|
|
69
|
-
};
|
|
23
|
+
} & ClientConfig;
|
package/dist/index.cjs
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const prismic = require('@prismicio/client');
|
|
6
|
-
const React = require('react');
|
|
7
|
-
const react = require('@prismicio/react');
|
|
8
|
-
const router = require('next/router');
|
|
9
|
-
|
|
10
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
-
|
|
12
|
-
function _interopNamespace(e) {
|
|
13
|
-
if (e && e.__esModule) return e;
|
|
14
|
-
const n = Object.create(null);
|
|
15
|
-
if (e) {
|
|
16
|
-
for (const k in e) {
|
|
17
|
-
if (k !== 'default') {
|
|
18
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
get: function () { return e[k]; }
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
n["default"] = e;
|
|
27
|
-
return Object.freeze(n);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const prismic__namespace = /*#__PURE__*/_interopNamespace(prismic);
|
|
31
|
-
const React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
32
|
-
|
|
33
|
-
function setPreviewData({ req, res }) {
|
|
34
|
-
const ref = req.query.token || req.cookies[prismic__namespace.cookie.preview];
|
|
35
|
-
if (ref) {
|
|
36
|
-
res.setPreviewData({ ref });
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function exitPreview(config) {
|
|
41
|
-
const { req } = config;
|
|
42
|
-
config.res.clearPreviewData();
|
|
43
|
-
if (req.headers.referer) {
|
|
44
|
-
const url = new URL(req.headers.referer);
|
|
45
|
-
if (url.pathname !== "/api/exit-preview") {
|
|
46
|
-
config.res.redirect(req.headers.referer);
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
config.res.redirect("/");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const readValue = (value) => {
|
|
54
|
-
return value.replace(/%3B/g, ";");
|
|
55
|
-
};
|
|
56
|
-
const parse = (cookieString) => {
|
|
57
|
-
const result = {};
|
|
58
|
-
const cookies = cookieString.split("; ");
|
|
59
|
-
for (const cookie of cookies) {
|
|
60
|
-
const parts = cookie.split("=");
|
|
61
|
-
const value = parts.slice(1).join("=");
|
|
62
|
-
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
63
|
-
result[name] = readValue(value);
|
|
64
|
-
}
|
|
65
|
-
return result;
|
|
66
|
-
};
|
|
67
|
-
const getAll = (cookieStore) => parse(cookieStore);
|
|
68
|
-
const getCookie = (name, cookieStore) => getAll(cookieStore)[name];
|
|
69
|
-
|
|
70
|
-
const extractFirstSubdomain = (host) => host.split(".")[0];
|
|
71
|
-
const extractRepositoryNameFromObjectRef = (previewRef) => {
|
|
72
|
-
try {
|
|
73
|
-
const parsed = JSON.parse(decodeURIComponent(previewRef));
|
|
74
|
-
const keys = Object.keys(parsed);
|
|
75
|
-
const domainKey = keys.find((key) => /\.prismic\.io$/.test(key));
|
|
76
|
-
if (domainKey) {
|
|
77
|
-
return extractFirstSubdomain(domainKey);
|
|
78
|
-
} else {
|
|
79
|
-
return void 0;
|
|
80
|
-
}
|
|
81
|
-
} catch (e) {
|
|
82
|
-
return void 0;
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
const extractRepositoryNameFromURLRef = (previewRef) => {
|
|
86
|
-
try {
|
|
87
|
-
const url = new URL(previewRef);
|
|
88
|
-
return extractFirstSubdomain(url.host);
|
|
89
|
-
} catch (e) {
|
|
90
|
-
return void 0;
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
const extractPreviewRefRepositoryName = (previewRef) => {
|
|
94
|
-
return extractRepositoryNameFromObjectRef(previewRef) || extractRepositoryNameFromURLRef(previewRef);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const isPrismicUpdateToolbarEvent = (event) => {
|
|
98
|
-
return "detail" in event && typeof event.detail.ref === "string";
|
|
99
|
-
};
|
|
100
|
-
function PrismicPreview({
|
|
101
|
-
repositoryName,
|
|
102
|
-
children,
|
|
103
|
-
updatePreviewURL = "/api/preview",
|
|
104
|
-
exitPreviewURL = "/api/exit-preview"
|
|
105
|
-
}) {
|
|
106
|
-
const router$1 = router.useRouter();
|
|
107
|
-
React.useEffect(() => {
|
|
108
|
-
const previewRefRepositoryName = extractPreviewRefRepositoryName(getCookie("io.prismic.preview", globalThis.document.cookie));
|
|
109
|
-
const startPreviewIfLoadedFromSharedLink = async () => {
|
|
110
|
-
if (previewRefRepositoryName === repositoryName && !router$1.isPreview) {
|
|
111
|
-
await fetch(updatePreviewURL);
|
|
112
|
-
window.location.reload();
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
startPreviewIfLoadedFromSharedLink();
|
|
116
|
-
const handlePrismicPreviewUpdate = async (event) => {
|
|
117
|
-
if (isPrismicUpdateToolbarEvent(event)) {
|
|
118
|
-
event.preventDefault();
|
|
119
|
-
await fetch(updatePreviewURL);
|
|
120
|
-
window.location.reload();
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
const handlePrismicPreviewEnd = async (event) => {
|
|
124
|
-
event.preventDefault();
|
|
125
|
-
await fetch(exitPreviewURL);
|
|
126
|
-
window.location.reload();
|
|
127
|
-
};
|
|
128
|
-
if (window) {
|
|
129
|
-
window.addEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
130
|
-
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
131
|
-
}
|
|
132
|
-
return () => {
|
|
133
|
-
if (window) {
|
|
134
|
-
window.removeEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
135
|
-
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
}, []);
|
|
139
|
-
return /* @__PURE__ */ React__default["default"].createElement(React__default["default"].Fragment, null, /* @__PURE__ */ React__default["default"].createElement(react.PrismicToolbar, {
|
|
140
|
-
repositoryName
|
|
141
|
-
}), children);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const isPrismicNextPreviewData = (previewData) => {
|
|
145
|
-
return typeof previewData === "object" && "ref" in previewData;
|
|
146
|
-
};
|
|
147
|
-
const enableAutoPreviews = (config) => {
|
|
148
|
-
if ("previewData" in config && config.previewData) {
|
|
149
|
-
const { previewData } = config;
|
|
150
|
-
if (isPrismicNextPreviewData(previewData) && previewData.ref) {
|
|
151
|
-
config.client.queryContentFromRef(previewData.ref);
|
|
152
|
-
}
|
|
153
|
-
} else if ("req" in config && config.req) {
|
|
154
|
-
config.client.enableAutoPreviewsFromReq(config.req);
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const isPrismicNextQuery = (query) => typeof query.documentId === "string" && typeof query.token === "string";
|
|
159
|
-
async function redirectToPreviewURL({
|
|
160
|
-
req,
|
|
161
|
-
res,
|
|
162
|
-
client,
|
|
163
|
-
linkResolver,
|
|
164
|
-
defaultURL = "/"
|
|
165
|
-
}) {
|
|
166
|
-
if (isPrismicNextQuery(req.query)) {
|
|
167
|
-
const { documentId, token } = req.query;
|
|
168
|
-
const previewUrl = await client.resolvePreviewURL({
|
|
169
|
-
linkResolver,
|
|
170
|
-
defaultURL,
|
|
171
|
-
documentID: documentId,
|
|
172
|
-
previewToken: token
|
|
173
|
-
});
|
|
174
|
-
res.redirect(previewUrl);
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
res.redirect(defaultURL);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
exports.PrismicPreview = PrismicPreview;
|
|
181
|
-
exports.enableAutoPreviews = enableAutoPreviews;
|
|
182
|
-
exports.exitPreview = exitPreview;
|
|
183
|
-
exports.redirectToPreviewURL = redirectToPreviewURL;
|
|
184
|
-
exports.setPreviewData = setPreviewData;
|
package/dist/index.js
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import * as prismic from '@prismicio/client';
|
|
2
|
-
import React, { useEffect } from 'react';
|
|
3
|
-
import { PrismicToolbar } from '@prismicio/react';
|
|
4
|
-
import { useRouter } from 'next/router';
|
|
5
|
-
|
|
6
|
-
function setPreviewData({ req, res }) {
|
|
7
|
-
const ref = req.query.token || req.cookies[prismic.cookie.preview];
|
|
8
|
-
if (ref) {
|
|
9
|
-
res.setPreviewData({ ref });
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function exitPreview(config) {
|
|
14
|
-
const { req } = config;
|
|
15
|
-
config.res.clearPreviewData();
|
|
16
|
-
if (req.headers.referer) {
|
|
17
|
-
const url = new URL(req.headers.referer);
|
|
18
|
-
if (url.pathname !== "/api/exit-preview") {
|
|
19
|
-
config.res.redirect(req.headers.referer);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
config.res.redirect("/");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const readValue = (value) => {
|
|
27
|
-
return value.replace(/%3B/g, ";");
|
|
28
|
-
};
|
|
29
|
-
const parse = (cookieString) => {
|
|
30
|
-
const result = {};
|
|
31
|
-
const cookies = cookieString.split("; ");
|
|
32
|
-
for (const cookie of cookies) {
|
|
33
|
-
const parts = cookie.split("=");
|
|
34
|
-
const value = parts.slice(1).join("=");
|
|
35
|
-
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
36
|
-
result[name] = readValue(value);
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
};
|
|
40
|
-
const getAll = (cookieStore) => parse(cookieStore);
|
|
41
|
-
const getCookie = (name, cookieStore) => getAll(cookieStore)[name];
|
|
42
|
-
|
|
43
|
-
const extractFirstSubdomain = (host) => host.split(".")[0];
|
|
44
|
-
const extractRepositoryNameFromObjectRef = (previewRef) => {
|
|
45
|
-
try {
|
|
46
|
-
const parsed = JSON.parse(decodeURIComponent(previewRef));
|
|
47
|
-
const keys = Object.keys(parsed);
|
|
48
|
-
const domainKey = keys.find((key) => /\.prismic\.io$/.test(key));
|
|
49
|
-
if (domainKey) {
|
|
50
|
-
return extractFirstSubdomain(domainKey);
|
|
51
|
-
} else {
|
|
52
|
-
return void 0;
|
|
53
|
-
}
|
|
54
|
-
} catch (e) {
|
|
55
|
-
return void 0;
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
const extractRepositoryNameFromURLRef = (previewRef) => {
|
|
59
|
-
try {
|
|
60
|
-
const url = new URL(previewRef);
|
|
61
|
-
return extractFirstSubdomain(url.host);
|
|
62
|
-
} catch (e) {
|
|
63
|
-
return void 0;
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
const extractPreviewRefRepositoryName = (previewRef) => {
|
|
67
|
-
return extractRepositoryNameFromObjectRef(previewRef) || extractRepositoryNameFromURLRef(previewRef);
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const isPrismicUpdateToolbarEvent = (event) => {
|
|
71
|
-
return "detail" in event && typeof event.detail.ref === "string";
|
|
72
|
-
};
|
|
73
|
-
function PrismicPreview({
|
|
74
|
-
repositoryName,
|
|
75
|
-
children,
|
|
76
|
-
updatePreviewURL = "/api/preview",
|
|
77
|
-
exitPreviewURL = "/api/exit-preview"
|
|
78
|
-
}) {
|
|
79
|
-
const router = useRouter();
|
|
80
|
-
useEffect(() => {
|
|
81
|
-
const previewRefRepositoryName = extractPreviewRefRepositoryName(getCookie("io.prismic.preview", globalThis.document.cookie));
|
|
82
|
-
const startPreviewIfLoadedFromSharedLink = async () => {
|
|
83
|
-
if (previewRefRepositoryName === repositoryName && !router.isPreview) {
|
|
84
|
-
await fetch(updatePreviewURL);
|
|
85
|
-
window.location.reload();
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
startPreviewIfLoadedFromSharedLink();
|
|
89
|
-
const handlePrismicPreviewUpdate = async (event) => {
|
|
90
|
-
if (isPrismicUpdateToolbarEvent(event)) {
|
|
91
|
-
event.preventDefault();
|
|
92
|
-
await fetch(updatePreviewURL);
|
|
93
|
-
window.location.reload();
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
const handlePrismicPreviewEnd = async (event) => {
|
|
97
|
-
event.preventDefault();
|
|
98
|
-
await fetch(exitPreviewURL);
|
|
99
|
-
window.location.reload();
|
|
100
|
-
};
|
|
101
|
-
if (window) {
|
|
102
|
-
window.addEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
103
|
-
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
104
|
-
}
|
|
105
|
-
return () => {
|
|
106
|
-
if (window) {
|
|
107
|
-
window.removeEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
108
|
-
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
}, []);
|
|
112
|
-
return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(PrismicToolbar, {
|
|
113
|
-
repositoryName
|
|
114
|
-
}), children);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const isPrismicNextPreviewData = (previewData) => {
|
|
118
|
-
return typeof previewData === "object" && "ref" in previewData;
|
|
119
|
-
};
|
|
120
|
-
const enableAutoPreviews = (config) => {
|
|
121
|
-
if ("previewData" in config && config.previewData) {
|
|
122
|
-
const { previewData } = config;
|
|
123
|
-
if (isPrismicNextPreviewData(previewData) && previewData.ref) {
|
|
124
|
-
config.client.queryContentFromRef(previewData.ref);
|
|
125
|
-
}
|
|
126
|
-
} else if ("req" in config && config.req) {
|
|
127
|
-
config.client.enableAutoPreviewsFromReq(config.req);
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const isPrismicNextQuery = (query) => typeof query.documentId === "string" && typeof query.token === "string";
|
|
132
|
-
async function redirectToPreviewURL({
|
|
133
|
-
req,
|
|
134
|
-
res,
|
|
135
|
-
client,
|
|
136
|
-
linkResolver,
|
|
137
|
-
defaultURL = "/"
|
|
138
|
-
}) {
|
|
139
|
-
if (isPrismicNextQuery(req.query)) {
|
|
140
|
-
const { documentId, token } = req.query;
|
|
141
|
-
const previewUrl = await client.resolvePreviewURL({
|
|
142
|
-
linkResolver,
|
|
143
|
-
defaultURL,
|
|
144
|
-
documentID: documentId,
|
|
145
|
-
previewToken: token
|
|
146
|
-
});
|
|
147
|
-
res.redirect(previewUrl);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
res.redirect(defaultURL);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export { PrismicPreview, enableAutoPreviews, exitPreview, redirectToPreviewURL, setPreviewData };
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the repository name from an object-style Prismic ref.
|
|
3
|
-
*
|
|
4
|
-
* @param host - Host string
|
|
5
|
-
*/
|
|
6
|
-
const extractFirstSubdomain = (host: string): string => host.split(".")[0];
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Parses ref object from cookie and returns proper preview link
|
|
10
|
-
*
|
|
11
|
-
* @param previewRef - Prismic Preview reference
|
|
12
|
-
*/
|
|
13
|
-
const extractRepositoryNameFromObjectRef = (
|
|
14
|
-
previewRef: string,
|
|
15
|
-
): string | undefined => {
|
|
16
|
-
try {
|
|
17
|
-
const parsed = JSON.parse(decodeURIComponent(previewRef));
|
|
18
|
-
const keys = Object.keys(parsed);
|
|
19
|
-
const domainKey = keys.find((key) => /\.prismic\.io$/.test(key));
|
|
20
|
-
|
|
21
|
-
if (domainKey) {
|
|
22
|
-
return extractFirstSubdomain(domainKey);
|
|
23
|
-
} else {
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
|
-
} catch {
|
|
27
|
-
return undefined;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Returns the repository name from a URL-style Prismic ref.
|
|
33
|
-
*
|
|
34
|
-
* @param previewRef - Preview ref from getCookie()
|
|
35
|
-
*/
|
|
36
|
-
const extractRepositoryNameFromURLRef = (
|
|
37
|
-
previewRef: string,
|
|
38
|
-
): string | undefined => {
|
|
39
|
-
try {
|
|
40
|
-
const url = new URL(previewRef);
|
|
41
|
-
|
|
42
|
-
return extractFirstSubdomain(url.host);
|
|
43
|
-
} catch {
|
|
44
|
-
return undefined;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Extracts preview reference repo name from stringified Prismic preview cookie
|
|
50
|
-
*
|
|
51
|
-
* @param previewRef - Preview Reference
|
|
52
|
-
*/
|
|
53
|
-
export const extractPreviewRefRepositoryName = (
|
|
54
|
-
previewRef: string,
|
|
55
|
-
): string | undefined => {
|
|
56
|
-
return (
|
|
57
|
-
extractRepositoryNameFromObjectRef(previewRef) ||
|
|
58
|
-
extractRepositoryNameFromURLRef(previewRef)
|
|
59
|
-
);
|
|
60
|
-
};
|
package/src/lib/getCookie.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The following code is a modifed version of `es-cookie` taken from
|
|
3
|
-
* https://github.com/theodorejb/es-cookie
|
|
4
|
-
*
|
|
5
|
-
* Copyright 2017 Theodore Brown
|
|
6
|
-
*
|
|
7
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
* in the Software without restriction, including without limitation the rights
|
|
10
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
* furnished to do so, subject to the following conditions:
|
|
13
|
-
*
|
|
14
|
-
* The above copyright notice and this permission notice shall be included in
|
|
15
|
-
* all copies or substantial portions of the Software.
|
|
16
|
-
*
|
|
17
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
-
* SOFTWARE.*
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
const readValue = (value: string): string => {
|
|
27
|
-
return value.replace(/%3B/g, ";");
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export const parse = (cookieString: string): { [name: string]: string } => {
|
|
31
|
-
const result: { [name: string]: string } = {};
|
|
32
|
-
const cookies = cookieString.split("; ");
|
|
33
|
-
|
|
34
|
-
for (const cookie of cookies) {
|
|
35
|
-
const parts = cookie.split("=");
|
|
36
|
-
const value = parts.slice(1).join("=");
|
|
37
|
-
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
38
|
-
result[name] = readValue(value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return result;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const getAll = (cookieStore: string): { [name: string]: string } =>
|
|
45
|
-
parse(cookieStore);
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Returns the value of a cookie from a given cookie store.
|
|
49
|
-
*
|
|
50
|
-
* @param name - Of the cookie.
|
|
51
|
-
* @param cookieStore - The stringified cookie store from which to read the cookie.
|
|
52
|
-
*
|
|
53
|
-
* @returns The value of the cookie, if it exists.
|
|
54
|
-
*/
|
|
55
|
-
export const getCookie = (
|
|
56
|
-
name: string,
|
|
57
|
-
cookieStore: string,
|
|
58
|
-
): string | undefined => getAll(cookieStore)[name];
|