@preprio/toolkit 0.5.0-rc.2 → 0.6.0-alpha.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.
- package/README.md +308 -27
- package/dist/astro/chunk-3UVUDDNY.js +173 -0
- package/dist/astro/graphql.cjs +983 -0
- package/dist/astro/graphql.d.cts +231 -0
- package/dist/astro/graphql.d.ts +231 -0
- package/dist/astro/graphql.js +893 -0
- package/dist/astro/index-XT_z-RYT.d.cts +130 -0
- package/dist/astro/index-XT_z-RYT.d.ts +130 -0
- package/dist/astro/index.cjs +14 -1
- package/dist/astro/index.d.cts +1 -120
- package/dist/astro/index.d.ts +1 -120
- package/dist/astro/index.js +15 -153
- package/dist/{chunk-FLPNN3CV.js → chunk-BJ2LBLWO.js} +71 -276
- package/dist/chunk-DGZA7KX7.js +172 -0
- package/dist/chunk-YR7P4IWE.js +187 -0
- package/dist/graphql.cjs +894 -0
- package/dist/graphql.d.cts +231 -0
- package/dist/graphql.d.ts +231 -0
- package/dist/graphql.js +787 -0
- package/dist/index.cjs +76 -119
- package/dist/index.d.cts +12 -64
- package/dist/index.d.ts +12 -64
- package/dist/index.js +23 -167
- package/dist/nextjs/chunk-U4HXENF7.js +231 -0
- package/dist/nextjs/components.cjs +190 -122
- package/dist/nextjs/components.d.cts +33 -2
- package/dist/nextjs/components.d.ts +33 -2
- package/dist/nextjs/components.js +132 -4
- package/dist/nextjs/graphql.cjs +1315 -0
- package/dist/nextjs/graphql.d.cts +361 -0
- package/dist/nextjs/graphql.d.ts +361 -0
- package/dist/nextjs/graphql.js +1218 -0
- package/dist/nextjs/index.cjs +126 -6
- package/dist/nextjs/index.d.cts +53 -147
- package/dist/nextjs/index.d.ts +53 -147
- package/dist/nextjs/index.js +113 -203
- package/dist/nextjs/server-CjpELYvq.d.cts +156 -0
- package/dist/nextjs/server-CjpELYvq.d.ts +156 -0
- package/dist/nuxt/chunk-P6ENNAEE.js +417 -0
- package/dist/nuxt/graphql.cjs +990 -0
- package/dist/nuxt/graphql.d.cts +228 -0
- package/dist/nuxt/graphql.d.ts +228 -0
- package/dist/nuxt/graphql.js +894 -0
- package/dist/nuxt/index.cjs +14 -1
- package/dist/nuxt/index.d.cts +3 -148
- package/dist/nuxt/index.d.ts +3 -148
- package/dist/nuxt/index.js +15 -391
- package/dist/nuxt/server-8v6OHbsw.d.cts +158 -0
- package/dist/nuxt/server-8v6OHbsw.d.ts +158 -0
- package/dist/pixel-DwW7cXJm.d.cts +49 -0
- package/dist/pixel-DwW7cXJm.d.ts +49 -0
- package/dist/react/components.cjs +88 -120
- package/dist/react/components.d.cts +2 -8
- package/dist/react/components.d.ts +2 -8
- package/dist/react/components.js +88 -120
- package/dist/server-Brr1UgtW.d.ts +59 -0
- package/dist/server-DJuS3YU0.d.cts +59 -0
- package/dist/sveltekit/chunk-FSARVMG5.js +413 -0
- package/dist/sveltekit/graphql.cjs +996 -0
- package/dist/sveltekit/graphql.d.cts +233 -0
- package/dist/sveltekit/graphql.d.ts +233 -0
- package/dist/sveltekit/graphql.js +900 -0
- package/dist/sveltekit/index.cjs +14 -1
- package/dist/sveltekit/index.d.cts +3 -88
- package/dist/sveltekit/index.d.ts +3 -88
- package/dist/sveltekit/index.js +15 -387
- package/dist/sveltekit/server-CO847mEV.d.cts +98 -0
- package/dist/sveltekit/server-CO847mEV.d.ts +98 -0
- package/dist/{pixel-B3oQNbZ3.d.cts → types-BYH9XqmN.d.cts} +4 -58
- package/dist/{pixel-B3oQNbZ3.d.ts → types-BYH9XqmN.d.ts} +4 -58
- package/package.json +31 -1
package/dist/nuxt/index.js
CHANGED
|
@@ -1,394 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// src/core/features.ts
|
|
16
|
-
function isEnabled(config) {
|
|
17
|
-
if (typeof config === "boolean") return config;
|
|
18
|
-
return config?.enabled ?? true;
|
|
19
|
-
}
|
|
20
|
-
function resolveFeatures(features) {
|
|
21
|
-
return {
|
|
22
|
-
segments: isEnabled(features?.segments),
|
|
23
|
-
abTesting: isEnabled(features?.abTesting),
|
|
24
|
-
editMode: isEnabled(features?.editMode)
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
var ALL_FEATURES_ENABLED = Object.freeze({
|
|
28
|
-
segments: true,
|
|
29
|
-
abTesting: true,
|
|
30
|
-
editMode: true
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// src/core/middleware.ts
|
|
34
|
-
var UTM_PARAMS = [
|
|
35
|
-
"utm_source",
|
|
36
|
-
"utm_medium",
|
|
37
|
-
"utm_term",
|
|
38
|
-
"utm_content",
|
|
39
|
-
"utm_campaign"
|
|
40
|
-
];
|
|
41
|
-
var MAX_HEADER_VALUE_LENGTH = 2048;
|
|
42
|
-
function sanitizeHeaderValue(value) {
|
|
43
|
-
const stripped = value.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
44
|
-
return stripped.length > MAX_HEADER_VALUE_LENGTH ? stripped.slice(0, MAX_HEADER_VALUE_LENGTH) : stripped;
|
|
45
|
-
}
|
|
46
|
-
function serializeCookie(cookie) {
|
|
47
|
-
const parts = [
|
|
48
|
-
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
49
|
-
`Max-Age=${cookie.maxAge}`,
|
|
50
|
-
`Path=${cookie.path}`
|
|
51
|
-
];
|
|
52
|
-
if (cookie.sameSite) {
|
|
53
|
-
parts.push(`SameSite=${cookie.sameSite}`);
|
|
54
|
-
}
|
|
55
|
-
if (cookie.secure) {
|
|
56
|
-
parts.push("Secure");
|
|
57
|
-
}
|
|
58
|
-
return parts.join("; ");
|
|
59
|
-
}
|
|
60
|
-
function parseCookies(request) {
|
|
61
|
-
const cookies = /* @__PURE__ */ new Map();
|
|
62
|
-
const header = request.headers.get("cookie");
|
|
63
|
-
if (!header) return cookies;
|
|
64
|
-
for (const part of header.split(";")) {
|
|
65
|
-
const eq = part.indexOf("=");
|
|
66
|
-
if (eq === -1) continue;
|
|
67
|
-
const name = part.slice(0, eq).trim();
|
|
68
|
-
const value = part.slice(eq + 1).trim();
|
|
69
|
-
if (!name) continue;
|
|
70
|
-
try {
|
|
71
|
-
cookies.set(name, decodeURIComponent(value));
|
|
72
|
-
} catch {
|
|
73
|
-
cookies.set(name, value);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return cookies;
|
|
77
|
-
}
|
|
78
|
-
function processFrameworkRequest(request, opts, framework) {
|
|
79
|
-
const requestHeaders = new Headers(request.headers);
|
|
80
|
-
const responseCookies = [];
|
|
81
|
-
const inboundPreprHeaders = [];
|
|
82
|
-
requestHeaders.forEach((_value, key) => {
|
|
83
|
-
if (key.toLowerCase().startsWith("prepr-")) {
|
|
84
|
-
inboundPreprHeaders.push(key);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
for (const key of inboundPreprHeaders) {
|
|
88
|
-
requestHeaders.delete(key);
|
|
89
|
-
}
|
|
90
|
-
const requestCookies = parseCookies(request);
|
|
91
|
-
const url = new URL(request.url);
|
|
92
|
-
const searchParams = url.searchParams;
|
|
93
|
-
const forwardedProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();
|
|
94
|
-
const isSecure = forwardedProto ? forwardedProto === "https" : url.protocol === "https:";
|
|
95
|
-
const crossSite = isSecure ? { sameSite: "None", secure: true } : {};
|
|
96
|
-
for (const key of UTM_PARAMS) {
|
|
97
|
-
const value = searchParams.get(key);
|
|
98
|
-
if (value !== null) {
|
|
99
|
-
requestHeaders.set(`Prepr-Context-${key}`, sanitizeHeaderValue(value));
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const referer = request.headers.get("referer");
|
|
103
|
-
if (referer) {
|
|
104
|
-
requestHeaders.set(
|
|
105
|
-
"Prepr-Context-initial_referral",
|
|
106
|
-
sanitizeHeaderValue(referer)
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
const userAgent = request.headers.get("user-agent");
|
|
110
|
-
if (userAgent) {
|
|
111
|
-
requestHeaders.set("User-Agent", sanitizeHeaderValue(userAgent));
|
|
112
|
-
}
|
|
113
|
-
const packageParts = [`@preprio/toolkit@${opts?.version ?? VERSION}`];
|
|
114
|
-
if (framework) packageParts.push(`framework=${framework}`);
|
|
115
|
-
packageParts.push(`preview=${opts?.preview ? "true" : "false"}`);
|
|
116
|
-
requestHeaders.set("Prepr-Package", packageParts.join("; "));
|
|
117
|
-
const ip = request.headers.get("Cf-Connecting-Ip") ?? request.headers.get("x-real-ip");
|
|
118
|
-
if (ip) {
|
|
119
|
-
requestHeaders.set("Prepr-Visitor-IP", sanitizeHeaderValue(ip));
|
|
120
|
-
}
|
|
121
|
-
const hutkCookie = requestCookies.get(COOKIE_HUBSPOT);
|
|
122
|
-
if (hutkCookie) {
|
|
123
|
-
requestHeaders.set("Prepr-Hubspot-Id", sanitizeHeaderValue(hutkCookie));
|
|
124
|
-
}
|
|
125
|
-
let uid = sanitizeHeaderValue(requestCookies.get(COOKIE_UID) ?? "");
|
|
126
|
-
if (!uid) {
|
|
127
|
-
uid = crypto.randomUUID();
|
|
128
|
-
responseCookies.push({
|
|
129
|
-
name: COOKIE_UID,
|
|
130
|
-
value: uid,
|
|
131
|
-
maxAge: ONE_YEAR_SECONDS,
|
|
132
|
-
path: "/"
|
|
133
|
-
});
|
|
134
|
-
requestHeaders.set("Prepr-Customer-Id-Created", "true");
|
|
135
|
-
}
|
|
136
|
-
requestHeaders.set("Prepr-Customer-Id", uid);
|
|
137
|
-
if (!opts?.preview) {
|
|
138
|
-
return { requestHeaders, responseCookies };
|
|
139
|
-
}
|
|
140
|
-
const features = resolveFeatures(opts?.features);
|
|
141
|
-
const hasPreviewParams = features.segments && searchParams.has(PARAM_SEGMENT) || features.abTesting && searchParams.has(PARAM_VARIANT);
|
|
142
|
-
if (!hasPreviewParams) {
|
|
143
|
-
const previewModeCookie = requestCookies.get(COOKIE_PREVIEW_MODE);
|
|
144
|
-
const previewModeEnabled = previewModeCookie !== "false";
|
|
145
|
-
if (!previewModeEnabled) {
|
|
146
|
-
return { requestHeaders, responseCookies };
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
150
|
-
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
151
|
-
if (!isLivePreview && !requestCookies.has(COOKIE_PREVIEW_MODE)) {
|
|
152
|
-
responseCookies.push({
|
|
153
|
-
name: COOKIE_PREVIEW_MODE,
|
|
154
|
-
value: "true",
|
|
155
|
-
maxAge: ONE_YEAR_SECONDS,
|
|
156
|
-
path: "/",
|
|
157
|
-
...crossSite
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
if (!isLivePreview) {
|
|
161
|
-
const segmentCookie = features.segments ? requestCookies.get(COOKIE_SEGMENT) : void 0;
|
|
162
|
-
if (segmentCookie) {
|
|
163
|
-
requestHeaders.set("Prepr-Segments", sanitizeHeaderValue(segmentCookie));
|
|
164
|
-
}
|
|
165
|
-
const abCookie = features.abTesting ? requestCookies.get(COOKIE_VARIANT) : void 0;
|
|
166
|
-
if (abCookie) {
|
|
167
|
-
requestHeaders.set("Prepr-ABtesting", sanitizeHeaderValue(abCookie));
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
const previewAb = features.abTesting ? searchParams.get(PARAM_VARIANT) : null;
|
|
171
|
-
if (previewAb !== null) {
|
|
172
|
-
requestHeaders.set("Prepr-ABtesting", sanitizeHeaderValue(previewAb));
|
|
173
|
-
if (!isLivePreview) {
|
|
174
|
-
responseCookies.push({
|
|
175
|
-
name: COOKIE_VARIANT,
|
|
176
|
-
value: previewAb,
|
|
177
|
-
maxAge: ONE_YEAR_SECONDS,
|
|
178
|
-
path: "/",
|
|
179
|
-
...crossSite
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
const previewSegment = features.segments ? searchParams.get(PARAM_SEGMENT) : null;
|
|
184
|
-
if (previewSegment !== null) {
|
|
185
|
-
requestHeaders.set("Prepr-Segments", sanitizeHeaderValue(previewSegment));
|
|
186
|
-
if (!isLivePreview) {
|
|
187
|
-
responseCookies.push({
|
|
188
|
-
name: COOKIE_SEGMENT,
|
|
189
|
-
value: previewSegment,
|
|
190
|
-
maxAge: ONE_YEAR_SECONDS,
|
|
191
|
-
path: "/",
|
|
192
|
-
...crossSite
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return { requestHeaders, responseCookies };
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// src/nuxt/middleware.ts
|
|
200
|
-
var PREPR_CONTEXT_KEY = "prepr";
|
|
201
|
-
function handlePreprRequest(event, options) {
|
|
202
|
-
const req = event.node.req;
|
|
203
|
-
const host = typeof req.headers.host === "string" ? req.headers.host : "localhost";
|
|
204
|
-
const request = new Request(`http://${host}${req.url ?? "/"}`);
|
|
205
|
-
for (const [key, value] of Object.entries(req.headers)) {
|
|
206
|
-
if (typeof value === "string") {
|
|
207
|
-
request.headers.set(key, value);
|
|
208
|
-
} else if (Array.isArray(value)) {
|
|
209
|
-
request.headers.set(key, value.join(", "));
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
const { requestHeaders, responseCookies } = processFrameworkRequest(
|
|
213
|
-
request,
|
|
214
|
-
options,
|
|
215
|
-
"nuxt"
|
|
216
|
-
);
|
|
217
|
-
requestHeaders.forEach((value, key) => {
|
|
218
|
-
req.headers[key.toLowerCase()] = value;
|
|
219
|
-
});
|
|
220
|
-
event.context[PREPR_CONTEXT_KEY] = requestHeaders;
|
|
221
|
-
for (const cookie of responseCookies) {
|
|
222
|
-
event.node.res.appendHeader("Set-Cookie", serializeCookie(cookie));
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
function getPreprHeadersFromEvent(event) {
|
|
226
|
-
const headers = event.context[PREPR_CONTEXT_KEY];
|
|
227
|
-
return headers instanceof Headers ? headers : new Headers();
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// src/core/server.ts
|
|
231
|
-
var PreprError = class extends Error {
|
|
232
|
-
constructor(message, code, context, originalError) {
|
|
233
|
-
super(message);
|
|
234
|
-
this.code = code;
|
|
235
|
-
this.context = context;
|
|
236
|
-
this.originalError = originalError;
|
|
237
|
-
this.name = "PreprError";
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
function getPreprUUIDFromHeaders(headers) {
|
|
241
|
-
return headers.get("prepr-customer-id");
|
|
242
|
-
}
|
|
243
|
-
function getActiveSegmentFromHeaders(headers) {
|
|
244
|
-
return headers.get("Prepr-Segments");
|
|
245
|
-
}
|
|
246
|
-
function getActiveVariantFromHeaders(headers) {
|
|
247
|
-
return headers.get("Prepr-ABtesting");
|
|
248
|
-
}
|
|
249
|
-
var PREPR_HEADER_KEYS = [
|
|
250
|
-
"prepr-customer-id",
|
|
251
|
-
"Prepr-Segments",
|
|
252
|
-
"Prepr-ABtesting",
|
|
253
|
-
"Prepr-Preview-Bar",
|
|
254
|
-
"Prepr-Context-utm_source",
|
|
255
|
-
"Prepr-Context-utm_medium",
|
|
256
|
-
"Prepr-Context-utm_term",
|
|
257
|
-
"Prepr-Context-utm_content",
|
|
258
|
-
"Prepr-Context-utm_campaign",
|
|
259
|
-
"Prepr-Context-initial_referral",
|
|
260
|
-
"Prepr-Visitor-IP",
|
|
261
|
-
"Prepr-Hubspot-Id",
|
|
262
|
-
"Prepr-Customer-Id-Created",
|
|
263
|
-
"User-Agent",
|
|
264
|
-
"Prepr-Package"
|
|
265
|
-
];
|
|
266
|
-
function getPreprHeadersFromHeaders(headers) {
|
|
267
|
-
const preprHeaders = {};
|
|
268
|
-
const copy = (key) => {
|
|
269
|
-
const value = headers.get(key);
|
|
270
|
-
if (value !== null) {
|
|
271
|
-
preprHeaders[key] = value;
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
for (const key of PREPR_HEADER_KEYS) {
|
|
275
|
-
copy(key);
|
|
276
|
-
}
|
|
277
|
-
return preprHeaders;
|
|
278
|
-
}
|
|
279
|
-
function validatePreprToken(token) {
|
|
280
|
-
if (!token) {
|
|
281
|
-
throw new PreprError(
|
|
282
|
-
"Token is required",
|
|
283
|
-
"MISSING_TOKEN",
|
|
284
|
-
"validatePreprToken"
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
if (!token.startsWith("https://")) {
|
|
288
|
-
throw new PreprError(
|
|
289
|
-
"Token must be a valid HTTPS URL",
|
|
290
|
-
"INVALID_TOKEN",
|
|
291
|
-
"validatePreprToken"
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
function extractAccessToken(graphqlUrl) {
|
|
296
|
-
let url;
|
|
297
|
-
try {
|
|
298
|
-
url = new URL(graphqlUrl);
|
|
299
|
-
} catch (error) {
|
|
300
|
-
throw new PreprError(
|
|
301
|
-
"Token must be a valid URL",
|
|
302
|
-
"INVALID_TOKEN",
|
|
303
|
-
"extractAccessToken",
|
|
304
|
-
error instanceof Error ? error : new Error(String(error))
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
if (url.hostname !== "graphql.prepr.io") {
|
|
308
|
-
throw new PreprError(
|
|
309
|
-
"Token must be a graphql.prepr.io URL",
|
|
310
|
-
"INVALID_TOKEN",
|
|
311
|
-
"extractAccessToken"
|
|
312
|
-
);
|
|
313
|
-
}
|
|
314
|
-
const pathParts = url.pathname.split("/");
|
|
315
|
-
const token = pathParts[pathParts.length - 1];
|
|
316
|
-
if (!token) {
|
|
317
|
-
throw new PreprError(
|
|
318
|
-
"Token URL is missing an access token segment",
|
|
319
|
-
"INVALID_TOKEN",
|
|
320
|
-
"extractAccessToken"
|
|
321
|
-
);
|
|
322
|
-
}
|
|
323
|
-
return token;
|
|
324
|
-
}
|
|
325
|
-
async function getPreprEnvironmentSegments(token) {
|
|
326
|
-
validatePreprToken(token);
|
|
327
|
-
try {
|
|
328
|
-
const response = await fetch(token, {
|
|
329
|
-
headers: {
|
|
330
|
-
"Prepr-Package": `@preprio/toolkit@${VERSION}`,
|
|
331
|
-
// Duplicates the version in Prepr-Package. It keeps the User-Agent
|
|
332
|
-
// format used by `@preprio/prepr-nextjs`, for Prepr API consumers that
|
|
333
|
-
// still identify the toolbar by that string.
|
|
334
|
-
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
335
|
-
"Content-Type": "application/json"
|
|
336
|
-
},
|
|
337
|
-
method: "POST",
|
|
338
|
-
body: JSON.stringify({
|
|
339
|
-
query: `{
|
|
340
|
-
_Segments {
|
|
341
|
-
_id
|
|
342
|
-
name
|
|
343
|
-
}
|
|
344
|
-
}`
|
|
345
|
-
})
|
|
346
|
-
});
|
|
347
|
-
if (!response.ok) {
|
|
348
|
-
throw new PreprError(
|
|
349
|
-
`HTTP ${response.status}: ${response.statusText}`,
|
|
350
|
-
"HTTP_ERROR",
|
|
351
|
-
"getPreprEnvironmentSegments"
|
|
352
|
-
);
|
|
353
|
-
}
|
|
354
|
-
const json = await response.json();
|
|
355
|
-
if (!json || !json.data || !json.data._Segments) {
|
|
356
|
-
throw new PreprError(
|
|
357
|
-
"Invalid response format from Prepr API",
|
|
358
|
-
"INVALID_RESPONSE",
|
|
359
|
-
"getPreprEnvironmentSegments"
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
return json.data._Segments;
|
|
363
|
-
} catch (error) {
|
|
364
|
-
if (error instanceof PreprError) {
|
|
365
|
-
throw error;
|
|
366
|
-
}
|
|
367
|
-
throw new PreprError(
|
|
368
|
-
"Failed to fetch segments from Prepr API",
|
|
369
|
-
"FETCH_ERROR",
|
|
370
|
-
"getPreprEnvironmentSegments",
|
|
371
|
-
error instanceof Error ? error : new Error(String(error))
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
async function getToolbarPropsFromHeaders(headers, token, features) {
|
|
376
|
-
const resolved = resolveFeatures(features);
|
|
377
|
-
let segments = [];
|
|
378
|
-
if (resolved.segments) {
|
|
379
|
-
try {
|
|
380
|
-
segments = await getPreprEnvironmentSegments(token);
|
|
381
|
-
} catch {
|
|
382
|
-
segments = [];
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
return {
|
|
386
|
-
activeSegment: resolved.segments ? getActiveSegmentFromHeaders(headers) : null,
|
|
387
|
-
activeVariant: resolved.abTesting ? getActiveVariantFromHeaders(headers) : null,
|
|
388
|
-
segments,
|
|
389
|
-
data: segments
|
|
390
|
-
};
|
|
391
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
PREPR_CONTEXT_KEY,
|
|
3
|
+
PreprError,
|
|
4
|
+
extractAccessToken,
|
|
5
|
+
getActiveSegmentFromHeaders,
|
|
6
|
+
getActiveVariantFromHeaders,
|
|
7
|
+
getPreprHeadersFromEvent,
|
|
8
|
+
getPreprHeadersFromHeaders,
|
|
9
|
+
getPreprUUIDFromHeaders,
|
|
10
|
+
getToolbarPropsFromHeaders,
|
|
11
|
+
handlePreprRequest,
|
|
12
|
+
isPersonalizedResponse,
|
|
13
|
+
validatePreprToken
|
|
14
|
+
} from "./chunk-P6ENNAEE.js";
|
|
392
15
|
|
|
393
16
|
// src/nuxt/server.ts
|
|
394
17
|
function getPreprUUID(headers) {
|
|
@@ -417,5 +40,6 @@ export {
|
|
|
417
40
|
getPreprUUID,
|
|
418
41
|
getToolbarProps,
|
|
419
42
|
handlePreprRequest,
|
|
43
|
+
isPersonalizedResponse,
|
|
420
44
|
validatePreprToken
|
|
421
45
|
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
interface PreprSegment {
|
|
2
|
+
readonly _id: string;
|
|
3
|
+
readonly name: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Per-feature config. `false` is shorthand for `{ enabled: false }`; the object
|
|
7
|
+
* form leaves room for per-feature options later without a breaking change.
|
|
8
|
+
*/
|
|
9
|
+
type PreprFeatureConfig = boolean | {
|
|
10
|
+
enabled?: boolean;
|
|
11
|
+
};
|
|
12
|
+
/** Toolkit features a consumer can turn off. Omitted keys stay enabled. */
|
|
13
|
+
interface PreprFeatures {
|
|
14
|
+
/** Segment picker, `Prepr-Segments` header and its cookie/query param. */
|
|
15
|
+
segments?: PreprFeatureConfig;
|
|
16
|
+
/** A/B variant switch, `Prepr-ABtesting` header and its cookie/query param. */
|
|
17
|
+
abTesting?: PreprFeatureConfig;
|
|
18
|
+
/**
|
|
19
|
+
* The site's own click-to-edit: the toolbar's Edit mode control, the stega
|
|
20
|
+
* tooltip/overlay and the close-edit pill.
|
|
21
|
+
*
|
|
22
|
+
* NOT a security control, and NOT the Prepr editor's live preview. When the
|
|
23
|
+
* site is framed by the editor, `prepr:initVE` still activates edit mode —
|
|
24
|
+
* that is the CMS operating inside its own iframe, not an affordance offered
|
|
25
|
+
* to visitors. See iframe-bridge.ts.
|
|
26
|
+
*/
|
|
27
|
+
editMode?: PreprFeatureConfig;
|
|
28
|
+
}
|
|
29
|
+
interface PreprToolbarProps {
|
|
30
|
+
/**
|
|
31
|
+
* Server-resolved active segment. Optional: omit it with `segments`
|
|
32
|
+
* disabled, where there is nothing to resolve. When enabled and omitted,
|
|
33
|
+
* the persisted cookie is used.
|
|
34
|
+
*/
|
|
35
|
+
readonly activeSegment?: string | null;
|
|
36
|
+
/**
|
|
37
|
+
* Server-resolved active A/B variant. Optional: omit it with `abTesting`
|
|
38
|
+
* disabled, where there is nothing to resolve. When enabled and omitted,
|
|
39
|
+
* the persisted cookie is used.
|
|
40
|
+
*/
|
|
41
|
+
readonly activeVariant?: string | null;
|
|
42
|
+
/** Available segments to personalize on. */
|
|
43
|
+
readonly segments?: readonly PreprSegment[];
|
|
44
|
+
/** @deprecated Alias for `segments`. */
|
|
45
|
+
readonly data?: readonly PreprSegment[];
|
|
46
|
+
}
|
|
47
|
+
type PreprVariant = 'A' | 'B';
|
|
48
|
+
interface PreprHeaders {
|
|
49
|
+
readonly 'prepr-customer-id'?: string;
|
|
50
|
+
readonly 'Prepr-Segments'?: string;
|
|
51
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
52
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
53
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
54
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
55
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
56
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
57
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
58
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
59
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
60
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
61
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
62
|
+
/** The visitor's browser User-Agent, used by the API for device detection. */
|
|
63
|
+
readonly 'User-Agent'?: string;
|
|
64
|
+
/** @deprecated Never populated; the visitor's value is under `User-Agent`. */
|
|
65
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
66
|
+
readonly 'Prepr-Package'?: string;
|
|
67
|
+
}
|
|
68
|
+
type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR' | 'GRAPHQL_ERROR' | 'INVALID_DOCUMENT';
|
|
69
|
+
|
|
70
|
+
interface PreprMiddlewareOptions {
|
|
71
|
+
/** Enable preview-mode headers (segment/AB overrides, preview bar). */
|
|
72
|
+
preview?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Disable features app-wide. Pass the same object you give the toolbar: a
|
|
75
|
+
* disabled feature injects no header and persists no cookie, whatever the
|
|
76
|
+
* request carries.
|
|
77
|
+
*/
|
|
78
|
+
features?: PreprFeatures;
|
|
79
|
+
/** Override the version reported in Prepr-Package (mainly for tests). */
|
|
80
|
+
version?: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Structural stand-in for h3's `H3Event`, so this package needs no build-time
|
|
85
|
+
* dependency on `h3`/`nuxt`. Only the fields `handlePreprRequest` uses.
|
|
86
|
+
*
|
|
87
|
+
* `node` ties this to Nitro's Node.js preset (the Nuxt default). Edge presets
|
|
88
|
+
* expose no raw Node request/response to fold headers back onto.
|
|
89
|
+
*/
|
|
90
|
+
interface H3EventLike {
|
|
91
|
+
node: {
|
|
92
|
+
req: {
|
|
93
|
+
url?: string;
|
|
94
|
+
headers: Record<string, string | string[] | undefined>;
|
|
95
|
+
};
|
|
96
|
+
res: {
|
|
97
|
+
/** Node's `ServerResponse.appendHeader` (Node >= 18.3). */
|
|
98
|
+
appendHeader(name: string, value: string): unknown;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
/** Request-scoped bag shared with route handlers and Vue via `useRequestEvent`. */
|
|
102
|
+
context: Record<string, unknown>;
|
|
103
|
+
}
|
|
104
|
+
/** Key under which `handlePreprRequest` mirrors the forwarded Prepr `Headers`. */
|
|
105
|
+
declare const PREPR_CONTEXT_KEY = "prepr";
|
|
106
|
+
/**
|
|
107
|
+
* Nuxt server middleware entry point — call it inside `defineEventHandler` in
|
|
108
|
+
* `server/middleware/prepr.ts`:
|
|
109
|
+
*
|
|
110
|
+
* ```ts
|
|
111
|
+
* export default defineEventHandler(event => {
|
|
112
|
+
* handlePreprRequest(event, { preview: import.meta.dev });
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* The computed Prepr headers are folded back onto the raw Node request
|
|
117
|
+
* (lowercased, as Node stores them), which is what `useRequestHeaders()` and
|
|
118
|
+
* downstream handlers read. The same `Headers` are mirrored onto
|
|
119
|
+
* `event.context.prepr` for callers who prefer an explicit read
|
|
120
|
+
* (`getPreprHeadersFromEvent`). Cookies are appended to the response.
|
|
121
|
+
*/
|
|
122
|
+
declare function handlePreprRequest(event: H3EventLike, options?: PreprMiddlewareOptions): void;
|
|
123
|
+
/**
|
|
124
|
+
* Personalization headers to forward to your GraphQL fetch, from the h3 event.
|
|
125
|
+
* Empty `Headers` if `handlePreprRequest` did not run, so the result is always
|
|
126
|
+
* safe to spread.
|
|
127
|
+
*/
|
|
128
|
+
declare function getPreprHeadersFromEvent(event: Pick<H3EventLike, 'context'>): Headers;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Whether a Prepr GraphQL response differs per visitor, read from its `Vary`
|
|
132
|
+
* header. Use it when fetching with your own GraphQL client; the toolkit's
|
|
133
|
+
* `createPreprClient` already returns it as `personalized`.
|
|
134
|
+
*
|
|
135
|
+
* Never throws; a response without `Vary` is not personalized.
|
|
136
|
+
*/
|
|
137
|
+
declare function isPersonalizedResponse(source: Response | Headers): boolean;
|
|
138
|
+
|
|
139
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
140
|
+
declare class PreprError extends Error {
|
|
141
|
+
readonly code: PreprErrorCode;
|
|
142
|
+
readonly context?: string | undefined;
|
|
143
|
+
readonly originalError?: Error | undefined;
|
|
144
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
|
|
148
|
+
* @throws PreprError if the token is missing or not an HTTPS URL.
|
|
149
|
+
*/
|
|
150
|
+
declare function validatePreprToken(token: string): void;
|
|
151
|
+
/**
|
|
152
|
+
* Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
|
|
153
|
+
* -> `abc123`.
|
|
154
|
+
* @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
|
|
155
|
+
*/
|
|
156
|
+
declare function extractAccessToken(graphqlUrl: string): string;
|
|
157
|
+
|
|
158
|
+
export { type H3EventLike as H, type PreprHeaders as P, PreprError as a, type PreprFeatures as b, type PreprToolbarProps as c, PREPR_CONTEXT_KEY as d, type PreprMiddlewareOptions as e, extractAccessToken as f, getPreprHeadersFromEvent as g, handlePreprRequest as h, isPersonalizedResponse as i, validatePreprToken as v };
|