@preprio/toolkit 0.5.0-rc.2 → 0.6.0-alpha.0

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 (71) hide show
  1. package/README.md +308 -27
  2. package/dist/astro/chunk-3UVUDDNY.js +173 -0
  3. package/dist/astro/graphql.cjs +983 -0
  4. package/dist/astro/graphql.d.cts +231 -0
  5. package/dist/astro/graphql.d.ts +231 -0
  6. package/dist/astro/graphql.js +893 -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-FLPNN3CV.js → chunk-BJ2LBLWO.js} +71 -276
  14. package/dist/chunk-DGZA7KX7.js +172 -0
  15. package/dist/chunk-YR7P4IWE.js +187 -0
  16. package/dist/graphql.cjs +894 -0
  17. package/dist/graphql.d.cts +231 -0
  18. package/dist/graphql.d.ts +231 -0
  19. package/dist/graphql.js +787 -0
  20. package/dist/index.cjs +76 -119
  21. package/dist/index.d.cts +12 -64
  22. package/dist/index.d.ts +12 -64
  23. package/dist/index.js +23 -167
  24. package/dist/nextjs/chunk-U4HXENF7.js +231 -0
  25. package/dist/nextjs/components.cjs +190 -122
  26. package/dist/nextjs/components.d.cts +33 -2
  27. package/dist/nextjs/components.d.ts +33 -2
  28. package/dist/nextjs/components.js +132 -4
  29. package/dist/nextjs/graphql.cjs +1315 -0
  30. package/dist/nextjs/graphql.d.cts +361 -0
  31. package/dist/nextjs/graphql.d.ts +361 -0
  32. package/dist/nextjs/graphql.js +1218 -0
  33. package/dist/nextjs/index.cjs +126 -6
  34. package/dist/nextjs/index.d.cts +53 -147
  35. package/dist/nextjs/index.d.ts +53 -147
  36. package/dist/nextjs/index.js +113 -203
  37. package/dist/nextjs/server-CjpELYvq.d.cts +156 -0
  38. package/dist/nextjs/server-CjpELYvq.d.ts +156 -0
  39. package/dist/nuxt/chunk-P6ENNAEE.js +417 -0
  40. package/dist/nuxt/graphql.cjs +990 -0
  41. package/dist/nuxt/graphql.d.cts +228 -0
  42. package/dist/nuxt/graphql.d.ts +228 -0
  43. package/dist/nuxt/graphql.js +894 -0
  44. package/dist/nuxt/index.cjs +14 -1
  45. package/dist/nuxt/index.d.cts +3 -148
  46. package/dist/nuxt/index.d.ts +3 -148
  47. package/dist/nuxt/index.js +15 -391
  48. package/dist/nuxt/server-8v6OHbsw.d.cts +158 -0
  49. package/dist/nuxt/server-8v6OHbsw.d.ts +158 -0
  50. package/dist/pixel-DwW7cXJm.d.cts +49 -0
  51. package/dist/pixel-DwW7cXJm.d.ts +49 -0
  52. package/dist/react/components.cjs +88 -120
  53. package/dist/react/components.d.cts +2 -8
  54. package/dist/react/components.d.ts +2 -8
  55. package/dist/react/components.js +88 -120
  56. package/dist/server-Brr1UgtW.d.ts +59 -0
  57. package/dist/server-DJuS3YU0.d.cts +59 -0
  58. package/dist/sveltekit/chunk-FSARVMG5.js +413 -0
  59. package/dist/sveltekit/graphql.cjs +996 -0
  60. package/dist/sveltekit/graphql.d.cts +233 -0
  61. package/dist/sveltekit/graphql.d.ts +233 -0
  62. package/dist/sveltekit/graphql.js +900 -0
  63. package/dist/sveltekit/index.cjs +14 -1
  64. package/dist/sveltekit/index.d.cts +3 -88
  65. package/dist/sveltekit/index.d.ts +3 -88
  66. package/dist/sveltekit/index.js +15 -387
  67. package/dist/sveltekit/server-CO847mEV.d.cts +98 -0
  68. package/dist/sveltekit/server-CO847mEV.d.ts +98 -0
  69. package/dist/{pixel-B3oQNbZ3.d.cts → types-BYH9XqmN.d.cts} +4 -58
  70. package/dist/{pixel-B3oQNbZ3.d.ts → types-BYH9XqmN.d.ts} +4 -58
  71. package/package.json +31 -1
