@preprio/toolkit 0.1.0-beta.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 +903 -0
- package/dist/astro/index.cjs +319 -0
- package/dist/astro/index.d.cts +76 -0
- package/dist/astro/index.d.ts +76 -0
- package/dist/astro/index.js +287 -0
- package/dist/chunk-H7HVQSSF.js +2081 -0
- package/dist/index.cjs +2393 -0
- package/dist/index.d.cts +179 -0
- package/dist/index.d.ts +179 -0
- package/dist/index.js +306 -0
- package/dist/nextjs/components.cjs +2087 -0
- package/dist/nextjs/components.d.cts +17 -0
- package/dist/nextjs/components.d.ts +17 -0
- package/dist/nextjs/components.js +34 -0
- package/dist/nextjs/image-loader.cjs +122 -0
- package/dist/nextjs/image-loader.d.cts +10 -0
- package/dist/nextjs/image-loader.d.ts +10 -0
- package/dist/nextjs/image-loader.js +101 -0
- package/dist/nextjs/index.cjs +386 -0
- package/dist/nextjs/index.d.cts +90 -0
- package/dist/nextjs/index.d.ts +90 -0
- package/dist/nextjs/index.js +352 -0
- package/dist/pixel-DVSJQC0K.d.cts +94 -0
- package/dist/pixel-DVSJQC0K.d.ts +94 -0
- package/dist/sveltekit/index.cjs +371 -0
- package/dist/sveltekit/index.d.cts +117 -0
- package/dist/sveltekit/index.d.ts +117 -0
- package/dist/sveltekit/index.js +334 -0
- package/package.json +93 -0
- package/src/astro/PreprToolbar.astro +53 -0
- package/src/astro/PreprTrackingPixel.astro +48 -0
- package/src/sveltekit/PreprToolbar.svelte +21 -0
- package/src/sveltekit/PreprTrackingPixel.svelte +22 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { L as Locale, P as PreprSegment, a as PreprVariant, b as PreprToolbarProps, c as PreprToolbarOptions, d as PreprErrorCode, e as PreprHeaders } from './pixel-DVSJQC0K.cjs';
|
|
2
|
+
export { f as PreprEnvironment, g as PreprEventType, h as PreprHeaderName, i as PreprPixelConfig, l as loadTrackingPixel, s as setTrackingParam, t as trackEvent } from './pixel-DVSJQC0K.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Package version; must match package.json. Lives in its own leaf module so
|
|
6
|
+
* core can import it without cycling through the barrel.
|
|
7
|
+
*/
|
|
8
|
+
declare const VERSION = "0.1.0-beta.1";
|
|
9
|
+
|
|
10
|
+
interface ToolbarState {
|
|
11
|
+
locale: Locale;
|
|
12
|
+
segments: PreprSegment[];
|
|
13
|
+
selectedSegment: string | null;
|
|
14
|
+
selectedVariant: PreprVariant | null;
|
|
15
|
+
editMode: boolean;
|
|
16
|
+
previewMode: boolean;
|
|
17
|
+
toolbarOpen: boolean;
|
|
18
|
+
isIframe: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface ToolbarStore {
|
|
21
|
+
get(): ToolbarState;
|
|
22
|
+
set(patch: Partial<ToolbarState>): void;
|
|
23
|
+
subscribe(fn: (s: ToolbarState) => void): () => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Framework-free store for toolbar state. Deliberately pure: it holds state and
|
|
27
|
+
* notifies subscribers, nothing else. Cookies, postMessage and reloads are side
|
|
28
|
+
* effects owned by the mount controller (create-toolbar.ts).
|
|
29
|
+
*/
|
|
30
|
+
declare function createToolbarStore(initial?: Partial<ToolbarState>): ToolbarStore;
|
|
31
|
+
|
|
32
|
+
/** Strip the invisible stega characters. Safe on unencoded strings. */
|
|
33
|
+
declare function stegaClean(text: string): string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Navigation seam that keeps the controller framework-free. Next.js/Astro inject
|
|
37
|
+
* their router here; the default uses `window.location`.
|
|
38
|
+
*/
|
|
39
|
+
interface PreprNavigationAdapter {
|
|
40
|
+
navigate(url: string): void;
|
|
41
|
+
currentPath(): string;
|
|
42
|
+
/**
|
|
43
|
+
* Refresh after a preview-mode toggle. Defaults to `window.location.reload()`;
|
|
44
|
+
* SPA routers can pass a soft refresh like Next.js `router.refresh()`.
|
|
45
|
+
*/
|
|
46
|
+
reload?(): void;
|
|
47
|
+
}
|
|
48
|
+
interface CreatePreprToolbarOptions {
|
|
49
|
+
props: PreprToolbarProps;
|
|
50
|
+
options?: PreprToolbarOptions;
|
|
51
|
+
navigation?: PreprNavigationAdapter;
|
|
52
|
+
}
|
|
53
|
+
interface PreprToolbarController {
|
|
54
|
+
destroy(): void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Composition root: hydrates state from props + cookies, mounts
|
|
58
|
+
* `<prepr-toolbar>`, and wires the collaborators — the change handler
|
|
59
|
+
* (toolbar-change-handler.ts), the stega click-to-edit controller, the
|
|
60
|
+
* auto-clean pass and the editor bridge (iframe-bridge.ts).
|
|
61
|
+
*/
|
|
62
|
+
declare function createPreprToolbar(opts: CreatePreprToolbarOptions): PreprToolbarController;
|
|
63
|
+
|
|
64
|
+
type Translate = (key: string) => string;
|
|
65
|
+
declare const HTMLElementBase: typeof HTMLElement;
|
|
66
|
+
/**
|
|
67
|
+
* `<prepr-toolbar>` — the toolbar UI, in a shadow root so its CSS neither leaks
|
|
68
|
+
* into nor is affected by the host page. Markup is Preact JSX in `panel.tsx`.
|
|
69
|
+
*
|
|
70
|
+
* This element is the controller: it owns interaction state (listbox open,
|
|
71
|
+
* keyboard nav, tooltip positioning, outside-click) and writes back ONLY via
|
|
72
|
+
* `store.set(...)`. Cookies/postMessage/navigation belong to create-toolbar.ts,
|
|
73
|
+
* which subscribes to the same store.
|
|
74
|
+
*
|
|
75
|
+
* State reaches `<Panel>` as a prop rather than internal Preact state, so every
|
|
76
|
+
* `render()` diffs synchronously against the shadow root.
|
|
77
|
+
*/
|
|
78
|
+
declare class PreprToolbarElement extends HTMLElementBase {
|
|
79
|
+
private store;
|
|
80
|
+
private t;
|
|
81
|
+
private unsubscribe;
|
|
82
|
+
private listboxOpen;
|
|
83
|
+
private readonly handlers;
|
|
84
|
+
/** Idempotent: calling connect twice re-wires to the (possibly new) store. */
|
|
85
|
+
connect(store: ToolbarStore, t: Translate): void;
|
|
86
|
+
disconnectedCallback(): void;
|
|
87
|
+
private get state();
|
|
88
|
+
private set;
|
|
89
|
+
private renderPanel;
|
|
90
|
+
private tip;
|
|
91
|
+
/**
|
|
92
|
+
* Show the tooltip for a trigger carrying `data-tooltip-key`. Applied
|
|
93
|
+
* imperatively rather than through the Preact tree: hover/focus never write
|
|
94
|
+
* to the store, so no re-render can clobber it in between. Positioning is
|
|
95
|
+
* centered above the trigger, clamped to the viewport with 8px padding,
|
|
96
|
+
* arrow tracking the trigger center.
|
|
97
|
+
*/
|
|
98
|
+
private showTooltip;
|
|
99
|
+
private hideTooltip;
|
|
100
|
+
private segmentButton;
|
|
101
|
+
private optionsList;
|
|
102
|
+
private onDocumentMouseDown;
|
|
103
|
+
private onListboxKeydown;
|
|
104
|
+
private optionEls;
|
|
105
|
+
private activeOptionIndex;
|
|
106
|
+
private moveActiveOption;
|
|
107
|
+
private toggleListbox;
|
|
108
|
+
private chooseSegment;
|
|
109
|
+
}
|
|
110
|
+
/** Idempotent, including across bundles that each try to define the element. */
|
|
111
|
+
declare function definePreprToolbar(): void;
|
|
112
|
+
|
|
113
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
114
|
+
declare class PreprError extends Error {
|
|
115
|
+
readonly code: PreprErrorCode;
|
|
116
|
+
readonly context?: string | undefined;
|
|
117
|
+
readonly originalError?: Error | undefined;
|
|
118
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
119
|
+
}
|
|
120
|
+
/** Prepr Customer ID, or null if the middleware did not run for this request. */
|
|
121
|
+
declare function getPreprUUIDFromHeaders(headers: Headers): string | null;
|
|
122
|
+
/** Active segment ID, or null when no segment is selected. */
|
|
123
|
+
declare function getActiveSegmentFromHeaders(headers: Headers): string | null;
|
|
124
|
+
/** Active A/B variant (`'A'`/`'B'`), or null when no test is running. */
|
|
125
|
+
declare function getActiveVariantFromHeaders(headers: Headers): string | null;
|
|
126
|
+
/**
|
|
127
|
+
* Collect the Prepr headers into a `PreprHeaders`. Lookups are case-insensitive
|
|
128
|
+
* per the Fetch spec, but the returned keys use the declared casing.
|
|
129
|
+
*/
|
|
130
|
+
declare function getPreprHeadersFromHeaders(headers: Headers): PreprHeaders;
|
|
131
|
+
/**
|
|
132
|
+
* Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
|
|
133
|
+
* @throws PreprError if the token is missing or not an HTTPS URL.
|
|
134
|
+
*/
|
|
135
|
+
declare function validatePreprToken(token: string): void;
|
|
136
|
+
/**
|
|
137
|
+
* Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
|
|
138
|
+
* -> `abc123`.
|
|
139
|
+
* @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
|
|
140
|
+
*/
|
|
141
|
+
declare function extractAccessToken(graphqlUrl: string): string;
|
|
142
|
+
/**
|
|
143
|
+
* Fetches the environment's segments from the Prepr API.
|
|
144
|
+
*
|
|
145
|
+
* `token` must be a Prepr GraphQL URL with scope 'segments'.
|
|
146
|
+
* @throws PreprError if the token is invalid or the request fails.
|
|
147
|
+
*/
|
|
148
|
+
declare function getPreprEnvironmentSegments(token: string): Promise<PreprSegment[]>;
|
|
149
|
+
/**
|
|
150
|
+
* Never throws: a failed segments fetch yields an empty list so the toolbar
|
|
151
|
+
* still renders. The list is returned under both `segments` and the deprecated
|
|
152
|
+
* `data` alias.
|
|
153
|
+
*/
|
|
154
|
+
declare function getToolbarPropsFromHeaders(headers: Headers, token: string): Promise<PreprToolbarProps>;
|
|
155
|
+
|
|
156
|
+
interface CookieSpec {
|
|
157
|
+
name: string;
|
|
158
|
+
value: string;
|
|
159
|
+
maxAge: number;
|
|
160
|
+
path: string;
|
|
161
|
+
}
|
|
162
|
+
interface PreprMiddlewareResult {
|
|
163
|
+
requestHeaders: Headers;
|
|
164
|
+
responseCookies: CookieSpec[];
|
|
165
|
+
}
|
|
166
|
+
interface PreprMiddlewareOptions {
|
|
167
|
+
/** Enable preview-mode headers (segment/AB overrides, preview bar). */
|
|
168
|
+
preview?: boolean;
|
|
169
|
+
/** Override the version reported in Prepr-Package (mainly for tests). */
|
|
170
|
+
version?: string;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Compute the headers to forward to the Prepr API and the cookies to persist,
|
|
174
|
+
* from a standard `Request` alone. Pure and framework-free — the Next.js/Astro
|
|
175
|
+
* wrappers apply `requestHeaders`/`responseCookies` to their own objects.
|
|
176
|
+
*/
|
|
177
|
+
declare function processPreprRequest(request: Request, opts?: PreprMiddlewareOptions): PreprMiddlewareResult;
|
|
178
|
+
|
|
179
|
+
export { type CookieSpec, type CreatePreprToolbarOptions, PreprError, PreprErrorCode, PreprHeaders, type PreprMiddlewareOptions, type PreprMiddlewareResult, type PreprNavigationAdapter, PreprSegment, type PreprToolbarController, PreprToolbarElement, PreprToolbarOptions, PreprToolbarProps, PreprVariant, type ToolbarState, type ToolbarStore, VERSION, createPreprToolbar, createToolbarStore, definePreprToolbar, extractAccessToken, getActiveSegmentFromHeaders, getActiveVariantFromHeaders, getPreprEnvironmentSegments, getPreprHeadersFromHeaders, getPreprUUIDFromHeaders, getToolbarPropsFromHeaders, processPreprRequest, stegaClean, validatePreprToken };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { L as Locale, P as PreprSegment, a as PreprVariant, b as PreprToolbarProps, c as PreprToolbarOptions, d as PreprErrorCode, e as PreprHeaders } from './pixel-DVSJQC0K.js';
|
|
2
|
+
export { f as PreprEnvironment, g as PreprEventType, h as PreprHeaderName, i as PreprPixelConfig, l as loadTrackingPixel, s as setTrackingParam, t as trackEvent } from './pixel-DVSJQC0K.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Package version; must match package.json. Lives in its own leaf module so
|
|
6
|
+
* core can import it without cycling through the barrel.
|
|
7
|
+
*/
|
|
8
|
+
declare const VERSION = "0.1.0-beta.1";
|
|
9
|
+
|
|
10
|
+
interface ToolbarState {
|
|
11
|
+
locale: Locale;
|
|
12
|
+
segments: PreprSegment[];
|
|
13
|
+
selectedSegment: string | null;
|
|
14
|
+
selectedVariant: PreprVariant | null;
|
|
15
|
+
editMode: boolean;
|
|
16
|
+
previewMode: boolean;
|
|
17
|
+
toolbarOpen: boolean;
|
|
18
|
+
isIframe: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface ToolbarStore {
|
|
21
|
+
get(): ToolbarState;
|
|
22
|
+
set(patch: Partial<ToolbarState>): void;
|
|
23
|
+
subscribe(fn: (s: ToolbarState) => void): () => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Framework-free store for toolbar state. Deliberately pure: it holds state and
|
|
27
|
+
* notifies subscribers, nothing else. Cookies, postMessage and reloads are side
|
|
28
|
+
* effects owned by the mount controller (create-toolbar.ts).
|
|
29
|
+
*/
|
|
30
|
+
declare function createToolbarStore(initial?: Partial<ToolbarState>): ToolbarStore;
|
|
31
|
+
|
|
32
|
+
/** Strip the invisible stega characters. Safe on unencoded strings. */
|
|
33
|
+
declare function stegaClean(text: string): string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Navigation seam that keeps the controller framework-free. Next.js/Astro inject
|
|
37
|
+
* their router here; the default uses `window.location`.
|
|
38
|
+
*/
|
|
39
|
+
interface PreprNavigationAdapter {
|
|
40
|
+
navigate(url: string): void;
|
|
41
|
+
currentPath(): string;
|
|
42
|
+
/**
|
|
43
|
+
* Refresh after a preview-mode toggle. Defaults to `window.location.reload()`;
|
|
44
|
+
* SPA routers can pass a soft refresh like Next.js `router.refresh()`.
|
|
45
|
+
*/
|
|
46
|
+
reload?(): void;
|
|
47
|
+
}
|
|
48
|
+
interface CreatePreprToolbarOptions {
|
|
49
|
+
props: PreprToolbarProps;
|
|
50
|
+
options?: PreprToolbarOptions;
|
|
51
|
+
navigation?: PreprNavigationAdapter;
|
|
52
|
+
}
|
|
53
|
+
interface PreprToolbarController {
|
|
54
|
+
destroy(): void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Composition root: hydrates state from props + cookies, mounts
|
|
58
|
+
* `<prepr-toolbar>`, and wires the collaborators — the change handler
|
|
59
|
+
* (toolbar-change-handler.ts), the stega click-to-edit controller, the
|
|
60
|
+
* auto-clean pass and the editor bridge (iframe-bridge.ts).
|
|
61
|
+
*/
|
|
62
|
+
declare function createPreprToolbar(opts: CreatePreprToolbarOptions): PreprToolbarController;
|
|
63
|
+
|
|
64
|
+
type Translate = (key: string) => string;
|
|
65
|
+
declare const HTMLElementBase: typeof HTMLElement;
|
|
66
|
+
/**
|
|
67
|
+
* `<prepr-toolbar>` — the toolbar UI, in a shadow root so its CSS neither leaks
|
|
68
|
+
* into nor is affected by the host page. Markup is Preact JSX in `panel.tsx`.
|
|
69
|
+
*
|
|
70
|
+
* This element is the controller: it owns interaction state (listbox open,
|
|
71
|
+
* keyboard nav, tooltip positioning, outside-click) and writes back ONLY via
|
|
72
|
+
* `store.set(...)`. Cookies/postMessage/navigation belong to create-toolbar.ts,
|
|
73
|
+
* which subscribes to the same store.
|
|
74
|
+
*
|
|
75
|
+
* State reaches `<Panel>` as a prop rather than internal Preact state, so every
|
|
76
|
+
* `render()` diffs synchronously against the shadow root.
|
|
77
|
+
*/
|
|
78
|
+
declare class PreprToolbarElement extends HTMLElementBase {
|
|
79
|
+
private store;
|
|
80
|
+
private t;
|
|
81
|
+
private unsubscribe;
|
|
82
|
+
private listboxOpen;
|
|
83
|
+
private readonly handlers;
|
|
84
|
+
/** Idempotent: calling connect twice re-wires to the (possibly new) store. */
|
|
85
|
+
connect(store: ToolbarStore, t: Translate): void;
|
|
86
|
+
disconnectedCallback(): void;
|
|
87
|
+
private get state();
|
|
88
|
+
private set;
|
|
89
|
+
private renderPanel;
|
|
90
|
+
private tip;
|
|
91
|
+
/**
|
|
92
|
+
* Show the tooltip for a trigger carrying `data-tooltip-key`. Applied
|
|
93
|
+
* imperatively rather than through the Preact tree: hover/focus never write
|
|
94
|
+
* to the store, so no re-render can clobber it in between. Positioning is
|
|
95
|
+
* centered above the trigger, clamped to the viewport with 8px padding,
|
|
96
|
+
* arrow tracking the trigger center.
|
|
97
|
+
*/
|
|
98
|
+
private showTooltip;
|
|
99
|
+
private hideTooltip;
|
|
100
|
+
private segmentButton;
|
|
101
|
+
private optionsList;
|
|
102
|
+
private onDocumentMouseDown;
|
|
103
|
+
private onListboxKeydown;
|
|
104
|
+
private optionEls;
|
|
105
|
+
private activeOptionIndex;
|
|
106
|
+
private moveActiveOption;
|
|
107
|
+
private toggleListbox;
|
|
108
|
+
private chooseSegment;
|
|
109
|
+
}
|
|
110
|
+
/** Idempotent, including across bundles that each try to define the element. */
|
|
111
|
+
declare function definePreprToolbar(): void;
|
|
112
|
+
|
|
113
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
114
|
+
declare class PreprError extends Error {
|
|
115
|
+
readonly code: PreprErrorCode;
|
|
116
|
+
readonly context?: string | undefined;
|
|
117
|
+
readonly originalError?: Error | undefined;
|
|
118
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
119
|
+
}
|
|
120
|
+
/** Prepr Customer ID, or null if the middleware did not run for this request. */
|
|
121
|
+
declare function getPreprUUIDFromHeaders(headers: Headers): string | null;
|
|
122
|
+
/** Active segment ID, or null when no segment is selected. */
|
|
123
|
+
declare function getActiveSegmentFromHeaders(headers: Headers): string | null;
|
|
124
|
+
/** Active A/B variant (`'A'`/`'B'`), or null when no test is running. */
|
|
125
|
+
declare function getActiveVariantFromHeaders(headers: Headers): string | null;
|
|
126
|
+
/**
|
|
127
|
+
* Collect the Prepr headers into a `PreprHeaders`. Lookups are case-insensitive
|
|
128
|
+
* per the Fetch spec, but the returned keys use the declared casing.
|
|
129
|
+
*/
|
|
130
|
+
declare function getPreprHeadersFromHeaders(headers: Headers): PreprHeaders;
|
|
131
|
+
/**
|
|
132
|
+
* Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
|
|
133
|
+
* @throws PreprError if the token is missing or not an HTTPS URL.
|
|
134
|
+
*/
|
|
135
|
+
declare function validatePreprToken(token: string): void;
|
|
136
|
+
/**
|
|
137
|
+
* Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
|
|
138
|
+
* -> `abc123`.
|
|
139
|
+
* @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
|
|
140
|
+
*/
|
|
141
|
+
declare function extractAccessToken(graphqlUrl: string): string;
|
|
142
|
+
/**
|
|
143
|
+
* Fetches the environment's segments from the Prepr API.
|
|
144
|
+
*
|
|
145
|
+
* `token` must be a Prepr GraphQL URL with scope 'segments'.
|
|
146
|
+
* @throws PreprError if the token is invalid or the request fails.
|
|
147
|
+
*/
|
|
148
|
+
declare function getPreprEnvironmentSegments(token: string): Promise<PreprSegment[]>;
|
|
149
|
+
/**
|
|
150
|
+
* Never throws: a failed segments fetch yields an empty list so the toolbar
|
|
151
|
+
* still renders. The list is returned under both `segments` and the deprecated
|
|
152
|
+
* `data` alias.
|
|
153
|
+
*/
|
|
154
|
+
declare function getToolbarPropsFromHeaders(headers: Headers, token: string): Promise<PreprToolbarProps>;
|
|
155
|
+
|
|
156
|
+
interface CookieSpec {
|
|
157
|
+
name: string;
|
|
158
|
+
value: string;
|
|
159
|
+
maxAge: number;
|
|
160
|
+
path: string;
|
|
161
|
+
}
|
|
162
|
+
interface PreprMiddlewareResult {
|
|
163
|
+
requestHeaders: Headers;
|
|
164
|
+
responseCookies: CookieSpec[];
|
|
165
|
+
}
|
|
166
|
+
interface PreprMiddlewareOptions {
|
|
167
|
+
/** Enable preview-mode headers (segment/AB overrides, preview bar). */
|
|
168
|
+
preview?: boolean;
|
|
169
|
+
/** Override the version reported in Prepr-Package (mainly for tests). */
|
|
170
|
+
version?: string;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Compute the headers to forward to the Prepr API and the cookies to persist,
|
|
174
|
+
* from a standard `Request` alone. Pure and framework-free — the Next.js/Astro
|
|
175
|
+
* wrappers apply `requestHeaders`/`responseCookies` to their own objects.
|
|
176
|
+
*/
|
|
177
|
+
declare function processPreprRequest(request: Request, opts?: PreprMiddlewareOptions): PreprMiddlewareResult;
|
|
178
|
+
|
|
179
|
+
export { type CookieSpec, type CreatePreprToolbarOptions, PreprError, PreprErrorCode, PreprHeaders, type PreprMiddlewareOptions, type PreprMiddlewareResult, type PreprNavigationAdapter, PreprSegment, type PreprToolbarController, PreprToolbarElement, PreprToolbarOptions, PreprToolbarProps, PreprVariant, type ToolbarState, type ToolbarStore, VERSION, createPreprToolbar, createToolbarStore, definePreprToolbar, extractAccessToken, getActiveSegmentFromHeaders, getActiveVariantFromHeaders, getPreprEnvironmentSegments, getPreprHeadersFromHeaders, getPreprUUIDFromHeaders, getToolbarPropsFromHeaders, processPreprRequest, stegaClean, validatePreprToken };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import {
|
|
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
|
+
PreprToolbarElement,
|
|
12
|
+
createPreprToolbar,
|
|
13
|
+
createToolbarStore,
|
|
14
|
+
definePreprToolbar,
|
|
15
|
+
loadTrackingPixel,
|
|
16
|
+
setTrackingParam,
|
|
17
|
+
stegaClean,
|
|
18
|
+
trackEvent
|
|
19
|
+
} from "./chunk-H7HVQSSF.js";
|
|
20
|
+
|
|
21
|
+
// src/version.ts
|
|
22
|
+
var VERSION = "0.1.0-beta.1";
|
|
23
|
+
|
|
24
|
+
// src/core/server.ts
|
|
25
|
+
var PreprError = class extends Error {
|
|
26
|
+
constructor(message, code, context, originalError) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.code = code;
|
|
29
|
+
this.context = context;
|
|
30
|
+
this.originalError = originalError;
|
|
31
|
+
this.name = "PreprError";
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
function getPreprUUIDFromHeaders(headers) {
|
|
35
|
+
return headers.get("prepr-customer-id");
|
|
36
|
+
}
|
|
37
|
+
function getActiveSegmentFromHeaders(headers) {
|
|
38
|
+
return headers.get("Prepr-Segments");
|
|
39
|
+
}
|
|
40
|
+
function getActiveVariantFromHeaders(headers) {
|
|
41
|
+
return headers.get("Prepr-ABtesting");
|
|
42
|
+
}
|
|
43
|
+
var PREPR_HEADER_KEYS = [
|
|
44
|
+
"prepr-customer-id",
|
|
45
|
+
"Prepr-Segments",
|
|
46
|
+
"Prepr-ABtesting",
|
|
47
|
+
"Prepr-Preview-Bar",
|
|
48
|
+
"Prepr-Context-utm_source",
|
|
49
|
+
"Prepr-Context-utm_medium",
|
|
50
|
+
"Prepr-Context-utm_term",
|
|
51
|
+
"Prepr-Context-utm_content",
|
|
52
|
+
"Prepr-Context-utm_campaign",
|
|
53
|
+
"Prepr-Context-initial_referral",
|
|
54
|
+
"Prepr-Visitor-IP",
|
|
55
|
+
"Prepr-Hubspot-Id",
|
|
56
|
+
"Prepr-Customer-Id-Created",
|
|
57
|
+
"Prepr-User-Agent"
|
|
58
|
+
];
|
|
59
|
+
function getPreprHeadersFromHeaders(headers) {
|
|
60
|
+
const preprHeaders = {};
|
|
61
|
+
const copy = (key) => {
|
|
62
|
+
const value = headers.get(key);
|
|
63
|
+
if (value !== null) {
|
|
64
|
+
preprHeaders[key] = value;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
for (const key of PREPR_HEADER_KEYS) {
|
|
68
|
+
copy(key);
|
|
69
|
+
}
|
|
70
|
+
return preprHeaders;
|
|
71
|
+
}
|
|
72
|
+
function validatePreprToken(token) {
|
|
73
|
+
if (!token) {
|
|
74
|
+
throw new PreprError("Token is required", "MISSING_TOKEN", "validatePreprToken");
|
|
75
|
+
}
|
|
76
|
+
if (!token.startsWith("https://")) {
|
|
77
|
+
throw new PreprError(
|
|
78
|
+
"Token must be a valid HTTPS URL",
|
|
79
|
+
"INVALID_TOKEN",
|
|
80
|
+
"validatePreprToken"
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function extractAccessToken(graphqlUrl) {
|
|
85
|
+
let url;
|
|
86
|
+
try {
|
|
87
|
+
url = new URL(graphqlUrl);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw new PreprError(
|
|
90
|
+
"Token must be a valid URL",
|
|
91
|
+
"INVALID_TOKEN",
|
|
92
|
+
"extractAccessToken",
|
|
93
|
+
error instanceof Error ? error : new Error(String(error))
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (url.hostname !== "graphql.prepr.io") {
|
|
97
|
+
throw new PreprError(
|
|
98
|
+
"Token must be a graphql.prepr.io URL",
|
|
99
|
+
"INVALID_TOKEN",
|
|
100
|
+
"extractAccessToken"
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
const pathParts = url.pathname.split("/");
|
|
104
|
+
const token = pathParts[pathParts.length - 1];
|
|
105
|
+
if (!token) {
|
|
106
|
+
throw new PreprError(
|
|
107
|
+
"Token URL is missing an access token segment",
|
|
108
|
+
"INVALID_TOKEN",
|
|
109
|
+
"extractAccessToken"
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return token;
|
|
113
|
+
}
|
|
114
|
+
async function getPreprEnvironmentSegments(token) {
|
|
115
|
+
validatePreprToken(token);
|
|
116
|
+
try {
|
|
117
|
+
const response = await fetch(token, {
|
|
118
|
+
headers: {
|
|
119
|
+
"User-Agent": `Prepr-Preview-Bar/${VERSION}`,
|
|
120
|
+
"Content-Type": "application/json"
|
|
121
|
+
},
|
|
122
|
+
method: "POST",
|
|
123
|
+
body: JSON.stringify({
|
|
124
|
+
query: `{
|
|
125
|
+
_Segments {
|
|
126
|
+
_id
|
|
127
|
+
name
|
|
128
|
+
}
|
|
129
|
+
}`
|
|
130
|
+
})
|
|
131
|
+
});
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
throw new PreprError(
|
|
134
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
135
|
+
"HTTP_ERROR",
|
|
136
|
+
"getPreprEnvironmentSegments"
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
const json = await response.json();
|
|
140
|
+
if (!json || !json.data || !json.data._Segments) {
|
|
141
|
+
throw new PreprError(
|
|
142
|
+
"Invalid response format from Prepr API",
|
|
143
|
+
"INVALID_RESPONSE",
|
|
144
|
+
"getPreprEnvironmentSegments"
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return json.data._Segments;
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error instanceof PreprError) {
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
throw new PreprError(
|
|
153
|
+
"Failed to fetch segments from Prepr API",
|
|
154
|
+
"FETCH_ERROR",
|
|
155
|
+
"getPreprEnvironmentSegments",
|
|
156
|
+
error instanceof Error ? error : new Error(String(error))
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async function getToolbarPropsFromHeaders(headers, token) {
|
|
161
|
+
let segments = [];
|
|
162
|
+
try {
|
|
163
|
+
segments = await getPreprEnvironmentSegments(token);
|
|
164
|
+
} catch {
|
|
165
|
+
segments = [];
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
activeSegment: getActiveSegmentFromHeaders(headers),
|
|
169
|
+
activeVariant: getActiveVariantFromHeaders(headers),
|
|
170
|
+
segments,
|
|
171
|
+
data: segments
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/core/middleware.ts
|
|
176
|
+
var UTM_PARAMS = [
|
|
177
|
+
"utm_source",
|
|
178
|
+
"utm_medium",
|
|
179
|
+
"utm_term",
|
|
180
|
+
"utm_content",
|
|
181
|
+
"utm_campaign"
|
|
182
|
+
];
|
|
183
|
+
function parseCookies(request) {
|
|
184
|
+
const cookies = /* @__PURE__ */ new Map();
|
|
185
|
+
const header = request.headers.get("cookie");
|
|
186
|
+
if (!header) return cookies;
|
|
187
|
+
for (const part of header.split(";")) {
|
|
188
|
+
const eq = part.indexOf("=");
|
|
189
|
+
if (eq === -1) continue;
|
|
190
|
+
const name = part.slice(0, eq).trim();
|
|
191
|
+
const value = part.slice(eq + 1).trim();
|
|
192
|
+
if (!name) continue;
|
|
193
|
+
cookies.set(name, decodeURIComponent(value));
|
|
194
|
+
}
|
|
195
|
+
return cookies;
|
|
196
|
+
}
|
|
197
|
+
function processPreprRequest(request, opts) {
|
|
198
|
+
const requestHeaders = new Headers(request.headers);
|
|
199
|
+
const responseCookies = [];
|
|
200
|
+
const requestCookies = parseCookies(request);
|
|
201
|
+
const searchParams = new URL(request.url).searchParams;
|
|
202
|
+
for (const key of UTM_PARAMS) {
|
|
203
|
+
const value = searchParams.get(key);
|
|
204
|
+
if (value !== null) {
|
|
205
|
+
requestHeaders.set(`Prepr-Context-${key}`, value);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const referer = request.headers.get("referer");
|
|
209
|
+
if (referer) {
|
|
210
|
+
requestHeaders.set("Prepr-Context-initial_referral", referer);
|
|
211
|
+
}
|
|
212
|
+
const userAgent = request.headers.get("user-agent");
|
|
213
|
+
if (userAgent) {
|
|
214
|
+
requestHeaders.set("Prepr-User-Agent", userAgent);
|
|
215
|
+
}
|
|
216
|
+
requestHeaders.set("Prepr-Package", `@preprio/toolkit@${opts?.version ?? VERSION}`);
|
|
217
|
+
const ip = request.headers.get("Cf-Connecting-Ip") ?? request.headers.get("x-real-ip");
|
|
218
|
+
if (ip) {
|
|
219
|
+
requestHeaders.set("Prepr-Visitor-IP", ip);
|
|
220
|
+
}
|
|
221
|
+
const hutkCookie = requestCookies.get(COOKIE_HUBSPOT);
|
|
222
|
+
if (hutkCookie) {
|
|
223
|
+
requestHeaders.set("Prepr-Hubspot-Id", hutkCookie);
|
|
224
|
+
}
|
|
225
|
+
let uid = requestCookies.get(COOKIE_UID);
|
|
226
|
+
if (!uid) {
|
|
227
|
+
uid = crypto.randomUUID();
|
|
228
|
+
responseCookies.push({
|
|
229
|
+
name: COOKIE_UID,
|
|
230
|
+
value: uid,
|
|
231
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
232
|
+
path: "/"
|
|
233
|
+
});
|
|
234
|
+
requestHeaders.set("Prepr-Customer-Id-Created", "true");
|
|
235
|
+
}
|
|
236
|
+
requestHeaders.set("Prepr-Customer-Id", uid);
|
|
237
|
+
if (!opts?.preview) {
|
|
238
|
+
return { requestHeaders, responseCookies };
|
|
239
|
+
}
|
|
240
|
+
const hasPreviewParams = searchParams.has(PARAM_SEGMENT) || searchParams.has(PARAM_VARIANT);
|
|
241
|
+
if (!hasPreviewParams) {
|
|
242
|
+
const previewModeCookie = requestCookies.get(COOKIE_PREVIEW_MODE);
|
|
243
|
+
const previewModeEnabled = previewModeCookie !== "false";
|
|
244
|
+
if (!previewModeEnabled) {
|
|
245
|
+
return { requestHeaders, responseCookies };
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
requestHeaders.set("Prepr-Preview-Bar", "true");
|
|
249
|
+
const isLivePreview = searchParams.get(PARAM_HIDE_BAR) === "true";
|
|
250
|
+
if (!isLivePreview) {
|
|
251
|
+
const segmentCookie = requestCookies.get(COOKIE_SEGMENT);
|
|
252
|
+
if (segmentCookie) {
|
|
253
|
+
requestHeaders.set("Prepr-Segments", segmentCookie);
|
|
254
|
+
}
|
|
255
|
+
const abCookie = requestCookies.get(COOKIE_VARIANT);
|
|
256
|
+
if (abCookie) {
|
|
257
|
+
requestHeaders.set("Prepr-ABtesting", abCookie);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const previewAb = searchParams.get(PARAM_VARIANT);
|
|
261
|
+
if (previewAb !== null) {
|
|
262
|
+
requestHeaders.set("Prepr-ABtesting", previewAb);
|
|
263
|
+
if (!isLivePreview) {
|
|
264
|
+
responseCookies.push({
|
|
265
|
+
name: COOKIE_VARIANT,
|
|
266
|
+
value: previewAb,
|
|
267
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
268
|
+
path: "/"
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const previewSegment = searchParams.get(PARAM_SEGMENT);
|
|
273
|
+
if (previewSegment !== null) {
|
|
274
|
+
requestHeaders.set("Prepr-Segments", previewSegment);
|
|
275
|
+
if (!isLivePreview) {
|
|
276
|
+
responseCookies.push({
|
|
277
|
+
name: COOKIE_SEGMENT,
|
|
278
|
+
value: previewSegment,
|
|
279
|
+
maxAge: ONE_YEAR_SECONDS,
|
|
280
|
+
path: "/"
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return { requestHeaders, responseCookies };
|
|
285
|
+
}
|
|
286
|
+
export {
|
|
287
|
+
PreprError,
|
|
288
|
+
PreprToolbarElement,
|
|
289
|
+
VERSION,
|
|
290
|
+
createPreprToolbar,
|
|
291
|
+
createToolbarStore,
|
|
292
|
+
definePreprToolbar,
|
|
293
|
+
extractAccessToken,
|
|
294
|
+
getActiveSegmentFromHeaders,
|
|
295
|
+
getActiveVariantFromHeaders,
|
|
296
|
+
getPreprEnvironmentSegments,
|
|
297
|
+
getPreprHeadersFromHeaders,
|
|
298
|
+
getPreprUUIDFromHeaders,
|
|
299
|
+
getToolbarPropsFromHeaders,
|
|
300
|
+
loadTrackingPixel,
|
|
301
|
+
processPreprRequest,
|
|
302
|
+
setTrackingParam,
|
|
303
|
+
stegaClean,
|
|
304
|
+
trackEvent,
|
|
305
|
+
validatePreprToken
|
|
306
|
+
};
|