@preprio/toolkit 0.5.0-rc.2 → 0.6.0-alpha.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 +415 -27
- package/dist/astro/chunk-O7N6GPAH.js +173 -0
- package/dist/astro/graphql.cjs +1030 -0
- package/dist/astro/graphql.d.cts +305 -0
- package/dist/astro/graphql.d.ts +305 -0
- package/dist/astro/graphql.js +936 -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-3JLWZQ37.js +179 -0
- package/dist/chunk-3NCEVJJ3.js +10 -0
- package/dist/chunk-S5RWVMDJ.js +148 -0
- package/dist/chunk-TBKSBXGH.js +452 -0
- package/dist/{chunk-FLPNN3CV.js → chunk-VI6QZTBM.js} +73 -276
- package/dist/chunk-YWIKKWBR.js +26 -0
- package/dist/graphql/codegen.cjs +510 -0
- package/dist/graphql/codegen.d.cts +23 -0
- package/dist/graphql/codegen.d.ts +23 -0
- package/dist/graphql/codegen.js +99 -0
- package/dist/graphql.cjs +941 -0
- package/dist/graphql.d.cts +305 -0
- package/dist/graphql.d.ts +305 -0
- package/dist/graphql.js +419 -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 +27 -167
- package/dist/nextjs/chunk-BQ6IGSH6.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 +133 -4
- package/dist/nextjs/graphql.cjs +1362 -0
- package/dist/nextjs/graphql.d.cts +435 -0
- package/dist/nextjs/graphql.d.ts +435 -0
- package/dist/nextjs/graphql.js +1261 -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-IQDSBPDN.js +417 -0
- package/dist/nuxt/graphql.cjs +1037 -0
- package/dist/nuxt/graphql.d.cts +302 -0
- package/dist/nuxt/graphql.d.ts +302 -0
- package/dist/nuxt/graphql.js +937 -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-OKYZ6HPH.js +413 -0
- package/dist/sveltekit/graphql.cjs +1043 -0
- package/dist/sveltekit/graphql.d.cts +307 -0
- package/dist/sveltekit/graphql.d.ts +307 -0
- package/dist/sveltekit/graphql.js +943 -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 +36 -1
|
@@ -0,0 +1,156 @@
|
|
|
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
|
+
/**
|
|
13
|
+
* Per-feature config. `false` is shorthand for `{ enabled: false }`; the object
|
|
14
|
+
* form leaves room for per-feature options later without a breaking change.
|
|
15
|
+
*/
|
|
16
|
+
type PreprFeatureConfig = boolean | {
|
|
17
|
+
enabled?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/** Toolkit features a consumer can turn off. Omitted keys stay enabled. */
|
|
20
|
+
interface PreprFeatures {
|
|
21
|
+
/** Segment picker, `Prepr-Segments` header and its cookie/query param. */
|
|
22
|
+
segments?: PreprFeatureConfig;
|
|
23
|
+
/** A/B variant switch, `Prepr-ABtesting` header and its cookie/query param. */
|
|
24
|
+
abTesting?: PreprFeatureConfig;
|
|
25
|
+
/**
|
|
26
|
+
* The site's own click-to-edit: the toolbar's Edit mode control, the stega
|
|
27
|
+
* tooltip/overlay and the close-edit pill.
|
|
28
|
+
*
|
|
29
|
+
* NOT a security control, and NOT the Prepr editor's live preview. When the
|
|
30
|
+
* site is framed by the editor, `prepr:initVE` still activates edit mode —
|
|
31
|
+
* that is the CMS operating inside its own iframe, not an affordance offered
|
|
32
|
+
* to visitors. See iframe-bridge.ts.
|
|
33
|
+
*/
|
|
34
|
+
editMode?: PreprFeatureConfig;
|
|
35
|
+
}
|
|
36
|
+
interface PreprToolbarOptions {
|
|
37
|
+
debug?: boolean;
|
|
38
|
+
locale?: Locale;
|
|
39
|
+
/** Disable features app-wide. Pass the same object to the middleware. */
|
|
40
|
+
features?: PreprFeatures;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Options for the preview runtime. Two independent axes: `features` decides
|
|
44
|
+
* *what runs*, `ui` decides *whether the toolbar is visible*.
|
|
45
|
+
*/
|
|
46
|
+
interface PreprPreviewOptions extends PreprToolbarOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Mount the visible `<prepr-toolbar>` element. Default `true`.
|
|
49
|
+
*
|
|
50
|
+
* `false` keeps every non-visual side effect wired — click-to-edit, the
|
|
51
|
+
* editor bridge, scroll restore, cookies and headers — with no chrome of its
|
|
52
|
+
* own. That is how a site gets live editing, or editor scroll restore, while
|
|
53
|
+
* rendering its own UI instead of the bar.
|
|
54
|
+
*
|
|
55
|
+
* Already implied inside the editor's iframe (the CMS owns the chrome there)
|
|
56
|
+
* and by `?prepr_hide_bar=true`, so this option is for consumers who want a
|
|
57
|
+
* headless preview by their own choice.
|
|
58
|
+
*/
|
|
59
|
+
ui?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Replace the `*.prepr.io` editor-origin wildcard with an exact list.
|
|
62
|
+
* Intended for self-hosted editors; when set, the wildcard no longer applies.
|
|
63
|
+
*/
|
|
64
|
+
allowedEditorOrigins?: string[];
|
|
65
|
+
/**
|
|
66
|
+
* Strip the invisible stega characters out of visible text at runtime.
|
|
67
|
+
* Default `true`.
|
|
68
|
+
*
|
|
69
|
+
* Preview content carries an encoded payload inside the text itself. Left in
|
|
70
|
+
* place it is invisible but real: it inflates `String.length`, breaks text
|
|
71
|
+
* measurement and truncation, and is announced by screen readers.
|
|
72
|
+
*
|
|
73
|
+
* Set `false` only when the site already strips the characters itself — for
|
|
74
|
+
* example by passing every field through `stegaClean` as it renders.
|
|
75
|
+
* Click-to-edit is unaffected either way: elements are still tagged from the
|
|
76
|
+
* payload, so the overlay keeps working.
|
|
77
|
+
*/
|
|
78
|
+
autoClean?: boolean;
|
|
79
|
+
}
|
|
80
|
+
interface PreprToolbarProps {
|
|
81
|
+
/**
|
|
82
|
+
* Server-resolved active segment. Optional: omit it with `segments`
|
|
83
|
+
* disabled, where there is nothing to resolve. When enabled and omitted,
|
|
84
|
+
* the persisted cookie is used.
|
|
85
|
+
*/
|
|
86
|
+
readonly activeSegment?: string | null;
|
|
87
|
+
/**
|
|
88
|
+
* Server-resolved active A/B variant. Optional: omit it with `abTesting`
|
|
89
|
+
* disabled, where there is nothing to resolve. When enabled and omitted,
|
|
90
|
+
* the persisted cookie is used.
|
|
91
|
+
*/
|
|
92
|
+
readonly activeVariant?: string | null;
|
|
93
|
+
/** Available segments to personalize on. */
|
|
94
|
+
readonly segments?: readonly PreprSegment[];
|
|
95
|
+
/** @deprecated Alias for `segments`. */
|
|
96
|
+
readonly data?: readonly PreprSegment[];
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Prop type for the framework `<PreprToolbar>` components: the toolbar data
|
|
100
|
+
* plus the options bag core `createPreprPreview` accepts.
|
|
101
|
+
*/
|
|
102
|
+
interface PreprToolbarComponentProps extends PreprToolbarProps {
|
|
103
|
+
options?: PreprPreviewOptions;
|
|
104
|
+
}
|
|
105
|
+
type PreprVariant = 'A' | 'B';
|
|
106
|
+
interface PreprHeaders {
|
|
107
|
+
readonly 'prepr-customer-id'?: string;
|
|
108
|
+
readonly 'Prepr-Segments'?: string;
|
|
109
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
110
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
111
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
112
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
113
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
114
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
115
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
116
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
117
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
118
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
119
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
120
|
+
/** The visitor's browser User-Agent, used by the API for device detection. */
|
|
121
|
+
readonly 'User-Agent'?: string;
|
|
122
|
+
/** @deprecated Never populated; the visitor's value is under `User-Agent`. */
|
|
123
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
124
|
+
readonly 'Prepr-Package'?: string;
|
|
125
|
+
}
|
|
126
|
+
type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR' | 'GRAPHQL_ERROR' | 'INVALID_DOCUMENT';
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether a Prepr GraphQL response differs per visitor, read from its `Vary`
|
|
130
|
+
* header. Use it when fetching with your own GraphQL client; the toolkit's
|
|
131
|
+
* `createPreprClient` already returns it as `personalized`.
|
|
132
|
+
*
|
|
133
|
+
* Never throws; a response without `Vary` is not personalized.
|
|
134
|
+
*/
|
|
135
|
+
declare function isPersonalizedResponse(source: Response | Headers): boolean;
|
|
136
|
+
|
|
137
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
138
|
+
declare class PreprError extends Error {
|
|
139
|
+
readonly code: PreprErrorCode;
|
|
140
|
+
readonly context?: string | undefined;
|
|
141
|
+
readonly originalError?: Error | undefined;
|
|
142
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
|
|
146
|
+
* @throws PreprError if the token is missing or not an HTTPS URL.
|
|
147
|
+
*/
|
|
148
|
+
declare function validatePreprToken(token: string): void;
|
|
149
|
+
/**
|
|
150
|
+
* Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
|
|
151
|
+
* -> `abc123`.
|
|
152
|
+
* @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
|
|
153
|
+
*/
|
|
154
|
+
declare function extractAccessToken(graphqlUrl: string): string;
|
|
155
|
+
|
|
156
|
+
export { type PreprHeaders as P, PreprError as a, type PreprFeatures as b, type PreprToolbarProps as c, type PreprToolbarComponentProps as d, extractAccessToken as e, isPersonalizedResponse as i, validatePreprToken as v };
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
// src/version.ts
|
|
2
|
+
var VERSION = "0.6.0-alpha.1";
|
|
3
|
+
|
|
4
|
+
// src/core/constants.ts
|
|
5
|
+
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
6
|
+
var COOKIE_SEGMENT = "Prepr-Segments";
|
|
7
|
+
var COOKIE_VARIANT = "Prepr-ABtesting";
|
|
8
|
+
var COOKIE_UID = "__prepr_uid";
|
|
9
|
+
var COOKIE_HUBSPOT = "hubspotutk";
|
|
10
|
+
var PARAM_SEGMENT = "prepr_preview_segment";
|
|
11
|
+
var PARAM_VARIANT = "prepr_preview_ab";
|
|
12
|
+
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
13
|
+
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
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
|
+
if (requestHeaders.has("Prepr-Segments")) {
|
|
197
|
+
requestHeaders.delete("Prepr-Customer-Id");
|
|
198
|
+
requestHeaders.delete("Prepr-Customer-Id-Created");
|
|
199
|
+
}
|
|
200
|
+
return { requestHeaders, responseCookies };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/nuxt/middleware.ts
|
|
204
|
+
var PREPR_CONTEXT_KEY = "prepr";
|
|
205
|
+
function handlePreprRequest(event, options) {
|
|
206
|
+
const req = event.node.req;
|
|
207
|
+
const host = typeof req.headers.host === "string" ? req.headers.host : "localhost";
|
|
208
|
+
const request = new Request(`http://${host}${req.url ?? "/"}`);
|
|
209
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
210
|
+
if (typeof value === "string") {
|
|
211
|
+
request.headers.set(key, value);
|
|
212
|
+
} else if (Array.isArray(value)) {
|
|
213
|
+
request.headers.set(key, value.join(", "));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const { requestHeaders, responseCookies } = processFrameworkRequest(
|
|
217
|
+
request,
|
|
218
|
+
options,
|
|
219
|
+
"nuxt"
|
|
220
|
+
);
|
|
221
|
+
requestHeaders.forEach((value, key) => {
|
|
222
|
+
req.headers[key.toLowerCase()] = value;
|
|
223
|
+
});
|
|
224
|
+
event.context[PREPR_CONTEXT_KEY] = requestHeaders;
|
|
225
|
+
for (const cookie of responseCookies) {
|
|
226
|
+
event.node.res.appendHeader("Set-Cookie", serializeCookie(cookie));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function getPreprHeadersFromEvent(event) {
|
|
230
|
+
const headers = event.context[PREPR_CONTEXT_KEY];
|
|
231
|
+
return headers instanceof Headers ? headers : new Headers();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// src/core/server.ts
|
|
235
|
+
var PreprError = class extends Error {
|
|
236
|
+
constructor(message, code, context, originalError) {
|
|
237
|
+
super(message);
|
|
238
|
+
this.code = code;
|
|
239
|
+
this.context = context;
|
|
240
|
+
this.originalError = originalError;
|
|
241
|
+
this.name = "PreprError";
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
function getPreprUUIDFromHeaders(headers) {
|
|
245
|
+
return headers.get("prepr-customer-id");
|
|
246
|
+
}
|
|
247
|
+
function getActiveSegmentFromHeaders(headers) {
|
|
248
|
+
return headers.get("Prepr-Segments");
|
|
249
|
+
}
|
|
250
|
+
function getActiveVariantFromHeaders(headers) {
|
|
251
|
+
return headers.get("Prepr-ABtesting");
|
|
252
|
+
}
|
|
253
|
+
var PREPR_HEADER_KEYS = [
|
|
254
|
+
"prepr-customer-id",
|
|
255
|
+
"Prepr-Segments",
|
|
256
|
+
"Prepr-ABtesting",
|
|
257
|
+
"Prepr-Preview-Bar",
|
|
258
|
+
"Prepr-Context-utm_source",
|
|
259
|
+
"Prepr-Context-utm_medium",
|
|
260
|
+
"Prepr-Context-utm_term",
|
|
261
|
+
"Prepr-Context-utm_content",
|
|
262
|
+
"Prepr-Context-utm_campaign",
|
|
263
|
+
"Prepr-Context-initial_referral",
|
|
264
|
+
"Prepr-Visitor-IP",
|
|
265
|
+
"Prepr-Hubspot-Id",
|
|
266
|
+
"Prepr-Customer-Id-Created",
|
|
267
|
+
"User-Agent",
|
|
268
|
+
"Prepr-Package"
|
|
269
|
+
];
|
|
270
|
+
function getPreprHeadersFromHeaders(headers) {
|
|
271
|
+
const preprHeaders = {};
|
|
272
|
+
const copy = (key) => {
|
|
273
|
+
const value = headers.get(key);
|
|
274
|
+
if (value !== null) {
|
|
275
|
+
preprHeaders[key] = value;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
for (const key of PREPR_HEADER_KEYS) {
|
|
279
|
+
copy(key);
|
|
280
|
+
}
|
|
281
|
+
return preprHeaders;
|
|
282
|
+
}
|
|
283
|
+
function validatePreprToken(token) {
|
|
284
|
+
if (!token) {
|
|
285
|
+
throw new PreprError(
|
|
286
|
+
"Token is required",
|
|
287
|
+
"MISSING_TOKEN",
|
|
288
|
+
"validatePreprToken"
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
if (!token.startsWith("https://")) {
|
|
292
|
+
throw new PreprError(
|
|
293
|
+
"Token must be a valid HTTPS URL",
|
|
294
|
+
"INVALID_TOKEN",
|
|
295
|
+
"validatePreprToken"
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function extractAccessToken(graphqlUrl) {
|
|
300
|
+
let url;
|
|
301
|
+
try {
|
|
302
|
+
url = new URL(graphqlUrl);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
throw new PreprError(
|
|
305
|
+
"Token must be a valid URL",
|
|
306
|
+
"INVALID_TOKEN",
|
|
307
|
+
"extractAccessToken",
|
|
308
|
+
error instanceof Error ? error : new Error(String(error))
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
if (url.hostname !== "graphql.prepr.io") {
|
|
312
|
+
throw new PreprError(
|
|
313
|
+
"Token must be a graphql.prepr.io URL",
|
|
314
|
+
"INVALID_TOKEN",
|
|
315
|
+
"extractAccessToken"
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
const pathParts = url.pathname.split("/");
|
|
319
|
+
const token = pathParts[pathParts.length - 1];
|
|
320
|
+
if (!token) {
|
|
321
|
+
throw new PreprError(
|
|
322
|
+
"Token URL is missing an access token segment",
|
|
323
|
+
"INVALID_TOKEN",
|
|
324
|
+
"extractAccessToken"
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
return token;
|
|
328
|
+
}
|
|
329
|
+
async function getPreprEnvironmentSegments(token) {
|
|
330
|
+
validatePreprToken(token);
|
|
331
|
+
try {
|
|
332
|
+
const response = await fetch(token, {
|
|
333
|
+
headers: {
|
|
334
|
+
"Prepr-Package": `@preprio/toolkit@${VERSION}`,
|
|
335
|
+
// Duplicates the version in Prepr-Package. It keeps the User-Agent
|
|
336
|
+
// format used by `@preprio/prepr-nextjs`, for Prepr API consumers that
|
|
337
|
+
// still identify the toolbar by that string.
|
|
338
|
+
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
339
|
+
"Content-Type": "application/json"
|
|
340
|
+
},
|
|
341
|
+
method: "POST",
|
|
342
|
+
body: JSON.stringify({
|
|
343
|
+
query: `{
|
|
344
|
+
_Segments {
|
|
345
|
+
_id
|
|
346
|
+
name
|
|
347
|
+
}
|
|
348
|
+
}`
|
|
349
|
+
})
|
|
350
|
+
});
|
|
351
|
+
if (!response.ok) {
|
|
352
|
+
throw new PreprError(
|
|
353
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
354
|
+
"HTTP_ERROR",
|
|
355
|
+
"getPreprEnvironmentSegments"
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
const json = await response.json();
|
|
359
|
+
if (!json || !json.data || !json.data._Segments) {
|
|
360
|
+
throw new PreprError(
|
|
361
|
+
"Invalid response format from Prepr API",
|
|
362
|
+
"INVALID_RESPONSE",
|
|
363
|
+
"getPreprEnvironmentSegments"
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
return json.data._Segments;
|
|
367
|
+
} catch (error) {
|
|
368
|
+
if (error instanceof PreprError) {
|
|
369
|
+
throw error;
|
|
370
|
+
}
|
|
371
|
+
throw new PreprError(
|
|
372
|
+
"Failed to fetch segments from Prepr API",
|
|
373
|
+
"FETCH_ERROR",
|
|
374
|
+
"getPreprEnvironmentSegments",
|
|
375
|
+
error instanceof Error ? error : new Error(String(error))
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
async function getToolbarPropsFromHeaders(headers, token, features) {
|
|
380
|
+
const resolved = resolveFeatures(features);
|
|
381
|
+
let segments = [];
|
|
382
|
+
if (resolved.segments) {
|
|
383
|
+
try {
|
|
384
|
+
segments = await getPreprEnvironmentSegments(token);
|
|
385
|
+
} catch {
|
|
386
|
+
segments = [];
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
activeSegment: resolved.segments ? getActiveSegmentFromHeaders(headers) : null,
|
|
391
|
+
activeVariant: resolved.abTesting ? getActiveVariantFromHeaders(headers) : null,
|
|
392
|
+
segments,
|
|
393
|
+
data: segments
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// src/core/personalization.ts
|
|
398
|
+
var PERSONALIZED_VARY = /\bPrepr-(Segments|ABTesting)/i;
|
|
399
|
+
function isPersonalizedResponse(source) {
|
|
400
|
+
const headers = "headers" in source ? source.headers : source;
|
|
401
|
+
return PERSONALIZED_VARY.test(headers.get("vary") ?? "");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export {
|
|
405
|
+
PREPR_CONTEXT_KEY,
|
|
406
|
+
handlePreprRequest,
|
|
407
|
+
getPreprHeadersFromEvent,
|
|
408
|
+
PreprError,
|
|
409
|
+
getPreprUUIDFromHeaders,
|
|
410
|
+
getActiveSegmentFromHeaders,
|
|
411
|
+
getActiveVariantFromHeaders,
|
|
412
|
+
getPreprHeadersFromHeaders,
|
|
413
|
+
validatePreprToken,
|
|
414
|
+
extractAccessToken,
|
|
415
|
+
getToolbarPropsFromHeaders,
|
|
416
|
+
isPersonalizedResponse
|
|
417
|
+
};
|