@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.
Files changed (78) hide show
  1. package/README.md +415 -27
  2. package/dist/astro/chunk-O7N6GPAH.js +173 -0
  3. package/dist/astro/graphql.cjs +1030 -0
  4. package/dist/astro/graphql.d.cts +305 -0
  5. package/dist/astro/graphql.d.ts +305 -0
  6. package/dist/astro/graphql.js +936 -0
  7. package/dist/astro/index-XT_z-RYT.d.cts +130 -0
  8. package/dist/astro/index-XT_z-RYT.d.ts +130 -0
  9. package/dist/astro/index.cjs +14 -1
  10. package/dist/astro/index.d.cts +1 -120
  11. package/dist/astro/index.d.ts +1 -120
  12. package/dist/astro/index.js +15 -153
  13. package/dist/chunk-3JLWZQ37.js +179 -0
  14. package/dist/chunk-3NCEVJJ3.js +10 -0
  15. package/dist/chunk-S5RWVMDJ.js +148 -0
  16. package/dist/chunk-TBKSBXGH.js +452 -0
  17. package/dist/{chunk-FLPNN3CV.js → chunk-VI6QZTBM.js} +73 -276
  18. package/dist/chunk-YWIKKWBR.js +26 -0
  19. package/dist/graphql/codegen.cjs +510 -0
  20. package/dist/graphql/codegen.d.cts +23 -0
  21. package/dist/graphql/codegen.d.ts +23 -0
  22. package/dist/graphql/codegen.js +99 -0
  23. package/dist/graphql.cjs +941 -0
  24. package/dist/graphql.d.cts +305 -0
  25. package/dist/graphql.d.ts +305 -0
  26. package/dist/graphql.js +419 -0
  27. package/dist/index.cjs +76 -119
  28. package/dist/index.d.cts +12 -64
  29. package/dist/index.d.ts +12 -64
  30. package/dist/index.js +27 -167
  31. package/dist/nextjs/chunk-BQ6IGSH6.js +231 -0
  32. package/dist/nextjs/components.cjs +190 -122
  33. package/dist/nextjs/components.d.cts +33 -2
  34. package/dist/nextjs/components.d.ts +33 -2
  35. package/dist/nextjs/components.js +133 -4
  36. package/dist/nextjs/graphql.cjs +1362 -0
  37. package/dist/nextjs/graphql.d.cts +435 -0
  38. package/dist/nextjs/graphql.d.ts +435 -0
  39. package/dist/nextjs/graphql.js +1261 -0
  40. package/dist/nextjs/index.cjs +126 -6
  41. package/dist/nextjs/index.d.cts +53 -147
  42. package/dist/nextjs/index.d.ts +53 -147
  43. package/dist/nextjs/index.js +113 -203
  44. package/dist/nextjs/server-CjpELYvq.d.cts +156 -0
  45. package/dist/nextjs/server-CjpELYvq.d.ts +156 -0
  46. package/dist/nuxt/chunk-IQDSBPDN.js +417 -0
  47. package/dist/nuxt/graphql.cjs +1037 -0
  48. package/dist/nuxt/graphql.d.cts +302 -0
  49. package/dist/nuxt/graphql.d.ts +302 -0
  50. package/dist/nuxt/graphql.js +937 -0
  51. package/dist/nuxt/index.cjs +14 -1
  52. package/dist/nuxt/index.d.cts +3 -148
  53. package/dist/nuxt/index.d.ts +3 -148
  54. package/dist/nuxt/index.js +15 -391
  55. package/dist/nuxt/server-8v6OHbsw.d.cts +158 -0
  56. package/dist/nuxt/server-8v6OHbsw.d.ts +158 -0
  57. package/dist/pixel-DwW7cXJm.d.cts +49 -0
  58. package/dist/pixel-DwW7cXJm.d.ts +49 -0
  59. package/dist/react/components.cjs +88 -120
  60. package/dist/react/components.d.cts +2 -8
  61. package/dist/react/components.d.ts +2 -8
  62. package/dist/react/components.js +88 -120
  63. package/dist/server-Brr1UgtW.d.ts +59 -0
  64. package/dist/server-DJuS3YU0.d.cts +59 -0
  65. package/dist/sveltekit/chunk-OKYZ6HPH.js +413 -0
  66. package/dist/sveltekit/graphql.cjs +1043 -0
  67. package/dist/sveltekit/graphql.d.cts +307 -0
  68. package/dist/sveltekit/graphql.d.ts +307 -0
  69. package/dist/sveltekit/graphql.js +943 -0
  70. package/dist/sveltekit/index.cjs +14 -1
  71. package/dist/sveltekit/index.d.cts +3 -88
  72. package/dist/sveltekit/index.d.ts +3 -88
  73. package/dist/sveltekit/index.js +15 -387
  74. package/dist/sveltekit/server-CO847mEV.d.cts +98 -0
  75. package/dist/sveltekit/server-CO847mEV.d.ts +98 -0
  76. package/dist/{pixel-B3oQNbZ3.d.cts → types-BYH9XqmN.d.cts} +4 -58
  77. package/dist/{pixel-B3oQNbZ3.d.ts → types-BYH9XqmN.d.ts} +4 -58
  78. package/package.json +36 -1
