@preprio/toolkit 0.1.0-beta.1
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 +903 -0
- package/dist/astro/index.cjs +319 -0
- package/dist/astro/index.d.cts +76 -0
- package/dist/astro/index.d.ts +76 -0
- package/dist/astro/index.js +287 -0
- package/dist/chunk-H7HVQSSF.js +2081 -0
- package/dist/index.cjs +2393 -0
- package/dist/index.d.cts +179 -0
- package/dist/index.d.ts +179 -0
- package/dist/index.js +306 -0
- package/dist/nextjs/components.cjs +2087 -0
- package/dist/nextjs/components.d.cts +17 -0
- package/dist/nextjs/components.d.ts +17 -0
- package/dist/nextjs/components.js +34 -0
- package/dist/nextjs/image-loader.cjs +122 -0
- package/dist/nextjs/image-loader.d.cts +10 -0
- package/dist/nextjs/image-loader.d.ts +10 -0
- package/dist/nextjs/image-loader.js +101 -0
- package/dist/nextjs/index.cjs +386 -0
- package/dist/nextjs/index.d.cts +90 -0
- package/dist/nextjs/index.d.ts +90 -0
- package/dist/nextjs/index.js +352 -0
- package/dist/pixel-DVSJQC0K.d.cts +94 -0
- package/dist/pixel-DVSJQC0K.d.ts +94 -0
- package/dist/sveltekit/index.cjs +371 -0
- package/dist/sveltekit/index.d.cts +117 -0
- package/dist/sveltekit/index.d.ts +117 -0
- package/dist/sveltekit/index.js +334 -0
- package/package.json +93 -0
- package/src/astro/PreprToolbar.astro +53 -0
- package/src/astro/PreprTrackingPixel.astro +48 -0
- package/src/sveltekit/PreprToolbar.svelte +21 -0
- package/src/sveltekit/PreprTrackingPixel.svelte +22 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/astro/index.ts
|
|
21
|
+
var astro_exports = {};
|
|
22
|
+
__export(astro_exports, {
|
|
23
|
+
getActiveSegment: () => getActiveSegment,
|
|
24
|
+
getActiveVariant: () => getActiveVariant,
|
|
25
|
+
getPreprHeaders: () => getPreprHeaders,
|
|
26
|
+
getPreprUUID: () => getPreprUUID,
|
|
27
|
+
getToolbarProps: () => getToolbarProps,
|
|
28
|
+
onPreprRequest: () => onPreprRequest
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(astro_exports);
|
|
31
|
+
|
|
32
|
+
// src/version.ts
|
|
33
|
+
var VERSION = "0.1.0-beta.1";
|
|
34
|
+
|
|
35
|
+
// src/core/server.ts
|
|
36
|
+
var PreprError = class extends Error {
|
|
37
|
+
constructor(message, code, context, originalError) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.code = code;
|
|
40
|
+
this.context = context;
|
|
41
|
+
this.originalError = originalError;
|
|
42
|
+
this.name = "PreprError";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
function getPreprUUIDFromHeaders(headers) {
|
|
46
|
+
return headers.get("prepr-customer-id");
|
|
47
|
+
}
|
|
48
|
+
function getActiveSegmentFromHeaders(headers) {
|
|
49
|
+
return headers.get("Prepr-Segments");
|
|
50
|
+
}
|
|
51
|
+
function getActiveVariantFromHeaders(headers) {
|
|
52
|
+
return headers.get("Prepr-ABtesting");
|
|
53
|
+
}
|
|
54
|
+
var PREPR_HEADER_KEYS = [
|
|
55
|
+
"prepr-customer-id",
|
|
56
|
+
"Prepr-Segments",
|
|
57
|
+
"Prepr-ABtesting",
|
|
58
|
+
"Prepr-Preview-Bar",
|
|
59
|
+
"Prepr-Context-utm_source",
|
|
60
|
+
"Prepr-Context-utm_medium",
|
|
61
|
+
"Prepr-Context-utm_term",
|
|
62
|
+
"Prepr-Context-utm_content",
|
|
63
|
+
"Prepr-Context-utm_campaign",
|
|
64
|
+
"Prepr-Context-initial_referral",
|
|
65
|
+
"Prepr-Visitor-IP",
|
|
66
|
+
"Prepr-Hubspot-Id",
|
|
67
|
+
"Prepr-Customer-Id-Created",
|
|
68
|
+
"Prepr-User-Agent"
|
|
69
|
+
];
|
|
70
|
+
function getPreprHeadersFromHeaders(headers) {
|
|
71
|
+
const preprHeaders = {};
|
|
72
|
+
const copy = (key) => {
|
|
73
|
+
const value = headers.get(key);
|
|
74
|
+
if (value !== null) {
|
|
75
|
+
preprHeaders[key] = value;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
for (const key of PREPR_HEADER_KEYS) {
|
|
79
|
+
copy(key);
|
|
80
|
+
}
|
|
81
|
+
return preprHeaders;
|
|
82
|
+
}
|
|
83
|
+
function validatePreprToken(token) {
|
|
84
|
+
if (!token) {
|
|
85
|
+
throw new PreprError("Token is required", "MISSING_TOKEN", "validatePreprToken");
|
|
86
|
+
}
|
|
87
|
+
if (!token.startsWith("https://")) {
|
|
88
|
+
throw new PreprError(
|
|
89
|
+
"Token must be a valid HTTPS URL",
|
|
90
|
+
"INVALID_TOKEN",
|
|
91
|
+
"validatePreprToken"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function getPreprEnvironmentSegments(token) {
|
|
96
|
+
validatePreprToken(token);
|
|
97
|
+
try {
|
|
98
|
+
const response = await fetch(token, {
|
|
99
|
+
headers: {
|
|
100
|
+
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
101
|
+
"Content-Type": "application/json"
|
|
102
|
+
},
|
|
103
|
+
method: "POST",
|
|
104
|
+
body: JSON.stringify({
|
|
105
|
+
query: `{
|
|
106
|
+
_Segments {
|
|
107
|
+
_id
|
|
108
|
+
name
|
|
109
|
+
}
|
|
110
|
+
}`
|
|
111
|
+
})
|
|
112
|
+
});
|
|
113
|
+
if (!response.ok) {
|
|
114
|
+
throw new PreprError(
|
|
115
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
116
|
+
"HTTP_ERROR",
|
|
117
|
+
"getPreprEnvironmentSegments"
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const json = await response.json();
|
|
121
|
+
if (!json || !json.data || !json.data._Segments) {
|
|
122
|
+
throw new PreprError(
|
|
123
|
+
"Invalid response format from Prepr API",
|
|
124
|
+
"INVALID_RESPONSE",
|
|
125
|
+
"getPreprEnvironmentSegments"
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return json.data._Segments;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (error instanceof PreprError) {
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
throw new PreprError(
|
|
134
|
+
"Failed to fetch segments from Prepr API",
|
|
135
|
+
"FETCH_ERROR",
|
|
136
|
+
"getPreprEnvironmentSegments",
|
|
137
|
+
error instanceof Error ? error : new Error(String(error))
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async function getToolbarPropsFromHeaders(headers, token) {
|
|
142
|
+
let segments = [];
|
|
143
|
+
try {
|
|
144
|
+
segments = await getPreprEnvironmentSegments(token);
|
|
145
|
+
} catch {
|
|
146
|
+
segments = [];
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
activeSegment: getActiveSegmentFromHeaders(headers),
|
|
150
|
+
activeVariant: getActiveVariantFromHeaders(headers),
|
|
151
|
+
segments,
|
|
152
|
+
data: segments
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/core/constants.ts
|
|
157
|
+
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
158
|
+
var COOKIE_SEGMENT = "Prepr-Segments";
|
|
159
|
+
var COOKIE_VARIANT = "Prepr-ABtesting";
|
|
160
|
+
var COOKIE_UID = "__prepr_uid";
|
|
161
|
+
var COOKIE_HUBSPOT = "hubspotutk";
|
|
162
|
+
var PARAM_SEGMENT = "prepr_preview_segment";
|
|
163
|
+
var PARAM_VARIANT = "prepr_preview_ab";
|
|
164
|
+
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
165
|
+
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
166
|
+
|
|
167
|
+
// src/core/middleware.ts
|
|
168
|
+
var UTM_PARAMS = [
|
|
169
|
+
"utm_source",
|
|
170
|
+
"utm_medium",
|
|
171
|
+
"utm_term",
|
|
172
|
+
"utm_content",
|
|
173
|
+
"utm_campaign"
|
|
174
|
+
];
|
|
175
|
+
function parseCookies(request) {
|
|
176
|
+
const cookies = /* @__PURE__ */ new Map();
|
|
177
|
+
const header = request.headers.get("cookie");
|
|
178
|
+
if (!header) return cookies;
|
|
179
|
+
for (const part of header.split(";")) {
|
|
180
|
+
const eq = part.indexOf("=");
|
|
181
|
+
if (eq === -1) continue;
|
|
182
|
+
const name = part.slice(0, eq).trim();
|
|
183
|
+
const value = part.slice(eq + 1).trim();
|
|
184
|
+
if (!name) continue;
|
|
185
|
+
cookies.set(name, decodeURIComponent(value));
|
|
186
|
+
}
|
|
187
|
+
return cookies;
|
|
188
|
+
}
|
|
189
|
+
function processPreprRequest(request, opts) {
|
|
190
|
+
const requestHeaders = new Headers(request.headers);
|
|
191
|
+
const responseCookies = [];
|
|
192
|
+
const requestCookies = parseCookies(request);
|
|
193
|
+
const searchParams = new URL(request.url).searchParams;
|
|
194
|
+
for (const key of UTM_PARAMS) {
|
|
195
|
+
const value = searchParams.get(key);
|
|
196
|
+
if (value !== null) {
|
|
197
|
+
requestHeaders.set(`Prepr-Context-${key}`, value);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const referer = request.headers.get("referer");
|
|
201
|
+
if (referer) {
|
|
202
|
+
requestHeaders.set("Prepr-Context-initial_referral", referer);
|
|
203
|
+
}
|
|
204
|
+
const userAgent = request.headers.get("user-agent");
|
|
205
|
+
if (userAgent) {
|
|
206
|
+
requestHeaders.set("Prepr-User-Agent", userAgent);
|
|
207
|
+
}
|
|
208
|
+
requestHeaders.set("Prepr-Package", `@preprio/toolkit@${opts?.version ?? VERSION}`);
|
|
209
|
+
const ip = request.headers.get("Cf-Connecting-Ip") ?? request.headers.get("x-real-ip");
|
|
210
|
+
if (ip) {
|
|
211
|
+
requestHeaders.set("Prepr-Visitor-IP", ip);
|
|
212
|
+
}
|
|
213
|
+
const hutkCookie = requestCookies.get(COOKIE_HUBSPOT);
|
|
214
|
+
if (hutkCookie) {
|
|
215
|
+
requestHeaders.set("Prepr-Hubspot-Id", hutkCookie);
|
|
216
|
+
}
|
|
217
|
+
let uid = requestCookies.get(COOKIE_UID);
|
|
218
|
+
if (!uid) {
|
|
219
|
+
uid = crypto.randomUUID();
|
|
220
|
+
responseCookies.push({
|
|
221
|
+
name: COOKIE_UID,
|
|
222
|
+
value: uid,
|
|
223
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
224
|
+
path: "/"
|
|
225
|
+
});
|
|
226
|
+
requestHeaders.set("Prepr-Customer-Id-Created", "true");
|
|
227
|
+
}
|
|
228
|
+
requestHeaders.set("Prepr-Customer-Id", uid);
|
|
229
|
+
if (!opts?.preview) {
|
|
230
|
+
return { requestHeaders, responseCookies };
|
|
231
|
+
}
|
|
232
|
+
const hasPreviewParams = searchParams.has(PARAM_SEGMENT) || searchParams.has(PARAM_VARIANT);
|
|
233
|
+
if (!hasPreviewParams) {
|
|
234
|
+
const previewModeCookie = requestCookies.get(COOKIE_PREVIEW_MODE);
|
|
235
|
+
const previewModeEnabled = previewModeCookie !== "false";
|
|
236
|
+
if (!previewModeEnabled) {
|
|
237
|
+
return { requestHeaders, responseCookies };
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
241
|
+
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
242
|
+
if (!isLivePreview) {
|
|
243
|
+
const segmentCookie = requestCookies.get(COOKIE_SEGMENT);
|
|
244
|
+
if (segmentCookie) {
|
|
245
|
+
requestHeaders.set("Prepr-Segments", segmentCookie);
|
|
246
|
+
}
|
|
247
|
+
const abCookie = requestCookies.get(COOKIE_VARIANT);
|
|
248
|
+
if (abCookie) {
|
|
249
|
+
requestHeaders.set("Prepr-ABtesting", abCookie);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
const previewAb = searchParams.get(PARAM_VARIANT);
|
|
253
|
+
if (previewAb !== null) {
|
|
254
|
+
requestHeaders.set("Prepr-ABtesting", previewAb);
|
|
255
|
+
if (!isLivePreview) {
|
|
256
|
+
responseCookies.push({
|
|
257
|
+
name: COOKIE_VARIANT,
|
|
258
|
+
value: previewAb,
|
|
259
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
260
|
+
path: "/"
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const previewSegment = searchParams.get(PARAM_SEGMENT);
|
|
265
|
+
if (previewSegment !== null) {
|
|
266
|
+
requestHeaders.set("Prepr-Segments", previewSegment);
|
|
267
|
+
if (!isLivePreview) {
|
|
268
|
+
responseCookies.push({
|
|
269
|
+
name: COOKIE_SEGMENT,
|
|
270
|
+
value: previewSegment,
|
|
271
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
272
|
+
path: "/"
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return { requestHeaders, responseCookies };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// src/astro/index.ts
|
|
280
|
+
async function onPreprRequest(context, next, options) {
|
|
281
|
+
const { requestHeaders, responseCookies } = processPreprRequest(context.request, options);
|
|
282
|
+
requestHeaders.forEach((value, key) => {
|
|
283
|
+
context.request.headers.set(key, value);
|
|
284
|
+
});
|
|
285
|
+
const response = await next();
|
|
286
|
+
for (const cookie of responseCookies) {
|
|
287
|
+
const parts = [
|
|
288
|
+
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
289
|
+
`Max-Age=${cookie.maxAge}`,
|
|
290
|
+
`Path=${cookie.path}`
|
|
291
|
+
];
|
|
292
|
+
response.headers.append("Set-Cookie", parts.join("; "));
|
|
293
|
+
}
|
|
294
|
+
return response;
|
|
295
|
+
}
|
|
296
|
+
function getPreprUUID(headers) {
|
|
297
|
+
return getPreprUUIDFromHeaders(headers);
|
|
298
|
+
}
|
|
299
|
+
function getActiveSegment(headers) {
|
|
300
|
+
return getActiveSegmentFromHeaders(headers);
|
|
301
|
+
}
|
|
302
|
+
function getActiveVariant(headers) {
|
|
303
|
+
return getActiveVariantFromHeaders(headers);
|
|
304
|
+
}
|
|
305
|
+
function getPreprHeaders(headers) {
|
|
306
|
+
return getPreprHeadersFromHeaders(headers);
|
|
307
|
+
}
|
|
308
|
+
async function getToolbarProps(headers, token) {
|
|
309
|
+
return getToolbarPropsFromHeaders(headers, token);
|
|
310
|
+
}
|
|
311
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
312
|
+
0 && (module.exports = {
|
|
313
|
+
getActiveSegment,
|
|
314
|
+
getActiveVariant,
|
|
315
|
+
getPreprHeaders,
|
|
316
|
+
getPreprUUID,
|
|
317
|
+
getToolbarProps,
|
|
318
|
+
onPreprRequest
|
|
319
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
interface PreprMiddlewareOptions {
|
|
2
|
+
/** Enable preview-mode headers (segment/AB overrides, preview bar). */
|
|
3
|
+
preview?: boolean;
|
|
4
|
+
/** Override the version reported in Prepr-Package (mainly for tests). */
|
|
5
|
+
version?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface PreprSegment {
|
|
9
|
+
readonly _id: string;
|
|
10
|
+
readonly name: string;
|
|
11
|
+
}
|
|
12
|
+
interface PreprToolbarProps {
|
|
13
|
+
readonly activeSegment: string | null;
|
|
14
|
+
readonly activeVariant: string | null;
|
|
15
|
+
/** Available segments to personalize on. */
|
|
16
|
+
readonly segments?: readonly PreprSegment[];
|
|
17
|
+
/** @deprecated Alias for `segments`. */
|
|
18
|
+
readonly data?: readonly PreprSegment[];
|
|
19
|
+
}
|
|
20
|
+
type PreprVariant = 'A' | 'B';
|
|
21
|
+
interface PreprHeaders {
|
|
22
|
+
readonly 'prepr-customer-id'?: string;
|
|
23
|
+
readonly 'Prepr-Segments'?: string;
|
|
24
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
25
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
26
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
27
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
28
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
29
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
30
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
31
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
32
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
33
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
34
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
35
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Structural stand-in for Astro's `APIContext`, so this package needs no
|
|
40
|
+
* build-time dependency on `astro`. Only the fields `onPreprRequest` uses.
|
|
41
|
+
*/
|
|
42
|
+
interface AstroLikeContext {
|
|
43
|
+
request: Request;
|
|
44
|
+
/** Unused; typed loosely so passing the real Astro context still checks. */
|
|
45
|
+
cookies?: unknown;
|
|
46
|
+
}
|
|
47
|
+
/** Astro middleware's `next()` — resolves to the `Response` from downstream. */
|
|
48
|
+
type AstroNext = () => Promise<Response>;
|
|
49
|
+
/**
|
|
50
|
+
* Astro middleware entry point: computes the Prepr headers/cookies for this
|
|
51
|
+
* request, forwards the headers downstream and sets the cookies on the response.
|
|
52
|
+
*
|
|
53
|
+
* Astro hands downstream code the same `Request` instance we get here, so the
|
|
54
|
+
* headers are mutated in place — building a new `Request` would go nowhere,
|
|
55
|
+
* there's no way to hand a replacement back out of middleware.
|
|
56
|
+
*/
|
|
57
|
+
declare function onPreprRequest(context: AstroLikeContext, next: AstroNext, options?: PreprMiddlewareOptions): Promise<Response>;
|
|
58
|
+
/** Prepr Customer ID for this request, from `Astro.request.headers`. */
|
|
59
|
+
declare function getPreprUUID(headers: Headers): string | null;
|
|
60
|
+
/** Active segment for this request, from `Astro.request.headers`. */
|
|
61
|
+
declare function getActiveSegment(headers: Headers): string | null;
|
|
62
|
+
/** Active A/B variant for this request, from `Astro.request.headers`. */
|
|
63
|
+
declare function getActiveVariant(headers: Headers): string | null;
|
|
64
|
+
/** All Prepr headers for this request, from `Astro.request.headers`. */
|
|
65
|
+
declare function getPreprHeaders(headers: Headers): PreprHeaders;
|
|
66
|
+
/**
|
|
67
|
+
* Props needed to mount `PreprToolbar.astro`, from `Astro.request.headers`
|
|
68
|
+
* plus a Prepr GraphQL token.
|
|
69
|
+
*
|
|
70
|
+
* Ungated: call it only when your app is in preview mode. Resolve that however
|
|
71
|
+
* your deployment already does — the toolkit reads no environment variables of
|
|
72
|
+
* its own.
|
|
73
|
+
*/
|
|
74
|
+
declare function getToolbarProps(headers: Headers, token: string): Promise<PreprToolbarProps>;
|
|
75
|
+
|
|
76
|
+
export { type AstroLikeContext, type AstroNext, type PreprMiddlewareOptions, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprUUID, getToolbarProps, onPreprRequest };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
interface PreprMiddlewareOptions {
|
|
2
|
+
/** Enable preview-mode headers (segment/AB overrides, preview bar). */
|
|
3
|
+
preview?: boolean;
|
|
4
|
+
/** Override the version reported in Prepr-Package (mainly for tests). */
|
|
5
|
+
version?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface PreprSegment {
|
|
9
|
+
readonly _id: string;
|
|
10
|
+
readonly name: string;
|
|
11
|
+
}
|
|
12
|
+
interface PreprToolbarProps {
|
|
13
|
+
readonly activeSegment: string | null;
|
|
14
|
+
readonly activeVariant: string | null;
|
|
15
|
+
/** Available segments to personalize on. */
|
|
16
|
+
readonly segments?: readonly PreprSegment[];
|
|
17
|
+
/** @deprecated Alias for `segments`. */
|
|
18
|
+
readonly data?: readonly PreprSegment[];
|
|
19
|
+
}
|
|
20
|
+
type PreprVariant = 'A' | 'B';
|
|
21
|
+
interface PreprHeaders {
|
|
22
|
+
readonly 'prepr-customer-id'?: string;
|
|
23
|
+
readonly 'Prepr-Segments'?: string;
|
|
24
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
25
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
26
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
27
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
28
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
29
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
30
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
31
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
32
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
33
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
34
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
35
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Structural stand-in for Astro's `APIContext`, so this package needs no
|
|
40
|
+
* build-time dependency on `astro`. Only the fields `onPreprRequest` uses.
|
|
41
|
+
*/
|
|
42
|
+
interface AstroLikeContext {
|
|
43
|
+
request: Request;
|
|
44
|
+
/** Unused; typed loosely so passing the real Astro context still checks. */
|
|
45
|
+
cookies?: unknown;
|
|
46
|
+
}
|
|
47
|
+
/** Astro middleware's `next()` — resolves to the `Response` from downstream. */
|
|
48
|
+
type AstroNext = () => Promise<Response>;
|
|
49
|
+
/**
|
|
50
|
+
* Astro middleware entry point: computes the Prepr headers/cookies for this
|
|
51
|
+
* request, forwards the headers downstream and sets the cookies on the response.
|
|
52
|
+
*
|
|
53
|
+
* Astro hands downstream code the same `Request` instance we get here, so the
|
|
54
|
+
* headers are mutated in place — building a new `Request` would go nowhere,
|
|
55
|
+
* there's no way to hand a replacement back out of middleware.
|
|
56
|
+
*/
|
|
57
|
+
declare function onPreprRequest(context: AstroLikeContext, next: AstroNext, options?: PreprMiddlewareOptions): Promise<Response>;
|
|
58
|
+
/** Prepr Customer ID for this request, from `Astro.request.headers`. */
|
|
59
|
+
declare function getPreprUUID(headers: Headers): string | null;
|
|
60
|
+
/** Active segment for this request, from `Astro.request.headers`. */
|
|
61
|
+
declare function getActiveSegment(headers: Headers): string | null;
|
|
62
|
+
/** Active A/B variant for this request, from `Astro.request.headers`. */
|
|
63
|
+
declare function getActiveVariant(headers: Headers): string | null;
|
|
64
|
+
/** All Prepr headers for this request, from `Astro.request.headers`. */
|
|
65
|
+
declare function getPreprHeaders(headers: Headers): PreprHeaders;
|
|
66
|
+
/**
|
|
67
|
+
* Props needed to mount `PreprToolbar.astro`, from `Astro.request.headers`
|
|
68
|
+
* plus a Prepr GraphQL token.
|
|
69
|
+
*
|
|
70
|
+
* Ungated: call it only when your app is in preview mode. Resolve that however
|
|
71
|
+
* your deployment already does — the toolkit reads no environment variables of
|
|
72
|
+
* its own.
|
|
73
|
+
*/
|
|
74
|
+
declare function getToolbarProps(headers: Headers, token: string): Promise<PreprToolbarProps>;
|
|
75
|
+
|
|
76
|
+
export { type AstroLikeContext, type AstroNext, type PreprMiddlewareOptions, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprUUID, getToolbarProps, onPreprRequest };
|