@preprio/toolkit 0.6.0-alpha.9 → 1.0.0-rc.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 (116) hide show
  1. package/README.md +111 -1363
  2. package/dist/astro/index.cjs +1555 -182
  3. package/dist/astro/index.d.cts +201 -1
  4. package/dist/astro/index.d.ts +201 -1
  5. package/dist/astro/index.js +1557 -29
  6. package/dist/{chunk-5QIWVDP2.js → chunk-33GWVTRJ.js} +1 -1
  7. package/dist/chunk-A2J7R4LR.js +256 -0
  8. package/dist/{chunk-WQBKQG4R.js → chunk-DSJ22P6J.js} +6 -1
  9. package/dist/chunk-KM2GME62.js +1255 -0
  10. package/dist/chunk-UTKH7DOI.js +58 -0
  11. package/dist/chunk-V7MCHPE7.js +85 -0
  12. package/dist/{chunk-J66UOCK6.js → chunk-WBMN4L7G.js} +1680 -579
  13. package/dist/chunk-Y7JWV7WV.js +374 -0
  14. package/dist/codegen/index.cjs +82 -0
  15. package/dist/codegen/index.d.cts +70 -0
  16. package/dist/codegen/index.d.ts +70 -0
  17. package/dist/codegen/index.js +64 -0
  18. package/dist/index.cjs +3729 -553
  19. package/dist/index.d.cts +403 -19
  20. package/dist/index.d.ts +403 -19
  21. package/dist/index.js +345 -24
  22. package/dist/live-preview-HCBNBC6C.js +19 -0
  23. package/dist/nextjs/components.cjs +1872 -754
  24. package/dist/nextjs/components.d.cts +5 -51
  25. package/dist/nextjs/components.d.ts +5 -51
  26. package/dist/nextjs/components.js +8 -299
  27. package/dist/nextjs/index.cjs +1228 -181
  28. package/dist/nextjs/index.d.cts +247 -92
  29. package/dist/nextjs/index.d.ts +247 -92
  30. package/dist/nextjs/index.js +1629 -111
  31. package/dist/nextjs/live-action.cjs +298 -0
  32. package/dist/nextjs/live-action.d.cts +12 -0
  33. package/dist/nextjs/live-action.d.ts +12 -0
  34. package/dist/nextjs/live-action.js +68 -0
  35. package/dist/nextjs/live.cjs +2010 -0
  36. package/dist/nextjs/live.d.cts +54 -0
  37. package/dist/nextjs/live.d.ts +54 -0
  38. package/dist/nextjs/live.js +287 -0
  39. package/dist/nuxt/chunk-5RBZPRQN.js +350 -0
  40. package/dist/nuxt/chunk-LUKVPOJ2.js +32 -0
  41. package/dist/nuxt/index.cjs +15 -16
  42. package/dist/nuxt/index.d.cts +167 -6
  43. package/dist/nuxt/index.d.ts +167 -6
  44. package/dist/nuxt/index.js +402 -15
  45. package/dist/nuxt/live-preview-KO4IKWTX.js +1234 -0
  46. package/dist/nuxt/live.cjs +1928 -0
  47. package/dist/nuxt/live.d.cts +45 -0
  48. package/dist/nuxt/live.d.ts +45 -0
  49. package/dist/nuxt/live.js +216 -0
  50. package/dist/{types-BYH9XqmN.d.cts → pixel-DqU7NvgW.d.cts} +102 -5
  51. package/dist/{types-BYH9XqmN.d.ts → pixel-DqU7NvgW.d.ts} +102 -5
  52. package/dist/react/components.cjs +1865 -480
  53. package/dist/react/components.d.cts +43 -8
  54. package/dist/react/components.d.ts +43 -8
  55. package/dist/react/components.js +1865 -480
  56. package/dist/sveltekit/chunk-5RBZPRQN.js +350 -0
  57. package/dist/sveltekit/chunk-LUKVPOJ2.js +32 -0
  58. package/dist/sveltekit/index.cjs +15 -16
  59. package/dist/sveltekit/index.d.cts +111 -10
  60. package/dist/sveltekit/index.d.ts +111 -10
  61. package/dist/sveltekit/index.js +398 -15
  62. package/dist/sveltekit/live-preview-KO4IKWTX.js +1234 -0
  63. package/dist/sveltekit/live.cjs +1940 -0
  64. package/dist/sveltekit/live.d.cts +61 -0
  65. package/dist/sveltekit/live.d.ts +61 -0
  66. package/dist/sveltekit/live.js +220 -0
  67. package/package.json +132 -71
  68. package/src/astro/PreprToolbar.astro +7 -1
  69. package/src/astro/PreprTrackingPixel.astro +2 -4
  70. package/src/nuxt/PreprToolbar.vue +12 -1
  71. package/src/nuxt/PreprTrackingPixel.vue +2 -4
  72. package/src/sveltekit/PreprToolbar.svelte +17 -2
  73. package/src/sveltekit/PreprTrackingPixel.svelte +2 -4
  74. package/dist/astro/chunk-UN4JADNU.js +0 -173
  75. package/dist/astro/graphql.cjs +0 -1078
  76. package/dist/astro/graphql.d.cts +0 -300
  77. package/dist/astro/graphql.d.ts +0 -300
  78. package/dist/astro/graphql.js +0 -984
  79. package/dist/astro/index-XT_z-RYT.d.cts +0 -130
  80. package/dist/astro/index-XT_z-RYT.d.ts +0 -130
  81. package/dist/chunk-3NCEVJJ3.js +0 -10
  82. package/dist/chunk-S5RWVMDJ.js +0 -148
  83. package/dist/chunk-XL4Y7FGW.js +0 -499
  84. package/dist/graphql/codegen.cjs +0 -557
  85. package/dist/graphql/codegen.d.cts +0 -23
  86. package/dist/graphql/codegen.d.ts +0 -23
  87. package/dist/graphql/codegen.js +0 -99
  88. package/dist/graphql.cjs +0 -1020
  89. package/dist/graphql.d.cts +0 -335
  90. package/dist/graphql.d.ts +0 -335
  91. package/dist/graphql.js +0 -448
  92. package/dist/nextjs/chunk-CMV6ZP6E.js +0 -549
  93. package/dist/nextjs/graphql.cjs +0 -1836
  94. package/dist/nextjs/graphql.d.cts +0 -498
  95. package/dist/nextjs/graphql.d.ts +0 -498
  96. package/dist/nextjs/graphql.js +0 -1422
  97. package/dist/nextjs/server-BL-sqZvu.d.cts +0 -156
  98. package/dist/nextjs/server-BL-sqZvu.d.ts +0 -156
  99. package/dist/nuxt/chunk-XWOABVMW.js +0 -417
  100. package/dist/nuxt/graphql.cjs +0 -1085
  101. package/dist/nuxt/graphql.d.cts +0 -297
  102. package/dist/nuxt/graphql.d.ts +0 -297
  103. package/dist/nuxt/graphql.js +0 -985
  104. package/dist/nuxt/server-8v6OHbsw.d.cts +0 -158
  105. package/dist/nuxt/server-8v6OHbsw.d.ts +0 -158
  106. package/dist/pixel-DwW7cXJm.d.cts +0 -49
  107. package/dist/pixel-DwW7cXJm.d.ts +0 -49
  108. package/dist/server-Brr1UgtW.d.ts +0 -59
  109. package/dist/server-DJuS3YU0.d.cts +0 -59
  110. package/dist/sveltekit/chunk-3S5RA6G5.js +0 -413
  111. package/dist/sveltekit/graphql.cjs +0 -1091
  112. package/dist/sveltekit/graphql.d.cts +0 -302
  113. package/dist/sveltekit/graphql.d.ts +0 -302
  114. package/dist/sveltekit/graphql.js +0 -991
  115. package/dist/sveltekit/server-CO847mEV.d.cts +0 -98
  116. package/dist/sveltekit/server-CO847mEV.d.ts +0 -98
