@preprio/toolkit 0.5.0-rc.1 → 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.
- package/README.md +300 -19
- package/dist/astro/chunk-3UVUDDNY.js +173 -0
- package/dist/astro/graphql.cjs +983 -0
- package/dist/astro/graphql.d.cts +231 -0
- package/dist/astro/graphql.d.ts +231 -0
- package/dist/astro/graphql.js +893 -0
- package/dist/astro/index-XT_z-RYT.d.cts +130 -0
- package/dist/astro/index-XT_z-RYT.d.ts +130 -0
- package/dist/astro/index.cjs +14 -1
- package/dist/astro/index.d.cts +1 -120
- package/dist/astro/index.d.ts +1 -120
- package/dist/astro/index.js +15 -153
- package/dist/{chunk-4JNKYIEV.js → chunk-BJ2LBLWO.js} +69 -268
- package/dist/chunk-DGZA7KX7.js +172 -0
- package/dist/chunk-YR7P4IWE.js +187 -0
- package/dist/graphql.cjs +894 -0
- package/dist/graphql.d.cts +231 -0
- package/dist/graphql.d.ts +231 -0
- package/dist/graphql.js +787 -0
- package/dist/index.cjs +74 -111
- package/dist/index.d.cts +11 -62
- package/dist/index.d.ts +11 -62
- package/dist/index.js +23 -167
- package/dist/nextjs/chunk-U4HXENF7.js +231 -0
- package/dist/nextjs/components.cjs +188 -114
- package/dist/nextjs/components.d.cts +33 -2
- package/dist/nextjs/components.d.ts +33 -2
- package/dist/nextjs/components.js +132 -4
- package/dist/nextjs/graphql.cjs +1315 -0
- package/dist/nextjs/graphql.d.cts +361 -0
- package/dist/nextjs/graphql.d.ts +361 -0
- package/dist/nextjs/graphql.js +1218 -0
- package/dist/nextjs/index.cjs +126 -6
- package/dist/nextjs/index.d.cts +53 -141
- package/dist/nextjs/index.d.ts +53 -141
- package/dist/nextjs/index.js +113 -203
- package/dist/nextjs/server-CjpELYvq.d.cts +156 -0
- package/dist/nextjs/server-CjpELYvq.d.ts +156 -0
- package/dist/nuxt/chunk-P6ENNAEE.js +417 -0
- package/dist/nuxt/graphql.cjs +990 -0
- package/dist/nuxt/graphql.d.cts +228 -0
- package/dist/nuxt/graphql.d.ts +228 -0
- package/dist/nuxt/graphql.js +894 -0
- package/dist/nuxt/index.cjs +14 -1
- package/dist/nuxt/index.d.cts +3 -148
- package/dist/nuxt/index.d.ts +3 -148
- package/dist/nuxt/index.js +15 -391
- package/dist/nuxt/server-8v6OHbsw.d.cts +158 -0
- package/dist/nuxt/server-8v6OHbsw.d.ts +158 -0
- package/dist/pixel-DwW7cXJm.d.cts +49 -0
- package/dist/pixel-DwW7cXJm.d.ts +49 -0
- package/dist/react/components.cjs +86 -112
- package/dist/react/components.js +86 -112
- package/dist/server-Brr1UgtW.d.ts +59 -0
- package/dist/server-DJuS3YU0.d.cts +59 -0
- package/dist/sveltekit/chunk-FSARVMG5.js +413 -0
- package/dist/sveltekit/graphql.cjs +996 -0
- package/dist/sveltekit/graphql.d.cts +233 -0
- package/dist/sveltekit/graphql.d.ts +233 -0
- package/dist/sveltekit/graphql.js +900 -0
- package/dist/sveltekit/index.cjs +14 -1
- package/dist/sveltekit/index.d.cts +3 -88
- package/dist/sveltekit/index.d.ts +3 -88
- package/dist/sveltekit/index.js +15 -387
- package/dist/sveltekit/server-CO847mEV.d.cts +98 -0
- package/dist/sveltekit/server-CO847mEV.d.ts +98 -0
- package/dist/{pixel-e1fUWl2Y.d.cts → types-BYH9XqmN.d.cts} +2 -50
- package/dist/{pixel-e1fUWl2Y.d.ts → types-BYH9XqmN.d.ts} +2 -50
- package/package.json +31 -1
|
@@ -0,0 +1,231 @@
|
|
|
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
|
+
interface ResolveVariantsOptions {
|
|
11
|
+
/**
|
|
12
|
+
* The visitor's per-group variants (from a decision query). Without it,
|
|
13
|
+
* personalization groups show their default variant.
|
|
14
|
+
*/
|
|
15
|
+
decisions?: PreprDecisions;
|
|
16
|
+
/** The visitor's A/B bucket. Without it, A/B groups use the decision, then `A`. */
|
|
17
|
+
bucket?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Keeps, in every list of a variants query result, only the variants this
|
|
21
|
+
* visitor gets, at any depth (page stacks, nested stacks, personalized
|
|
22
|
+
* buttons). Items outside a group are kept as they are.
|
|
23
|
+
*/
|
|
24
|
+
declare function resolveVariants<T>(data: T, options?: ResolveVariantsOptions): T;
|
|
25
|
+
|
|
26
|
+
/** A run of stack items that renders the same way: see {@link splitStack}. */
|
|
27
|
+
interface PreprStackBlock<T> {
|
|
28
|
+
key: string;
|
|
29
|
+
items: T[];
|
|
30
|
+
/**
|
|
31
|
+
* Rendered position of the block's first item: a group (whichever variant
|
|
32
|
+
* shows) takes one position, any other item one each. Use it for anything
|
|
33
|
+
* that depends on page position, like which section owns the h1.
|
|
34
|
+
*/
|
|
35
|
+
index: number;
|
|
36
|
+
/**
|
|
37
|
+
* Some item in the block (or nested inside one) is a segment variant, so
|
|
38
|
+
* the block depends on the visitor and is decided per request.
|
|
39
|
+
*/
|
|
40
|
+
personalized: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Some item in the block (or nested inside one) is an A/B variant. Without
|
|
43
|
+
* a known bucket, such a block renders once per bucket and the visitor's
|
|
44
|
+
* bucket is picked in the browser before first paint.
|
|
45
|
+
*/
|
|
46
|
+
abTest: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Splits a variants-query stack into blocks. Each personalization or A/B
|
|
50
|
+
* group becomes one block (a variant can hold a different number of items
|
|
51
|
+
* than the default); consecutive items that never vary are merged.
|
|
52
|
+
*/
|
|
53
|
+
declare function splitStack<T>(items: readonly T[]): PreprStackBlock<T>[];
|
|
54
|
+
|
|
55
|
+
/** The attribute Prepr's tracking pixel reads impressions and conversions from. */
|
|
56
|
+
interface PreprVariantProps {
|
|
57
|
+
'data-prepr-variant-key'?: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Attributes that let Prepr's tracking pixel count impressions of this item's
|
|
61
|
+
* variant, for an item from a query run with `variants: true`. Spread them on
|
|
62
|
+
* the element that shows the item; an item outside any personalization or A/B
|
|
63
|
+
* group gets none.
|
|
64
|
+
*
|
|
65
|
+
* Items rendered by the Next.js `PreprStack` are tagged for you. Reach for this
|
|
66
|
+
* for items nested inside them (cards, buttons, models in a section) and in
|
|
67
|
+
* frameworks without `PreprStack`.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* <article {...preprVariantProps(card)}>…</article>
|
|
71
|
+
*/
|
|
72
|
+
declare function preprVariantProps(item: unknown): PreprVariantProps;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A GraphQL document accepted by `query`: a plain string, graphql-codegen's
|
|
76
|
+
* `TypedDocumentString` (`documentMode: 'string'`), or any `DocumentNode`,
|
|
77
|
+
* including codegen's default `TypedDocumentNode` output.
|
|
78
|
+
*
|
|
79
|
+
* The phantom `__apiType` field is how codegen attaches result and variable
|
|
80
|
+
* types; matching it structurally keeps `@graphql-typed-document-node/core`
|
|
81
|
+
* out of this package.
|
|
82
|
+
*/
|
|
83
|
+
type PreprDocument<TResult, TVariables> = string | ({
|
|
84
|
+
__apiType?: (variables: TVariables) => TResult;
|
|
85
|
+
} & (String | DocumentNode));
|
|
86
|
+
/**
|
|
87
|
+
* How stega markers are stripped from the response:
|
|
88
|
+
* - `'urls'`: only from values under URL-like keys (`url`, `slug`, `link`,
|
|
89
|
+
* `href`), where markers break links. Text keeps its markers for
|
|
90
|
+
* click-to-edit.
|
|
91
|
+
* - `'all'`: from every string.
|
|
92
|
+
* - `'none'`: never.
|
|
93
|
+
*/
|
|
94
|
+
type PreprStegaMode = 'urls' | 'all' | 'none';
|
|
95
|
+
interface PreprClientOptions {
|
|
96
|
+
/** Full Prepr GraphQL URL, `https://graphql.prepr.io/<token>`. */
|
|
97
|
+
url: string;
|
|
98
|
+
/**
|
|
99
|
+
* Fetch implementation. Defaults to the global `fetch`, read at call time so
|
|
100
|
+
* framework-patched versions (such as Next.js's) are used.
|
|
101
|
+
*/
|
|
102
|
+
fetch?: typeof fetch;
|
|
103
|
+
/**
|
|
104
|
+
* Preview mode: every query skips the HTTP cache so editors see each edit,
|
|
105
|
+
* and stega defaults to `'urls'` so click-to-edit keeps working.
|
|
106
|
+
*/
|
|
107
|
+
preview?: boolean;
|
|
108
|
+
/** Default: `'urls'` when `preview` is true, otherwise `'all'`. */
|
|
109
|
+
stega?: PreprStegaMode;
|
|
110
|
+
/**
|
|
111
|
+
* Send queries as a GET with the request body base64-encoded in a
|
|
112
|
+
* `Fastly-Query` header, which Prepr's edge serves from the nearest point
|
|
113
|
+
* of presence (about half the latency of a POST in our measurements).
|
|
114
|
+
* Queries too large for a header still go as a POST. Off by default: it
|
|
115
|
+
* relies on Prepr edge behavior that is not part of the documented API.
|
|
116
|
+
*/
|
|
117
|
+
fastlyQuery?: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* @internal Fetch options for the one-time schema introspection, so a
|
|
120
|
+
* framework wrapper can put it in its data cache.
|
|
121
|
+
*/
|
|
122
|
+
schemaInit?: RequestInit;
|
|
123
|
+
/**
|
|
124
|
+
* Headers sent with every request, such as a header a proxy in front of the
|
|
125
|
+
* API requires. They are fixed when the client is created, so they cannot
|
|
126
|
+
* vary per visitor and do not stop responses from being cached. Put
|
|
127
|
+
* per-request values in the query's `headers` instead.
|
|
128
|
+
*/
|
|
129
|
+
headers?: HeadersInit;
|
|
130
|
+
}
|
|
131
|
+
interface PreprQueryOptions {
|
|
132
|
+
/**
|
|
133
|
+
* Prepr headers to forward, typically from `getPreprHeaders`. Passing any
|
|
134
|
+
* headers object, even an empty one, makes the request `no-store`: the
|
|
135
|
+
* response may be personalized and must not be shared between visitors.
|
|
136
|
+
*/
|
|
137
|
+
headers?: PreprHeaders | HeadersInit;
|
|
138
|
+
/**
|
|
139
|
+
* Extra fetch options such as `signal`. `method` and `body` are set by the
|
|
140
|
+
* client. `init.headers` are merged like `headers`, and also make the
|
|
141
|
+
* request `no-store`.
|
|
142
|
+
*/
|
|
143
|
+
init?: RequestInit;
|
|
144
|
+
/** Per-query fetch, overriding the client's (e.g. SvelteKit's `event.fetch`). */
|
|
145
|
+
fetch?: typeof fetch;
|
|
146
|
+
/**
|
|
147
|
+
* Fetch every variant instead of the visitor's: `personalize: false` is
|
|
148
|
+
* added to every personalizable field in the query, at any depth, and each
|
|
149
|
+
* item gets its `_context` (under `_preprContext`). The result is the same
|
|
150
|
+
* for every visitor, so it can be cached; pass it to `resolveVariants` with
|
|
151
|
+
* the visitor's `decide()` result to get what they see.
|
|
152
|
+
*
|
|
153
|
+
* The client learns which fields are personalizable from one schema
|
|
154
|
+
* introspection request, made on first use.
|
|
155
|
+
*/
|
|
156
|
+
variants?: boolean;
|
|
157
|
+
}
|
|
158
|
+
/** Options for `decide()`. */
|
|
159
|
+
interface PreprDecideOptions extends Omit<PreprQueryOptions, 'variants'> {
|
|
160
|
+
/**
|
|
161
|
+
* The `variants: true` result this decision is for (the whole `data`
|
|
162
|
+
* object). The decision query then asks only about the fields that hold
|
|
163
|
+
* segment personalization groups, and is skipped entirely when there are
|
|
164
|
+
* none, e.g. on pages with only A/B tests or no personalization.
|
|
165
|
+
*/
|
|
166
|
+
from?: unknown;
|
|
167
|
+
/**
|
|
168
|
+
* @internal Ask only about the first personalizable list on each path,
|
|
169
|
+
* without `from`: what middleware needs for groups at the top of a stack.
|
|
170
|
+
*/
|
|
171
|
+
topLevelOnly?: boolean;
|
|
172
|
+
}
|
|
173
|
+
interface PreprQueryResult<TResult> {
|
|
174
|
+
data: TResult;
|
|
175
|
+
/**
|
|
176
|
+
* True when Prepr reports, through the response's `Vary` header, that the
|
|
177
|
+
* queried fields hold segment variants or A/B tests.
|
|
178
|
+
*/
|
|
179
|
+
personalized: boolean;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Query options for frameworks without an implicit request scope.
|
|
184
|
+
* `personalize: true` requires the request `event`; the compiler enforces it.
|
|
185
|
+
*/
|
|
186
|
+
type PreprEventQueryOptions<TEvent> = PreprQueryOptions & ({
|
|
187
|
+
personalize?: false;
|
|
188
|
+
event?: TEvent;
|
|
189
|
+
} | {
|
|
190
|
+
personalize: true;
|
|
191
|
+
event: TEvent;
|
|
192
|
+
});
|
|
193
|
+
interface PreprEventClient<TEvent> {
|
|
194
|
+
/**
|
|
195
|
+
* Run a GraphQL query against Prepr. With `personalize: true`, the Prepr
|
|
196
|
+
* headers of `event` are forwarded and the request is `no-store`; without
|
|
197
|
+
* it the request is identical for every visitor.
|
|
198
|
+
*
|
|
199
|
+
* @throws PreprError — as `PreprClient.query`, plus `CONTEXT_ERROR` when
|
|
200
|
+
* `personalize` is true and no `event` is passed.
|
|
201
|
+
*/
|
|
202
|
+
query<TResult, TVariables>(document: PreprDocument<TResult, TVariables>, variables?: TVariables, options?: PreprEventQueryOptions<TEvent>): Promise<PreprQueryResult<TResult>>;
|
|
203
|
+
/**
|
|
204
|
+
* Which variant of each group this visitor gets, for the query you render
|
|
205
|
+
* with `variants: true`. Forwards the Prepr headers of `event`.
|
|
206
|
+
*
|
|
207
|
+
* @throws PreprError — `CONTEXT_ERROR` without an `event`.
|
|
208
|
+
*/
|
|
209
|
+
decide<TVariables>(document: PreprDocument<unknown, TVariables>, variables: TVariables | undefined, options: PreprDecideOptions & {
|
|
210
|
+
event: TEvent;
|
|
211
|
+
}): Promise<PreprDecisions>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
215
|
+
declare class PreprError extends Error {
|
|
216
|
+
readonly code: PreprErrorCode;
|
|
217
|
+
readonly context?: string | undefined;
|
|
218
|
+
readonly originalError?: Error | undefined;
|
|
219
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Prepr GraphQL client for Astro. Pass `Astro` (in a page or component) or
|
|
224
|
+
* the endpoint or middleware context as `event`. With `personalize: true`,
|
|
225
|
+
* the Prepr headers `onPreprRequest` set on the request are forwarded.
|
|
226
|
+
*
|
|
227
|
+
* Requires the `graphql` package, which graphql-codegen already installs.
|
|
228
|
+
*/
|
|
229
|
+
declare function createPreprClient(options: PreprClientOptions): PreprEventClient<AstroLikeContext>;
|
|
230
|
+
|
|
231
|
+
export { AstroLikeContext, PREPR_DEFAULT_VARIANT, type PreprClientOptions, type PreprDecideOptions, type PreprDecisions, type PreprDocument, PreprError, type PreprEventClient, type PreprEventQueryOptions, type PreprQueryResult, type PreprStackBlock, type PreprStegaMode, type PreprVariantProps, type ResolveVariantsOptions, createPreprClient, preprVariantProps, resolveVariants, splitStack };
|
|
@@ -0,0 +1,231 @@
|
|
|
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
|
+
interface ResolveVariantsOptions {
|
|
11
|
+
/**
|
|
12
|
+
* The visitor's per-group variants (from a decision query). Without it,
|
|
13
|
+
* personalization groups show their default variant.
|
|
14
|
+
*/
|
|
15
|
+
decisions?: PreprDecisions;
|
|
16
|
+
/** The visitor's A/B bucket. Without it, A/B groups use the decision, then `A`. */
|
|
17
|
+
bucket?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Keeps, in every list of a variants query result, only the variants this
|
|
21
|
+
* visitor gets, at any depth (page stacks, nested stacks, personalized
|
|
22
|
+
* buttons). Items outside a group are kept as they are.
|
|
23
|
+
*/
|
|
24
|
+
declare function resolveVariants<T>(data: T, options?: ResolveVariantsOptions): T;
|
|
25
|
+
|
|
26
|
+
/** A run of stack items that renders the same way: see {@link splitStack}. */
|
|
27
|
+
interface PreprStackBlock<T> {
|
|
28
|
+
key: string;
|
|
29
|
+
items: T[];
|
|
30
|
+
/**
|
|
31
|
+
* Rendered position of the block's first item: a group (whichever variant
|
|
32
|
+
* shows) takes one position, any other item one each. Use it for anything
|
|
33
|
+
* that depends on page position, like which section owns the h1.
|
|
34
|
+
*/
|
|
35
|
+
index: number;
|
|
36
|
+
/**
|
|
37
|
+
* Some item in the block (or nested inside one) is a segment variant, so
|
|
38
|
+
* the block depends on the visitor and is decided per request.
|
|
39
|
+
*/
|
|
40
|
+
personalized: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Some item in the block (or nested inside one) is an A/B variant. Without
|
|
43
|
+
* a known bucket, such a block renders once per bucket and the visitor's
|
|
44
|
+
* bucket is picked in the browser before first paint.
|
|
45
|
+
*/
|
|
46
|
+
abTest: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Splits a variants-query stack into blocks. Each personalization or A/B
|
|
50
|
+
* group becomes one block (a variant can hold a different number of items
|
|
51
|
+
* than the default); consecutive items that never vary are merged.
|
|
52
|
+
*/
|
|
53
|
+
declare function splitStack<T>(items: readonly T[]): PreprStackBlock<T>[];
|
|
54
|
+
|
|
55
|
+
/** The attribute Prepr's tracking pixel reads impressions and conversions from. */
|
|
56
|
+
interface PreprVariantProps {
|
|
57
|
+
'data-prepr-variant-key'?: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Attributes that let Prepr's tracking pixel count impressions of this item's
|
|
61
|
+
* variant, for an item from a query run with `variants: true`. Spread them on
|
|
62
|
+
* the element that shows the item; an item outside any personalization or A/B
|
|
63
|
+
* group gets none.
|
|
64
|
+
*
|
|
65
|
+
* Items rendered by the Next.js `PreprStack` are tagged for you. Reach for this
|
|
66
|
+
* for items nested inside them (cards, buttons, models in a section) and in
|
|
67
|
+
* frameworks without `PreprStack`.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* <article {...preprVariantProps(card)}>…</article>
|
|
71
|
+
*/
|
|
72
|
+
declare function preprVariantProps(item: unknown): PreprVariantProps;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A GraphQL document accepted by `query`: a plain string, graphql-codegen's
|
|
76
|
+
* `TypedDocumentString` (`documentMode: 'string'`), or any `DocumentNode`,
|
|
77
|
+
* including codegen's default `TypedDocumentNode` output.
|
|
78
|
+
*
|
|
79
|
+
* The phantom `__apiType` field is how codegen attaches result and variable
|
|
80
|
+
* types; matching it structurally keeps `@graphql-typed-document-node/core`
|
|
81
|
+
* out of this package.
|
|
82
|
+
*/
|
|
83
|
+
type PreprDocument<TResult, TVariables> = string | ({
|
|
84
|
+
__apiType?: (variables: TVariables) => TResult;
|
|
85
|
+
} & (String | DocumentNode));
|
|
86
|
+
/**
|
|
87
|
+
* How stega markers are stripped from the response:
|
|
88
|
+
* - `'urls'`: only from values under URL-like keys (`url`, `slug`, `link`,
|
|
89
|
+
* `href`), where markers break links. Text keeps its markers for
|
|
90
|
+
* click-to-edit.
|
|
91
|
+
* - `'all'`: from every string.
|
|
92
|
+
* - `'none'`: never.
|
|
93
|
+
*/
|
|
94
|
+
type PreprStegaMode = 'urls' | 'all' | 'none';
|
|
95
|
+
interface PreprClientOptions {
|
|
96
|
+
/** Full Prepr GraphQL URL, `https://graphql.prepr.io/<token>`. */
|
|
97
|
+
url: string;
|
|
98
|
+
/**
|
|
99
|
+
* Fetch implementation. Defaults to the global `fetch`, read at call time so
|
|
100
|
+
* framework-patched versions (such as Next.js's) are used.
|
|
101
|
+
*/
|
|
102
|
+
fetch?: typeof fetch;
|
|
103
|
+
/**
|
|
104
|
+
* Preview mode: every query skips the HTTP cache so editors see each edit,
|
|
105
|
+
* and stega defaults to `'urls'` so click-to-edit keeps working.
|
|
106
|
+
*/
|
|
107
|
+
preview?: boolean;
|
|
108
|
+
/** Default: `'urls'` when `preview` is true, otherwise `'all'`. */
|
|
109
|
+
stega?: PreprStegaMode;
|
|
110
|
+
/**
|
|
111
|
+
* Send queries as a GET with the request body base64-encoded in a
|
|
112
|
+
* `Fastly-Query` header, which Prepr's edge serves from the nearest point
|
|
113
|
+
* of presence (about half the latency of a POST in our measurements).
|
|
114
|
+
* Queries too large for a header still go as a POST. Off by default: it
|
|
115
|
+
* relies on Prepr edge behavior that is not part of the documented API.
|
|
116
|
+
*/
|
|
117
|
+
fastlyQuery?: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* @internal Fetch options for the one-time schema introspection, so a
|
|
120
|
+
* framework wrapper can put it in its data cache.
|
|
121
|
+
*/
|
|
122
|
+
schemaInit?: RequestInit;
|
|
123
|
+
/**
|
|
124
|
+
* Headers sent with every request, such as a header a proxy in front of the
|
|
125
|
+
* API requires. They are fixed when the client is created, so they cannot
|
|
126
|
+
* vary per visitor and do not stop responses from being cached. Put
|
|
127
|
+
* per-request values in the query's `headers` instead.
|
|
128
|
+
*/
|
|
129
|
+
headers?: HeadersInit;
|
|
130
|
+
}
|
|
131
|
+
interface PreprQueryOptions {
|
|
132
|
+
/**
|
|
133
|
+
* Prepr headers to forward, typically from `getPreprHeaders`. Passing any
|
|
134
|
+
* headers object, even an empty one, makes the request `no-store`: the
|
|
135
|
+
* response may be personalized and must not be shared between visitors.
|
|
136
|
+
*/
|
|
137
|
+
headers?: PreprHeaders | HeadersInit;
|
|
138
|
+
/**
|
|
139
|
+
* Extra fetch options such as `signal`. `method` and `body` are set by the
|
|
140
|
+
* client. `init.headers` are merged like `headers`, and also make the
|
|
141
|
+
* request `no-store`.
|
|
142
|
+
*/
|
|
143
|
+
init?: RequestInit;
|
|
144
|
+
/** Per-query fetch, overriding the client's (e.g. SvelteKit's `event.fetch`). */
|
|
145
|
+
fetch?: typeof fetch;
|
|
146
|
+
/**
|
|
147
|
+
* Fetch every variant instead of the visitor's: `personalize: false` is
|
|
148
|
+
* added to every personalizable field in the query, at any depth, and each
|
|
149
|
+
* item gets its `_context` (under `_preprContext`). The result is the same
|
|
150
|
+
* for every visitor, so it can be cached; pass it to `resolveVariants` with
|
|
151
|
+
* the visitor's `decide()` result to get what they see.
|
|
152
|
+
*
|
|
153
|
+
* The client learns which fields are personalizable from one schema
|
|
154
|
+
* introspection request, made on first use.
|
|
155
|
+
*/
|
|
156
|
+
variants?: boolean;
|
|
157
|
+
}
|
|
158
|
+
/** Options for `decide()`. */
|
|
159
|
+
interface PreprDecideOptions extends Omit<PreprQueryOptions, 'variants'> {
|
|
160
|
+
/**
|
|
161
|
+
* The `variants: true` result this decision is for (the whole `data`
|
|
162
|
+
* object). The decision query then asks only about the fields that hold
|
|
163
|
+
* segment personalization groups, and is skipped entirely when there are
|
|
164
|
+
* none, e.g. on pages with only A/B tests or no personalization.
|
|
165
|
+
*/
|
|
166
|
+
from?: unknown;
|
|
167
|
+
/**
|
|
168
|
+
* @internal Ask only about the first personalizable list on each path,
|
|
169
|
+
* without `from`: what middleware needs for groups at the top of a stack.
|
|
170
|
+
*/
|
|
171
|
+
topLevelOnly?: boolean;
|
|
172
|
+
}
|
|
173
|
+
interface PreprQueryResult<TResult> {
|
|
174
|
+
data: TResult;
|
|
175
|
+
/**
|
|
176
|
+
* True when Prepr reports, through the response's `Vary` header, that the
|
|
177
|
+
* queried fields hold segment variants or A/B tests.
|
|
178
|
+
*/
|
|
179
|
+
personalized: boolean;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Query options for frameworks without an implicit request scope.
|
|
184
|
+
* `personalize: true` requires the request `event`; the compiler enforces it.
|
|
185
|
+
*/
|
|
186
|
+
type PreprEventQueryOptions<TEvent> = PreprQueryOptions & ({
|
|
187
|
+
personalize?: false;
|
|
188
|
+
event?: TEvent;
|
|
189
|
+
} | {
|
|
190
|
+
personalize: true;
|
|
191
|
+
event: TEvent;
|
|
192
|
+
});
|
|
193
|
+
interface PreprEventClient<TEvent> {
|
|
194
|
+
/**
|
|
195
|
+
* Run a GraphQL query against Prepr. With `personalize: true`, the Prepr
|
|
196
|
+
* headers of `event` are forwarded and the request is `no-store`; without
|
|
197
|
+
* it the request is identical for every visitor.
|
|
198
|
+
*
|
|
199
|
+
* @throws PreprError — as `PreprClient.query`, plus `CONTEXT_ERROR` when
|
|
200
|
+
* `personalize` is true and no `event` is passed.
|
|
201
|
+
*/
|
|
202
|
+
query<TResult, TVariables>(document: PreprDocument<TResult, TVariables>, variables?: TVariables, options?: PreprEventQueryOptions<TEvent>): Promise<PreprQueryResult<TResult>>;
|
|
203
|
+
/**
|
|
204
|
+
* Which variant of each group this visitor gets, for the query you render
|
|
205
|
+
* with `variants: true`. Forwards the Prepr headers of `event`.
|
|
206
|
+
*
|
|
207
|
+
* @throws PreprError — `CONTEXT_ERROR` without an `event`.
|
|
208
|
+
*/
|
|
209
|
+
decide<TVariables>(document: PreprDocument<unknown, TVariables>, variables: TVariables | undefined, options: PreprDecideOptions & {
|
|
210
|
+
event: TEvent;
|
|
211
|
+
}): Promise<PreprDecisions>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
215
|
+
declare class PreprError extends Error {
|
|
216
|
+
readonly code: PreprErrorCode;
|
|
217
|
+
readonly context?: string | undefined;
|
|
218
|
+
readonly originalError?: Error | undefined;
|
|
219
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Prepr GraphQL client for Astro. Pass `Astro` (in a page or component) or
|
|
224
|
+
* the endpoint or middleware context as `event`. With `personalize: true`,
|
|
225
|
+
* the Prepr headers `onPreprRequest` set on the request are forwarded.
|
|
226
|
+
*
|
|
227
|
+
* Requires the `graphql` package, which graphql-codegen already installs.
|
|
228
|
+
*/
|
|
229
|
+
declare function createPreprClient(options: PreprClientOptions): PreprEventClient<AstroLikeContext>;
|
|
230
|
+
|
|
231
|
+
export { AstroLikeContext, PREPR_DEFAULT_VARIANT, type PreprClientOptions, type PreprDecideOptions, type PreprDecisions, type PreprDocument, PreprError, type PreprEventClient, type PreprEventQueryOptions, type PreprQueryResult, type PreprStackBlock, type PreprStegaMode, type PreprVariantProps, type ResolveVariantsOptions, createPreprClient, preprVariantProps, resolveVariants, splitStack };
|