@@ -30,12 +30,13 @@ __export(nuxt_exports, {
30
30
  getPreprUUID: () => getPreprUUID,
31
31
  getToolbarProps: () => getToolbarProps,
32
32
  handlePreprRequest: () => handlePreprRequest,
33
+ isPersonalizedResponse: () => isPersonalizedResponse,
33
34
  validatePreprToken: () => validatePreprToken
34
35
  });
35
36
  module.exports = __toCommonJS(nuxt_exports);
36
37
 
37
38
  // src/version.ts
38
- var VERSION = "0.5.0-rc.2";
39
+ var VERSION = "0.6.0-alpha.0";
39
40
 
40
41
  // src/core/constants.ts
41
42
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -229,6 +230,10 @@ function processFrameworkRequest(request, opts, framework) {
229
230
  });
230
231
  }
231
232
  }
233
+ if (requestHeaders.has("Prepr-Segments")) {
234
+ requestHeaders.delete("Prepr-Customer-Id");
235
+ requestHeaders.delete("Prepr-Customer-Id-Created");
236
+ }
232
237
  return { requestHeaders, responseCookies };
233
238
  }
234
239
 
@@ -442,6 +447,13 @@ function getPreprHeaders(headers) {
442
447
  async function getToolbarProps(headers, token, features) {
443
448
  return getToolbarPropsFromHeaders(headers, token, features);
444
449
  }
450
+
451
+ // src/core/personalization.ts
452
+ var PERSONALIZED_VARY = /\bPrepr-(Segments|ABTesting)/i;
453
+ function isPersonalizedResponse(source) {
454
+ const headers = "headers" in source ? source.headers : source;
455
+ return PERSONALIZED_VARY.test(headers.get("vary") ?? "");
456
+ }
445
457
  // Annotate the CommonJS export names for ESM import in node:
446
458
  0 && (module.exports = {
447
459
  PREPR_CONTEXT_KEY,
@@ -454,5 +466,6 @@ async function getToolbarProps(headers, token, features) {
454
466
  getPreprUUID,
455
467
  getToolbarProps,
456
468
  handlePreprRequest,
469
+ isPersonalizedResponse,
457
470
  validatePreprToken
458
471
  });
@@ -1,131 +1,5 @@
1
- interface PreprSegment {
2
- readonly _id: string;
3
- readonly name: string;
4
- }
5
- /**
6
- * Per-feature config. `false` is shorthand for `{ enabled: false }`; the object
7
- * form leaves room for per-feature options later without a breaking change.
8
- */
9
- type PreprFeatureConfig = boolean | {
10
- enabled?: boolean;
11
- };
12
- /** Toolkit features a consumer can turn off. Omitted keys stay enabled. */
13
- interface PreprFeatures {
14
- /** Segment picker, `Prepr-Segments` header and its cookie/query param. */
15
- segments?: PreprFeatureConfig;
16
- /** A/B variant switch, `Prepr-ABtesting` header and its cookie/query param. */
17
- abTesting?: PreprFeatureConfig;
18
- /**
19
- * The site's own click-to-edit: the toolbar's Edit mode control, the stega
20
- * tooltip/overlay and the close-edit pill.
21
- *
22
- * NOT a security control, and NOT the Prepr editor's live preview. When the
23
- * site is framed by the editor, `prepr:initVE` still activates edit mode —
24
- * that is the CMS operating inside its own iframe, not an affordance offered
25
- * to visitors. See iframe-bridge.ts.
26
- */
27
- editMode?: PreprFeatureConfig;
28
- }
29
- interface PreprToolbarProps {
30
- /**
31
- * Server-resolved active segment. Optional: omit it with `segments`
32
- * disabled, where there is nothing to resolve. When enabled and omitted,
33
- * the persisted cookie is used.
34
- */
35
- readonly activeSegment?: string | null;
36
- /**
37
- * Server-resolved active A/B variant. Optional: omit it with `abTesting`
38
- * disabled, where there is nothing to resolve. When enabled and omitted,
39
- * the persisted cookie is used.
40
- */
41
- readonly activeVariant?: string | null;
42
- /** Available segments to personalize on. */
43
- readonly segments?: readonly PreprSegment[];
44
- /** @deprecated Alias for `segments`. */
45
- readonly data?: readonly PreprSegment[];
46
- }
47
- type PreprVariant = 'A' | 'B';
48
- interface PreprHeaders {
49
- readonly 'prepr-customer-id'?: string;
50
- readonly 'Prepr-Segments'?: string;
51
- readonly 'Prepr-ABtesting'?: PreprVariant;
52
- readonly 'Prepr-Preview-Bar'?: 'true';
53
- readonly 'Prepr-Context-utm_source'?: string;
54
- readonly 'Prepr-Context-utm_medium'?: string;
55
- readonly 'Prepr-Context-utm_term'?: string;
56
- readonly 'Prepr-Context-utm_content'?: string;
57
- readonly 'Prepr-Context-utm_campaign'?: string;
58
- readonly 'Prepr-Context-initial_referral'?: string;
59
- readonly 'Prepr-Visitor-IP'?: string;
60
- readonly 'Prepr-Hubspot-Id'?: string;
61
- readonly 'Prepr-Customer-Id-Created'?: 'true';
62
- /** The visitor's browser User-Agent, used by the API for device detection. */
63
- readonly 'User-Agent'?: string;
64
- /** @deprecated Never populated; the visitor's value is under `User-Agent`. */
65
- readonly 'Prepr-User-Agent'?: string;
66
- readonly 'Prepr-Package'?: string;
67
- }
68
- type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR';
69
-
70
- interface PreprMiddlewareOptions {
71
- /** Enable preview-mode headers (segment/AB overrides, preview bar). */
72
- preview?: boolean;
73
- /**
74
- * Disable features app-wide. Pass the same object you give the toolbar: a
75
- * disabled feature injects no header and persists no cookie, whatever the
76
- * request carries.
77
- */
78
- features?: PreprFeatures;
79
- /** Override the version reported in Prepr-Package (mainly for tests). */
80
- version?: string;
81
- }
82
-
83
- /**
84
- * Structural stand-in for h3's `H3Event`, so this package needs no build-time
85
- * dependency on `h3`/`nuxt`. Only the fields `handlePreprRequest` uses.
86
- *
87
- * `node` ties this to Nitro's Node.js preset (the Nuxt default). Edge presets
88
- * expose no raw Node request/response to fold headers back onto.
89
- */
90
- interface H3EventLike {
91
- node: {
92
- req: {
93
- url?: string;
94
- headers: Record<string, string | string[] | undefined>;
95
- };
96
- res: {
97
- /** Node's `ServerResponse.appendHeader` (Node >= 18.3). */
98
- appendHeader(name: string, value: string): unknown;
99
- };
100
- };
101
- /** Request-scoped bag shared with route handlers and Vue via `useRequestEvent`. */
102
- context: Record<string, unknown>;
103
- }
104
- /** Key under which `handlePreprRequest` mirrors the forwarded Prepr `Headers`. */
105
- declare const PREPR_CONTEXT_KEY = "prepr";
106
- /**
107
- * Nuxt server middleware entry point — call it inside `defineEventHandler` in
108
- * `server/middleware/prepr.ts`:
109
- *
110
- * ```ts
111
- * export default defineEventHandler(event => {
112
- * handlePreprRequest(event, { preview: import.meta.dev });
113
- * });
114
- * ```
115
- *
116
- * The computed Prepr headers are folded back onto the raw Node request
117
- * (lowercased, as Node stores them), which is what `useRequestHeaders()` and
118
- * downstream handlers read. The same `Headers` are mirrored onto
119
- * `event.context.prepr` for callers who prefer an explicit read
120
- * (`getPreprHeadersFromEvent`). Cookies are appended to the response.
121
- */
122
- declare function handlePreprRequest(event: H3EventLike, options?: PreprMiddlewareOptions): void;
123
- /**
124
- * Personalization headers to forward to your GraphQL fetch, from the h3 event.
125
- * Empty `Headers` if `handlePreprRequest` did not run, so the result is always
126
- * safe to spread.
127
- */
128
- declare function getPreprHeadersFromEvent(event: Pick<H3EventLike, 'context'>): Headers;
1
+ import { P as PreprHeaders, b as PreprFeatures, c as PreprToolbarProps } from './server-8v6OHbsw.cjs';
2
+ export { H as H3EventLike, d as PREPR_CONTEXT_KEY, a as PreprError, e as PreprMiddlewareOptions, f as extractAccessToken, g as getPreprHeadersFromEvent, h as handlePreprRequest, i as isPersonalizedResponse, v as validatePreprToken } from './server-8v6OHbsw.cjs';
129
3
 
130
4
  /** Returns the Prepr Customer ID from the request headers. */
131
5
  declare function getPreprUUID(headers: Headers): string | null;
@@ -142,23 +16,4 @@ declare function getPreprHeaders(headers: Headers): PreprHeaders;
142
16
  */
143
17
  declare function getToolbarProps(headers: Headers, token: string, features?: PreprFeatures): Promise<PreprToolbarProps>;
144
18
 
145
- /** Error thrown by the token helpers and the segments fetch. */
146
- declare class PreprError extends Error {
147
- readonly code: PreprErrorCode;
148
- readonly context?: string | undefined;
149
- readonly originalError?: Error | undefined;
150
- constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
151
- }
152
- /**
153
- * Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
154
- * @throws PreprError if the token is missing or not an HTTPS URL.
155
- */
156
- declare function validatePreprToken(token: string): void;
157
- /**
158
- * Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
159
- * -> `abc123`.
160
- * @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
161
- */
162
- declare function extractAccessToken(graphqlUrl: string): string;
163
-
164
- export { type H3EventLike, PREPR_CONTEXT_KEY, PreprError, type PreprMiddlewareOptions, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprHeadersFromEvent, getPreprUUID, getToolbarProps, handlePreprRequest, validatePreprToken };
19
+ export { getActiveSegment, getActiveVariant, getPreprHeaders, getPreprUUID, getToolbarProps };
@@ -1,131 +1,5 @@
1
- interface PreprSegment {
2
- readonly _id: string;
3
- readonly name: string;
4
- }
5
- /**
6
- * Per-feature config. `false` is shorthand for `{ enabled: false }`; the object
7
- * form leaves room for per-feature options later without a breaking change.
8
- */
9
- type PreprFeatureConfig = boolean | {
10
- enabled?: boolean;
11
- };
12
- /** Toolkit features a consumer can turn off. Omitted keys stay enabled. */
13
- interface PreprFeatures {
14
- /** Segment picker, `Prepr-Segments` header and its cookie/query param. */
15
- segments?: PreprFeatureConfig;
16
- /** A/B variant switch, `Prepr-ABtesting` header and its cookie/query param. */
17
- abTesting?: PreprFeatureConfig;
18
- /**
19
- * The site's own click-to-edit: the toolbar's Edit mode control, the stega
20
- * tooltip/overlay and the close-edit pill.
21
- *
22
- * NOT a security control, and NOT the Prepr editor's live preview. When the
23
- * site is framed by the editor, `prepr:initVE` still activates edit mode —
24
- * that is the CMS operating inside its own iframe, not an affordance offered
25
- * to visitors. See iframe-bridge.ts.
26
- */
27
- editMode?: PreprFeatureConfig;
28
- }
29
- interface PreprToolbarProps {
30
- /**
31
- * Server-resolved active segment. Optional: omit it with `segments`
32
- * disabled, where there is nothing to resolve. When enabled and omitted,
33
- * the persisted cookie is used.
34
- */
35
- readonly activeSegment?: string | null;
36
- /**
37
- * Server-resolved active A/B variant. Optional: omit it with `abTesting`
38
- * disabled, where there is nothing to resolve. When enabled and omitted,
39
- * the persisted cookie is used.
40
- */
41
- readonly activeVariant?: string | null;
42
- /** Available segments to personalize on. */
43
- readonly segments?: readonly PreprSegment[];
44
- /** @deprecated Alias for `segments`. */
45
- readonly data?: readonly PreprSegment[];
46
- }
47
- type PreprVariant = 'A' | 'B';
48
- interface PreprHeaders {
49
- readonly 'prepr-customer-id'?: string;
50
- readonly 'Prepr-Segments'?: string;
51
- readonly 'Prepr-ABtesting'?: PreprVariant;
52
- readonly 'Prepr-Preview-Bar'?: 'true';
53
- readonly 'Prepr-Context-utm_source'?: string;
54
- readonly 'Prepr-Context-utm_medium'?: string;
55
- readonly 'Prepr-Context-utm_term'?: string;
56
- readonly 'Prepr-Context-utm_content'?: string;
57
- readonly 'Prepr-Context-utm_campaign'?: string;
58
- readonly 'Prepr-Context-initial_referral'?: string;
59
- readonly 'Prepr-Visitor-IP'?: string;
60
- readonly 'Prepr-Hubspot-Id'?: string;
61
- readonly 'Prepr-Customer-Id-Created'?: 'true';
62
- /** The visitor's browser User-Agent, used by the API for device detection. */
63
- readonly 'User-Agent'?: string;
64
- /** @deprecated Never populated; the visitor's value is under `User-Agent`. */
65
- readonly 'Prepr-User-Agent'?: string;
66
- readonly 'Prepr-Package'?: string;
67
- }
68
- type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR';
69
-
70
- interface PreprMiddlewareOptions {
71
- /** Enable preview-mode headers (segment/AB overrides, preview bar). */
72
- preview?: boolean;
73
- /**
74
- * Disable features app-wide. Pass the same object you give the toolbar: a
75
- * disabled feature injects no header and persists no cookie, whatever the
76
- * request carries.
77
- */
78
- features?: PreprFeatures;
79
- /** Override the version reported in Prepr-Package (mainly for tests). */
80
- version?: string;
81
- }
82
-
83
- /**
84
- * Structural stand-in for h3's `H3Event`, so this package needs no build-time
85
- * dependency on `h3`/`nuxt`. Only the fields `handlePreprRequest` uses.
86
- *
87
- * `node` ties this to Nitro's Node.js preset (the Nuxt default). Edge presets
88
- * expose no raw Node request/response to fold headers back onto.
89
- */
90
- interface H3EventLike {
91
- node: {
92
- req: {
93
- url?: string;
94
- headers: Record<string, string | string[] | undefined>;
95
- };
96
- res: {
97
- /** Node's `ServerResponse.appendHeader` (Node >= 18.3). */
98
- appendHeader(name: string, value: string): unknown;
99
- };
100
- };
101
- /** Request-scoped bag shared with route handlers and Vue via `useRequestEvent`. */
102
- context: Record<string, unknown>;
103
- }
104
- /** Key under which `handlePreprRequest` mirrors the forwarded Prepr `Headers`. */
105
- declare const PREPR_CONTEXT_KEY = "prepr";
106
- /**
107
- * Nuxt server middleware entry point — call it inside `defineEventHandler` in
108
- * `server/middleware/prepr.ts`:
109
- *
110
- * ```ts
111
- * export default defineEventHandler(event => {
112
- * handlePreprRequest(event, { preview: import.meta.dev });
113
- * });
114
- * ```
115
- *
116
- * The computed Prepr headers are folded back onto the raw Node request
117
- * (lowercased, as Node stores them), which is what `useRequestHeaders()` and
118
- * downstream handlers read. The same `Headers` are mirrored onto
119
- * `event.context.prepr` for callers who prefer an explicit read
120
- * (`getPreprHeadersFromEvent`). Cookies are appended to the response.
121
- */
122
- declare function handlePreprRequest(event: H3EventLike, options?: PreprMiddlewareOptions): void;
123
- /**
124
- * Personalization headers to forward to your GraphQL fetch, from the h3 event.
125
- * Empty `Headers` if `handlePreprRequest` did not run, so the result is always
126
- * safe to spread.
127
- */
128
- declare function getPreprHeadersFromEvent(event: Pick<H3EventLike, 'context'>): Headers;
1
+ import { P as PreprHeaders, b as PreprFeatures, c as PreprToolbarProps } from './server-8v6OHbsw.js';
2
+ export { H as H3EventLike, d as PREPR_CONTEXT_KEY, a as PreprError, e as PreprMiddlewareOptions, f as extractAccessToken, g as getPreprHeadersFromEvent, h as handlePreprRequest, i as isPersonalizedResponse, v as validatePreprToken } from './server-8v6OHbsw.js';
129
3
 
130
4
  /** Returns the Prepr Customer ID from the request headers. */
131
5
  declare function getPreprUUID(headers: Headers): string | null;
@@ -142,23 +16,4 @@ declare function getPreprHeaders(headers: Headers): PreprHeaders;
142
16
  */
143
17
  declare function getToolbarProps(headers: Headers, token: string, features?: PreprFeatures): Promise<PreprToolbarProps>;
144
18
 
145
- /** Error thrown by the token helpers and the segments fetch. */
146
- declare class PreprError extends Error {
147
- readonly code: PreprErrorCode;
148
- readonly context?: string | undefined;
149
- readonly originalError?: Error | undefined;
150
- constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
151
- }
152
- /**
153
- * Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
154
- * @throws PreprError if the token is missing or not an HTTPS URL.
155
- */
156
- declare function validatePreprToken(token: string): void;
157
- /**
158
- * Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
159
- * -> `abc123`.
160
- * @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
161
- */
162
- declare function extractAccessToken(graphqlUrl: string): string;
163
-
164
- export { type H3EventLike, PREPR_CONTEXT_KEY, PreprError, type PreprMiddlewareOptions, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprHeadersFromEvent, getPreprUUID, getToolbarProps, handlePreprRequest, validatePreprToken };
19
+ export { getActiveSegment, getActiveVariant, getPreprHeaders, getPreprUUID, getToolbarProps };