@@ -1,156 +1,13 @@
1
- // src/version.ts
2
- var VERSION = "0.5.0-rc.2";
3
-
4
- // src/core/features.ts
5
- function isEnabled(config) {
6
- if (typeof config === "boolean") return config;
7
- return config?.enabled ?? true;
8
- }
9
- function resolveFeatures(features) {
10
- return {
11
- segments: isEnabled(features?.segments),
12
- abTesting: isEnabled(features?.abTesting),
13
- editMode: isEnabled(features?.editMode)
14
- };
15
- }
16
- var ALL_FEATURES_ENABLED = Object.freeze({
17
- segments: true,
18
- abTesting: true,
19
- editMode: true
20
- });
21
-
22
- // src/core/server.ts
23
- var PreprError = class extends Error {
24
- constructor(message, code, context, originalError) {
25
- super(message);
26
- this.code = code;
27
- this.context = context;
28
- this.originalError = originalError;
29
- this.name = "PreprError";
30
- }
31
- };
32
- function getPreprUUIDFromHeaders(headers) {
33
- return headers.get("prepr-customer-id");
34
- }
35
- function getActiveSegmentFromHeaders(headers) {
36
- return headers.get("Prepr-Segments");
37
- }
38
- function getActiveVariantFromHeaders(headers) {
39
- return headers.get("Prepr-ABtesting");
40
- }
41
- var PREPR_HEADER_KEYS = [
42
- "prepr-customer-id",
43
- "Prepr-Segments",
44
- "Prepr-ABtesting",
45
- "Prepr-Preview-Bar",
46
- "Prepr-Context-utm_source",
47
- "Prepr-Context-utm_medium",
48
- "Prepr-Context-utm_term",
49
- "Prepr-Context-utm_content",
50
- "Prepr-Context-utm_campaign",
51
- "Prepr-Context-initial_referral",
52
- "Prepr-Visitor-IP",
53
- "Prepr-Hubspot-Id",
54
- "Prepr-Customer-Id-Created",
55
- "User-Agent",
56
- "Prepr-Package"
57
- ];
58
- function getPreprHeadersFromHeaders(headers) {
59
- const preprHeaders = {};
60
- const copy = (key) => {
61
- const value = headers.get(key);
62
- if (value !== null) {
63
- preprHeaders[key] = value;
64
- }
65
- };
66
- for (const key of PREPR_HEADER_KEYS) {
67
- copy(key);
68
- }
69
- return preprHeaders;
70
- }
71
- function validatePreprToken(token) {
72
- if (!token) {
73
- throw new PreprError(
74
- "Token is required",
75
- "MISSING_TOKEN",
76
- "validatePreprToken"
77
- );
78
- }
79
- if (!token.startsWith("https://")) {
80
- throw new PreprError(
81
- "Token must be a valid HTTPS URL",
82
- "INVALID_TOKEN",
83
- "validatePreprToken"
84
- );
85
- }
86
- }
87
- async function getPreprEnvironmentSegments(token) {
88
- validatePreprToken(token);
89
- try {
90
- const response = await fetch(token, {
91
- headers: {
92
- "Prepr-Package": `@preprio/toolkit@${VERSION}`,
93
- // Duplicates the version in Prepr-Package. It keeps the User-Agent
94
- // format used by `@preprio/prepr-nextjs`, for Prepr API consumers that
95
- // still identify the toolbar by that string.
96
- "User-Agent": `Prepr-Preview-Bar/${VERSION}`,
97
- "Content-Type": "application/json"
98
- },
99
- method: "POST",
100
- body: JSON.stringify({
101
- query: `{
102
- _Segments {
103
- _id
104
- name
105
- }
106
- }`
107
- })
108
- });
109
- if (!response.ok) {
110
- throw new PreprError(
111
- `HTTP ${response.status}: ${response.statusText}`,
112
- "HTTP_ERROR",
113
- "getPreprEnvironmentSegments"
114
- );
115
- }
116
- const json = await response.json();
117
- if (!json || !json.data || !json.data._Segments) {
118
- throw new PreprError(
119
- "Invalid response format from Prepr API",
120
- "INVALID_RESPONSE",
121
- "getPreprEnvironmentSegments"
122
- );
123
- }
124
- return json.data._Segments;
125
- } catch (error) {
126
- if (error instanceof PreprError) {
127
- throw error;
128
- }
129
- throw new PreprError(
130
- "Failed to fetch segments from Prepr API",
131
- "FETCH_ERROR",
132
- "getPreprEnvironmentSegments",
133
- error instanceof Error ? error : new Error(String(error))
134
- );
135
- }
136
- }
137
- async function getToolbarPropsFromHeaders(headers, token, features) {
138
- const resolved = resolveFeatures(features);
139
- let segments = [];
140
- if (resolved.segments) {
141
- try {
142
- segments = await getPreprEnvironmentSegments(token);
143
- } catch {
144
- segments = [];
145
- }
146
- }
147
- return {
148
- activeSegment: resolved.segments ? getActiveSegmentFromHeaders(headers) : null,
149
- activeVariant: resolved.abTesting ? getActiveVariantFromHeaders(headers) : null,
150
- segments,
151
- data: segments
152
- };
153
- }
1
+ import {
2
+ VERSION,
3
+ getActiveSegmentFromHeaders,
4
+ getActiveVariantFromHeaders,
5
+ getPreprHeadersFromHeaders,
6
+ getPreprUUIDFromHeaders,
7
+ getToolbarPropsFromHeaders,
8
+ isPersonalizedResponse,
9
+ resolveFeatures
10
+ } from "./chunk-O7N6GPAH.js";
154
11
 
