@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,94 @@
|
|
|
1
|
+
type Dictionary = Record<string, unknown>;
|
|
2
|
+
declare const dictionaries: {
|
|
3
|
+
en: Dictionary;
|
|
4
|
+
nl: Dictionary;
|
|
5
|
+
};
|
|
6
|
+
type Locale = keyof typeof dictionaries;
|
|
7
|
+
|
|
8
|
+
interface PreprSegment {
|
|
9
|
+
readonly _id: string;
|
|
10
|
+
readonly name: string;
|
|
11
|
+
}
|
|
12
|
+
interface PreprToolbarOptions {
|
|
13
|
+
debug?: boolean;
|
|
14
|
+
locale?: Locale;
|
|
15
|
+
}
|
|
16
|
+
interface PreprToolbarProps {
|
|
17
|
+
readonly activeSegment: string | null;
|
|
18
|
+
readonly activeVariant: string | null;
|
|
19
|
+
/** Available segments to personalize on. */
|
|
20
|
+
readonly segments?: readonly PreprSegment[];
|
|
21
|
+
/** @deprecated Alias for `segments`. */
|
|
22
|
+
readonly data?: readonly PreprSegment[];
|
|
23
|
+
}
|
|
24
|
+
type PreprHeaderName = 'prepr-customer-id' | 'Prepr-Segments' | 'Prepr-ABtesting' | 'Prepr-User-Agent';
|
|
25
|
+
type PreprVariant = 'A' | 'B';
|
|
26
|
+
type PreprEnvironment = 'preview' | 'production';
|
|
27
|
+
type PreprEventType = 'segment_changed' | 'variant_changed' | 'edit_mode_toggled' | 'preview_mode_toggled' | 'getScrollPosition' | 'loaded' | 'field_edit_requested';
|
|
28
|
+
interface PreprHeaders {
|
|
29
|
+
readonly 'prepr-customer-id'?: string;
|
|
30
|
+
readonly 'Prepr-Segments'?: string;
|
|
31
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
32
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
33
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
34
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
35
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
36
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
37
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
38
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
39
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
40
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
41
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
42
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
43
|
+
}
|
|
44
|
+
type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Loader + typed facade for Prepr's CDN tracking pixel, so consumers don't have
|
|
48
|
+
* to paste a `<script>` tag. The pixel itself lives on the CDN; this only sets
|
|
49
|
+
* up the handoff.
|
|
50
|
+
*
|
|
51
|
+
* The CDN script expects a queue stub to already exist when it loads, so the
|
|
52
|
+
* contract must be reproduced exactly:
|
|
53
|
+
* - `window.prepr` pushes `arguments` onto `.queue` until the real script
|
|
54
|
+
* lands and replaces `.process`.
|
|
55
|
+
* - `.t` must be a `Date.now()` timestamp.
|
|
56
|
+
* - The tag is inserted async, `src` cache-busted every 24h via
|
|
57
|
+
* `Math.ceil(Date.now() / cacheTime) * cacheTime`.
|
|
58
|
+
*/
|
|
59
|
+
/** Config accepted by the CDN pixel. */
|
|
60
|
+
interface PreprPixelConfig {
|
|
61
|
+
destinations?: {
|
|
62
|
+
googleTagManager?: boolean;
|
|
63
|
+
};
|
|
64
|
+
variantImpressionThreshold?: number;
|
|
65
|
+
}
|
|
66
|
+
type PreprQueueArgs = unknown[];
|
|
67
|
+
interface PreprQueueFn {
|
|
68
|
+
(...args: PreprQueueArgs): void;
|
|
69
|
+
queue: PreprQueueArgs[];
|
|
70
|
+
t: number;
|
|
71
|
+
process?: (...args: PreprQueueArgs) => void;
|
|
72
|
+
}
|
|
73
|
+
declare global {
|
|
74
|
+
interface Window {
|
|
75
|
+
prepr?: PreprQueueFn;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Install the queue stub, inject the pixel script, then queue
|
|
80
|
+
* `('init', trackingId, config?)` and `('event', 'pageload')` in that order.
|
|
81
|
+
*
|
|
82
|
+
* Idempotent, and a no-op outside a browser.
|
|
83
|
+
*/
|
|
84
|
+
declare function loadTrackingPixel(trackingId: string, config?: PreprPixelConfig): void;
|
|
85
|
+
/**
|
|
86
|
+
* Send a tracking event through `window.prepr`. Only depends on `window.prepr`
|
|
87
|
+
* existing, never on how it got installed. Warns once and returns quietly when
|
|
88
|
+
* no pixel is present.
|
|
89
|
+
*/
|
|
90
|
+
declare function trackEvent(name: string, data?: string | Record<string, unknown>): void;
|
|
91
|
+
/** Loader-independent like `trackEvent`; warns once if no pixel is installed. */
|
|
92
|
+
declare function setTrackingParam(key: string, value: string): void;
|
|
93
|
+
|
|
94
|
+
export { type Locale as L, type PreprSegment as P, type PreprVariant as a, type PreprToolbarProps as b, type PreprToolbarOptions as c, type PreprErrorCode as d, type PreprHeaders as e, type PreprEnvironment as f, type PreprEventType as g, type PreprHeaderName as h, type PreprPixelConfig as i, loadTrackingPixel as l, setTrackingParam as s, trackEvent as t };
|
|
@@ -0,0 +1,371 @@
|
|
|
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/sveltekit/index.ts
|
|
21
|
+
var sveltekit_exports = {};
|
|
22
|
+
__export(sveltekit_exports, {
|
|
23
|
+
PREPR_LOCALS_KEY: () => PREPR_LOCALS_KEY,
|
|
24
|
+
PreprError: () => PreprError,
|
|
25
|
+
extractAccessToken: () => extractAccessToken,
|
|
26
|
+
getActiveSegment: () => getActiveSegment,
|
|
27
|
+
getActiveVariant: () => getActiveVariant,
|
|
28
|
+
getPreprHeaders: () => getPreprHeaders,
|
|
29
|
+
getPreprHeadersFromLocals: () => getPreprHeadersFromLocals,
|
|
30
|
+
getPreprUUID: () => getPreprUUID,
|
|
31
|
+
getToolbarProps: () => getToolbarProps,
|
|
32
|
+
preprHandle: () => preprHandle,
|
|
33
|
+
validatePreprToken: () => validatePreprToken
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(sveltekit_exports);
|
|
36
|
+
|
|
37
|
+
// src/version.ts
|
|
38
|
+
var VERSION = "0.1.0-beta.1";
|
|
39
|
+
|
|
40
|
+
// src/core/constants.ts
|
|
41
|
+
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
42
|
+
var COOKIE_SEGMENT = "Prepr-Segments";
|
|
43
|
+
var COOKIE_VARIANT = "Prepr-ABtesting";
|
|
44
|
+
var COOKIE_UID = "__prepr_uid";
|
|
45
|
+
var COOKIE_HUBSPOT = "hubspotutk";
|
|
46
|
+
var PARAM_SEGMENT = "prepr_preview_segment";
|
|
47
|
+
var PARAM_VARIANT = "prepr_preview_ab";
|
|
48
|
+
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
49
|
+
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
50
|
+
|
|
51
|
+
// src/core/middleware.ts
|
|
52
|
+
var UTM_PARAMS = [
|
|
53
|
+
"utm_source",
|
|
54
|
+
"utm_medium",
|
|
55
|
+
"utm_term",
|
|
56
|
+
"utm_content",
|
|
57
|
+
"utm_campaign"
|
|
58
|
+
];
|
|
59
|
+
function parseCookies(request) {
|
|
60
|
+
const cookies = /* @__PURE__ */ new Map();
|
|
61
|
+
const header = request.headers.get("cookie");
|
|
62
|
+
if (!header) return cookies;
|
|
63
|
+
for (const part of header.split(";")) {
|
|
64
|
+
const eq = part.indexOf("=");
|
|
65
|
+
if (eq === -1) continue;
|
|
66
|
+
const name = part.slice(0, eq).trim();
|
|
67
|
+
const value = part.slice(eq + 1).trim();
|
|
68
|
+
if (!name) continue;
|
|
69
|
+
cookies.set(name, decodeURIComponent(value));
|
|
70
|
+
}
|
|
71
|
+
return cookies;
|
|
72
|
+
}
|
|
73
|
+
function processPreprRequest(request, opts) {
|
|
74
|
+
const requestHeaders = new Headers(request.headers);
|
|
75
|
+
const responseCookies = [];
|
|
76
|
+
const requestCookies = parseCookies(request);
|
|
77
|
+
const searchParams = new URL(request.url).searchParams;
|
|
78
|
+
for (const key of UTM_PARAMS) {
|
|
79
|
+
const value = searchParams.get(key);
|
|
80
|
+
if (value !== null) {
|
|
81
|
+
requestHeaders.set(`Prepr-Context-${key}`, value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const referer = request.headers.get("referer");
|
|
85
|
+
if (referer) {
|
|
86
|
+
requestHeaders.set("Prepr-Context-initial_referral", referer);
|
|
87
|
+
}
|
|
88
|
+
const userAgent = request.headers.get("user-agent");
|
|
89
|
+
if (userAgent) {
|
|
90
|
+
requestHeaders.set("Prepr-User-Agent", userAgent);
|
|
91
|
+
}
|
|
92
|
+
requestHeaders.set("Prepr-Package", `@preprio/toolkit@${opts?.version ?? VERSION}`);
|
|
93
|
+
const ip = request.headers.get("Cf-Connecting-Ip") ?? request.headers.get("x-real-ip");
|
|
94
|
+
if (ip) {
|
|
95
|
+
requestHeaders.set("Prepr-Visitor-IP", ip);
|
|
96
|
+
}
|
|
97
|
+
const hutkCookie = requestCookies.get(COOKIE_HUBSPOT);
|
|
98
|
+
if (hutkCookie) {
|
|
99
|
+
requestHeaders.set("Prepr-Hubspot-Id", hutkCookie);
|
|
100
|
+
}
|
|
101
|
+
let uid = requestCookies.get(COOKIE_UID);
|
|
102
|
+
if (!uid) {
|
|
103
|
+
uid = crypto.randomUUID();
|
|
104
|
+
responseCookies.push({
|
|
105
|
+
name: COOKIE_UID,
|
|
106
|
+
value: uid,
|
|
107
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
108
|
+
path: "/"
|
|
109
|
+
});
|
|
110
|
+
requestHeaders.set("Prepr-Customer-Id-Created", "true");
|
|
111
|
+
}
|
|
112
|
+
requestHeaders.set("Prepr-Customer-Id", uid);
|
|
113
|
+
if (!opts?.preview) {
|
|
114
|
+
return { requestHeaders, responseCookies };
|
|
115
|
+
}
|
|
116
|
+
const hasPreviewParams = searchParams.has(PARAM_SEGMENT) || searchParams.has(PARAM_VARIANT);
|
|
117
|
+
if (!hasPreviewParams) {
|
|
118
|
+
const previewModeCookie = requestCookies.get(COOKIE_PREVIEW_MODE);
|
|
119
|
+
const previewModeEnabled = previewModeCookie !== "false";
|
|
120
|
+
if (!previewModeEnabled) {
|
|
121
|
+
return { requestHeaders, responseCookies };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
125
|
+
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
126
|
+
if (!isLivePreview) {
|
|
127
|
+
const segmentCookie = requestCookies.get(COOKIE_SEGMENT);
|
|
128
|
+
if (segmentCookie) {
|
|
129
|
+
requestHeaders.set("Prepr-Segments", segmentCookie);
|
|
130
|
+
}
|
|
131
|
+
const abCookie = requestCookies.get(COOKIE_VARIANT);
|
|
132
|
+
if (abCookie) {
|
|
133
|
+
requestHeaders.set("Prepr-ABtesting", abCookie);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const previewAb = searchParams.get(PARAM_VARIANT);
|
|
137
|
+
if (previewAb !== null) {
|
|
138
|
+
requestHeaders.set("Prepr-ABtesting", previewAb);
|
|
139
|
+
if (!isLivePreview) {
|
|
140
|
+
responseCookies.push({
|
|
141
|
+
name: COOKIE_VARIANT,
|
|
142
|
+
value: previewAb,
|
|
143
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
144
|
+
path: "/"
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const previewSegment = searchParams.get(PARAM_SEGMENT);
|
|
149
|
+
if (previewSegment !== null) {
|
|
150
|
+
requestHeaders.set("Prepr-Segments", previewSegment);
|
|
151
|
+
if (!isLivePreview) {
|
|
152
|
+
responseCookies.push({
|
|
153
|
+
name: COOKIE_SEGMENT,
|
|
154
|
+
value: previewSegment,
|
|
155
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
156
|
+
path: "/"
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return { requestHeaders, responseCookies };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/sveltekit/hooks.ts
|
|
164
|
+
var PREPR_LOCALS_KEY = "prepr";
|
|
165
|
+
function preprHandle(options) {
|
|
166
|
+
return async ({ event, resolve }) => {
|
|
167
|
+
const { requestHeaders, responseCookies } = processPreprRequest(event.request, options);
|
|
168
|
+
requestHeaders.forEach((value, key) => {
|
|
169
|
+
event.request.headers.set(key, value);
|
|
170
|
+
});
|
|
171
|
+
if (event.locals) {
|
|
172
|
+
event.locals[PREPR_LOCALS_KEY] = requestHeaders;
|
|
173
|
+
}
|
|
174
|
+
const response = await resolve(event);
|
|
175
|
+
for (const cookie of responseCookies) {
|
|
176
|
+
const parts = [
|
|
177
|
+
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
178
|
+
`Max-Age=${cookie.maxAge}`,
|
|
179
|
+
`Path=${cookie.path}`
|
|
180
|
+
];
|
|
181
|
+
response.headers.append("Set-Cookie", parts.join("; "));
|
|
182
|
+
}
|
|
183
|
+
return response;
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function getPreprHeadersFromLocals(locals) {
|
|
187
|
+
const headers = locals[PREPR_LOCALS_KEY];
|
|
188
|
+
return headers instanceof Headers ? headers : new Headers();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// src/core/server.ts
|
|
192
|
+
var PreprError = class extends Error {
|
|
193
|
+
constructor(message, code, context, originalError) {
|
|
194
|
+
super(message);
|
|
195
|
+
this.code = code;
|
|
196
|
+
this.context = context;
|
|
197
|
+
this.originalError = originalError;
|
|
198
|
+
this.name = "PreprError";
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
function getPreprUUIDFromHeaders(headers) {
|
|
202
|
+
return headers.get("prepr-customer-id");
|
|
203
|
+
}
|
|
204
|
+
function getActiveSegmentFromHeaders(headers) {
|
|
205
|
+
return headers.get("Prepr-Segments");
|
|
206
|
+
}
|
|
207
|
+
function getActiveVariantFromHeaders(headers) {
|
|
208
|
+
return headers.get("Prepr-ABtesting");
|
|
209
|
+
}
|
|
210
|
+
var PREPR_HEADER_KEYS = [
|
|
211
|
+
"prepr-customer-id",
|
|
212
|
+
"Prepr-Segments",
|
|
213
|
+
"Prepr-ABtesting",
|
|
214
|
+
"Prepr-Preview-Bar",
|
|
215
|
+
"Prepr-Context-utm_source",
|
|
216
|
+
"Prepr-Context-utm_medium",
|
|
217
|
+
"Prepr-Context-utm_term",
|
|
218
|
+
"Prepr-Context-utm_content",
|
|
219
|
+
"Prepr-Context-utm_campaign",
|
|
220
|
+
"Prepr-Context-initial_referral",
|
|
221
|
+
"Prepr-Visitor-IP",
|
|
222
|
+
"Prepr-Hubspot-Id",
|
|
223
|
+
"Prepr-Customer-Id-Created",
|
|
224
|
+
"Prepr-User-Agent"
|
|
225
|
+
];
|
|
226
|
+
function getPreprHeadersFromHeaders(headers) {
|
|
227
|
+
const preprHeaders = {};
|
|
228
|
+
const copy = (key) => {
|
|
229
|
+
const value = headers.get(key);
|
|
230
|
+
if (value !== null) {
|
|
231
|
+
preprHeaders[key] = value;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
for (const key of PREPR_HEADER_KEYS) {
|
|
235
|
+
copy(key);
|
|
236
|
+
}
|
|
237
|
+
return preprHeaders;
|
|
238
|
+
}
|
|
239
|
+
function validatePreprToken(token) {
|
|
240
|
+
if (!token) {
|
|
241
|
+
throw new PreprError("Token is required", "MISSING_TOKEN", "validatePreprToken");
|
|
242
|
+
}
|
|
243
|
+
if (!token.startsWith("https://")) {
|
|
244
|
+
throw new PreprError(
|
|
245
|
+
"Token must be a valid HTTPS URL",
|
|
246
|
+
"INVALID_TOKEN",
|
|
247
|
+
"validatePreprToken"
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function extractAccessToken(graphqlUrl) {
|
|
252
|
+
let url;
|
|
253
|
+
try {
|
|
254
|
+
url = new URL(graphqlUrl);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
throw new PreprError(
|
|
257
|
+
"Token must be a valid URL",
|
|
258
|
+
"INVALID_TOKEN",
|
|
259
|
+
"extractAccessToken",
|
|
260
|
+
error instanceof Error ? error : new Error(String(error))
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
if (url.hostname !== "graphql.prepr.io") {
|
|
264
|
+
throw new PreprError(
|
|
265
|
+
"Token must be a graphql.prepr.io URL",
|
|
266
|
+
"INVALID_TOKEN",
|
|
267
|
+
"extractAccessToken"
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
const pathParts = url.pathname.split("/");
|
|
271
|
+
const token = pathParts[pathParts.length - 1];
|
|
272
|
+
if (!token) {
|
|
273
|
+
throw new PreprError(
|
|
274
|
+
"Token URL is missing an access token segment",
|
|
275
|
+
"INVALID_TOKEN",
|
|
276
|
+
"extractAccessToken"
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
return token;
|
|
280
|
+
}
|
|
281
|
+
async function getPreprEnvironmentSegments(token) {
|
|
282
|
+
validatePreprToken(token);
|
|
283
|
+
try {
|
|
284
|
+
const response = await fetch(token, {
|
|
285
|
+
headers: {
|
|
286
|
+
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
287
|
+
"Content-Type": "application/json"
|
|
288
|
+
},
|
|
289
|
+
method: "POST",
|
|
290
|
+
body: JSON.stringify({
|
|
291
|
+
query: `{
|
|
292
|
+
_Segments {
|
|
293
|
+
_id
|
|
294
|
+
name
|
|
295
|
+
}
|
|
296
|
+
}`
|
|
297
|
+
})
|
|
298
|
+
});
|
|
299
|
+
if (!response.ok) {
|
|
300
|
+
throw new PreprError(
|
|
301
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
302
|
+
"HTTP_ERROR",
|
|
303
|
+
"getPreprEnvironmentSegments"
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const json = await response.json();
|
|
307
|
+
if (!json || !json.data || !json.data._Segments) {
|
|
308
|
+
throw new PreprError(
|
|
309
|
+
"Invalid response format from Prepr API",
|
|
310
|
+
"INVALID_RESPONSE",
|
|
311
|
+
"getPreprEnvironmentSegments"
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
return json.data._Segments;
|
|
315
|
+
} catch (error) {
|
|
316
|
+
if (error instanceof PreprError) {
|
|
317
|
+
throw error;
|
|
318
|
+
}
|
|
319
|
+
throw new PreprError(
|
|
320
|
+
"Failed to fetch segments from Prepr API",
|
|
321
|
+
"FETCH_ERROR",
|
|
322
|
+
"getPreprEnvironmentSegments",
|
|
323
|
+
error instanceof Error ? error : new Error(String(error))
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
async function getToolbarPropsFromHeaders(headers, token) {
|
|
328
|
+
let segments = [];
|
|
329
|
+
try {
|
|
330
|
+
segments = await getPreprEnvironmentSegments(token);
|
|
331
|
+
} catch {
|
|
332
|
+
segments = [];
|
|
333
|
+
}
|
|
334
|
+
return {
|
|
335
|
+
activeSegment: getActiveSegmentFromHeaders(headers),
|
|
336
|
+
activeVariant: getActiveVariantFromHeaders(headers),
|
|
337
|
+
segments,
|
|
338
|
+
data: segments
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// src/sveltekit/server.ts
|
|
343
|
+
function getPreprUUID(headers) {
|
|
344
|
+
return getPreprUUIDFromHeaders(headers);
|
|
345
|
+
}
|
|
346
|
+
function getActiveSegment(headers) {
|
|
347
|
+
return getActiveSegmentFromHeaders(headers);
|
|
348
|
+
}
|
|
349
|
+
function getActiveVariant(headers) {
|
|
350
|
+
return getActiveVariantFromHeaders(headers);
|
|
351
|
+
}
|
|
352
|
+
function getPreprHeaders(headers) {
|
|
353
|
+
return getPreprHeadersFromHeaders(headers);
|
|
354
|
+
}
|
|
355
|
+
async function getToolbarProps(headers, token) {
|
|
356
|
+
return getToolbarPropsFromHeaders(headers, token);
|
|
357
|
+
}
|
|
358
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
359
|
+
0 && (module.exports = {
|
|
360
|
+
PREPR_LOCALS_KEY,
|
|
361
|
+
PreprError,
|
|
362
|
+
extractAccessToken,
|
|
363
|
+
getActiveSegment,
|
|
364
|
+
getActiveVariant,
|
|
365
|
+
getPreprHeaders,
|
|
366
|
+
getPreprHeadersFromLocals,
|
|
367
|
+
getPreprUUID,
|
|
368
|
+
getToolbarProps,
|
|
369
|
+
preprHandle,
|
|
370
|
+
validatePreprToken
|
|
371
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* Structural stand-in for SvelteKit's `RequestEvent`, so this package needs no
|
|
10
|
+
* build-time dependency on `@sveltejs/kit`. Only the fields `preprHandle` uses.
|
|
11
|
+
*/
|
|
12
|
+
interface SvelteKitRequestEvent {
|
|
13
|
+
request: Request;
|
|
14
|
+
/**
|
|
15
|
+
* Request-scoped bag shared across `handle` → `load`. `preprHandle` mirrors
|
|
16
|
+
* the computed Prepr headers here for load functions that prefer an explicit
|
|
17
|
+
* read over `request.headers` (see `getPreprHeadersFromLocals`). Optional —
|
|
18
|
+
* hand-built events (tests, non-page endpoints) may omit it.
|
|
19
|
+
*/
|
|
20
|
+
locals?: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
/** Key under which `preprHandle` mirrors the forwarded Prepr `Headers`. */
|
|
23
|
+
declare const PREPR_LOCALS_KEY = "prepr";
|
|
24
|
+
/** SvelteKit's `resolve(event)` — produces the downstream `Response`. */
|
|
25
|
+
type SvelteKitResolve = (event: SvelteKitRequestEvent) => Promise<Response>;
|
|
26
|
+
/** Structural match for SvelteKit's `Handle`. */
|
|
27
|
+
type Handle = (input: {
|
|
28
|
+
event: SvelteKitRequestEvent;
|
|
29
|
+
resolve: SvelteKitResolve;
|
|
30
|
+
}) => Promise<Response>;
|
|
31
|
+
/**
|
|
32
|
+
* Builds the SvelteKit `Handle` for `src/hooks.server.ts`.
|
|
33
|
+
*
|
|
34
|
+
* The computed Prepr headers are set on `event.request.headers` in place, which
|
|
35
|
+
* load functions do see: SvelteKit shallow-spreads the event before `handle`
|
|
36
|
+
* (`respond.js`) and again before `load` (`load_data.js`), so `request` is
|
|
37
|
+
* carried by reference and never reconstructed — same mechanism as the Astro
|
|
38
|
+
* wrapper's `onPreprRequest`. Verified against @sveltejs/kit 2.70.1. The same
|
|
39
|
+
* `Headers` are mirrored onto `event.locals` for callers who prefer reading
|
|
40
|
+
* them explicitly (`getPreprHeadersFromLocals`). Cookies are appended to the
|
|
41
|
+
* resolved response.
|
|
42
|
+
*/
|
|
43
|
+
declare function preprHandle(options?: PreprMiddlewareOptions): Handle;
|
|
44
|
+
/**
|
|
45
|
+
* Personalization headers to forward to your GraphQL fetch, for use in load
|
|
46
|
+
* functions. Equivalent to reading `event.request.headers` — `preprHandle`
|
|
47
|
+
* writes both. Empty `Headers` if `preprHandle` did not run, so the result is
|
|
48
|
+
* always safe to spread.
|
|
49
|
+
*/
|
|
50
|
+
declare function getPreprHeadersFromLocals(locals: Record<string, unknown>): Headers;
|
|
51
|
+
|
|
52
|
+
interface PreprSegment {
|
|
53
|
+
readonly _id: string;
|
|
54
|
+
readonly name: string;
|
|
55
|
+
}
|
|
56
|
+
interface PreprToolbarProps {
|
|
57
|
+
readonly activeSegment: string | null;
|
|
58
|
+
readonly activeVariant: string | null;
|
|
59
|
+
/** Available segments to personalize on. */
|
|
60
|
+
readonly segments?: readonly PreprSegment[];
|
|
61
|
+
/** @deprecated Alias for `segments`. */
|
|
62
|
+
readonly data?: readonly PreprSegment[];
|
|
63
|
+
}
|
|
64
|
+
type PreprVariant = 'A' | 'B';
|
|
65
|
+
interface PreprHeaders {
|
|
66
|
+
readonly 'prepr-customer-id'?: string;
|
|
67
|
+
readonly 'Prepr-Segments'?: string;
|
|
68
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
69
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
70
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
71
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
72
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
73
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
74
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
75
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
76
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
77
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
78
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
79
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
80
|
+
}
|
|
81
|
+
type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR';
|
|
82
|
+
|
|
83
|
+
/** Returns the Prepr Customer ID from `event.request.headers`. */
|
|
84
|
+
declare function getPreprUUID(headers: Headers): string | null;
|
|
85
|
+
/** Returns the active segment from `event.request.headers`. */
|
|
86
|
+
declare function getActiveSegment(headers: Headers): string | null;
|
|
87
|
+
/** Returns the active A/B testing variant from `event.request.headers`. */
|
|
88
|
+
declare function getActiveVariant(headers: Headers): string | null;
|
|
89
|
+
/** Returns all Prepr headers from `event.request.headers`. */
|
|
90
|
+
declare function getPreprHeaders(headers: Headers): PreprHeaders;
|
|
91
|
+
/**
|
|
92
|
+
* Props needed to mount `PreprToolbar.svelte`, from `event.request.headers`
|
|
93
|
+
* plus a Prepr GraphQL token. Ungated — gate on preview mode yourself in
|
|
94
|
+
* `+layout.server.ts`.
|
|
95
|
+
*/
|
|
96
|
+
declare function getToolbarProps(headers: Headers, token: string): Promise<PreprToolbarProps>;
|
|
97
|
+
|
|
98
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
99
|
+
declare class PreprError extends Error {
|
|
100
|
+
readonly code: PreprErrorCode;
|
|
101
|
+
readonly context?: string | undefined;
|
|
102
|
+
readonly originalError?: Error | undefined;
|
|
103
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
|
|
107
|
+
* @throws PreprError if the token is missing or not an HTTPS URL.
|
|
108
|
+
*/
|
|
109
|
+
declare function validatePreprToken(token: string): void;
|
|
110
|
+
/**
|
|
111
|
+
* Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
|
|
112
|
+
* -> `abc123`.
|
|
113
|
+
* @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
|
|
114
|
+
*/
|
|
115
|
+
declare function extractAccessToken(graphqlUrl: string): string;
|
|
116
|
+
|
|
117
|
+
export { type Handle, PREPR_LOCALS_KEY, PreprError, type PreprMiddlewareOptions, type SvelteKitRequestEvent, type SvelteKitResolve, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprHeadersFromLocals, getPreprUUID, getToolbarProps, preprHandle, validatePreprToken };
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* Structural stand-in for SvelteKit's `RequestEvent`, so this package needs no
|
|
10
|
+
* build-time dependency on `@sveltejs/kit`. Only the fields `preprHandle` uses.
|
|
11
|
+
*/
|
|
12
|
+
interface SvelteKitRequestEvent {
|
|
13
|
+
request: Request;
|
|
14
|
+
/**
|
|
15
|
+
* Request-scoped bag shared across `handle` → `load`. `preprHandle` mirrors
|
|
16
|
+
* the computed Prepr headers here for load functions that prefer an explicit
|
|
17
|
+
* read over `request.headers` (see `getPreprHeadersFromLocals`). Optional —
|
|
18
|
+
* hand-built events (tests, non-page endpoints) may omit it.
|
|
19
|
+
*/
|
|
20
|
+
locals?: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
/** Key under which `preprHandle` mirrors the forwarded Prepr `Headers`. */
|
|
23
|
+
declare const PREPR_LOCALS_KEY = "prepr";
|
|
24
|
+
/** SvelteKit's `resolve(event)` — produces the downstream `Response`. */
|
|
25
|
+
type SvelteKitResolve = (event: SvelteKitRequestEvent) => Promise<Response>;
|
|
26
|
+
/** Structural match for SvelteKit's `Handle`. */
|
|
27
|
+
type Handle = (input: {
|
|
28
|
+
event: SvelteKitRequestEvent;
|
|
29
|
+
resolve: SvelteKitResolve;
|
|
30
|
+
}) => Promise<Response>;
|
|
31
|
+
/**
|
|
32
|
+
* Builds the SvelteKit `Handle` for `src/hooks.server.ts`.
|
|
33
|
+
*
|
|
34
|
+
* The computed Prepr headers are set on `event.request.headers` in place, which
|
|
35
|
+
* load functions do see: SvelteKit shallow-spreads the event before `handle`
|
|
36
|
+
* (`respond.js`) and again before `load` (`load_data.js`), so `request` is
|
|
37
|
+
* carried by reference and never reconstructed — same mechanism as the Astro
|
|
38
|
+
* wrapper's `onPreprRequest`. Verified against @sveltejs/kit 2.70.1. The same
|
|
39
|
+
* `Headers` are mirrored onto `event.locals` for callers who prefer reading
|
|
40
|
+
* them explicitly (`getPreprHeadersFromLocals`). Cookies are appended to the
|
|
41
|
+
* resolved response.
|
|
42
|
+
*/
|
|
43
|
+
declare function preprHandle(options?: PreprMiddlewareOptions): Handle;
|
|
44
|
+
/**
|
|
45
|
+
* Personalization headers to forward to your GraphQL fetch, for use in load
|
|
46
|
+
* functions. Equivalent to reading `event.request.headers` — `preprHandle`
|
|
47
|
+
* writes both. Empty `Headers` if `preprHandle` did not run, so the result is
|
|
48
|
+
* always safe to spread.
|
|
49
|
+
*/
|
|
50
|
+
declare function getPreprHeadersFromLocals(locals: Record<string, unknown>): Headers;
|
|
51
|
+
|
|
52
|
+
interface PreprSegment {
|
|
53
|
+
readonly _id: string;
|
|
54
|
+
readonly name: string;
|
|
55
|
+
}
|
|
56
|
+
interface PreprToolbarProps {
|
|
57
|
+
readonly activeSegment: string | null;
|
|
58
|
+
readonly activeVariant: string | null;
|
|
59
|
+
/** Available segments to personalize on. */
|
|
60
|
+
readonly segments?: readonly PreprSegment[];
|
|
61
|
+
/** @deprecated Alias for `segments`. */
|
|
62
|
+
readonly data?: readonly PreprSegment[];
|
|
63
|
+
}
|
|
64
|
+
type PreprVariant = 'A' | 'B';
|
|
65
|
+
interface PreprHeaders {
|
|
66
|
+
readonly 'prepr-customer-id'?: string;
|
|
67
|
+
readonly 'Prepr-Segments'?: string;
|
|
68
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
69
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
70
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
71
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
72
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
73
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
74
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
75
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
76
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
77
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
78
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
79
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
80
|
+
}
|
|
81
|
+
type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR';
|
|
82
|
+
|
|
83
|
+
/** Returns the Prepr Customer ID from `event.request.headers`. */
|
|
84
|
+
declare function getPreprUUID(headers: Headers): string | null;
|
|
85
|
+
/** Returns the active segment from `event.request.headers`. */
|
|
86
|
+
declare function getActiveSegment(headers: Headers): string | null;
|
|
87
|
+
/** Returns the active A/B testing variant from `event.request.headers`. */
|
|
88
|
+
declare function getActiveVariant(headers: Headers): string | null;
|
|
89
|
+
/** Returns all Prepr headers from `event.request.headers`. */
|
|
90
|
+
declare function getPreprHeaders(headers: Headers): PreprHeaders;
|
|
91
|
+
/**
|
|
92
|
+
* Props needed to mount `PreprToolbar.svelte`, from `event.request.headers`
|
|
93
|
+
* plus a Prepr GraphQL token. Ungated — gate on preview mode yourself in
|
|
94
|
+
* `+layout.server.ts`.
|
|
95
|
+
*/
|
|
96
|
+
declare function getToolbarProps(headers: Headers, token: string): Promise<PreprToolbarProps>;
|
|
97
|
+
|
|
98
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
99
|
+
declare class PreprError extends Error {
|
|
100
|
+
readonly code: PreprErrorCode;
|
|
101
|
+
readonly context?: string | undefined;
|
|
102
|
+
readonly originalError?: Error | undefined;
|
|
103
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
|
|
107
|
+
* @throws PreprError if the token is missing or not an HTTPS URL.
|
|
108
|
+
*/
|
|
109
|
+
declare function validatePreprToken(token: string): void;
|
|
110
|
+
/**
|
|
111
|
+
* Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
|
|
112
|
+
* -> `abc123`.
|
|
113
|
+
* @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
|
|
114
|
+
*/
|
|
115
|
+
declare function extractAccessToken(graphqlUrl: string): string;
|
|
116
|
+
|
|
117
|
+
export { type Handle, PREPR_LOCALS_KEY, PreprError, type PreprMiddlewareOptions, type SvelteKitRequestEvent, type SvelteKitResolve, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprHeadersFromLocals, getPreprUUID, getToolbarProps, preprHandle, validatePreprToken };
|