@preprio/toolkit 0.6.0-alpha.9 → 1.0.0-rc.2
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 +111 -1363
- package/dist/astro/index.cjs +1555 -182
- package/dist/astro/index.d.cts +201 -1
- package/dist/astro/index.d.ts +201 -1
- package/dist/astro/index.js +1557 -29
- package/dist/chunk-A2J7R4LR.js +256 -0
- package/dist/{chunk-5QIWVDP2.js → chunk-DBKZWOXA.js} +1 -1
- package/dist/{chunk-WQBKQG4R.js → chunk-EWXBE3JD.js} +6 -1
- package/dist/chunk-KM2GME62.js +1255 -0
- package/dist/{chunk-J66UOCK6.js → chunk-LJA5K4KM.js} +1680 -579
- package/dist/chunk-UTKH7DOI.js +58 -0
- package/dist/chunk-V7MCHPE7.js +85 -0
- package/dist/chunk-Y7JWV7WV.js +374 -0
- package/dist/codegen/index.cjs +82 -0
- package/dist/codegen/index.d.cts +70 -0
- package/dist/codegen/index.d.ts +70 -0
- package/dist/codegen/index.js +64 -0
- package/dist/index.cjs +3729 -553
- package/dist/index.d.cts +403 -19
- package/dist/index.d.ts +403 -19
- package/dist/index.js +345 -24
- package/dist/live-preview-HCBNBC6C.js +19 -0
- package/dist/nextjs/components.cjs +1872 -754
- package/dist/nextjs/components.d.cts +5 -51
- package/dist/nextjs/components.d.ts +5 -51
- package/dist/nextjs/components.js +8 -299
- package/dist/nextjs/index.cjs +1228 -181
- package/dist/nextjs/index.d.cts +247 -92
- package/dist/nextjs/index.d.ts +247 -92
- package/dist/nextjs/index.js +1629 -111
- package/dist/nextjs/live-action.cjs +298 -0
- package/dist/nextjs/live-action.d.cts +12 -0
- package/dist/nextjs/live-action.d.ts +12 -0
- package/dist/nextjs/live-action.js +68 -0
- package/dist/nextjs/live.cjs +2010 -0
- package/dist/nextjs/live.d.cts +54 -0
- package/dist/nextjs/live.d.ts +54 -0
- package/dist/nextjs/live.js +287 -0
- package/dist/nuxt/chunk-5RBZPRQN.js +350 -0
- package/dist/nuxt/chunk-LUKVPOJ2.js +32 -0
- package/dist/nuxt/index.cjs +15 -16
- package/dist/nuxt/index.d.cts +167 -6
- package/dist/nuxt/index.d.ts +167 -6
- package/dist/nuxt/index.js +402 -15
- package/dist/nuxt/live-preview-KO4IKWTX.js +1234 -0
- package/dist/nuxt/live.cjs +1928 -0
- package/dist/nuxt/live.d.cts +45 -0
- package/dist/nuxt/live.d.ts +45 -0
- package/dist/nuxt/live.js +216 -0
- package/dist/{types-BYH9XqmN.d.cts → pixel-DqU7NvgW.d.cts} +102 -5
- package/dist/{types-BYH9XqmN.d.ts → pixel-DqU7NvgW.d.ts} +102 -5
- package/dist/react/components.cjs +1865 -480
- package/dist/react/components.d.cts +43 -8
- package/dist/react/components.d.ts +43 -8
- package/dist/react/components.js +1865 -480
- package/dist/sveltekit/chunk-5RBZPRQN.js +350 -0
- package/dist/sveltekit/chunk-LUKVPOJ2.js +32 -0
- package/dist/sveltekit/index.cjs +15 -16
- package/dist/sveltekit/index.d.cts +111 -10
- package/dist/sveltekit/index.d.ts +111 -10
- package/dist/sveltekit/index.js +398 -15
- package/dist/sveltekit/live-preview-KO4IKWTX.js +1234 -0
- package/dist/sveltekit/live.cjs +1940 -0
- package/dist/sveltekit/live.d.cts +61 -0
- package/dist/sveltekit/live.d.ts +61 -0
- package/dist/sveltekit/live.js +220 -0
- package/package.json +132 -71
- package/src/astro/PreprToolbar.astro +7 -1
- package/src/astro/PreprTrackingPixel.astro +2 -4
- package/src/nuxt/PreprToolbar.vue +12 -1
- package/src/nuxt/PreprTrackingPixel.vue +2 -4
- package/src/sveltekit/PreprToolbar.svelte +17 -2
- package/src/sveltekit/PreprTrackingPixel.svelte +2 -4
- package/dist/astro/chunk-UN4JADNU.js +0 -173
- package/dist/astro/graphql.cjs +0 -1078
- package/dist/astro/graphql.d.cts +0 -300
- package/dist/astro/graphql.d.ts +0 -300
- package/dist/astro/graphql.js +0 -984
- package/dist/astro/index-XT_z-RYT.d.cts +0 -130
- package/dist/astro/index-XT_z-RYT.d.ts +0 -130
- package/dist/chunk-3NCEVJJ3.js +0 -10
- package/dist/chunk-S5RWVMDJ.js +0 -148
- package/dist/chunk-XL4Y7FGW.js +0 -499
- package/dist/graphql/codegen.cjs +0 -557
- package/dist/graphql/codegen.d.cts +0 -23
- package/dist/graphql/codegen.d.ts +0 -23
- package/dist/graphql/codegen.js +0 -99
- package/dist/graphql.cjs +0 -1020
- package/dist/graphql.d.cts +0 -335
- package/dist/graphql.d.ts +0 -335
- package/dist/graphql.js +0 -448
- package/dist/nextjs/chunk-CMV6ZP6E.js +0 -549
- package/dist/nextjs/graphql.cjs +0 -1836
- package/dist/nextjs/graphql.d.cts +0 -498
- package/dist/nextjs/graphql.d.ts +0 -498
- package/dist/nextjs/graphql.js +0 -1422
- package/dist/nextjs/server-BL-sqZvu.d.cts +0 -156
- package/dist/nextjs/server-BL-sqZvu.d.ts +0 -156
- package/dist/nuxt/chunk-XWOABVMW.js +0 -417
- package/dist/nuxt/graphql.cjs +0 -1085
- package/dist/nuxt/graphql.d.cts +0 -297
- package/dist/nuxt/graphql.d.ts +0 -297
- package/dist/nuxt/graphql.js +0 -985
- package/dist/nuxt/server-8v6OHbsw.d.cts +0 -158
- package/dist/nuxt/server-8v6OHbsw.d.ts +0 -158
- package/dist/pixel-DwW7cXJm.d.cts +0 -49
- package/dist/pixel-DwW7cXJm.d.ts +0 -49
- package/dist/server-Brr1UgtW.d.ts +0 -59
- package/dist/server-DJuS3YU0.d.cts +0 -59
- package/dist/sveltekit/chunk-3S5RA6G5.js +0 -413
- package/dist/sveltekit/graphql.cjs +0 -1091
- package/dist/sveltekit/graphql.d.cts +0 -302
- package/dist/sveltekit/graphql.d.ts +0 -302
- package/dist/sveltekit/graphql.js +0 -991
- package/dist/sveltekit/server-CO847mEV.d.cts +0 -98
- package/dist/sveltekit/server-CO847mEV.d.ts +0 -98
|
@@ -1,6 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/** A segment from Prepr, as listed in the toolbar's segment picker. */
|
|
2
|
+
interface PreprSegment {
|
|
3
|
+
readonly _id: string;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Per-feature config. `false` is shorthand for `{ enabled: false }`; the object
|
|
8
|
+
* form leaves room for per-feature options later without a breaking change.
|
|
9
|
+
*/
|
|
10
|
+
type PreprFeatureConfig = boolean | {
|
|
11
|
+
enabled?: boolean;
|
|
12
|
+
};
|
|
13
|
+
/** Toolkit features a consumer can turn off. Omitted keys stay enabled. */
|
|
14
|
+
interface PreprFeatures {
|
|
15
|
+
/** Segment picker, `Prepr-Segments` header and its cookie/query param. */
|
|
16
|
+
segments?: PreprFeatureConfig;
|
|
17
|
+
/** A/B variant switch, `Prepr-ABtesting` header and its cookie/query param. */
|
|
18
|
+
abTesting?: PreprFeatureConfig;
|
|
19
|
+
/**
|
|
20
|
+
* The site's own click-to-edit: the toolbar's Edit mode control, the stega
|
|
21
|
+
* tooltip/overlay and the close-edit pill.
|
|
22
|
+
*
|
|
23
|
+
* NOT a security control, and NOT the Prepr editor's live preview. When the
|
|
24
|
+
* site is framed by the editor, `prepr:initVE` still activates edit mode —
|
|
25
|
+
* that is the CMS operating inside its own iframe, not an affordance offered
|
|
26
|
+
* to visitors. See iframe-bridge.ts.
|
|
27
|
+
*/
|
|
28
|
+
editMode?: PreprFeatureConfig;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The data the toolbar shows. `getToolbarProps` builds it on the server from
|
|
32
|
+
* the request headers.
|
|
33
|
+
*/
|
|
34
|
+
interface PreprToolbarProps {
|
|
35
|
+
/**
|
|
36
|
+
* Server-resolved active segment. Optional: omit it with `segments`
|
|
37
|
+
* disabled, where there is nothing to resolve. When enabled and omitted,
|
|
38
|
+
* the persisted cookie is used.
|
|
39
|
+
*/
|
|
40
|
+
readonly activeSegment?: string | null;
|
|
41
|
+
/**
|
|
42
|
+
* Server-resolved active A/B variant. Optional: omit it with `abTesting`
|
|
43
|
+
* disabled, where there is nothing to resolve. When enabled and omitted,
|
|
44
|
+
* the persisted cookie is used.
|
|
45
|
+
*/
|
|
46
|
+
readonly activeVariant?: string | null;
|
|
47
|
+
/** Available segments to personalize on. */
|
|
48
|
+
readonly segments?: readonly PreprSegment[];
|
|
49
|
+
/** @deprecated Alias for `segments`. */
|
|
50
|
+
readonly data?: readonly PreprSegment[];
|
|
51
|
+
}
|
|
52
|
+
/** The two sides of an A/B test. */
|
|
53
|
+
type PreprVariant = 'A' | 'B';
|
|
54
|
+
/**
|
|
55
|
+
* The headers to send with a Prepr GraphQL request so the API personalizes
|
|
56
|
+
* the response for this visitor. `getPreprHeaders` returns them; spread them
|
|
57
|
+
* into the request's `headers`.
|
|
58
|
+
*/
|
|
59
|
+
interface PreprHeaders {
|
|
60
|
+
readonly 'prepr-customer-id'?: string;
|
|
61
|
+
readonly 'Prepr-Segments'?: string;
|
|
62
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
63
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
64
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
65
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
66
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
67
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
68
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
69
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
70
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
71
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
72
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
73
|
+
/** The visitor's browser User-Agent, used by the API for device detection. */
|
|
74
|
+
readonly 'User-Agent'?: string;
|
|
75
|
+
/** @deprecated Never populated; the visitor's value is under `User-Agent`. */
|
|
76
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
77
|
+
readonly 'Prepr-Package'?: string;
|
|
78
|
+
}
|
|
79
|
+
/** Why a `PreprError` was thrown; read it from `error.code`. */
|
|
80
|
+
type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR';
|
|
3
81
|
|
|
82
|
+
/** Options for the Prepr middleware of every framework. */
|
|
4
83
|
interface PreprMiddlewareOptions {
|
|
5
84
|
/** Enable preview-mode headers (segment/AB overrides, preview bar). */
|
|
6
85
|
preview?: boolean;
|
|
@@ -58,19 +137,41 @@ declare function preprHandle(options?: PreprMiddlewareOptions): Handle;
|
|
|
58
137
|
*/
|
|
59
138
|
declare function getPreprHeadersFromLocals(locals: Record<string, unknown>): Headers;
|
|
60
139
|
|
|
61
|
-
/** Returns the Prepr Customer ID from
|
|
140
|
+
/** Returns the Prepr Customer ID from the request headers. */
|
|
62
141
|
declare function getPreprUUID(headers: Headers): string | null;
|
|
63
|
-
/** Returns the active segment from
|
|
142
|
+
/** Returns the active segment from the request headers. */
|
|
64
143
|
declare function getActiveSegment(headers: Headers): string | null;
|
|
65
|
-
/** Returns the active A/B testing variant from
|
|
144
|
+
/** Returns the active A/B testing variant from the request headers. */
|
|
66
145
|
declare function getActiveVariant(headers: Headers): string | null;
|
|
67
|
-
/** Returns all Prepr headers from
|
|
146
|
+
/** Returns all Prepr headers from the request headers. */
|
|
68
147
|
declare function getPreprHeaders(headers: Headers): PreprHeaders;
|
|
69
148
|
/**
|
|
70
|
-
* Props needed to mount `PreprToolbar
|
|
71
|
-
* plus a Prepr GraphQL token.
|
|
72
|
-
*
|
|
149
|
+
* Props needed to mount the `PreprToolbar` component, from the request
|
|
150
|
+
* headers plus a Prepr GraphQL token.
|
|
151
|
+
*
|
|
152
|
+
* Ungated: call it only when your app is in preview mode. Resolve that however
|
|
153
|
+
* your deployment already does — the toolkit reads no environment variables of
|
|
154
|
+
* its own.
|
|
73
155
|
*/
|
|
74
156
|
declare function getToolbarProps(headers: Headers, token: string, features?: PreprFeatures): Promise<PreprToolbarProps>;
|
|
75
157
|
|
|
76
|
-
|
|
158
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
159
|
+
declare class PreprError extends Error {
|
|
160
|
+
readonly code: PreprErrorCode;
|
|
161
|
+
readonly context?: string | undefined;
|
|
162
|
+
readonly originalError?: Error | undefined;
|
|
163
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
|
|
167
|
+
* @throws PreprError if the token is missing or not an HTTPS URL.
|
|
168
|
+
*/
|
|
169
|
+
declare function validatePreprToken(token: string): void;
|
|
170
|
+
/**
|
|
171
|
+
* Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
|
|
172
|
+
* -> `abc123`.
|
|
173
|
+
* @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
|
|
174
|
+
*/
|
|
175
|
+
declare function extractAccessToken(graphqlUrl: string): string;
|
|
176
|
+
|
|
177
|
+
export { type Handle, PREPR_LOCALS_KEY, PreprError, type PreprMiddlewareOptions, type SvelteKitRequestEvent, type SvelteKitResolve, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprHeadersFromLocals, getPreprUUID, getToolbarProps, preprHandle, validatePreprToken };
|
package/dist/sveltekit/index.js
CHANGED
|
@@ -1,19 +1,403 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
COOKIE_HUBSPOT,
|
|
3
|
+
COOKIE_PREVIEW_MODE,
|
|
4
|
+
COOKIE_SEGMENT,
|
|
5
|
+
COOKIE_UID,
|
|
6
|
+
COOKIE_VARIANT,
|
|
7
|
+
ONE_YEAR_SECONDS,
|
|
8
|
+
PARAM_HIDE_BAR,
|
|
9
|
+
PARAM_SEGMENT,
|
|
10
|
+
PARAM_VARIANT
|
|
11
|
+
} from "./chunk-LUKVPOJ2.js";
|
|
12
|
+
|
|
13
|
+
// src/version.ts
|
|
14
|
+
var VERSION = "1.0.0-rc.2";
|
|
15
|
+
|
|
16
|
+
// src/core/features.ts
|
|
17
|
+
function isEnabled(config) {
|
|
18
|
+
if (typeof config === "boolean") return config;
|
|
19
|
+
return config?.enabled ?? true;
|
|
20
|
+
}
|
|
21
|
+
function resolveFeatures(features) {
|
|
22
|
+
return {
|
|
23
|
+
segments: isEnabled(features?.segments),
|
|
24
|
+
abTesting: isEnabled(features?.abTesting),
|
|
25
|
+
editMode: isEnabled(features?.editMode)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
var ALL_FEATURES_ENABLED = Object.freeze({
|
|
29
|
+
segments: true,
|
|
30
|
+
abTesting: true,
|
|
31
|
+
editMode: true
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// src/core/middleware.ts
|
|
35
|
+
var UTM_PARAMS = [
|
|
36
|
+
"utm_source",
|
|
37
|
+
"utm_medium",
|
|
38
|
+
"utm_term",
|
|
39
|
+
"utm_content",
|
|
40
|
+
"utm_campaign"
|
|
41
|
+
];
|
|
42
|
+
var MAX_HEADER_VALUE_LENGTH = 2048;
|
|
43
|
+
function randomUUID() {
|
|
44
|
+
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
45
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
46
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
47
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
48
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
|
|
49
|
+
""
|
|
50
|
+
);
|
|
51
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
52
|
+
}
|
|
53
|
+
function sanitizeHeaderValue(value) {
|
|
54
|
+
const stripped = value.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
55
|
+
return stripped.length > MAX_HEADER_VALUE_LENGTH ? stripped.slice(0, MAX_HEADER_VALUE_LENGTH) : stripped;
|
|
56
|
+
}
|
|
57
|
+
function serializeCookie(cookie) {
|
|
58
|
+
const parts = [
|
|
59
|
+
`${cookie.name}=${encodeURIComponent(cookie.value)}`,
|
|
60
|
+
`Max-Age=${cookie.maxAge}`,
|
|
61
|
+
`Path=${cookie.path}`
|
|
62
|
+
];
|
|
63
|
+
if (cookie.sameSite) {
|
|
64
|
+
parts.push(`SameSite=${cookie.sameSite}`);
|
|
65
|
+
}
|
|
66
|
+
if (cookie.secure) {
|
|
67
|
+
parts.push("Secure");
|
|
68
|
+
}
|
|
69
|
+
return parts.join("; ");
|
|
70
|
+
}
|
|
71
|
+
function parseCookies(request) {
|
|
72
|
+
const cookies = /* @__PURE__ */ new Map();
|
|
73
|
+
const header = request.headers.get("cookie");
|
|
74
|
+
if (!header) return cookies;
|
|
75
|
+
for (const part of header.split(";")) {
|
|
76
|
+
const eq = part.indexOf("=");
|
|
77
|
+
if (eq === -1) continue;
|
|
78
|
+
const name = part.slice(0, eq).trim();
|
|
79
|
+
const value = part.slice(eq + 1).trim();
|
|
80
|
+
if (!name) continue;
|
|
81
|
+
try {
|
|
82
|
+
cookies.set(name, decodeURIComponent(value));
|
|
83
|
+
} catch {
|
|
84
|
+
cookies.set(name, value);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return cookies;
|
|
88
|
+
}
|
|
89
|
+
function processFrameworkRequest(request, opts, framework) {
|
|
90
|
+
const requestHeaders = new Headers(request.headers);
|
|
91
|
+
const responseCookies = [];
|
|
92
|
+
const inboundPreprHeaders = [];
|
|
93
|
+
requestHeaders.forEach((_value, key) => {
|
|
94
|
+
if (key.toLowerCase().startsWith("prepr-")) {
|
|
95
|
+
inboundPreprHeaders.push(key);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
for (const key of inboundPreprHeaders) {
|
|
99
|
+
requestHeaders.delete(key);
|
|
100
|
+
}
|
|
101
|
+
const requestCookies = parseCookies(request);
|
|
102
|
+
const url = new URL(request.url);
|
|
103
|
+
const searchParams = url.searchParams;
|
|
104
|
+
const forwardedProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();
|
|
105
|
+
const isSecure = forwardedProto ? forwardedProto === "https" : url.protocol === "https:";
|
|
106
|
+
const crossSite = isSecure ? { sameSite: "None", secure: true } : {};
|
|
107
|
+
for (const key of UTM_PARAMS) {
|
|
108
|
+
const value = searchParams.get(key);
|
|
109
|
+
if (value !== null) {
|
|
110
|
+
requestHeaders.set(`Prepr-Context-${key}`, sanitizeHeaderValue(value));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const referer = request.headers.get("referer");
|
|
114
|
+
if (referer) {
|
|
115
|
+
requestHeaders.set(
|
|
116
|
+
"Prepr-Context-initial_referral",
|
|
117
|
+
sanitizeHeaderValue(referer)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const userAgent = request.headers.get("user-agent");
|
|
121
|
+
if (userAgent) {
|
|
122
|
+
requestHeaders.set("User-Agent", sanitizeHeaderValue(userAgent));
|
|
123
|
+
}
|
|
124
|
+
const packageParts = [`@preprio/toolkit@${opts?.version ?? VERSION}`];
|
|
125
|
+
if (framework) packageParts.push(`framework=${framework}`);
|
|
126
|
+
packageParts.push(`preview=${opts?.preview ? "true" : "false"}`);
|
|
127
|
+
requestHeaders.set("Prepr-Package", packageParts.join("; "));
|
|
128
|
+
const ip = request.headers.get("Cf-Connecting-Ip") ?? request.headers.get("x-real-ip");
|
|
129
|
+
if (ip) {
|
|
130
|
+
requestHeaders.set("Prepr-Visitor-IP", sanitizeHeaderValue(ip));
|
|
131
|
+
}
|
|
132
|
+
const hutkCookie = requestCookies.get(COOKIE_HUBSPOT);
|
|
133
|
+
if (hutkCookie) {
|
|
134
|
+
requestHeaders.set("Prepr-Hubspot-Id", sanitizeHeaderValue(hutkCookie));
|
|
135
|
+
}
|
|
136
|
+
let uid = sanitizeHeaderValue(requestCookies.get(COOKIE_UID) ?? "");
|
|
137
|
+
if (!uid) {
|
|
138
|
+
uid = randomUUID();
|
|
139
|
+
responseCookies.push({
|
|
140
|
+
name: COOKIE_UID,
|
|
141
|
+
value: uid,
|
|
142
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
143
|
+
path: "/"
|
|
144
|
+
});
|
|
145
|
+
requestHeaders.set("Prepr-Customer-Id-Created", "true");
|
|
146
|
+
}
|
|
147
|
+
requestHeaders.set("Prepr-Customer-Id", uid);
|
|
148
|
+
if (!opts?.preview) {
|
|
149
|
+
return { requestHeaders, responseCookies };
|
|
150
|
+
}
|
|
151
|
+
const features = resolveFeatures(opts?.features);
|
|
152
|
+
const hasPreviewParams = features.segments && searchParams.has(PARAM_SEGMENT) || features.abTesting && searchParams.has(PARAM_VARIANT);
|
|
153
|
+
if (!hasPreviewParams) {
|
|
154
|
+
const previewModeCookie = requestCookies.get(COOKIE_PREVIEW_MODE);
|
|
155
|
+
const previewModeEnabled = previewModeCookie !== "false";
|
|
156
|
+
if (!previewModeEnabled) {
|
|
157
|
+
return { requestHeaders, responseCookies };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
161
|
+
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
162
|
+
if (!isLivePreview && !requestCookies.has(COOKIE_PREVIEW_MODE)) {
|
|
163
|
+
responseCookies.push({
|
|
164
|
+
name: COOKIE_PREVIEW_MODE,
|
|
165
|
+
value: "true",
|
|
166
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
167
|
+
path: "/",
|
|
168
|
+
...crossSite
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
if (!isLivePreview) {
|
|
172
|
+
const segmentCookie = features.segments ? requestCookies.get(COOKIE_SEGMENT) : void 0;
|
|
173
|
+
if (segmentCookie) {
|
|
174
|
+
requestHeaders.set("Prepr-Segments", sanitizeHeaderValue(segmentCookie));
|
|
175
|
+
}
|
|
176
|
+
const abCookie = features.abTesting ? requestCookies.get(COOKIE_VARIANT) : void 0;
|
|
177
|
+
if (abCookie) {
|
|
178
|
+
requestHeaders.set("Prepr-ABtesting", sanitizeHeaderValue(abCookie));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const previewAb = features.abTesting ? searchParams.get(PARAM_VARIANT) : null;
|
|
182
|
+
if (previewAb !== null) {
|
|
183
|
+
requestHeaders.set("Prepr-ABtesting", sanitizeHeaderValue(previewAb));
|
|
184
|
+
if (!isLivePreview) {
|
|
185
|
+
responseCookies.push({
|
|
186
|
+
name: COOKIE_VARIANT,
|
|
187
|
+
value: previewAb,
|
|
188
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
189
|
+
path: "/",
|
|
190
|
+
...crossSite
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const previewSegment = features.segments ? searchParams.get(PARAM_SEGMENT) : null;
|
|
195
|
+
if (previewSegment !== null) {
|
|
196
|
+
requestHeaders.set("Prepr-Segments", sanitizeHeaderValue(previewSegment));
|
|
197
|
+
if (!isLivePreview) {
|
|
198
|
+
responseCookies.push({
|
|
199
|
+
name: COOKIE_SEGMENT,
|
|
200
|
+
value: previewSegment,
|
|
201
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
202
|
+
path: "/",
|
|
203
|
+
...crossSite
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return { requestHeaders, responseCookies };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/sveltekit/hooks.ts
|
|
211
|
+
var PREPR_LOCALS_KEY = "prepr";
|
|
212
|
+
function preprHandle(options) {
|
|
213
|
+
return async ({ event, resolve }) => {
|
|
214
|
+
const { requestHeaders, responseCookies } = processFrameworkRequest(
|
|
215
|
+
event.request,
|
|
216
|
+
options,
|
|
217
|
+
"sveltekit"
|
|
218
|
+
);
|
|
219
|
+
requestHeaders.forEach((value, key) => {
|
|
220
|
+
event.request.headers.set(key, value);
|
|
221
|
+
});
|
|
222
|
+
if (event.locals) {
|
|
223
|
+
event.locals[PREPR_LOCALS_KEY] = requestHeaders;
|
|
224
|
+
}
|
|
225
|
+
const response = await resolve(event);
|
|
226
|
+
for (const cookie of responseCookies) {
|
|
227
|
+
response.headers.append("Set-Cookie", serializeCookie(cookie));
|
|
228
|
+
}
|
|
229
|
+
return response;
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function getPreprHeadersFromLocals(locals) {
|
|
233
|
+
const headers = locals[PREPR_LOCALS_KEY];
|
|
234
|
+
return headers instanceof Headers ? headers : new Headers();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// src/core/server.ts
|
|
238
|
+
var PreprError = class extends Error {
|
|
239
|
+
constructor(message, code, context, originalError) {
|
|
240
|
+
super(message);
|
|
241
|
+
this.code = code;
|
|
242
|
+
this.context = context;
|
|
243
|
+
this.originalError = originalError;
|
|
244
|
+
this.name = "PreprError";
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
function getPreprUUIDFromHeaders(headers) {
|
|
248
|
+
return headers.get("prepr-customer-id");
|
|
249
|
+
}
|
|
250
|
+
function getActiveSegmentFromHeaders(headers) {
|
|
251
|
+
return headers.get("Prepr-Segments");
|
|
252
|
+
}
|
|
253
|
+
function getActiveVariantFromHeaders(headers) {
|
|
254
|
+
return headers.get("Prepr-ABtesting");
|
|
255
|
+
}
|
|
256
|
+
var PREPR_HEADER_KEYS = [
|
|
257
|
+
"prepr-customer-id",
|
|
258
|
+
"Prepr-Segments",
|
|
259
|
+
"Prepr-ABtesting",
|
|
260
|
+
"Prepr-Preview-Bar",
|
|
261
|
+
"Prepr-Context-utm_source",
|
|
262
|
+
"Prepr-Context-utm_medium",
|
|
263
|
+
"Prepr-Context-utm_term",
|
|
264
|
+
"Prepr-Context-utm_content",
|
|
265
|
+
"Prepr-Context-utm_campaign",
|
|
266
|
+
"Prepr-Context-initial_referral",
|
|
267
|
+
"Prepr-Visitor-IP",
|
|
268
|
+
"Prepr-Hubspot-Id",
|
|
269
|
+
"Prepr-Customer-Id-Created",
|
|
270
|
+
"User-Agent",
|
|
271
|
+
"Prepr-Package"
|
|
272
|
+
];
|
|
273
|
+
function getPreprHeadersFromHeaders(headers) {
|
|
274
|
+
const preprHeaders = {};
|
|
275
|
+
const copy = (key) => {
|
|
276
|
+
const value = headers.get(key);
|
|
277
|
+
if (value !== null) {
|
|
278
|
+
preprHeaders[key] = value;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
for (const key of PREPR_HEADER_KEYS) {
|
|
282
|
+
copy(key);
|
|
283
|
+
}
|
|
284
|
+
return preprHeaders;
|
|
285
|
+
}
|
|
286
|
+
function validatePreprToken(token) {
|
|
287
|
+
if (!token) {
|
|
288
|
+
throw new PreprError(
|
|
289
|
+
"Token is required",
|
|
290
|
+
"MISSING_TOKEN",
|
|
291
|
+
"validatePreprToken"
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
if (!token.startsWith("https://")) {
|
|
295
|
+
throw new PreprError(
|
|
296
|
+
"Token must be a valid HTTPS URL",
|
|
297
|
+
"INVALID_TOKEN",
|
|
298
|
+
"validatePreprToken"
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function extractAccessToken(graphqlUrl) {
|
|
303
|
+
let url;
|
|
304
|
+
try {
|
|
305
|
+
url = new URL(graphqlUrl);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
throw new PreprError(
|
|
308
|
+
"Token must be a valid URL",
|
|
309
|
+
"INVALID_TOKEN",
|
|
310
|
+
"extractAccessToken",
|
|
311
|
+
error instanceof Error ? error : new Error(String(error))
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
if (url.hostname !== "graphql.prepr.io") {
|
|
315
|
+
throw new PreprError(
|
|
316
|
+
"Token must be a graphql.prepr.io URL",
|
|
317
|
+
"INVALID_TOKEN",
|
|
318
|
+
"extractAccessToken"
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
const pathParts = url.pathname.split("/");
|
|
322
|
+
const token = pathParts[pathParts.length - 1];
|
|
323
|
+
if (!token) {
|
|
324
|
+
throw new PreprError(
|
|
325
|
+
"Token URL is missing an access token segment",
|
|
326
|
+
"INVALID_TOKEN",
|
|
327
|
+
"extractAccessToken"
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
return token;
|
|
331
|
+
}
|
|
332
|
+
async function getPreprEnvironmentSegments(token) {
|
|
333
|
+
validatePreprToken(token);
|
|
334
|
+
try {
|
|
335
|
+
const response = await fetch(token, {
|
|
336
|
+
headers: {
|
|
337
|
+
"Prepr-Package": `@preprio/toolkit@${VERSION}`,
|
|
338
|
+
// Duplicates the version in Prepr-Package. It keeps the User-Agent
|
|
339
|
+
// format used by `@preprio/prepr-nextjs`, for Prepr API consumers that
|
|
340
|
+
// still identify the toolbar by that string.
|
|
341
|
+
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
342
|
+
"Content-Type": "application/json"
|
|
343
|
+
},
|
|
344
|
+
method: "POST",
|
|
345
|
+
body: JSON.stringify({
|
|
346
|
+
query: `{
|
|
347
|
+
_Segments {
|
|
348
|
+
_id
|
|
349
|
+
name
|
|
350
|
+
}
|
|
351
|
+
}`
|
|
352
|
+
})
|
|
353
|
+
});
|
|
354
|
+
if (!response.ok) {
|
|
355
|
+
throw new PreprError(
|
|
356
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
357
|
+
"HTTP_ERROR",
|
|
358
|
+
"getPreprEnvironmentSegments"
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
const json = await response.json();
|
|
362
|
+
if (!json || !json.data || !json.data._Segments) {
|
|
363
|
+
throw new PreprError(
|
|
364
|
+
"Invalid response format from Prepr API",
|
|
365
|
+
"INVALID_RESPONSE",
|
|
366
|
+
"getPreprEnvironmentSegments"
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
return json.data._Segments;
|
|
370
|
+
} catch (error) {
|
|
371
|
+
if (error instanceof PreprError) {
|
|
372
|
+
throw error;
|
|
373
|
+
}
|
|
374
|
+
throw new PreprError(
|
|
375
|
+
"Failed to fetch segments from Prepr API",
|
|
376
|
+
"FETCH_ERROR",
|
|
377
|
+
"getPreprEnvironmentSegments",
|
|
378
|
+
error instanceof Error ? error : new Error(String(error))
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
async function getToolbarPropsFromHeaders(headers, token, features) {
|
|
383
|
+
const resolved = resolveFeatures(features);
|
|
384
|
+
let segments = [];
|
|
385
|
+
if (resolved.segments) {
|
|
386
|
+
try {
|
|
387
|
+
segments = await getPreprEnvironmentSegments(token);
|
|
388
|
+
} catch {
|
|
389
|
+
segments = [];
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return {
|
|
393
|
+
activeSegment: resolved.segments ? getActiveSegmentFromHeaders(headers) : null,
|
|
394
|
+
activeVariant: resolved.abTesting ? getActiveVariantFromHeaders(headers) : null,
|
|
395
|
+
segments,
|
|
396
|
+
data: segments
|
|
397
|
+
};
|
|
398
|
+
}
|
|
15
399
|
|
|
16
|
-
// src/
|
|
400
|
+
// src/core/request-helpers.ts
|
|
17
401
|
function getPreprUUID(headers) {
|
|
18
402
|
return getPreprUUIDFromHeaders(headers);
|
|
19
403
|
}
|
|
@@ -39,7 +423,6 @@ export {
|
|
|
39
423
|
getPreprHeadersFromLocals,
|
|
40
424
|
getPreprUUID,
|
|
41
425
|
getToolbarProps,
|
|
42
|
-
isPersonalizedResponse,
|
|
43
426
|
preprHandle,
|
|
44
427
|
validatePreprToken
|
|
45
428
|
};
|