@@ -1,300 +0,0 @@
1
- import { DocumentNode } from 'graphql';
2
- import { P as PreprHeaders, a as PreprErrorCode, A as AstroLikeContext } from './index-XT_z-RYT.cjs';
3
- export { i as isPersonalizedResponse } from './index-XT_z-RYT.cjs';
4
-
5
- /** Prepr's variant id for visitors outside every targeted segment. */
6
- declare const PREPR_DEFAULT_VARIANT = "ALL_OTHER_USERS";
7
-
8
- /** The variant ids Prepr chose for one visitor, per personalization group. */
9
- type PreprDecisions = ReadonlyMap<string, readonly string[]>;
10
- /**
11
- * Variant ids per group from a decision response, de-duplicated and in
12
- * Prepr's order. A visitor in several segments can get more than one variant
13
- * of the same group, so each entry is a list.
14
- */
15
- declare function decisionsFrom(data: unknown): PreprDecisions;
16
- interface ResolveVariantsOptions {
17
- /**
18
- * The visitor's per-group variants (from a decision query). Without it,
19
- * personalization groups show their default variant.
20
- */
21
- decisions?: PreprDecisions;
22
- /** The visitor's A/B bucket. Without it, A/B groups use the decision, then `A`. */
23
- bucket?: string;
24
- }
25
- /**
26
- * Keeps, in every list of a variants query result, only the variants this
27
- * visitor gets, at any depth (page stacks, nested stacks, personalized
28
- * buttons). Items outside a group are kept as they are.
29
- */
30
- declare function resolveVariants<T>(data: T, options?: ResolveVariantsOptions): T;
31
-
32
- interface FieldInfo {
33
- /** Named type of the field, without list/non-null wrappers. */
34
- type: string;
35
- /** The field accepts Prepr's `personalize` argument (a stack). */
36
- personalizable: boolean;
37
- }
38
- interface TypeInfo {
39
- fields: Record<string, FieldInfo>;
40
- /** Members of a union or implementations of an interface. */
41
- possibleTypes: string[];
42
- hasContext: boolean;
43
- }
44
- /** The slice of the schema the variant transforms need. */
45
- interface PreprSchemaInfo {
46
- queryType: string;
47
- types: Record<string, TypeInfo>;
48
- }
49
-
50
- /** A run of stack items that renders the same way: see {@link splitStack}. */
51
- interface PreprStackBlock<T> {
52
- key: string;
53
- items: T[];
54
- /**
55
- * Rendered position of the block's first item: a group (whichever variant
56
- * shows) takes one position, any other item one each. Use it for anything
57
- * that depends on page position, like which section owns the h1.
58
- */
59
- index: number;
60
- /**
61
- * Some item in the block (or nested inside one) is a segment variant, so
62
- * the block depends on the visitor and is decided per request.
63
- */
64
- personalized: boolean;
65
- /**
66
- * Some item in the block (or nested inside one) is an A/B variant. Without
67
- * a known bucket, such a block renders once per bucket and the visitor's
68
- * bucket is picked in the browser before first paint.
69
- */
70
- abTest: boolean;
71
- }
72
- /**
73
- * Splits a variants-query stack into blocks. Each personalization or A/B
74
- * group becomes one block (a variant can hold a different number of items
75
- * than the default); consecutive items that never vary are merged.
76
- */
77
- declare function splitStack<T>(items: readonly T[]): PreprStackBlock<T>[];
78
-
79
- /** The attribute Prepr's tracking pixel reads impressions and conversions from. */
80
- interface PreprVariantProps {
81
- 'data-prepr-variant-key'?: string;
82
- }
83
- /**
84
- * Attributes that let Prepr's tracking pixel count impressions of this item's
85
- * variant, for an item from a query run with `variants: true`. Spread them on
86
- * the element that shows the item; an item outside any personalization or A/B
87
- * group gets none.
88
- *
89
- * Items rendered by the Next.js `PreprStack` are tagged for you. Reach for this
90
- * for items nested inside them (cards, buttons, models in a section) and in
91
- * frameworks without `PreprStack`.
92
- *
93
- * @example
94
- * <article {...preprVariantProps(card)}>…</article>
95
- */
96
- declare function preprVariantProps(item: unknown): PreprVariantProps;
97
-
98
- /**
99
- * A GraphQL document accepted by `query`: a plain string, graphql-codegen's
100
- * `TypedDocumentString` (`documentMode: 'string'`), or any `DocumentNode`,
101
- * including codegen's default `TypedDocumentNode` output.
102
- *
103
- * The phantom `__apiType` field is how codegen attaches result and variable
104
- * types; matching it structurally keeps `@graphql-typed-document-node/core`
105
- * out of this package.
106
- */
107
- type PreprDocument<TResult, TVariables> = string | ({
108
- __apiType?: (variables: TVariables) => TResult;
109
- } & (String | DocumentNode));
110
- /**
111
- * How stega markers are stripped from the response:
112
- * - `'urls'`: only from values under URL-like keys (`url`, `slug`, `link`,
113
- * `href`), where markers break links. Text keeps its markers for
114
- * click-to-edit.
115
- * - `'all'`: from every string.
116
- * - `'none'`: never.
117
- */
118
- type PreprStegaMode = 'urls' | 'all' | 'none';
119
- interface PreprClientOptions {
120
- /** Full Prepr GraphQL URL, `https://graphql.prepr.io/<token>`. */
121
- url: string;
122
- /**
123
- * Fetch implementation. Defaults to the global `fetch`, read at call time so
124
- * framework-patched versions (such as Next.js's) are used.
125
- */
126
- fetch?: typeof fetch;
127
- /**
128
- * Preview mode: every query skips the HTTP cache so editors see each edit,
129
- * and stega defaults to `'urls'` so click-to-edit keeps working.
130
- */
131
- preview?: boolean;
132
- /** Default: `'urls'` when `preview` is true, otherwise `'all'`. */
133
- stega?: PreprStegaMode;
134
- /**
135
- * Send queries as a GET with the request body base64-encoded in a
136
- * `Fastly-Query` header, which Prepr's edge serves from the nearest point
137
- * of presence (about half the latency of a POST in our measurements).
138
- * Queries too large for a header still go as a POST. Off by default: it
139
- * relies on Prepr edge behavior that is not part of the documented API.
140
- */
141
- fastlyQuery?: boolean;
142
- /**
143
- * @internal Fetch options for the one-time schema introspection, so a
144
- * framework wrapper can put it in its data cache.
145
- */
146
- schemaInit?: RequestInit;
147
- /**
148
- * Headers sent with every request, such as a header a proxy in front of the
149
- * API requires. They are fixed when the client is created, so they cannot
150
- * vary per visitor and do not stop responses from being cached. Put
151
- * per-request values in the query's `headers` instead.
152
- */
153
- headers?: HeadersInit;
154
- }
155
- interface PreprQueryOptions {
156
- /**
157
- * Prepr headers to forward, typically from `getPreprHeaders`. Passing any
158
- * headers object, even an empty one, makes the request `no-store`: the
159
- * response may be personalized and must not be shared between visitors.
160
- */
161
- headers?: PreprHeaders | HeadersInit;
162
- /**
163
- * Extra fetch options such as `signal`. `method` and `body` are set by the
164
- * client. `init.headers` are merged like `headers`, and also make the
165
- * request `no-store`.
166
- */
167
- init?: RequestInit;
168
- /** Per-query fetch, overriding the client's (e.g. SvelteKit's `event.fetch`). */
169
- fetch?: typeof fetch;
170
- /**
171
- * Fetch every variant instead of the visitor's: `personalize: false` is
172
- * added to every personalizable field in the query, at any depth, and each
173
- * item gets its `_context` (under `_preprContext`). The result is the same
174
- * for every visitor, so it can be cached; pass it to `resolveVariants` with
175
- * the visitor's `decide()` result to get what they see.
176
- *
177
- * The client learns which fields are personalizable from one schema
178
- * introspection request, made on first use.
179
- */
180
- variants?: boolean;
181
- }
182
- /** Options for `decide()`. */
183
- interface PreprDecideOptions extends Omit<PreprQueryOptions, 'variants'> {
184
- /**
185
- * The `variants: true` result this decision is for (the whole `data`
186
- * object). The decision query then asks only about the fields that hold
187
- * segment personalization groups, and is skipped entirely when there are
188
- * none, e.g. on pages with only A/B tests or no personalization.
189
- */
190
- from?: unknown;
191
- }
192
- interface PreprQueryResult<TResult> {
193
- data: TResult;
194
- /**
195
- * True when Prepr reports, through the response's `Vary` header, that the
196
- * queried fields hold segment variants or A/B tests.
197
- */
198
- personalized: boolean;
199
- }
200
-
201
- /**
202
- * The schema facts the query rewrites need, from `loadPreprSchema`. Pass it
203
- * along; its shape is not part of the public API.
204
- */
205
- type PreprSchema = PreprSchemaInfo;
206
- /**
207
- * Your query, asking for every personalization and A/B variant: what
208
- * `query(..., { variants: true })` sends, as a string for your own GraphQL
209
- * client. The result is the same for every visitor, so cache it like any
210
- * other content query, and render it with `resolveVariants` or `PreprStack`.
211
- *
212
- * A query without personalizable fields comes back unchanged (minified).
213
- */
214
- declare function toVariantsQuery(document: PreprDocument<unknown, unknown> | string, schema: PreprSchema): string;
215
- /**
216
- * The per-visitor decision query for the same query: send it with the
217
- * visitor's Prepr headers, never cached, and pass the response's `data` to
218
- * `decisionsFrom`.
219
- *
220
- * Pass the variants result as `from` whenever you have it: the query then
221
- * asks only about the lists that hold segment groups on this page, which is
222
- * a few hundred bytes instead of the whole page and markedly faster for a
223
- * visitor Prepr has not seen before. Without `from` it asks about every
224
- * personalizable list. `topLevelOnly` stops at the first personalizable list
225
- * on each path, for middleware that decides only the top of a stack.
226
- *
227
- * Null when there is nothing to decide: send no request then.
228
- */
229
- declare function toDecisionQuery(document: PreprDocument<unknown, unknown> | string, schema: PreprSchema, options?: {
230
- from?: unknown;
231
- topLevelOnly?: boolean;
232
- }): string | null;
233
-
234
- /**
235
- * Query options for frameworks without an implicit request scope.
236
- * `personalize: true` requires the request `event`; the compiler enforces it.
237
- */
238
- type PreprEventQueryOptions<TEvent> = PreprQueryOptions & ({
239
- personalize?: false;
240
- event?: TEvent;
241
- } | {
242
- personalize: true;
243
- event: TEvent;
244
- });
245
- interface PreprEventClient<TEvent> {
246
- /**
247
- * Run a GraphQL query against Prepr. With `personalize: true`, the Prepr
248
- * headers of `event` are forwarded and the request is `no-store`; without
249
- * it the request is identical for every visitor.
250
- *
251
- * @throws PreprError — as `PreprClient.query`, plus `CONTEXT_ERROR` when
252
- * `personalize` is true and no `event` is passed.
253
- */
254
- query<TResult, TVariables>(document: PreprDocument<TResult, TVariables>, variables?: TVariables, options?: PreprEventQueryOptions<TEvent>): Promise<PreprQueryResult<TResult>>;
255
- /**
256
- * Which variant of each group this visitor gets, for the query you render
257
- * with `variants: true`. Forwards the Prepr headers of `event`.
258
- *
259
- * @throws PreprError — `CONTEXT_ERROR` without an `event`.
260
- */
261
- decide<TVariables>(document: PreprDocument<unknown, TVariables>, variables: TVariables | undefined, options: PreprDecideOptions & {
262
- event: TEvent;
263
- }): Promise<PreprDecisions>;
264
- }
265
-
266
- /**
267
- * Loads what `toVariantsQuery` and `toDecisionQuery` need to know about your
268
- * Prepr schema (which fields take `personalize`, which types can hold
269
- * variants) with one introspection request, kept in memory per URL. For a
270
- * site that uses its own GraphQL client; `createPreprClient` does this
271
- * itself.
272
- *
273
- * Load it once at startup or lazily before the first rewrite. Schema changes
274
- * in Prepr take effect after a restart or redeploy.
275
- *
276
- * @throws PreprError when the URL is invalid or the request fails; the URL,
277
- * which holds your access token, is never part of the message.
278
- */
279
- declare function loadPreprSchema(url: string, options?: {
280
- fetch?: typeof fetch;
281
- }): Promise<PreprSchemaInfo>;
282
-
283
- /** Error thrown by the token helpers and the segments fetch. */
284
- declare class PreprError extends Error {
285
- readonly code: PreprErrorCode;
286
- readonly context?: string | undefined;
287
- readonly originalError?: Error | undefined;
288
- constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
289
- }
290
-
291
- /**
292
- * Prepr GraphQL client for Astro. Pass `Astro` (in a page or component) or
293
- * the endpoint or middleware context as `event`. With `personalize: true`,
294
- * the Prepr headers `onPreprRequest` set on the request are forwarded.
295
- *
296
- * Requires the `graphql` package, which graphql-codegen already installs.
297
- */
298
- declare function createPreprClient(options: PreprClientOptions): PreprEventClient<AstroLikeContext>;
299
-
300
- export { AstroLikeContext, PREPR_DEFAULT_VARIANT, type PreprClientOptions, type PreprDecideOptions, type PreprDecisions, type PreprDocument, PreprError, type PreprEventClient, type PreprEventQueryOptions, type PreprQueryResult, type PreprSchema, type PreprStackBlock, type PreprStegaMode, type PreprVariantProps, type ResolveVariantsOptions, createPreprClient, decisionsFrom, loadPreprSchema, preprVariantProps, resolveVariants, splitStack, toDecisionQuery, toVariantsQuery };
@@ -1,300 +0,0 @@
1
- import { DocumentNode } from 'graphql';
2
- import { P as PreprHeaders, a as PreprErrorCode, A as AstroLikeContext } from './index-XT_z-RYT.js';
3
- export { i as isPersonalizedResponse } from './index-XT_z-RYT.js';
4
-
5
- /** Prepr's variant id for visitors outside every targeted segment. */
6
- declare const PREPR_DEFAULT_VARIANT = "ALL_OTHER_USERS";
7
-
8
- /** The variant ids Prepr chose for one visitor, per personalization group. */
9
- type PreprDecisions = ReadonlyMap<string, readonly string[]>;
10
- /**
11
- * Variant ids per group from a decision response, de-duplicated and in
12
- * Prepr's order. A visitor in several segments can get more than one variant
13
- * of the same group, so each entry is a list.
14
- */
15
- declare function decisionsFrom(data: unknown): PreprDecisions;
16
- interface ResolveVariantsOptions {
17
- /**
18
- * The visitor's per-group variants (from a decision query). Without it,
19
- * personalization groups show their default variant.
20
- */
21
- decisions?: PreprDecisions;
22
- /** The visitor's A/B bucket. Without it, A/B groups use the decision, then `A`. */
23
- bucket?: string;
24
- }
25
- /**
26
- * Keeps, in every list of a variants query result, only the variants this
27
- * visitor gets, at any depth (page stacks, nested stacks, personalized
28
- * buttons). Items outside a group are kept as they are.
29
- */
30
- declare function resolveVariants<T>(data: T, options?: ResolveVariantsOptions): T;
31
-
32
- interface FieldInfo {
33
- /** Named type of the field, without list/non-null wrappers. */
34
- type: string;
35
- /** The field accepts Prepr's `personalize` argument (a stack). */
36
- personalizable: boolean;
37
- }
38
- interface TypeInfo {
39
- fields: Record<string, FieldInfo>;
40
- /** Members of a union or implementations of an interface. */
41
- possibleTypes: string[];
42
- hasContext: boolean;
43
- }
44
- /** The slice of the schema the variant transforms need. */
45
- interface PreprSchemaInfo {
46
- queryType: string;
47
- types: Record<string, TypeInfo>;
48
- }
49
-
50
- /** A run of stack items that renders the same way: see {@link splitStack}. */
51
- interface PreprStackBlock<T> {
52
- key: string;
53
- items: T[];
54
- /**
55
- * Rendered position of the block's first item: a group (whichever variant
56
- * shows) takes one position, any other item one each. Use it for anything
57
- * that depends on page position, like which section owns the h1.
58
- */
59
- index: number;
60
- /**
61
- * Some item in the block (or nested inside one) is a segment variant, so
62
- * the block depends on the visitor and is decided per request.
63
- */
64
- personalized: boolean;
65
- /**
66
- * Some item in the block (or nested inside one) is an A/B variant. Without
67
- * a known bucket, such a block renders once per bucket and the visitor's
68
- * bucket is picked in the browser before first paint.
69
- */
70
- abTest: boolean;
71
- }
72
- /**
73
- * Splits a variants-query stack into blocks. Each personalization or A/B
74
- * group becomes one block (a variant can hold a different number of items
75
- * than the default); consecutive items that never vary are merged.
76
- */
77
- declare function splitStack<T>(items: readonly T[]): PreprStackBlock<T>[];
78
-
79
- /** The attribute Prepr's tracking pixel reads impressions and conversions from. */
80
- interface PreprVariantProps {
81
- 'data-prepr-variant-key'?: string;
82
- }
83
- /**
84
- * Attributes that let Prepr's tracking pixel count impressions of this item's
85
- * variant, for an item from a query run with `variants: true`. Spread them on
86
- * the element that shows the item; an item outside any personalization or A/B
87
- * group gets none.
88
- *
89
- * Items rendered by the Next.js `PreprStack` are tagged for you. Reach for this
90
- * for items nested inside them (cards, buttons, models in a section) and in
91
- * frameworks without `PreprStack`.
92
- *
93
- * @example
94
- * <article {...preprVariantProps(card)}>…</article>
95
- */
96
- declare function preprVariantProps(item: unknown): PreprVariantProps;
97
-
98
- /**
99
- * A GraphQL document accepted by `query`: a plain string, graphql-codegen's
100
- * `TypedDocumentString` (`documentMode: 'string'`), or any `DocumentNode`,
101
- * including codegen's default `TypedDocumentNode` output.
102
- *
103
- * The phantom `__apiType` field is how codegen attaches result and variable
104
- * types; matching it structurally keeps `@graphql-typed-document-node/core`
105
- * out of this package.
106
- */
107
- type PreprDocument<TResult, TVariables> = string | ({
108
- __apiType?: (variables: TVariables) => TResult;
109
- } & (String | DocumentNode));
110
- /**
111
- * How stega markers are stripped from the response:
112
- * - `'urls'`: only from values under URL-like keys (`url`, `slug`, `link`,
113
- * `href`), where markers break links. Text keeps its markers for
114
- * click-to-edit.
115
- * - `'all'`: from every string.
116
- * - `'none'`: never.
117
- */
118
- type PreprStegaMode = 'urls' | 'all' | 'none';
119
- interface PreprClientOptions {
120
- /** Full Prepr GraphQL URL, `https://graphql.prepr.io/<token>`. */
121
- url: string;
122
- /**
123
- * Fetch implementation. Defaults to the global `fetch`, read at call time so
124
- * framework-patched versions (such as Next.js's) are used.
125
- */
126
- fetch?: typeof fetch;
127
- /**
128
- * Preview mode: every query skips the HTTP cache so editors see each edit,
129
- * and stega defaults to `'urls'` so click-to-edit keeps working.
130
- */
131
- preview?: boolean;
132
- /** Default: `'urls'` when `preview` is true, otherwise `'all'`. */
133
- stega?: PreprStegaMode;
134
- /**
135
- * Send queries as a GET with the request body base64-encoded in a
136
- * `Fastly-Query` header, which Prepr's edge serves from the nearest point
137
- * of presence (about half the latency of a POST in our measurements).
138
- * Queries too large for a header still go as a POST. Off by default: it
139
- * relies on Prepr edge behavior that is not part of the documented API.
140
- */
141
- fastlyQuery?: boolean;
142
- /**
143
- * @internal Fetch options for the one-time schema introspection, so a
144
- * framework wrapper can put it in its data cache.
145
- */
146
- schemaInit?: RequestInit;
147
- /**
148
- * Headers sent with every request, such as a header a proxy in front of the
149
- * API requires. They are fixed when the client is created, so they cannot
150
- * vary per visitor and do not stop responses from being cached. Put
151
- * per-request values in the query's `headers` instead.
152
- */
153
- headers?: HeadersInit;
154
- }
155
- interface PreprQueryOptions {
156
- /**
157
- * Prepr headers to forward, typically from `getPreprHeaders`. Passing any
158
- * headers object, even an empty one, makes the request `no-store`: the
159
- * response may be personalized and must not be shared between visitors.
160
- */
161
- headers?: PreprHeaders | HeadersInit;
162
- /**
163
- * Extra fetch options such as `signal`. `method` and `body` are set by the
164
- * client. `init.headers` are merged like `headers`, and also make the
165
- * request `no-store`.
166
- */
167
- init?: RequestInit;
168
- /** Per-query fetch, overriding the client's (e.g. SvelteKit's `event.fetch`). */
169
- fetch?: typeof fetch;
170
- /**
171
- * Fetch every variant instead of the visitor's: `personalize: false` is
172
- * added to every personalizable field in the query, at any depth, and each
173
- * item gets its `_context` (under `_preprContext`). The result is the same
174
- * for every visitor, so it can be cached; pass it to `resolveVariants` with
175
- * the visitor's `decide()` result to get what they see.
176
- *
177
- * The client learns which fields are personalizable from one schema
178
- * introspection request, made on first use.
179
- */
180
- variants?: boolean;
181
- }
182
- /** Options for `decide()`. */
183
- interface PreprDecideOptions extends Omit<PreprQueryOptions, 'variants'> {
184
- /**
185
- * The `variants: true` result this decision is for (the whole `data`
186
- * object). The decision query then asks only about the fields that hold
187
- * segment personalization groups, and is skipped entirely when there are
188
- * none, e.g. on pages with only A/B tests or no personalization.
189
- */
190
- from?: unknown;
191
- }
192
- interface PreprQueryResult<TResult> {
193
- data: TResult;
194
- /**
195
- * True when Prepr reports, through the response's `Vary` header, that the
196
- * queried fields hold segment variants or A/B tests.
197
- */
198
- personalized: boolean;
199
- }
200
-
201
- /**
202
- * The schema facts the query rewrites need, from `loadPreprSchema`. Pass it
203
- * along; its shape is not part of the public API.
204
- */
205
- type PreprSchema = PreprSchemaInfo;
206
- /**
207
- * Your query, asking for every personalization and A/B variant: what
208
- * `query(..., { variants: true })` sends, as a string for your own GraphQL
209
- * client. The result is the same for every visitor, so cache it like any
210
- * other content query, and render it with `resolveVariants` or `PreprStack`.
211
- *
212
- * A query without personalizable fields comes back unchanged (minified).
213
- */
214
- declare function toVariantsQuery(document: PreprDocument<unknown, unknown> | string, schema: PreprSchema): string;
215
- /**
216
- * The per-visitor decision query for the same query: send it with the
217
- * visitor's Prepr headers, never cached, and pass the response's `data` to
218
- * `decisionsFrom`.
219
- *
220
- * Pass the variants result as `from` whenever you have it: the query then
221
- * asks only about the lists that hold segment groups on this page, which is
222
- * a few hundred bytes instead of the whole page and markedly faster for a
223
- * visitor Prepr has not seen before. Without `from` it asks about every
224
- * personalizable list. `topLevelOnly` stops at the first personalizable list
225
- * on each path, for middleware that decides only the top of a stack.
226
- *
227
- * Null when there is nothing to decide: send no request then.
228
- */
229
- declare function toDecisionQuery(document: PreprDocument<unknown, unknown> | string, schema: PreprSchema, options?: {
230
- from?: unknown;
231
- topLevelOnly?: boolean;
232
- }): string | null;
233
-
234
- /**
235
- * Query options for frameworks without an implicit request scope.
236
- * `personalize: true` requires the request `event`; the compiler enforces it.
237
- */
238
- type PreprEventQueryOptions<TEvent> = PreprQueryOptions & ({
239
- personalize?: false;
240
- event?: TEvent;
241
- } | {
242
- personalize: true;
243
- event: TEvent;
244
- });
245
- interface PreprEventClient<TEvent> {
246
- /**
247
- * Run a GraphQL query against Prepr. With `personalize: true`, the Prepr
248
- * headers of `event` are forwarded and the request is `no-store`; without
249
- * it the request is identical for every visitor.
250
- *
251
- * @throws PreprError — as `PreprClient.query`, plus `CONTEXT_ERROR` when
252
- * `personalize` is true and no `event` is passed.
253
- */
254
- query<TResult, TVariables>(document: PreprDocument<TResult, TVariables>, variables?: TVariables, options?: PreprEventQueryOptions<TEvent>): Promise<PreprQueryResult<TResult>>;
255
- /**
256
- * Which variant of each group this visitor gets, for the query you render
257
- * with `variants: true`. Forwards the Prepr headers of `event`.
258
- *
259
- * @throws PreprError — `CONTEXT_ERROR` without an `event`.
260
- */
261
- decide<TVariables>(document: PreprDocument<unknown, TVariables>, variables: TVariables | undefined, options: PreprDecideOptions & {
262
- event: TEvent;
263
- }): Promise<PreprDecisions>;
264
- }
265
-
266
- /**
267
- * Loads what `toVariantsQuery` and `toDecisionQuery` need to know about your
268
- * Prepr schema (which fields take `personalize`, which types can hold
269
- * variants) with one introspection request, kept in memory per URL. For a
270
- * site that uses its own GraphQL client; `createPreprClient` does this
271
- * itself.
272
- *
273
- * Load it once at startup or lazily before the first rewrite. Schema changes
274
- * in Prepr take effect after a restart or redeploy.
275
- *
276
- * @throws PreprError when the URL is invalid or the request fails; the URL,
277
- * which holds your access token, is never part of the message.
278
- */
279
- declare function loadPreprSchema(url: string, options?: {
280
- fetch?: typeof fetch;
281
- }): Promise<PreprSchemaInfo>;
282
-
283
- /** Error thrown by the token helpers and the segments fetch. */
284
- declare class PreprError extends Error {
285
- readonly code: PreprErrorCode;
286
- readonly context?: string | undefined;
287
- readonly originalError?: Error | undefined;
288
- constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
289
- }
290
-
291
- /**
292
- * Prepr GraphQL client for Astro. Pass `Astro` (in a page or component) or
293
- * the endpoint or middleware context as `event`. With `personalize: true`,
294
- * the Prepr headers `onPreprRequest` set on the request are forwarded.
295
- *
296
- * Requires the `graphql` package, which graphql-codegen already installs.
297
- */
298
- declare function createPreprClient(options: PreprClientOptions): PreprEventClient<AstroLikeContext>;
299
-
300
- export { AstroLikeContext, PREPR_DEFAULT_VARIANT, type PreprClientOptions, type PreprDecideOptions, type PreprDecisions, type PreprDocument, PreprError, type PreprEventClient, type PreprEventQueryOptions, type PreprQueryResult, type PreprSchema, type PreprStackBlock, type PreprStegaMode, type PreprVariantProps, type ResolveVariantsOptions, createPreprClient, decisionsFrom, loadPreprSchema, preprVariantProps, resolveVariants, splitStack, toDecisionQuery, toVariantsQuery };