@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,893 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PreprError,
|
|
3
|
+
getPreprHeadersFromHeaders,
|
|
4
|
+
isPersonalizedResponse,
|
|
5
|
+
validatePreprToken
|
|
6
|
+
} from "./chunk-3UVUDDNY.js";
|
|
7
|
+
|
|
8
|
+
// src/core/variants/constants.ts
|
|
9
|
+
var PREPR_CONTEXT_ALIAS = "_preprContext";
|
|
10
|
+
var PREPR_TYPE_ALIAS = "_preprType";
|
|
11
|
+
var PREPR_DEFAULT_VARIANT = "ALL_OTHER_USERS";
|
|
12
|
+
|
|
13
|
+
// src/core/variants/ast.ts
|
|
14
|
+
import { parse } from "graphql/language/parser.js";
|
|
15
|
+
var name = (value) => ({ kind: "Name", value });
|
|
16
|
+
function selectionSet(selections) {
|
|
17
|
+
return { kind: "SelectionSet", selections };
|
|
18
|
+
}
|
|
19
|
+
function contextField(withKey) {
|
|
20
|
+
const fields = ["kind", "group_id", "variant_id"];
|
|
21
|
+
if (withKey) fields.push("variant_key");
|
|
22
|
+
return {
|
|
23
|
+
kind: "Field",
|
|
24
|
+
alias: name(PREPR_CONTEXT_ALIAS),
|
|
25
|
+
name: name("_context"),
|
|
26
|
+
selectionSet: selectionSet(
|
|
27
|
+
fields.map(
|
|
28
|
+
(field) => ({ kind: "Field", name: name(field) })
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function typenameField() {
|
|
34
|
+
return {
|
|
35
|
+
kind: "Field",
|
|
36
|
+
alias: name(PREPR_TYPE_ALIAS),
|
|
37
|
+
name: name("__typename")
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function inlineFragment(typeName, selections) {
|
|
41
|
+
return {
|
|
42
|
+
kind: "InlineFragment",
|
|
43
|
+
typeCondition: { kind: "NamedType", name: name(typeName) },
|
|
44
|
+
selectionSet: selectionSet(selections)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function contextSelections(typeName, schema, onlyTypes, withType = true) {
|
|
48
|
+
const fields = () => withType ? [contextField(true), typenameField()] : [contextField(false)];
|
|
49
|
+
const type = schema.types[typeName];
|
|
50
|
+
if (!type) return [];
|
|
51
|
+
if (type.possibleTypes.length > 0) {
|
|
52
|
+
return type.possibleTypes.filter(
|
|
53
|
+
(member) => schema.types[member]?.hasContext && (!onlyTypes || onlyTypes.has(member))
|
|
54
|
+
).map((member) => inlineFragment(member, fields()));
|
|
55
|
+
}
|
|
56
|
+
return type.hasContext ? fields() : [];
|
|
57
|
+
}
|
|
58
|
+
function withoutPersonalizeArg(args = []) {
|
|
59
|
+
return args.filter((arg) => arg.name.value !== "personalize");
|
|
60
|
+
}
|
|
61
|
+
function documentOf(document2) {
|
|
62
|
+
if (typeof document2 === "string") return parse(document2);
|
|
63
|
+
if (document2 instanceof String) return parse(document2.toString());
|
|
64
|
+
return document2;
|
|
65
|
+
}
|
|
66
|
+
function collectVariables(node, used) {
|
|
67
|
+
if (!node || typeof node !== "object") return;
|
|
68
|
+
if (Array.isArray(node)) {
|
|
69
|
+
for (const item of node) collectVariables(item, used);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const value = node;
|
|
73
|
+
if (value.kind === "Variable") {
|
|
74
|
+
used.add(value.name.value);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
for (const key of Object.keys(value)) {
|
|
78
|
+
if (key !== "loc") collectVariables(value[key], used);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/core/variants/decision-document.ts
|
|
83
|
+
function decisionSelectionSet(set, typeName, path, ctx) {
|
|
84
|
+
const selections = [];
|
|
85
|
+
for (const selection of set.selections) {
|
|
86
|
+
if (selection.kind === "FragmentSpread") {
|
|
87
|
+
const fragment = ctx.fragments[selection.name.value];
|
|
88
|
+
if (!fragment) continue;
|
|
89
|
+
const pruned = decisionSelectionSet(
|
|
90
|
+
fragment.selectionSet,
|
|
91
|
+
fragment.typeCondition.name.value,
|
|
92
|
+
path,
|
|
93
|
+
ctx
|
|
94
|
+
);
|
|
95
|
+
if (pruned) {
|
|
96
|
+
selections.push(
|
|
97
|
+
inlineFragment(fragment.typeCondition.name.value, [
|
|
98
|
+
...pruned.selections
|
|
99
|
+
])
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (selection.kind === "InlineFragment") {
|
|
105
|
+
const pruned = decisionSelectionSet(
|
|
106
|
+
selection.selectionSet,
|
|
107
|
+
selection.typeCondition?.name.value ?? typeName,
|
|
108
|
+
path,
|
|
109
|
+
ctx
|
|
110
|
+
);
|
|
111
|
+
if (pruned) selections.push({ ...selection, selectionSet: pruned });
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (!selection.selectionSet) continue;
|
|
115
|
+
const field = ctx.schema.types[typeName]?.fields[selection.name.value];
|
|
116
|
+
if (!field) continue;
|
|
117
|
+
const key = selection.alias?.value ?? selection.name.value;
|
|
118
|
+
const fieldPath = path ? `${path}.${key}` : key;
|
|
119
|
+
if (ctx.paths && !ctx.paths.prefixes.has(fieldPath)) continue;
|
|
120
|
+
const child = decisionSelectionSet(
|
|
121
|
+
selection.selectionSet,
|
|
122
|
+
field.type,
|
|
123
|
+
fieldPath,
|
|
124
|
+
ctx
|
|
125
|
+
);
|
|
126
|
+
const groupTypes = ctx.paths?.groups.get(fieldPath);
|
|
127
|
+
const holdsGroups = field.personalizable && (!ctx.paths || groupTypes !== void 0);
|
|
128
|
+
if (holdsGroups) {
|
|
129
|
+
selections.push({
|
|
130
|
+
...selection,
|
|
131
|
+
arguments: withoutPersonalizeArg(selection.arguments),
|
|
132
|
+
selectionSet: selectionSet([
|
|
133
|
+
...ctx.topLevelOnly ? [] : child?.selections ?? [],
|
|
134
|
+
...contextSelections(
|
|
135
|
+
field.type,
|
|
136
|
+
ctx.schema,
|
|
137
|
+
groupTypes && !groupTypes.has("*") ? groupTypes : void 0,
|
|
138
|
+
false
|
|
139
|
+
)
|
|
140
|
+
])
|
|
141
|
+
});
|
|
142
|
+
} else if (child) {
|
|
143
|
+
selections.push({
|
|
144
|
+
...selection,
|
|
145
|
+
arguments: field.personalizable ? withoutPersonalizeArg(selection.arguments) : selection.arguments,
|
|
146
|
+
selectionSet: child
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return selections.length > 0 ? selectionSet(selections) : null;
|
|
151
|
+
}
|
|
152
|
+
function toDecisionDocument(document2, schema, groupPaths, options) {
|
|
153
|
+
const doc = documentOf(document2);
|
|
154
|
+
const fragments = {};
|
|
155
|
+
for (const definition of doc.definitions) {
|
|
156
|
+
if (definition.kind === "FragmentDefinition") {
|
|
157
|
+
fragments[definition.name.value] = definition;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const operation = doc.definitions.find(
|
|
161
|
+
(definition) => definition.kind === "OperationDefinition"
|
|
162
|
+
);
|
|
163
|
+
if (!operation) return null;
|
|
164
|
+
let paths;
|
|
165
|
+
if (groupPaths) {
|
|
166
|
+
const prefixes = /* @__PURE__ */ new Set();
|
|
167
|
+
for (const groupPath of groupPaths.keys()) {
|
|
168
|
+
const parts = groupPath.split(".");
|
|
169
|
+
for (let i = 1; i <= parts.length; i++) {
|
|
170
|
+
prefixes.add(parts.slice(0, i).join("."));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
paths = { groups: groupPaths, prefixes };
|
|
174
|
+
}
|
|
175
|
+
const pruned = decisionSelectionSet(
|
|
176
|
+
operation.selectionSet,
|
|
177
|
+
schema.queryType,
|
|
178
|
+
"",
|
|
179
|
+
{ schema, fragments, paths, topLevelOnly: options?.topLevelOnly }
|
|
180
|
+
);
|
|
181
|
+
if (!pruned) return null;
|
|
182
|
+
const used = /* @__PURE__ */ new Set();
|
|
183
|
+
collectVariables(pruned, used);
|
|
184
|
+
collectVariables(operation.directives, used);
|
|
185
|
+
return {
|
|
186
|
+
kind: "Document",
|
|
187
|
+
definitions: [
|
|
188
|
+
{
|
|
189
|
+
...operation,
|
|
190
|
+
name: operation.name && name(`${operation.name.value}Decision`),
|
|
191
|
+
variableDefinitions: (operation.variableDefinitions ?? []).filter(
|
|
192
|
+
(definition) => used.has(definition.variable.name.value)
|
|
193
|
+
),
|
|
194
|
+
selectionSet: pruned
|
|
195
|
+
}
|
|
196
|
+
]
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/core/variants/probe-document.ts
|
|
201
|
+
function probeSelectionSet(set, fragments) {
|
|
202
|
+
const selections = [
|
|
203
|
+
{ kind: "Field", name: name("__typename") }
|
|
204
|
+
];
|
|
205
|
+
for (const selection of set.selections) {
|
|
206
|
+
if (selection.kind === "Field") {
|
|
207
|
+
if (!selection.selectionSet) continue;
|
|
208
|
+
selections.push({
|
|
209
|
+
...selection,
|
|
210
|
+
selectionSet: probeSelectionSet(selection.selectionSet, fragments)
|
|
211
|
+
});
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const fragment = selection.kind === "InlineFragment" ? selection : fragments[selection.name.value];
|
|
215
|
+
if (!fragment) continue;
|
|
216
|
+
const child = probeSelectionSet(fragment.selectionSet, fragments);
|
|
217
|
+
if (child.selections.length === 1) continue;
|
|
218
|
+
selections.push({
|
|
219
|
+
kind: "InlineFragment",
|
|
220
|
+
typeCondition: fragment.typeCondition,
|
|
221
|
+
directives: selection.directives,
|
|
222
|
+
selectionSet: child
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
return selectionSet(selections);
|
|
226
|
+
}
|
|
227
|
+
function toAbProbeDocument(document2) {
|
|
228
|
+
const doc = documentOf(document2);
|
|
229
|
+
const fragments = {};
|
|
230
|
+
for (const definition of doc.definitions) {
|
|
231
|
+
if (definition.kind === "FragmentDefinition") {
|
|
232
|
+
fragments[definition.name.value] = definition;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const operation = doc.definitions.find(
|
|
236
|
+
(definition) => definition.kind === "OperationDefinition"
|
|
237
|
+
);
|
|
238
|
+
if (!operation) return null;
|
|
239
|
+
const probe = probeSelectionSet(operation.selectionSet, fragments);
|
|
240
|
+
const used = /* @__PURE__ */ new Set();
|
|
241
|
+
collectVariables(probe, used);
|
|
242
|
+
collectVariables(operation.directives, used);
|
|
243
|
+
return {
|
|
244
|
+
kind: "Document",
|
|
245
|
+
definitions: [
|
|
246
|
+
{
|
|
247
|
+
...operation,
|
|
248
|
+
name: operation.name && name(`${operation.name.value}AbCheck`),
|
|
249
|
+
variableDefinitions: (operation.variableDefinitions ?? []).filter(
|
|
250
|
+
(definition) => used.has(definition.variable.name.value)
|
|
251
|
+
),
|
|
252
|
+
selectionSet: probe
|
|
253
|
+
}
|
|
254
|
+
]
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// src/core/variants/resolve.ts
|
|
259
|
+
function personalizationPaths(data) {
|
|
260
|
+
const paths = /* @__PURE__ */ new Map();
|
|
261
|
+
const walk = (value, path) => {
|
|
262
|
+
if (Array.isArray(value)) {
|
|
263
|
+
for (const item of value) {
|
|
264
|
+
if (contextOf(item)?.kind === "PERSONALIZATION") {
|
|
265
|
+
const record = item;
|
|
266
|
+
const type = record[PREPR_TYPE_ALIAS] ?? record.__typename;
|
|
267
|
+
const types = paths.get(path) ?? /* @__PURE__ */ new Set();
|
|
268
|
+
types.add(typeof type === "string" ? type : "*");
|
|
269
|
+
paths.set(path, types);
|
|
270
|
+
}
|
|
271
|
+
walk(item, path);
|
|
272
|
+
}
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (!value || typeof value !== "object") return;
|
|
276
|
+
for (const [key, child] of Object.entries(value)) {
|
|
277
|
+
if (key !== PREPR_CONTEXT_ALIAS && key !== PREPR_TYPE_ALIAS)
|
|
278
|
+
walk(child, path ? `${path}.${key}` : key);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
walk(data, "");
|
|
282
|
+
return paths;
|
|
283
|
+
}
|
|
284
|
+
function contextOf(item) {
|
|
285
|
+
if (!item || typeof item !== "object") return null;
|
|
286
|
+
const context = item[PREPR_CONTEXT_ALIAS];
|
|
287
|
+
const ctx = context;
|
|
288
|
+
return ctx?.kind && ctx.group_id ? ctx : null;
|
|
289
|
+
}
|
|
290
|
+
function decisionsFrom(data) {
|
|
291
|
+
const decisions = /* @__PURE__ */ new Map();
|
|
292
|
+
const walk = (value) => {
|
|
293
|
+
if (Array.isArray(value)) return value.forEach(walk);
|
|
294
|
+
if (!value || typeof value !== "object") return;
|
|
295
|
+
const ctx = contextOf(value);
|
|
296
|
+
if (ctx?.group_id && ctx.variant_id) {
|
|
297
|
+
const ids = decisions.get(ctx.group_id) ?? [];
|
|
298
|
+
if (!ids.includes(ctx.variant_id)) ids.push(ctx.variant_id);
|
|
299
|
+
decisions.set(ctx.group_id, ids);
|
|
300
|
+
}
|
|
301
|
+
Object.values(value).forEach(walk);
|
|
302
|
+
};
|
|
303
|
+
walk(data);
|
|
304
|
+
return decisions;
|
|
305
|
+
}
|
|
306
|
+
function chosenVariants(ctx, options) {
|
|
307
|
+
const decided = options.decisions?.get(ctx.group_id);
|
|
308
|
+
if (ctx.kind === "AB_TEST") {
|
|
309
|
+
return options.bucket ? [options.bucket] : decided ?? ["A"];
|
|
310
|
+
}
|
|
311
|
+
return decided ?? [PREPR_DEFAULT_VARIANT];
|
|
312
|
+
}
|
|
313
|
+
function resolveVariants(data, options = {}) {
|
|
314
|
+
const resolve = (value) => {
|
|
315
|
+
if (Array.isArray(value)) {
|
|
316
|
+
return value.filter((item) => {
|
|
317
|
+
const ctx = contextOf(item);
|
|
318
|
+
return !ctx || chosenVariants(ctx, options).includes(ctx.variant_id ?? "");
|
|
319
|
+
}).map(resolve);
|
|
320
|
+
}
|
|
321
|
+
if (!value || typeof value !== "object") return value;
|
|
322
|
+
const out = {};
|
|
323
|
+
for (const [key, child] of Object.entries(value)) out[key] = resolve(child);
|
|
324
|
+
return out;
|
|
325
|
+
};
|
|
326
|
+
return resolve(data);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// src/core/variants/schema.ts
|
|
330
|
+
var PREPR_SCHEMA_QUERY = `query PreprPersonalizationSchema {
|
|
331
|
+
__schema {
|
|
332
|
+
queryType { name }
|
|
333
|
+
types {
|
|
334
|
+
name
|
|
335
|
+
possibleTypes { name }
|
|
336
|
+
fields {
|
|
337
|
+
name
|
|
338
|
+
args { name }
|
|
339
|
+
type { name ofType { name ofType { name ofType { name ofType { name } } } } }
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}`;
|
|
344
|
+
function namedType(ref) {
|
|
345
|
+
let current = ref;
|
|
346
|
+
while (current && !current.name) current = current.ofType;
|
|
347
|
+
return current?.name ?? "";
|
|
348
|
+
}
|
|
349
|
+
function toSchemaInfo(introspection) {
|
|
350
|
+
const types = {};
|
|
351
|
+
for (const type of introspection.__schema.types) {
|
|
352
|
+
if (type.name.startsWith("__")) continue;
|
|
353
|
+
const fields = {};
|
|
354
|
+
for (const field of type.fields ?? []) {
|
|
355
|
+
fields[field.name] = {
|
|
356
|
+
type: namedType(field.type),
|
|
357
|
+
personalizable: field.args.some((arg) => arg.name === "personalize")
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
types[type.name] = {
|
|
361
|
+
fields,
|
|
362
|
+
possibleTypes: (type.possibleTypes ?? []).map((t) => t.name),
|
|
363
|
+
hasContext: "_context" in fields
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
return { queryType: introspection.__schema.queryType.name, types };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// src/core/variants/stack.ts
|
|
370
|
+
function containsKind(value, kind) {
|
|
371
|
+
if (Array.isArray(value))
|
|
372
|
+
return value.some((item) => containsKind(item, kind));
|
|
373
|
+
if (!value || typeof value !== "object") return false;
|
|
374
|
+
if (contextOf(value)?.kind === kind) return true;
|
|
375
|
+
return Object.values(value).some((child) => containsKind(child, kind));
|
|
376
|
+
}
|
|
377
|
+
function splitStack(items) {
|
|
378
|
+
const blocks = [];
|
|
379
|
+
let position = 0;
|
|
380
|
+
const positionsOf = (block) => block.key.startsWith("group:") ? 1 : block.items.length;
|
|
381
|
+
items.forEach((item, index) => {
|
|
382
|
+
const ctx = contextOf(item);
|
|
383
|
+
const groupKey = ctx ? `group:${ctx.group_id}` : null;
|
|
384
|
+
const personalized = containsKind(item, "PERSONALIZATION");
|
|
385
|
+
const abTest = containsKind(item, "AB_TEST");
|
|
386
|
+
const last = blocks.at(-1);
|
|
387
|
+
const plain = !groupKey && !personalized && !abTest;
|
|
388
|
+
if (groupKey && last?.key === groupKey) {
|
|
389
|
+
last.personalized || (last.personalized = personalized);
|
|
390
|
+
last.abTest || (last.abTest = abTest);
|
|
391
|
+
last.items.push(item);
|
|
392
|
+
} else if (plain && last && !last.personalized && !last.abTest) {
|
|
393
|
+
last.items.push(item);
|
|
394
|
+
} else {
|
|
395
|
+
const last2 = blocks.at(-1);
|
|
396
|
+
if (last2) position = last2.index + positionsOf(last2);
|
|
397
|
+
blocks.push({
|
|
398
|
+
key: groupKey ?? `items:${index}`,
|
|
399
|
+
items: [item],
|
|
400
|
+
index: position,
|
|
401
|
+
personalized,
|
|
402
|
+
abTest
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
return blocks;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// src/core/variants/tracking.ts
|
|
410
|
+
function preprVariantProps(item) {
|
|
411
|
+
const key = contextOf(item)?.variant_key;
|
|
412
|
+
return key ? { "data-prepr-variant-key": key } : {};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// src/core/variants/variants-document.ts
|
|
416
|
+
function variantsSelectionSet(set, typeName, schema) {
|
|
417
|
+
const selections = set.selections.map((selection) => {
|
|
418
|
+
if (selection.kind === "InlineFragment") {
|
|
419
|
+
return {
|
|
420
|
+
...selection,
|
|
421
|
+
selectionSet: variantsSelectionSet(
|
|
422
|
+
selection.selectionSet,
|
|
423
|
+
selection.typeCondition?.name.value ?? typeName,
|
|
424
|
+
schema
|
|
425
|
+
)
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
if (selection.kind !== "Field" || !selection.selectionSet) return selection;
|
|
429
|
+
const field = schema.types[typeName]?.fields[selection.name.value];
|
|
430
|
+
if (!field) return selection;
|
|
431
|
+
const child = variantsSelectionSet(
|
|
432
|
+
selection.selectionSet,
|
|
433
|
+
field.type,
|
|
434
|
+
schema
|
|
435
|
+
);
|
|
436
|
+
if (!field.personalizable) return { ...selection, selectionSet: child };
|
|
437
|
+
return {
|
|
438
|
+
...selection,
|
|
439
|
+
arguments: [
|
|
440
|
+
...withoutPersonalizeArg(selection.arguments),
|
|
441
|
+
{
|
|
442
|
+
kind: "Argument",
|
|
443
|
+
name: name("personalize"),
|
|
444
|
+
value: { kind: "BooleanValue", value: false }
|
|
445
|
+
}
|
|
446
|
+
],
|
|
447
|
+
selectionSet: selectionSet([
|
|
448
|
+
...child.selections,
|
|
449
|
+
...contextSelections(field.type, schema)
|
|
450
|
+
])
|
|
451
|
+
};
|
|
452
|
+
});
|
|
453
|
+
return { ...set, selections };
|
|
454
|
+
}
|
|
455
|
+
function toVariantsDocument(document2, schema) {
|
|
456
|
+
const doc = documentOf(document2);
|
|
457
|
+
return {
|
|
458
|
+
...doc,
|
|
459
|
+
definitions: doc.definitions.map((definition) => {
|
|
460
|
+
if (definition.kind === "OperationDefinition") {
|
|
461
|
+
return {
|
|
462
|
+
...definition,
|
|
463
|
+
selectionSet: variantsSelectionSet(
|
|
464
|
+
definition.selectionSet,
|
|
465
|
+
schema.queryType,
|
|
466
|
+
schema
|
|
467
|
+
)
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
if (definition.kind === "FragmentDefinition") {
|
|
471
|
+
return {
|
|
472
|
+
...definition,
|
|
473
|
+
selectionSet: variantsSelectionSet(
|
|
474
|
+
definition.selectionSet,
|
|
475
|
+
definition.typeCondition.name.value,
|
|
476
|
+
schema
|
|
477
|
+
)
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
return definition;
|
|
481
|
+
})
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// src/core/graphql/transport.ts
|
|
486
|
+
import { VERCEL_STEGA_REGEX } from "@vercel/stega";
|
|
487
|
+
|
|
488
|
+
// src/core/stega/clean.ts
|
|
489
|
+
import { vercelStegaClean, vercelStegaDecode } from "@vercel/stega";
|
|
490
|
+
|
|
491
|
+
// src/core/utils.ts
|
|
492
|
+
var DebugLogger = class {
|
|
493
|
+
constructor(options) {
|
|
494
|
+
this.options = {
|
|
495
|
+
prefix: "[Prepr]",
|
|
496
|
+
...options
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
// A local `enabled` wins; otherwise defer to the global logger so scoped
|
|
500
|
+
// loggers created before initDebugLogger() still pick up the setting.
|
|
501
|
+
isEnabled() {
|
|
502
|
+
if (this.options.enabled !== void 0) {
|
|
503
|
+
return this.options.enabled;
|
|
504
|
+
}
|
|
505
|
+
return globalDebugLogger?.options?.enabled ?? false;
|
|
506
|
+
}
|
|
507
|
+
log(message, ...args) {
|
|
508
|
+
if (!this.isEnabled()) return;
|
|
509
|
+
const prefix = this.options.prefix;
|
|
510
|
+
console.log(`${prefix} ${message}`, ...args);
|
|
511
|
+
}
|
|
512
|
+
warn(message, ...args) {
|
|
513
|
+
if (!this.isEnabled()) return;
|
|
514
|
+
const prefix = this.options.prefix;
|
|
515
|
+
console.warn(`${prefix} ${message}`, ...args);
|
|
516
|
+
}
|
|
517
|
+
error(message, ...args) {
|
|
518
|
+
if (!this.isEnabled()) return;
|
|
519
|
+
const prefix = this.options.prefix;
|
|
520
|
+
console.error(`${prefix} ${message}`, ...args);
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
var globalDebugLogger = null;
|
|
524
|
+
function createScopedLogger(scopeName) {
|
|
525
|
+
return new DebugLogger({
|
|
526
|
+
prefix: `[Prepr][${scopeName}]`
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// src/core/stega/clean.ts
|
|
531
|
+
var debug = createScopedLogger("stega:clean");
|
|
532
|
+
function stegaClean(text) {
|
|
533
|
+
if (!text) return text;
|
|
534
|
+
return vercelStegaClean(text);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// src/core/graphql/document.ts
|
|
538
|
+
import { print } from "graphql/language/printer.js";
|
|
539
|
+
import { stripIgnoredCharacters } from "graphql/utilities/stripIgnoredCharacters.js";
|
|
540
|
+
var printed = /* @__PURE__ */ new WeakMap();
|
|
541
|
+
function isDocumentNode(value) {
|
|
542
|
+
return typeof value === "object" && value !== null && value.kind === "Document" && Array.isArray(value.definitions);
|
|
543
|
+
}
|
|
544
|
+
function invalidDocument(originalError) {
|
|
545
|
+
return new PreprError(
|
|
546
|
+
"Expected a GraphQL query string, TypedDocumentString or DocumentNode",
|
|
547
|
+
"INVALID_DOCUMENT",
|
|
548
|
+
"createPreprClient",
|
|
549
|
+
originalError
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
var minified = /* @__PURE__ */ new Map();
|
|
553
|
+
var MINIFIED_CACHE_LIMIT = 500;
|
|
554
|
+
function minify(query) {
|
|
555
|
+
const cached = minified.get(query);
|
|
556
|
+
if (cached !== void 0) return cached;
|
|
557
|
+
let result = query;
|
|
558
|
+
try {
|
|
559
|
+
result = stripIgnoredCharacters(query);
|
|
560
|
+
} catch {
|
|
561
|
+
}
|
|
562
|
+
if (minified.size < MINIFIED_CACHE_LIMIT) minified.set(query, result);
|
|
563
|
+
return result;
|
|
564
|
+
}
|
|
565
|
+
function resolveQuery(document2) {
|
|
566
|
+
if (typeof document2 === "string") return minify(document2);
|
|
567
|
+
if (document2 instanceof String) return minify(document2.toString());
|
|
568
|
+
if (!isDocumentNode(document2)) throw invalidDocument();
|
|
569
|
+
let query = printed.get(document2);
|
|
570
|
+
if (query === void 0) {
|
|
571
|
+
try {
|
|
572
|
+
query = minify(print(document2));
|
|
573
|
+
} catch (error) {
|
|
574
|
+
throw invalidDocument(error instanceof Error ? error : void 0);
|
|
575
|
+
}
|
|
576
|
+
printed.set(document2, query);
|
|
577
|
+
}
|
|
578
|
+
return query;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// src/core/graphql/transport.ts
|
|
582
|
+
var OPERATION_NAME = /\b(?:query|mutation|subscription)\s+([_A-Za-z][_0-9A-Za-z]*)/;
|
|
583
|
+
var STEGA_RAW = new RegExp(
|
|
584
|
+
VERCEL_STEGA_REGEX.source,
|
|
585
|
+
VERCEL_STEGA_REGEX.flags.replace("g", "")
|
|
586
|
+
);
|
|
587
|
+
var STEGA_ESCAPED = /\\u(?:200[bcd]|206[0-3]|feff|d834)/i;
|
|
588
|
+
var URL_KEY = /(url|slug|link|href)$/i;
|
|
589
|
+
var FASTLY_QUERY_MAX = 96 * 1024;
|
|
590
|
+
function base64(text) {
|
|
591
|
+
const bytes = new TextEncoder().encode(text);
|
|
592
|
+
let binary = "";
|
|
593
|
+
for (let i = 0; i < bytes.length; i += 32768) {
|
|
594
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
595
|
+
}
|
|
596
|
+
return btoa(binary);
|
|
597
|
+
}
|
|
598
|
+
function cacheInit(init, noStore) {
|
|
599
|
+
const result = { ...init };
|
|
600
|
+
if (noStore) {
|
|
601
|
+
result.cache = "no-store";
|
|
602
|
+
delete result.next;
|
|
603
|
+
}
|
|
604
|
+
return result;
|
|
605
|
+
}
|
|
606
|
+
function isFrameworkSignal(error) {
|
|
607
|
+
return typeof error === "object" && error !== null && typeof error.digest === "string";
|
|
608
|
+
}
|
|
609
|
+
function mergePreprHeaders(base, extra) {
|
|
610
|
+
const merged = new Headers(base);
|
|
611
|
+
new Headers(extra).forEach((value, key) => {
|
|
612
|
+
merged.set(key, value);
|
|
613
|
+
});
|
|
614
|
+
return merged;
|
|
615
|
+
}
|
|
616
|
+
function cleanEveryString(_key, value) {
|
|
617
|
+
return typeof value === "string" ? stegaClean(value) : value;
|
|
618
|
+
}
|
|
619
|
+
function cleanUrlStrings(key, value) {
|
|
620
|
+
return typeof value === "string" && URL_KEY.test(key) ? stegaClean(value) : value;
|
|
621
|
+
}
|
|
622
|
+
function parseBody(text, stega) {
|
|
623
|
+
if (stega === "none" || !(STEGA_RAW.test(text) || STEGA_ESCAPED.test(text))) {
|
|
624
|
+
return JSON.parse(text);
|
|
625
|
+
}
|
|
626
|
+
return JSON.parse(text, stega === "all" ? cleanEveryString : cleanUrlStrings);
|
|
627
|
+
}
|
|
628
|
+
function createTransport(options) {
|
|
629
|
+
const { url, preview = false } = options;
|
|
630
|
+
const stega = options.stega ?? (preview ? "urls" : "all");
|
|
631
|
+
const clientHeaders = new Headers(options.headers);
|
|
632
|
+
return async (document2, variables, queryOptions = {}) => {
|
|
633
|
+
const query = resolveQuery(document2);
|
|
634
|
+
const operation = OPERATION_NAME.exec(query)?.[1] ?? "anonymous";
|
|
635
|
+
const headers = new Headers(clientHeaders);
|
|
636
|
+
for (const layer of [queryOptions.init?.headers, queryOptions.headers]) {
|
|
637
|
+
new Headers(layer).forEach((value, key) => {
|
|
638
|
+
headers.set(key, value);
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
headers.set("Content-Type", "application/json");
|
|
642
|
+
const perRequestHeaders = queryOptions.headers !== void 0 || queryOptions.init?.headers !== void 0;
|
|
643
|
+
const payload = JSON.stringify({ query, variables });
|
|
644
|
+
const fastly = options.fastlyQuery ? base64(payload) : null;
|
|
645
|
+
const viaGet = fastly !== null && fastly.length <= FASTLY_QUERY_MAX;
|
|
646
|
+
if (viaGet) headers.set("Fastly-Query", fastly);
|
|
647
|
+
const init = {
|
|
648
|
+
...cacheInit(queryOptions.init, preview || perRequestHeaders),
|
|
649
|
+
method: viaGet ? "GET" : "POST",
|
|
650
|
+
headers,
|
|
651
|
+
body: viaGet ? void 0 : payload
|
|
652
|
+
};
|
|
653
|
+
const fetchImpl = queryOptions.fetch ?? options.fetch;
|
|
654
|
+
let response;
|
|
655
|
+
try {
|
|
656
|
+
response = await (fetchImpl ? fetchImpl(url, init) : fetch(url, init));
|
|
657
|
+
} catch (error) {
|
|
658
|
+
if (isFrameworkSignal(error)) throw error;
|
|
659
|
+
throw new PreprError(
|
|
660
|
+
`Prepr GraphQL request failed for ${operation}`,
|
|
661
|
+
"FETCH_ERROR",
|
|
662
|
+
operation,
|
|
663
|
+
error instanceof Error ? error : void 0
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
if (!response.ok) {
|
|
667
|
+
throw new PreprError(
|
|
668
|
+
`Prepr GraphQL HTTP ${response.status} for ${operation}${response.status === 412 ? " (Prepr rejects queries longer than 27,500 characters)" : ""}`,
|
|
669
|
+
"HTTP_ERROR",
|
|
670
|
+
operation
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
let body;
|
|
674
|
+
try {
|
|
675
|
+
body = parseBody(await response.text(), stega);
|
|
676
|
+
} catch (error) {
|
|
677
|
+
throw new PreprError(
|
|
678
|
+
`Prepr GraphQL response for ${operation} is not valid JSON`,
|
|
679
|
+
"INVALID_RESPONSE",
|
|
680
|
+
operation,
|
|
681
|
+
error instanceof Error ? error : void 0
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
if (typeof body !== "object" || body === null) {
|
|
685
|
+
throw new PreprError(
|
|
686
|
+
`Prepr GraphQL response for ${operation} is not an object`,
|
|
687
|
+
"INVALID_RESPONSE",
|
|
688
|
+
operation
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
const { data, errors } = body;
|
|
692
|
+
if (Array.isArray(errors) && errors.length > 0) {
|
|
693
|
+
const messages = errors.map((e) => String(e?.message ?? "Unknown error")).join("; ");
|
|
694
|
+
throw new PreprError(
|
|
695
|
+
`Prepr GraphQL errors for ${operation}: ${messages}`,
|
|
696
|
+
"GRAPHQL_ERROR",
|
|
697
|
+
operation
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
if (data === null || data === void 0) {
|
|
701
|
+
throw new PreprError(
|
|
702
|
+
`Prepr GraphQL response for ${operation} has no data`,
|
|
703
|
+
"INVALID_RESPONSE",
|
|
704
|
+
operation
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
return { data, personalized: isPersonalizedResponse(response) };
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// src/core/graphql/probe.ts
|
|
712
|
+
var AB_TEST_VARY = /\bPrepr-ABTesting/i;
|
|
713
|
+
var SEGMENTS_VARY = /\bPrepr-Segments/i;
|
|
714
|
+
var AB_CHECK_TTL_MS = 6e4;
|
|
715
|
+
var AB_CHECK_MAX_ENTRIES = 1e3;
|
|
716
|
+
function createVaryProbe(run, options) {
|
|
717
|
+
const abProbes = /* @__PURE__ */ new Map();
|
|
718
|
+
const abChecks = /* @__PURE__ */ new Map();
|
|
719
|
+
return (document2, variables) => {
|
|
720
|
+
const key = JSON.stringify(variables ?? null);
|
|
721
|
+
let answers = abChecks.get(document2);
|
|
722
|
+
if (!answers) {
|
|
723
|
+
answers = /* @__PURE__ */ new Map();
|
|
724
|
+
abChecks.set(document2, answers);
|
|
725
|
+
}
|
|
726
|
+
const cached = answers.get(key);
|
|
727
|
+
if (cached && Date.now() - cached.at < AB_CHECK_TTL_MS) return cached.value;
|
|
728
|
+
const value = (async () => {
|
|
729
|
+
let probe = abProbes.get(document2);
|
|
730
|
+
if (probe === void 0) {
|
|
731
|
+
probe = toAbProbeDocument(document2);
|
|
732
|
+
abProbes.set(document2, probe);
|
|
733
|
+
}
|
|
734
|
+
if (!probe) return "";
|
|
735
|
+
let vary = "";
|
|
736
|
+
const send = options.fetch ?? fetch;
|
|
737
|
+
await run(probe, variables, {
|
|
738
|
+
fetch: async (input, init) => {
|
|
739
|
+
const response = await send(input, init);
|
|
740
|
+
vary = response.headers.get("vary") ?? "";
|
|
741
|
+
return response;
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
return vary;
|
|
745
|
+
})().catch((error) => {
|
|
746
|
+
if (isFrameworkSignal(error)) throw error;
|
|
747
|
+
answers.delete(key);
|
|
748
|
+
return null;
|
|
749
|
+
});
|
|
750
|
+
if (answers.size >= AB_CHECK_MAX_ENTRIES) {
|
|
751
|
+
answers.delete(answers.keys().next().value);
|
|
752
|
+
}
|
|
753
|
+
answers.set(key, { value, at: Date.now() });
|
|
754
|
+
return value;
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// src/core/graphql/client.ts
|
|
759
|
+
function isParseableUrl(url) {
|
|
760
|
+
try {
|
|
761
|
+
new URL(url);
|
|
762
|
+
return true;
|
|
763
|
+
} catch {
|
|
764
|
+
return false;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
function createPreprClient(options) {
|
|
768
|
+
const { url } = options;
|
|
769
|
+
validatePreprToken(url);
|
|
770
|
+
if (!isParseableUrl(url)) {
|
|
771
|
+
throw new PreprError(
|
|
772
|
+
"Prepr GraphQL URL is not a valid URL",
|
|
773
|
+
"INVALID_TOKEN",
|
|
774
|
+
"createPreprClient"
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
const run = createTransport(options);
|
|
778
|
+
const probeVary = createVaryProbe(run, options);
|
|
779
|
+
let schema;
|
|
780
|
+
const loadSchema = () => schema ?? (schema = run(
|
|
781
|
+
PREPR_SCHEMA_QUERY,
|
|
782
|
+
void 0,
|
|
783
|
+
{ init: options.schemaInit }
|
|
784
|
+
).then(
|
|
785
|
+
(result) => toSchemaInfo(result.data),
|
|
786
|
+
(error) => {
|
|
787
|
+
schema = void 0;
|
|
788
|
+
throw error;
|
|
789
|
+
}
|
|
790
|
+
));
|
|
791
|
+
const variantsDocuments = /* @__PURE__ */ new Map();
|
|
792
|
+
const decisionDocuments = /* @__PURE__ */ new Map();
|
|
793
|
+
return {
|
|
794
|
+
async query(document2, variables, queryOptions = {}) {
|
|
795
|
+
const { variants, ...rest } = queryOptions;
|
|
796
|
+
if (!variants) return run(document2, variables, rest);
|
|
797
|
+
let derived = variantsDocuments.get(document2);
|
|
798
|
+
if (!derived) {
|
|
799
|
+
derived = toVariantsDocument(document2, await loadSchema());
|
|
800
|
+
variantsDocuments.set(document2, derived);
|
|
801
|
+
}
|
|
802
|
+
return run(derived, variables, rest);
|
|
803
|
+
},
|
|
804
|
+
async decide(document2, variables, queryOptions = {}) {
|
|
805
|
+
const { from, topLevelOnly, ...rest } = queryOptions;
|
|
806
|
+
const paths = from === void 0 ? void 0 : personalizationPaths(from);
|
|
807
|
+
if (paths && paths.size === 0) return /* @__PURE__ */ new Map();
|
|
808
|
+
const signature = paths ? [...paths].map(([path, types]) => `${path}:${[...types].sort().join(",")}`).sort().join("|") : topLevelOnly ? "top" : "*";
|
|
809
|
+
let byPaths = decisionDocuments.get(document2);
|
|
810
|
+
if (!byPaths) {
|
|
811
|
+
byPaths = /* @__PURE__ */ new Map();
|
|
812
|
+
decisionDocuments.set(document2, byPaths);
|
|
813
|
+
}
|
|
814
|
+
let derived = byPaths.get(signature);
|
|
815
|
+
if (derived === void 0) {
|
|
816
|
+
derived = toDecisionDocument(document2, await loadSchema(), paths, {
|
|
817
|
+
topLevelOnly: !paths && topLevelOnly
|
|
818
|
+
});
|
|
819
|
+
byPaths.set(signature, derived);
|
|
820
|
+
}
|
|
821
|
+
if (!derived) return /* @__PURE__ */ new Map();
|
|
822
|
+
const { data } = await run(derived, variables, rest);
|
|
823
|
+
return decisionsFrom(data);
|
|
824
|
+
},
|
|
825
|
+
hasAbTests: (document2, variables) => probeVary(document2, variables).then(
|
|
826
|
+
(vary) => vary === null || AB_TEST_VARY.test(vary)
|
|
827
|
+
),
|
|
828
|
+
hasSegments: (document2, variables) => probeVary(document2, variables).then(
|
|
829
|
+
(vary) => vary === null || SEGMENTS_VARY.test(vary)
|
|
830
|
+
)
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// src/core/graphql/event-client.ts
|
|
835
|
+
function createEventClient(options, adapter) {
|
|
836
|
+
const client = createPreprClient(options);
|
|
837
|
+
return {
|
|
838
|
+
async query(document2, variables, queryOptions = {}) {
|
|
839
|
+
const { personalize, event, ...rest } = queryOptions;
|
|
840
|
+
if (personalize && event === void 0) {
|
|
841
|
+
throw new PreprError(
|
|
842
|
+
"personalize: true requires the request event",
|
|
843
|
+
"CONTEXT_ERROR",
|
|
844
|
+
"createPreprClient"
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
const fetchImpl = rest.fetch ?? (event === void 0 ? void 0 : adapter.fetch?.(event));
|
|
848
|
+
const headers = personalize ? mergePreprHeaders(
|
|
849
|
+
getPreprHeadersFromHeaders(adapter.headers(event)),
|
|
850
|
+
rest.headers
|
|
851
|
+
) : rest.headers;
|
|
852
|
+
return client.query(document2, variables, {
|
|
853
|
+
...rest,
|
|
854
|
+
headers,
|
|
855
|
+
fetch: fetchImpl
|
|
856
|
+
});
|
|
857
|
+
},
|
|
858
|
+
async decide(document2, variables, queryOptions) {
|
|
859
|
+
const { event, ...rest } = queryOptions;
|
|
860
|
+
if (event === void 0) {
|
|
861
|
+
throw new PreprError(
|
|
862
|
+
"decide requires the request event",
|
|
863
|
+
"CONTEXT_ERROR",
|
|
864
|
+
"createPreprClient"
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
return client.decide(document2, variables, {
|
|
868
|
+
...rest,
|
|
869
|
+
headers: mergePreprHeaders(
|
|
870
|
+
getPreprHeadersFromHeaders(adapter.headers(event)),
|
|
871
|
+
rest.headers
|
|
872
|
+
),
|
|
873
|
+
fetch: rest.fetch ?? adapter.fetch?.(event)
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// src/astro/graphql.ts
|
|
880
|
+
function createPreprClient2(options) {
|
|
881
|
+
return createEventClient(options, {
|
|
882
|
+
headers: (event) => event.request.headers
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
export {
|
|
886
|
+
PREPR_DEFAULT_VARIANT,
|
|
887
|
+
PreprError,
|
|
888
|
+
createPreprClient2 as createPreprClient,
|
|
889
|
+
isPersonalizedResponse,
|
|
890
|
+
preprVariantProps,
|
|
891
|
+
resolveVariants,
|
|
892
|
+
splitStack
|
|
893
|
+
};
|