@preprio/toolkit 0.5.0-rc.2 → 0.6.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +415 -27
- package/dist/astro/chunk-O7N6GPAH.js +173 -0
- package/dist/astro/graphql.cjs +1030 -0
- package/dist/astro/graphql.d.cts +305 -0
- package/dist/astro/graphql.d.ts +305 -0
- package/dist/astro/graphql.js +936 -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-3JLWZQ37.js +179 -0
- package/dist/chunk-3NCEVJJ3.js +10 -0
- package/dist/chunk-S5RWVMDJ.js +148 -0
- package/dist/chunk-TBKSBXGH.js +452 -0
- package/dist/{chunk-FLPNN3CV.js → chunk-VI6QZTBM.js} +73 -276
- package/dist/chunk-YWIKKWBR.js +26 -0
- package/dist/graphql/codegen.cjs +510 -0
- package/dist/graphql/codegen.d.cts +23 -0
- package/dist/graphql/codegen.d.ts +23 -0
- package/dist/graphql/codegen.js +99 -0
- package/dist/graphql.cjs +941 -0
- package/dist/graphql.d.cts +305 -0
- package/dist/graphql.d.ts +305 -0
- package/dist/graphql.js +419 -0
- package/dist/index.cjs +76 -119
- package/dist/index.d.cts +12 -64
- package/dist/index.d.ts +12 -64
- package/dist/index.js +27 -167
- package/dist/nextjs/chunk-BQ6IGSH6.js +231 -0
- package/dist/nextjs/components.cjs +190 -122
- package/dist/nextjs/components.d.cts +33 -2
- package/dist/nextjs/components.d.ts +33 -2
- package/dist/nextjs/components.js +133 -4
- package/dist/nextjs/graphql.cjs +1362 -0
- package/dist/nextjs/graphql.d.cts +435 -0
- package/dist/nextjs/graphql.d.ts +435 -0
- package/dist/nextjs/graphql.js +1261 -0
- package/dist/nextjs/index.cjs +126 -6
- package/dist/nextjs/index.d.cts +53 -147
- package/dist/nextjs/index.d.ts +53 -147
- 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-IQDSBPDN.js +417 -0
- package/dist/nuxt/graphql.cjs +1037 -0
- package/dist/nuxt/graphql.d.cts +302 -0
- package/dist/nuxt/graphql.d.ts +302 -0
- package/dist/nuxt/graphql.js +937 -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 +88 -120
- package/dist/react/components.d.cts +2 -8
- package/dist/react/components.d.ts +2 -8
- package/dist/react/components.js +88 -120
- package/dist/server-Brr1UgtW.d.ts +59 -0
- package/dist/server-DJuS3YU0.d.cts +59 -0
- package/dist/sveltekit/chunk-OKYZ6HPH.js +413 -0
- package/dist/sveltekit/graphql.cjs +1043 -0
- package/dist/sveltekit/graphql.d.cts +307 -0
- package/dist/sveltekit/graphql.d.ts +307 -0
- package/dist/sveltekit/graphql.js +943 -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-B3oQNbZ3.d.cts → types-BYH9XqmN.d.cts} +4 -58
- package/dist/{pixel-B3oQNbZ3.d.ts → types-BYH9XqmN.d.ts} +4 -58
- package/package.json +36 -1
package/dist/graphql.js
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isPersonalizedResponse
|
|
3
|
+
} from "./chunk-3NCEVJJ3.js";
|
|
4
|
+
import {
|
|
5
|
+
stegaClean
|
|
6
|
+
} from "./chunk-S5RWVMDJ.js";
|
|
7
|
+
import {
|
|
8
|
+
PREPR_DEFAULT_VARIANT,
|
|
9
|
+
PREPR_SCHEMA_QUERY,
|
|
10
|
+
collectVariables,
|
|
11
|
+
contextOf,
|
|
12
|
+
decisionsFrom,
|
|
13
|
+
documentOf,
|
|
14
|
+
name,
|
|
15
|
+
personalizationPaths,
|
|
16
|
+
resolveQuery,
|
|
17
|
+
resolveVariants,
|
|
18
|
+
selectionSet,
|
|
19
|
+
toDecisionDocument,
|
|
20
|
+
toDecisionQuery,
|
|
21
|
+
toSchemaInfo,
|
|
22
|
+
toVariantsDocument,
|
|
23
|
+
toVariantsQuery
|
|
24
|
+
} from "./chunk-TBKSBXGH.js";
|
|
25
|
+
import {
|
|
26
|
+
PreprError,
|
|
27
|
+
validatePreprToken
|
|
28
|
+
} from "./chunk-3JLWZQ37.js";
|
|
29
|
+
import "./chunk-YWIKKWBR.js";
|
|
30
|
+
|
|
31
|
+
// src/core/variants/probe-document.ts
|
|
32
|
+
function probeSelectionSet(set, fragments) {
|
|
33
|
+
const selections = [
|
|
34
|
+
{ kind: "Field", name: name("__typename") }
|
|
35
|
+
];
|
|
36
|
+
for (const selection of set.selections) {
|
|
37
|
+
if (selection.kind === "Field") {
|
|
38
|
+
if (!selection.selectionSet) continue;
|
|
39
|
+
selections.push({
|
|
40
|
+
...selection,
|
|
41
|
+
selectionSet: probeSelectionSet(selection.selectionSet, fragments)
|
|
42
|
+
});
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const fragment = selection.kind === "InlineFragment" ? selection : fragments[selection.name.value];
|
|
46
|
+
if (!fragment) continue;
|
|
47
|
+
const child = probeSelectionSet(fragment.selectionSet, fragments);
|
|
48
|
+
if (child.selections.length === 1) continue;
|
|
49
|
+
selections.push({
|
|
50
|
+
kind: "InlineFragment",
|
|
51
|
+
typeCondition: fragment.typeCondition,
|
|
52
|
+
directives: selection.directives,
|
|
53
|
+
selectionSet: child
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return selectionSet(selections);
|
|
57
|
+
}
|
|
58
|
+
function toAbProbeDocument(document) {
|
|
59
|
+
const doc = documentOf(document);
|
|
60
|
+
const fragments = {};
|
|
61
|
+
for (const definition of doc.definitions) {
|
|
62
|
+
if (definition.kind === "FragmentDefinition") {
|
|
63
|
+
fragments[definition.name.value] = definition;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const operation = doc.definitions.find(
|
|
67
|
+
(definition) => definition.kind === "OperationDefinition"
|
|
68
|
+
);
|
|
69
|
+
if (!operation) return null;
|
|
70
|
+
const probe = probeSelectionSet(operation.selectionSet, fragments);
|
|
71
|
+
const used = /* @__PURE__ */ new Set();
|
|
72
|
+
collectVariables(probe, used);
|
|
73
|
+
collectVariables(operation.directives, used);
|
|
74
|
+
return {
|
|
75
|
+
kind: "Document",
|
|
76
|
+
definitions: [
|
|
77
|
+
{
|
|
78
|
+
...operation,
|
|
79
|
+
name: operation.name && name(`${operation.name.value}AbCheck`),
|
|
80
|
+
variableDefinitions: (operation.variableDefinitions ?? []).filter(
|
|
81
|
+
(definition) => used.has(definition.variable.name.value)
|
|
82
|
+
),
|
|
83
|
+
selectionSet: probe
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/core/variants/stack.ts
|
|
90
|
+
function containsKind(value, kind) {
|
|
91
|
+
if (Array.isArray(value))
|
|
92
|
+
return value.some((item) => containsKind(item, kind));
|
|
93
|
+
if (!value || typeof value !== "object") return false;
|
|
94
|
+
if (contextOf(value)?.kind === kind) return true;
|
|
95
|
+
return Object.values(value).some((child) => containsKind(child, kind));
|
|
96
|
+
}
|
|
97
|
+
function splitStack(items) {
|
|
98
|
+
const blocks = [];
|
|
99
|
+
let position = 0;
|
|
100
|
+
const positionsOf = (block) => block.key.startsWith("group:") ? 1 : block.items.length;
|
|
101
|
+
items.forEach((item, index) => {
|
|
102
|
+
const ctx = contextOf(item);
|
|
103
|
+
const groupKey = ctx ? `group:${ctx.group_id}` : null;
|
|
104
|
+
const personalized = containsKind(item, "PERSONALIZATION");
|
|
105
|
+
const abTest = containsKind(item, "AB_TEST");
|
|
106
|
+
const last = blocks.at(-1);
|
|
107
|
+
const plain = !groupKey && !personalized && !abTest;
|
|
108
|
+
if (groupKey && last?.key === groupKey) {
|
|
109
|
+
last.personalized || (last.personalized = personalized);
|
|
110
|
+
last.abTest || (last.abTest = abTest);
|
|
111
|
+
last.items.push(item);
|
|
112
|
+
} else if (plain && last && !last.personalized && !last.abTest) {
|
|
113
|
+
last.items.push(item);
|
|
114
|
+
} else {
|
|
115
|
+
const last2 = blocks.at(-1);
|
|
116
|
+
if (last2) position = last2.index + positionsOf(last2);
|
|
117
|
+
blocks.push({
|
|
118
|
+
key: groupKey ?? `items:${index}`,
|
|
119
|
+
items: [item],
|
|
120
|
+
index: position,
|
|
121
|
+
personalized,
|
|
122
|
+
abTest
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return blocks;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/core/variants/tracking.ts
|
|
130
|
+
function preprVariantProps(item) {
|
|
131
|
+
const key = contextOf(item)?.variant_key;
|
|
132
|
+
return key ? { "data-prepr-variant-key": key } : {};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/core/graphql/transport.ts
|
|
136
|
+
import { VERCEL_STEGA_REGEX } from "@vercel/stega";
|
|
137
|
+
var OPERATION_NAME = /\b(?:query|mutation|subscription)\s+([_A-Za-z][_0-9A-Za-z]*)/;
|
|
138
|
+
var STEGA_RAW = new RegExp(
|
|
139
|
+
VERCEL_STEGA_REGEX.source,
|
|
140
|
+
VERCEL_STEGA_REGEX.flags.replace("g", "")
|
|
141
|
+
);
|
|
142
|
+
var STEGA_ESCAPED = /\\u(?:200[bcd]|206[0-3]|feff|d834)/i;
|
|
143
|
+
var URL_KEY = /(url|slug|link|href)$/i;
|
|
144
|
+
var FASTLY_QUERY_MAX = 96 * 1024;
|
|
145
|
+
function base64(text) {
|
|
146
|
+
const bytes = new TextEncoder().encode(text);
|
|
147
|
+
let binary = "";
|
|
148
|
+
for (let i = 0; i < bytes.length; i += 32768) {
|
|
149
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
150
|
+
}
|
|
151
|
+
return btoa(binary);
|
|
152
|
+
}
|
|
153
|
+
function cacheInit(init, noStore) {
|
|
154
|
+
const result = { ...init };
|
|
155
|
+
if (noStore) {
|
|
156
|
+
result.cache = "no-store";
|
|
157
|
+
delete result.next;
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
function isFrameworkSignal(error) {
|
|
162
|
+
return typeof error === "object" && error !== null && typeof error.digest === "string";
|
|
163
|
+
}
|
|
164
|
+
function cleanEveryString(_key, value) {
|
|
165
|
+
return typeof value === "string" ? stegaClean(value) : value;
|
|
166
|
+
}
|
|
167
|
+
function cleanUrlStrings(key, value) {
|
|
168
|
+
return typeof value === "string" && URL_KEY.test(key) ? stegaClean(value) : value;
|
|
169
|
+
}
|
|
170
|
+
function parseBody(text, stega) {
|
|
171
|
+
if (stega === "none" || !(STEGA_RAW.test(text) || STEGA_ESCAPED.test(text))) {
|
|
172
|
+
return JSON.parse(text);
|
|
173
|
+
}
|
|
174
|
+
return JSON.parse(text, stega === "all" ? cleanEveryString : cleanUrlStrings);
|
|
175
|
+
}
|
|
176
|
+
function createTransport(options) {
|
|
177
|
+
const { url, preview = false } = options;
|
|
178
|
+
const stega = options.stega ?? (preview ? "urls" : "all");
|
|
179
|
+
const clientHeaders = new Headers(options.headers);
|
|
180
|
+
return async (document, variables, queryOptions = {}) => {
|
|
181
|
+
const query = resolveQuery(document);
|
|
182
|
+
const operation = OPERATION_NAME.exec(query)?.[1] ?? "anonymous";
|
|
183
|
+
const headers = new Headers(clientHeaders);
|
|
184
|
+
for (const layer of [queryOptions.init?.headers, queryOptions.headers]) {
|
|
185
|
+
new Headers(layer).forEach((value, key) => {
|
|
186
|
+
headers.set(key, value);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
headers.set("Content-Type", "application/json");
|
|
190
|
+
const perRequestHeaders = queryOptions.headers !== void 0 || queryOptions.init?.headers !== void 0;
|
|
191
|
+
const payload = JSON.stringify({ query, variables });
|
|
192
|
+
const fastly = options.fastlyQuery ? base64(payload) : null;
|
|
193
|
+
const viaGet = fastly !== null && fastly.length <= FASTLY_QUERY_MAX;
|
|
194
|
+
if (viaGet) headers.set("Fastly-Query", fastly);
|
|
195
|
+
const init = {
|
|
196
|
+
...cacheInit(queryOptions.init, preview || perRequestHeaders),
|
|
197
|
+
method: viaGet ? "GET" : "POST",
|
|
198
|
+
headers,
|
|
199
|
+
body: viaGet ? void 0 : payload
|
|
200
|
+
};
|
|
201
|
+
const fetchImpl = queryOptions.fetch ?? options.fetch;
|
|
202
|
+
let response;
|
|
203
|
+
try {
|
|
204
|
+
response = await (fetchImpl ? fetchImpl(url, init) : fetch(url, init));
|
|
205
|
+
} catch (error) {
|
|
206
|
+
if (isFrameworkSignal(error)) throw error;
|
|
207
|
+
throw new PreprError(
|
|
208
|
+
`Prepr GraphQL request failed for ${operation}`,
|
|
209
|
+
"FETCH_ERROR",
|
|
210
|
+
operation,
|
|
211
|
+
error instanceof Error ? error : void 0
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
if (!response.ok) {
|
|
215
|
+
throw new PreprError(
|
|
216
|
+
`Prepr GraphQL HTTP ${response.status} for ${operation}${response.status === 412 ? " (Prepr rejects queries longer than 27,500 characters)" : ""}`,
|
|
217
|
+
"HTTP_ERROR",
|
|
218
|
+
operation
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
let body;
|
|
222
|
+
try {
|
|
223
|
+
body = parseBody(await response.text(), stega);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
throw new PreprError(
|
|
226
|
+
`Prepr GraphQL response for ${operation} is not valid JSON`,
|
|
227
|
+
"INVALID_RESPONSE",
|
|
228
|
+
operation,
|
|
229
|
+
error instanceof Error ? error : void 0
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
if (typeof body !== "object" || body === null) {
|
|
233
|
+
throw new PreprError(
|
|
234
|
+
`Prepr GraphQL response for ${operation} is not an object`,
|
|
235
|
+
"INVALID_RESPONSE",
|
|
236
|
+
operation
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
const { data, errors } = body;
|
|
240
|
+
if (Array.isArray(errors) && errors.length > 0) {
|
|
241
|
+
const messages = errors.map((e) => String(e?.message ?? "Unknown error")).join("; ");
|
|
242
|
+
throw new PreprError(
|
|
243
|
+
`Prepr GraphQL errors for ${operation}: ${messages}`,
|
|
244
|
+
"GRAPHQL_ERROR",
|
|
245
|
+
operation
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
if (data === null || data === void 0) {
|
|
249
|
+
throw new PreprError(
|
|
250
|
+
`Prepr GraphQL response for ${operation} has no data`,
|
|
251
|
+
"INVALID_RESPONSE",
|
|
252
|
+
operation
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
return { data, personalized: isPersonalizedResponse(response) };
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/core/graphql/probe.ts
|
|
260
|
+
var AB_TEST_VARY = /\bPrepr-ABTesting/i;
|
|
261
|
+
var SEGMENTS_VARY = /\bPrepr-Segments/i;
|
|
262
|
+
var AB_CHECK_TTL_MS = 6e4;
|
|
263
|
+
var AB_CHECK_MAX_ENTRIES = 1e3;
|
|
264
|
+
function createVaryProbe(run, options) {
|
|
265
|
+
const abProbes = /* @__PURE__ */ new Map();
|
|
266
|
+
const abChecks = /* @__PURE__ */ new Map();
|
|
267
|
+
return (document, variables) => {
|
|
268
|
+
const key = JSON.stringify(variables ?? null);
|
|
269
|
+
let answers = abChecks.get(document);
|
|
270
|
+
if (!answers) {
|
|
271
|
+
answers = /* @__PURE__ */ new Map();
|
|
272
|
+
abChecks.set(document, answers);
|
|
273
|
+
}
|
|
274
|
+
const cached = answers.get(key);
|
|
275
|
+
if (cached && Date.now() - cached.at < AB_CHECK_TTL_MS) return cached.value;
|
|
276
|
+
const value = (async () => {
|
|
277
|
+
let probe = abProbes.get(document);
|
|
278
|
+
if (probe === void 0) {
|
|
279
|
+
probe = toAbProbeDocument(document);
|
|
280
|
+
abProbes.set(document, probe);
|
|
281
|
+
}
|
|
282
|
+
if (!probe) return "";
|
|
283
|
+
let vary = "";
|
|
284
|
+
const send = options.fetch ?? fetch;
|
|
285
|
+
await run(probe, variables, {
|
|
286
|
+
fetch: async (input, init) => {
|
|
287
|
+
const response = await send(input, init);
|
|
288
|
+
vary = response.headers.get("vary") ?? "";
|
|
289
|
+
return response;
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
return vary;
|
|
293
|
+
})().catch((error) => {
|
|
294
|
+
if (isFrameworkSignal(error)) throw error;
|
|
295
|
+
answers.delete(key);
|
|
296
|
+
return null;
|
|
297
|
+
});
|
|
298
|
+
if (answers.size >= AB_CHECK_MAX_ENTRIES) {
|
|
299
|
+
answers.delete(answers.keys().next().value);
|
|
300
|
+
}
|
|
301
|
+
answers.set(key, { value, at: Date.now() });
|
|
302
|
+
return value;
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// src/core/graphql/client.ts
|
|
307
|
+
function isParseableUrl(url) {
|
|
308
|
+
try {
|
|
309
|
+
new URL(url);
|
|
310
|
+
return true;
|
|
311
|
+
} catch {
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function assertPreprUrl(url, source) {
|
|
316
|
+
validatePreprToken(url);
|
|
317
|
+
if (!isParseableUrl(url)) {
|
|
318
|
+
throw new PreprError(
|
|
319
|
+
"Prepr GraphQL URL is not a valid URL",
|
|
320
|
+
"INVALID_TOKEN",
|
|
321
|
+
source
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function createPreprClient(options) {
|
|
326
|
+
assertPreprUrl(options.url, "createPreprClient");
|
|
327
|
+
const run = createTransport(options);
|
|
328
|
+
const probeVary = createVaryProbe(run, options);
|
|
329
|
+
let schema;
|
|
330
|
+
const loadSchema = () => schema ?? (schema = run(
|
|
331
|
+
PREPR_SCHEMA_QUERY,
|
|
332
|
+
void 0,
|
|
333
|
+
{ init: options.schemaInit }
|
|
334
|
+
).then(
|
|
335
|
+
(result) => toSchemaInfo(result.data),
|
|
336
|
+
(error) => {
|
|
337
|
+
schema = void 0;
|
|
338
|
+
throw error;
|
|
339
|
+
}
|
|
340
|
+
));
|
|
341
|
+
const variantsDocuments = /* @__PURE__ */ new Map();
|
|
342
|
+
const decisionDocuments = /* @__PURE__ */ new Map();
|
|
343
|
+
return {
|
|
344
|
+
async query(document, variables, queryOptions = {}) {
|
|
345
|
+
const { variants, ...rest } = queryOptions;
|
|
346
|
+
if (!variants) return run(document, variables, rest);
|
|
347
|
+
let derived = variantsDocuments.get(document);
|
|
348
|
+
if (!derived) {
|
|
349
|
+
derived = toVariantsDocument(document, await loadSchema());
|
|
350
|
+
variantsDocuments.set(document, derived);
|
|
351
|
+
}
|
|
352
|
+
return run(derived, variables, rest);
|
|
353
|
+
},
|
|
354
|
+
async decide(document, variables, queryOptions = {}) {
|
|
355
|
+
const { from, topLevelOnly, ...rest } = queryOptions;
|
|
356
|
+
const paths = from === void 0 ? void 0 : personalizationPaths(from);
|
|
357
|
+
if (paths && paths.size === 0) return /* @__PURE__ */ new Map();
|
|
358
|
+
const signature = paths ? [...paths].map(([path, types]) => `${path}:${[...types].sort().join(",")}`).sort().join("|") : topLevelOnly ? "top" : "*";
|
|
359
|
+
let byPaths = decisionDocuments.get(document);
|
|
360
|
+
if (!byPaths) {
|
|
361
|
+
byPaths = /* @__PURE__ */ new Map();
|
|
362
|
+
decisionDocuments.set(document, byPaths);
|
|
363
|
+
}
|
|
364
|
+
let derived = byPaths.get(signature);
|
|
365
|
+
if (derived === void 0) {
|
|
366
|
+
derived = toDecisionDocument(document, await loadSchema(), paths, {
|
|
367
|
+
topLevelOnly: !paths && topLevelOnly
|
|
368
|
+
});
|
|
369
|
+
byPaths.set(signature, derived);
|
|
370
|
+
}
|
|
371
|
+
if (!derived) return /* @__PURE__ */ new Map();
|
|
372
|
+
const { data } = await run(derived, variables, rest);
|
|
373
|
+
return decisionsFrom(data);
|
|
374
|
+
},
|
|
375
|
+
hasAbTests: (document, variables) => probeVary(document, variables).then(
|
|
376
|
+
(vary) => vary === null || AB_TEST_VARY.test(vary)
|
|
377
|
+
),
|
|
378
|
+
hasSegments: (document, variables) => probeVary(document, variables).then(
|
|
379
|
+
(vary) => vary === null || SEGMENTS_VARY.test(vary)
|
|
380
|
+
)
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// src/core/graphql/schema-loader.ts
|
|
385
|
+
var schemas = /* @__PURE__ */ new Map();
|
|
386
|
+
async function loadPreprSchema(url, options = {}) {
|
|
387
|
+
assertPreprUrl(url, "loadPreprSchema");
|
|
388
|
+
let schema = schemas.get(url);
|
|
389
|
+
if (!schema) {
|
|
390
|
+
const run = createTransport({ url, fetch: options.fetch, stega: "none" });
|
|
391
|
+
schema = run(
|
|
392
|
+
PREPR_SCHEMA_QUERY
|
|
393
|
+
).then((result) => toSchemaInfo(result.data)).catch((error) => {
|
|
394
|
+
schemas.delete(url);
|
|
395
|
+
if (error instanceof PreprError) throw error;
|
|
396
|
+
throw new PreprError(
|
|
397
|
+
"Prepr schema response is not an introspection result",
|
|
398
|
+
"INVALID_RESPONSE",
|
|
399
|
+
"loadPreprSchema",
|
|
400
|
+
error instanceof Error ? error : void 0
|
|
401
|
+
);
|
|
402
|
+
});
|
|
403
|
+
schemas.set(url, schema);
|
|
404
|
+
}
|
|
405
|
+
return schema;
|
|
406
|
+
}
|
|
407
|
+
export {
|
|
408
|
+
PREPR_DEFAULT_VARIANT,
|
|
409
|
+
PreprError,
|
|
410
|
+
createPreprClient,
|
|
411
|
+
decisionsFrom,
|
|
412
|
+
isPersonalizedResponse,
|
|
413
|
+
loadPreprSchema,
|
|
414
|
+
preprVariantProps,
|
|
415
|
+
resolveVariants,
|
|
416
|
+
splitStack,
|
|
417
|
+
toDecisionQuery,
|
|
418
|
+
toVariantsQuery
|
|
419
|
+
};
|