155
12
  // src/core/constants.ts
156
13
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -326,6 +183,10 @@ function processFrameworkRequest(request, opts, framework) {
326
183
  });
327
184
  }
328
185
  }
186
+ if (requestHeaders.has("Prepr-Segments")) {
187
+ requestHeaders.delete("Prepr-Customer-Id");
188
+ requestHeaders.delete("Prepr-Customer-Id-Created");
189
+ }
329
190
  return { requestHeaders, responseCookies };
330
191
  }
331
192
 
@@ -366,5 +227,6 @@ export {
366
227
  getPreprHeaders,
367
228
  getPreprUUID,
368
229
  getToolbarProps,
230
+ isPersonalizedResponse,
369
231
  onPreprRequest
370
232
  };
@@ -0,0 +1,179 @@
1
+ import {
2
+ VERSION,
3
+ resolveFeatures
4
+ } from "./chunk-YWIKKWBR.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 PREPR_HEADER_KEYS = [
26
+ "prepr-customer-id",
27
+ "Prepr-Segments",
28
+ "Prepr-ABtesting",
29
+ "Prepr-Preview-Bar",
30
+ "Prepr-Context-utm_source",
31
+ "Prepr-Context-utm_medium",
32
+ "Prepr-Context-utm_term",
33
+ "Prepr-Context-utm_content",
34
+ "Prepr-Context-utm_campaign",
35
+ "Prepr-Context-initial_referral",
36
+ "Prepr-Visitor-IP",
37
+ "Prepr-Hubspot-Id",
38
+ "Prepr-Customer-Id-Created",
39
+ "User-Agent",
40
+ "Prepr-Package"
41
+ ];
42
+ function getPreprHeadersFromHeaders(headers) {
43
+ const preprHeaders = {};
44
+ const copy = (key) => {
45
+ const value = headers.get(key);
46
+ if (value !== null) {
47
+ preprHeaders[key] = value;
48
+ }
49
+ };
50
+ for (const key of PREPR_HEADER_KEYS) {
51
+ copy(key);
52
+ }
53
+ return preprHeaders;
54
+ }
55
+ function validatePreprToken(token) {
56
+ if (!token) {
57
+ throw new PreprError(
58
+ "Token is required",
59
+ "MISSING_TOKEN",
60
+ "validatePreprToken"
61
+ );
62
+ }
63
+ if (!token.startsWith("https://")) {
64
+ throw new PreprError(
65
+ "Token must be a valid HTTPS URL",
66
+ "INVALID_TOKEN",
67
+ "validatePreprToken"
68
+ );
69
+ }
70
+ }
71
+ function extractAccessToken(graphqlUrl) {
72
+ let url;
73
+ try {
74
+ url = new URL(graphqlUrl);
75
+ } catch (error) {
76
+ throw new PreprError(
77
+ "Token must be a valid URL",
78
+ "INVALID_TOKEN",
79
+ "extractAccessToken",
80
+ error instanceof Error ? error : new Error(String(error))
81
+ );
82
+ }
83
+ if (url.hostname !== "graphql.prepr.io") {
84
+ throw new PreprError(
85
+ "Token must be a graphql.prepr.io URL",
86
+ "INVALID_TOKEN",
87
+ "extractAccessToken"
88
+ );
89
+ }
90
+ const pathParts = url.pathname.split("/");
91
+ const token = pathParts[pathParts.length - 1];
92
+ if (!token) {
93
+ throw new PreprError(
94
+ "Token URL is missing an access token segment",
95
+ "INVALID_TOKEN",
96
+ "extractAccessToken"
97
+ );
98
+ }
99
+ return token;
100
+ }
101
+ async function getPreprEnvironmentSegments(token) {
102
+ validatePreprToken(token);
103
+ try {
104
+ const response = await fetch(token, {
105
+ headers: {
106
+ "Prepr-Package": `@preprio/toolkit@${VERSION}`,
107
+ // Duplicates the version in Prepr-Package. It keeps the User-Agent
108
+ // format used by `@preprio/prepr-nextjs`, for Prepr API consumers that
109
+ // still identify the toolbar by that string.
110
+ "User-Agent": `Prepr-Preview-Bar/${VERSION}`,
111
+ "Content-Type": "application/json"
112
+ },
113
+ method: "POST",
114
+ body: JSON.stringify({
115
+ query: `{
116
+ _Segments {
117
+ _id
118
+ name
119
+ }
120
+ }`
121
+ })
122
+ });
123
+ if (!response.ok) {
124
+ throw new PreprError(
125
+ `HTTP ${response.status}: ${response.statusText}`,
126
+ "HTTP_ERROR",
127
+ "getPreprEnvironmentSegments"
128
+ );
129
+ }
130
+ const json = await response.json();
131
+ if (!json || !json.data || !json.data._Segments) {
132
+ throw new PreprError(
133
+ "Invalid response format from Prepr API",
134
+ "INVALID_RESPONSE",
135
+ "getPreprEnvironmentSegments"
136
+ );
137
+ }
138
+ return json.data._Segments;
139
+ } catch (error) {
140
+ if (error instanceof PreprError) {
141
+ throw error;
142
+ }
143
+ throw new PreprError(
144
+ "Failed to fetch segments from Prepr API",
145
+ "FETCH_ERROR",
146
+ "getPreprEnvironmentSegments",
147
+ error instanceof Error ? error : new Error(String(error))
148
+ );
149
+ }
150
+ }
151
+ async function getToolbarPropsFromHeaders(headers, token, features) {
152
+ const resolved = resolveFeatures(features);
153
+ let segments = [];
154
+ if (resolved.segments) {
155
+ try {
156
+ segments = await getPreprEnvironmentSegments(token);
157
+ } catch {
158
+ segments = [];
159
+ }
160
+ }
161
+ return {
162
+ activeSegment: resolved.segments ? getActiveSegmentFromHeaders(headers) : null,
163
+ activeVariant: resolved.abTesting ? getActiveVariantFromHeaders(headers) : null,
164
+ segments,
165
+ data: segments
166
+ };
167
+ }
168
+
169
+ export {
170
+ PreprError,
171
+ getPreprUUIDFromHeaders,
172
+ getActiveSegmentFromHeaders,
173
+ getActiveVariantFromHeaders,
174
+ getPreprHeadersFromHeaders,
175
+ validatePreprToken,
176
+ extractAccessToken,
177
+ getPreprEnvironmentSegments,
178
+ getToolbarPropsFromHeaders
179
+ };
@@ -0,0 +1,10 @@
1
+ // src/core/personalization.ts
2
+ var PERSONALIZED_VARY = /\bPrepr-(Segments|ABTesting)/i;
3
+ function isPersonalizedResponse(source) {
4
+ const headers = "headers" in source ? source.headers : source;
5
+ return PERSONALIZED_VARY.test(headers.get("vary") ?? "");
6
+ }
7
+
8
+ export {
9
+ isPersonalizedResponse
10
+ };
@@ -0,0 +1,148 @@
1
+ // src/core/stega/clean.ts
2
+ import { vercelStegaClean, vercelStegaDecode } from "@vercel/stega";
3
+
4
+ // src/core/utils.ts
5
+ var trustedParentOrigin = null;
6
+ function setTrustedParentOrigin(origin) {
7
+ trustedParentOrigin = origin;
8
+ }
9
+ function sendPreprEvent(event, data, options) {
10
+ if (typeof window !== "undefined") {
11
+ const message = {
12
+ name: "prepr_preview_bar",
13
+ event,
14
+ ...data
15
+ };
16
+ window.dispatchEvent(
17
+ new CustomEvent("prepr_preview_bar", { detail: message })
18
+ );
19
+ if (window.parent && window.parent !== window) {
20
+ if (trustedParentOrigin) {
21
+ window.parent.postMessage(message, trustedParentOrigin);
22
+ } else if (options?.allowUntrustedTarget) {
23
+ window.parent.postMessage(message, "*");
24
+ }
25
+ }
26
+ }
27
+ }
28
+ var DebugLogger = class {
29
+ constructor(options) {
30
+ this.options = {
31
+ prefix: "[Prepr]",
32
+ ...options
33
+ };
34
+ }
35
+ // A local `enabled` wins; otherwise defer to the global logger so scoped
36
+ // loggers created before initDebugLogger() still pick up the setting.
37
+ isEnabled() {
38
+ if (this.options.enabled !== void 0) {
39
+ return this.options.enabled;
40
+ }
41
+ return globalDebugLogger?.options?.enabled ?? false;
42
+ }
43
+ log(message, ...args) {
44
+ if (!this.isEnabled()) return;
45
+ const prefix = this.options.prefix;
46
+ console.log(`${prefix} ${message}`, ...args);
47
+ }
48
+ warn(message, ...args) {
49
+ if (!this.isEnabled()) return;
50
+ const prefix = this.options.prefix;
51
+ console.warn(`${prefix} ${message}`, ...args);
52
+ }
53
+ error(message, ...args) {
54
+ if (!this.isEnabled()) return;
55
+ const prefix = this.options.prefix;
56
+ console.error(`${prefix} ${message}`, ...args);
57
+ }
58
+ };
59
+ var globalDebugLogger = null;
60
+ function initDebugLogger(enabled = false) {
61
+ globalDebugLogger = new DebugLogger({ enabled });
62
+ }
63
+ function createScopedLogger(scopeName) {
64
+ return new DebugLogger({
65
+ prefix: `[Prepr][${scopeName}]`
66
+ });
67
+ }
68
+ function throttle(func, delay) {
69
+ let timeoutId = null;
70
+ let lastExecTime = 0;
71
+ const throttledFunc = ((...args) => {
72
+ const currentTime = Date.now();
73
+ const timeSinceLastExec = currentTime - lastExecTime;
74
+ if (timeSinceLastExec >= delay) {
75
+ func(...args);
76
+ lastExecTime = currentTime;
77
+ } else {
78
+ if (timeoutId) {
79
+ clearTimeout(timeoutId);
80
+ }
81
+ timeoutId = setTimeout(() => {
82
+ func(...args);
83
+ lastExecTime = Date.now();
84
+ timeoutId = null;
85
+ }, delay - timeSinceLastExec);
86
+ }
87
+ });
88
+ throttledFunc.cancel = () => {
89
+ if (timeoutId) {
90
+ clearTimeout(timeoutId);
91
+ timeoutId = null;
92
+ }
93
+ };
94
+ return throttledFunc;
95
+ }
96
+ function createElementCache(query, ttl = 1e3) {
97
+ let cache = null;
98
+ let lastCacheTime = 0;
99
+ return () => {
100
+ const now = Date.now();
101
+ if (!cache || now - lastCacheTime > ttl) {
102
+ cache = document.querySelectorAll(query);
103
+ lastCacheTime = now;
104
+ }
105
+ return cache;
106
+ };
107
+ }
108
+
109
+ // src/core/stega/clean.ts
110
+ var debug = createScopedLogger("stega:clean");
111
+ function stegaClean(text) {
112
+ if (!text) return text;
113
+ return vercelStegaClean(text);
114
+ }
115
+ function decodeStega(str) {
116
+ if (!str) return null;
117
+ try {
118
+ const decoded = vercelStegaDecode(str);
119
+ if (decoded?.href) {
120
+ return decoded;
121
+ }
122
+ } catch (error) {
123
+ debug.log("error decoding stega data", error);
124
+ const match = str.match(/{"origin.*?}/);
125
+ if (match) {
126
+ try {
127
+ const decodedMatch = vercelStegaDecode(match[0]);
128
+ if (decodedMatch?.href) {
129
+ return decodedMatch;
130
+ }
131
+ } catch (innerError) {
132
+ debug.log("error decoding stega regex match", innerError);
133
+ }
134
+ }
135
+ }
136
+ return null;
137
+ }
138
+
139
+ export {
140
+ setTrustedParentOrigin,
141
+ sendPreprEvent,
142
+ initDebugLogger,
143
+ createScopedLogger,
144
+ throttle,
145
+ createElementCache,
146
+ stegaClean,
147
+ decodeStega
148
+ };