@preprio/toolkit 0.5.0-rc.2 → 0.5.0-rc.4
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 +209 -10
- package/dist/astro/index.cjs +1194 -1
- package/dist/astro/index.d.cts +47 -1
- package/dist/astro/index.d.ts +47 -1
- package/dist/astro/index.js +1197 -1
- package/dist/chunk-46ISDRHT.js +26 -0
- package/dist/chunk-74KY5EAM.js +1219 -0
- package/dist/chunk-AHBN6FQP.js +184 -0
- package/dist/{chunk-FLPNN3CV.js → chunk-PDNCNTDB.js} +1370 -623
- package/dist/chunk-S3VT4UWM.js +75 -0
- package/dist/chunk-TG6N3C22.js +246 -0
- package/dist/chunk-URXSDJOU.js +356 -0
- package/dist/index.cjs +3166 -392
- package/dist/index.d.cts +238 -4
- package/dist/index.d.ts +238 -4
- package/dist/index.js +189 -149
- package/dist/live-preview-H4643QYF.js +18 -0
- package/dist/nextjs/components.cjs +1522 -359
- package/dist/nextjs/components.d.cts +1 -1
- package/dist/nextjs/components.d.ts +1 -1
- package/dist/nextjs/components.js +4 -1
- package/dist/nextjs/index.cjs +1085 -6
- package/dist/nextjs/index.d.cts +73 -1
- package/dist/nextjs/index.d.ts +73 -1
- package/dist/nextjs/index.js +1079 -7
- package/dist/nextjs/live-action.cjs +285 -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 +1896 -0
- package/dist/nextjs/live.d.cts +52 -0
- package/dist/nextjs/live.d.ts +52 -0
- package/dist/nextjs/live.js +265 -0
- package/dist/nuxt/index.cjs +1 -1
- package/dist/nuxt/index.js +1 -1
- package/dist/nuxt/live-preview-O2ZTJNW4.js +1520 -0
- package/dist/nuxt/live.cjs +1720 -0
- package/dist/nuxt/live.d.cts +43 -0
- package/dist/nuxt/live.d.ts +43 -0
- package/dist/nuxt/live.js +94 -0
- package/dist/{pixel-B3oQNbZ3.d.cts → pixel-CX422dCO.d.cts} +10 -1
- package/dist/{pixel-B3oQNbZ3.d.ts → pixel-CX422dCO.d.ts} +10 -1
- package/dist/react/components.cjs +1522 -359
- package/dist/react/components.d.cts +9 -0
- package/dist/react/components.d.ts +9 -0
- package/dist/react/components.js +1522 -359
- package/dist/sveltekit/index.cjs +1 -1
- package/dist/sveltekit/index.js +1 -1
- package/package.json +12 -1
- package/src/astro/PreprToolbar.astro +7 -1
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import {
|
|
2
|
+
VERSION,
|
|
3
|
+
resolveFeatures
|
|
4
|
+
} from "./chunk-46ISDRHT.js";
|
|
5
|
+
|
|
6
|
+
// src/core/server.ts
|
|
7
|
+
var PreprError = class extends Error {
|
|
8
|
+
constructor(message, code, context, originalError) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.context = context;
|
|
12
|
+
this.originalError = originalError;
|
|
13
|
+
this.name = "PreprError";
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function getPreprUUIDFromHeaders(headers) {
|
|
17
|
+
return headers.get("prepr-customer-id");
|
|
18
|
+
}
|
|
19
|
+
function getActiveSegmentFromHeaders(headers) {
|
|
20
|
+
return headers.get("Prepr-Segments");
|
|
21
|
+
}
|
|
22
|
+
function getActiveVariantFromHeaders(headers) {
|
|
23
|
+
return headers.get("Prepr-ABtesting");
|
|
24
|
+
}
|
|
25
|
+
var PREVIEW_PACKAGE_PART = /(?:^|;)\s*preview=true\s*(?:;|$)/;
|
|
26
|
+
function isPreviewFromHeaders(headers) {
|
|
27
|
+
return PREVIEW_PACKAGE_PART.test(headers.get("Prepr-Package") ?? "");
|
|
28
|
+
}
|
|
29
|
+
var PREPR_HEADER_KEYS = [
|
|
30
|
+
"prepr-customer-id",
|
|
31
|
+
"Prepr-Segments",
|
|
32
|
+
"Prepr-ABtesting",
|
|
33
|
+
"Prepr-Preview-Bar",
|
|
34
|
+
"Prepr-Context-utm_source",
|
|
35
|
+
"Prepr-Context-utm_medium",
|
|
36
|
+
"Prepr-Context-utm_term",
|
|
37
|
+
"Prepr-Context-utm_content",
|
|
38
|
+
"Prepr-Context-utm_campaign",
|
|
39
|
+
"Prepr-Context-initial_referral",
|
|
40
|
+
"Prepr-Visitor-IP",
|
|
41
|
+
"Prepr-Hubspot-Id",
|
|
42
|
+
"Prepr-Customer-Id-Created",
|
|
43
|
+
"User-Agent",
|
|
44
|
+
"Prepr-Package"
|
|
45
|
+
];
|
|
46
|
+
function getPreprHeadersFromHeaders(headers) {
|
|
47
|
+
const preprHeaders = {};
|
|
48
|
+
const copy = (key) => {
|
|
49
|
+
const value = headers.get(key);
|
|
50
|
+
if (value !== null) {
|
|
51
|
+
preprHeaders[key] = value;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
for (const key of PREPR_HEADER_KEYS) {
|
|
55
|
+
copy(key);
|
|
56
|
+
}
|
|
57
|
+
return preprHeaders;
|
|
58
|
+
}
|
|
59
|
+
function validatePreprToken(token) {
|
|
60
|
+
if (!token) {
|
|
61
|
+
throw new PreprError(
|
|
62
|
+
"Token is required",
|
|
63
|
+
"MISSING_TOKEN",
|
|
64
|
+
"validatePreprToken"
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
if (!token.startsWith("https://")) {
|
|
68
|
+
throw new PreprError(
|
|
69
|
+
"Token must be a valid HTTPS URL",
|
|
70
|
+
"INVALID_TOKEN",
|
|
71
|
+
"validatePreprToken"
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function extractAccessToken(graphqlUrl) {
|
|
76
|
+
let url;
|
|
77
|
+
try {
|
|
78
|
+
url = new URL(graphqlUrl);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
throw new PreprError(
|
|
81
|
+
"Token must be a valid URL",
|
|
82
|
+
"INVALID_TOKEN",
|
|
83
|
+
"extractAccessToken",
|
|
84
|
+
error instanceof Error ? error : new Error(String(error))
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (url.hostname !== "graphql.prepr.io") {
|
|
88
|
+
throw new PreprError(
|
|
89
|
+
"Token must be a graphql.prepr.io URL",
|
|
90
|
+
"INVALID_TOKEN",
|
|
91
|
+
"extractAccessToken"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
const pathParts = url.pathname.split("/");
|
|
95
|
+
const token = pathParts[pathParts.length - 1];
|
|
96
|
+
if (!token) {
|
|
97
|
+
throw new PreprError(
|
|
98
|
+
"Token URL is missing an access token segment",
|
|
99
|
+
"INVALID_TOKEN",
|
|
100
|
+
"extractAccessToken"
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return token;
|
|
104
|
+
}
|
|
105
|
+
async function getPreprEnvironmentSegments(token) {
|
|
106
|
+
validatePreprToken(token);
|
|
107
|
+
try {
|
|
108
|
+
const response = await fetch(token, {
|
|
109
|
+
headers: {
|
|
110
|
+
"Prepr-Package": `@preprio/toolkit@${VERSION}`,
|
|
111
|
+
// Duplicates the version in Prepr-Package. It keeps the User-Agent
|
|
112
|
+
// format used by `@preprio/prepr-nextjs`, for Prepr API consumers that
|
|
113
|
+
// still identify the toolbar by that string.
|
|
114
|
+
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
115
|
+
"Content-Type": "application/json"
|
|
116
|
+
},
|
|
117
|
+
method: "POST",
|
|
118
|
+
body: JSON.stringify({
|
|
119
|
+
query: `{
|
|
120
|
+
_Segments {
|
|
121
|
+
_id
|
|
122
|
+
name
|
|
123
|
+
}
|
|
124
|
+
}`
|
|
125
|
+
})
|
|
126
|
+
});
|
|
127
|
+
if (!response.ok) {
|
|
128
|
+
throw new PreprError(
|
|
129
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
130
|
+
"HTTP_ERROR",
|
|
131
|
+
"getPreprEnvironmentSegments"
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
const json = await response.json();
|
|
135
|
+
if (!json || !json.data || !json.data._Segments) {
|
|
136
|
+
throw new PreprError(
|
|
137
|
+
"Invalid response format from Prepr API",
|
|
138
|
+
"INVALID_RESPONSE",
|
|
139
|
+
"getPreprEnvironmentSegments"
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return json.data._Segments;
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (error instanceof PreprError) {
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
throw new PreprError(
|
|
148
|
+
"Failed to fetch segments from Prepr API",
|
|
149
|
+
"FETCH_ERROR",
|
|
150
|
+
"getPreprEnvironmentSegments",
|
|
151
|
+
error instanceof Error ? error : new Error(String(error))
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async function getToolbarPropsFromHeaders(headers, token, features) {
|
|
156
|
+
const resolved = resolveFeatures(features);
|
|
157
|
+
let segments = [];
|
|
158
|
+
if (resolved.segments) {
|
|
159
|
+
try {
|
|
160
|
+
segments = await getPreprEnvironmentSegments(token);
|
|
161
|
+
} catch {
|
|
162
|
+
segments = [];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
activeSegment: resolved.segments ? getActiveSegmentFromHeaders(headers) : null,
|
|
167
|
+
activeVariant: resolved.abTesting ? getActiveVariantFromHeaders(headers) : null,
|
|
168
|
+
segments,
|
|
169
|
+
data: segments
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export {
|
|
174
|
+
PreprError,
|
|
175
|
+
getPreprUUIDFromHeaders,
|
|
176
|
+
getActiveSegmentFromHeaders,
|
|
177
|
+
getActiveVariantFromHeaders,
|
|
178
|
+
isPreviewFromHeaders,
|
|
179
|
+
getPreprHeadersFromHeaders,
|
|
180
|
+
validatePreprToken,
|
|
181
|
+
extractAccessToken,
|
|
182
|
+
getPreprEnvironmentSegments,
|
|
183
|
+
getToolbarPropsFromHeaders
|
|
184
|
+
};
|