@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.
Files changed (78) hide show
  1. package/README.md +415 -27
  2. package/dist/astro/chunk-O7N6GPAH.js +173 -0
  3. package/dist/astro/graphql.cjs +1030 -0
  4. package/dist/astro/graphql.d.cts +305 -0
  5. package/dist/astro/graphql.d.ts +305 -0
  6. package/dist/astro/graphql.js +936 -0
  7. package/dist/astro/index-XT_z-RYT.d.cts +130 -0
  8. package/dist/astro/index-XT_z-RYT.d.ts +130 -0
  9. package/dist/astro/index.cjs +14 -1
  10. package/dist/astro/index.d.cts +1 -120
  11. package/dist/astro/index.d.ts +1 -120
  12. package/dist/astro/index.js +15 -153
  13. package/dist/chunk-3JLWZQ37.js +179 -0
  14. package/dist/chunk-3NCEVJJ3.js +10 -0
  15. package/dist/chunk-S5RWVMDJ.js +148 -0
  16. package/dist/chunk-TBKSBXGH.js +452 -0
  17. package/dist/{chunk-FLPNN3CV.js → chunk-VI6QZTBM.js} +73 -276
  18. package/dist/chunk-YWIKKWBR.js +26 -0
  19. package/dist/graphql/codegen.cjs +510 -0
  20. package/dist/graphql/codegen.d.cts +23 -0
  21. package/dist/graphql/codegen.d.ts +23 -0
  22. package/dist/graphql/codegen.js +99 -0
  23. package/dist/graphql.cjs +941 -0
  24. package/dist/graphql.d.cts +305 -0
  25. package/dist/graphql.d.ts +305 -0
  26. package/dist/graphql.js +419 -0
  27. package/dist/index.cjs +76 -119
  28. package/dist/index.d.cts +12 -64
  29. package/dist/index.d.ts +12 -64
  30. package/dist/index.js +27 -167
  31. package/dist/nextjs/chunk-BQ6IGSH6.js +231 -0
  32. package/dist/nextjs/components.cjs +190 -122
  33. package/dist/nextjs/components.d.cts +33 -2
  34. package/dist/nextjs/components.d.ts +33 -2
  35. package/dist/nextjs/components.js +133 -4
  36. package/dist/nextjs/graphql.cjs +1362 -0
  37. package/dist/nextjs/graphql.d.cts +435 -0
  38. package/dist/nextjs/graphql.d.ts +435 -0
  39. package/dist/nextjs/graphql.js +1261 -0
  40. package/dist/nextjs/index.cjs +126 -6
  41. package/dist/nextjs/index.d.cts +53 -147
  42. package/dist/nextjs/index.d.ts +53 -147
  43. package/dist/nextjs/index.js +113 -203
  44. package/dist/nextjs/server-CjpELYvq.d.cts +156 -0
  45. package/dist/nextjs/server-CjpELYvq.d.ts +156 -0
  46. package/dist/nuxt/chunk-IQDSBPDN.js +417 -0
  47. package/dist/nuxt/graphql.cjs +1037 -0
  48. package/dist/nuxt/graphql.d.cts +302 -0
  49. package/dist/nuxt/graphql.d.ts +302 -0
  50. package/dist/nuxt/graphql.js +937 -0
  51. package/dist/nuxt/index.cjs +14 -1
  52. package/dist/nuxt/index.d.cts +3 -148
  53. package/dist/nuxt/index.d.ts +3 -148
  54. package/dist/nuxt/index.js +15 -391
  55. package/dist/nuxt/server-8v6OHbsw.d.cts +158 -0
  56. package/dist/nuxt/server-8v6OHbsw.d.ts +158 -0
  57. package/dist/pixel-DwW7cXJm.d.cts +49 -0
  58. package/dist/pixel-DwW7cXJm.d.ts +49 -0
  59. package/dist/react/components.cjs +88 -120
  60. package/dist/react/components.d.cts +2 -8
  61. package/dist/react/components.d.ts +2 -8
  62. package/dist/react/components.js +88 -120
  63. package/dist/server-Brr1UgtW.d.ts +59 -0
  64. package/dist/server-DJuS3YU0.d.cts +59 -0
  65. package/dist/sveltekit/chunk-OKYZ6HPH.js +413 -0
  66. package/dist/sveltekit/graphql.cjs +1043 -0
  67. package/dist/sveltekit/graphql.d.cts +307 -0
  68. package/dist/sveltekit/graphql.d.ts +307 -0
  69. package/dist/sveltekit/graphql.js +943 -0
  70. package/dist/sveltekit/index.cjs +14 -1
  71. package/dist/sveltekit/index.d.cts +3 -88
  72. package/dist/sveltekit/index.d.ts +3 -88
  73. package/dist/sveltekit/index.js +15 -387
  74. package/dist/sveltekit/server-CO847mEV.d.cts +98 -0
  75. package/dist/sveltekit/server-CO847mEV.d.ts +98 -0
  76. package/dist/{pixel-B3oQNbZ3.d.cts → types-BYH9XqmN.d.cts} +4 -58
  77. package/dist/{pixel-B3oQNbZ3.d.ts → types-BYH9XqmN.d.ts} +4 -58
  78. package/package.json +36 -1
@@ -0,0 +1,452 @@
1
+ import {
2
+ PreprError
3
+ } from "./chunk-3JLWZQ37.js";
4
+
5
+ // src/core/variants/constants.ts
6
+ var PREPR_CONTEXT_ALIAS = "_preprContext";
7
+ var PREPR_TYPE_ALIAS = "_preprType";
8
+ var PREPR_DEFAULT_VARIANT = "ALL_OTHER_USERS";
9
+
10
+ // src/core/variants/resolve.ts
11
+ function personalizationPaths(data) {
12
+ const paths = /* @__PURE__ */ new Map();
13
+ const walk = (value, path) => {
14
+ if (Array.isArray(value)) {
15
+ for (const item of value) {
16
+ if (contextOf(item)?.kind === "PERSONALIZATION") {
17
+ const record = item;
18
+ const type = record[PREPR_TYPE_ALIAS] ?? record.__typename;
19
+ const types = paths.get(path) ?? /* @__PURE__ */ new Set();
20
+ types.add(typeof type === "string" ? type : "*");
21
+ paths.set(path, types);
22
+ }
23
+ walk(item, path);
24
+ }
25
+ return;
26
+ }
27
+ if (!value || typeof value !== "object") return;
28
+ for (const [key, child] of Object.entries(value)) {
29
+ if (key !== PREPR_CONTEXT_ALIAS && key !== PREPR_TYPE_ALIAS)
30
+ walk(child, path ? `${path}.${key}` : key);
31
+ }
32
+ };
33
+ walk(data, "");
34
+ return paths;
35
+ }
36
+ function contextOf(item) {
37
+ if (!item || typeof item !== "object") return null;
38
+ const context = item[PREPR_CONTEXT_ALIAS];
39
+ const ctx = context;
40
+ return ctx?.kind && ctx.group_id ? ctx : null;
41
+ }
42
+ function decisionsFrom(data) {
43
+ const decisions = /* @__PURE__ */ new Map();
44
+ const walk = (value) => {
45
+ if (Array.isArray(value)) return value.forEach(walk);
46
+ if (!value || typeof value !== "object") return;
47
+ const ctx = contextOf(value);
48
+ if (ctx?.group_id && ctx.variant_id) {
49
+ const ids = decisions.get(ctx.group_id) ?? [];
50
+ if (!ids.includes(ctx.variant_id)) ids.push(ctx.variant_id);
51
+ decisions.set(ctx.group_id, ids);
52
+ }
53
+ Object.values(value).forEach(walk);
54
+ };
55
+ walk(data);
56
+ return decisions;
57
+ }
58
+ function chosenVariants(ctx, options) {
59
+ const decided = options.decisions?.get(ctx.group_id);
60
+ if (ctx.kind === "AB_TEST") {
61
+ return options.bucket ? [options.bucket] : decided ?? ["A"];
62
+ }
63
+ return decided ?? [PREPR_DEFAULT_VARIANT];
64
+ }
65
+ function resolveVariants(data, options = {}) {
66
+ const resolve = (value) => {
67
+ if (Array.isArray(value)) {
68
+ return value.filter((item) => {
69
+ const ctx = contextOf(item);
70
+ return !ctx || chosenVariants(ctx, options).includes(ctx.variant_id ?? "");
71
+ }).map(resolve);
72
+ }
73
+ if (!value || typeof value !== "object") return value;
74
+ const out = {};
75
+ for (const [key, child] of Object.entries(value)) out[key] = resolve(child);
76
+ return out;
77
+ };
78
+ return resolve(data);
79
+ }
80
+
81
+ // src/core/variants/schema.ts
82
+ var PREPR_SCHEMA_QUERY = `query PreprPersonalizationSchema {
83
+ __schema {
84
+ queryType { name }
85
+ types {
86
+ name
87
+ possibleTypes { name }
88
+ fields {
89
+ name
90
+ args { name }
91
+ type { name ofType { name ofType { name ofType { name ofType { name } } } } }
92
+ }
93
+ }
94
+ }
95
+ }`;
96
+ function namedType(ref) {
97
+ let current = ref;
98
+ while (current && !current.name) current = current.ofType;
99
+ return current?.name ?? "";
100
+ }
101
+ function toSchemaInfo(introspection) {
102
+ const types = {};
103
+ for (const type of introspection.__schema.types) {
104
+ if (type.name.startsWith("__")) continue;
105
+ const fields = {};
106
+ for (const field of type.fields ?? []) {
107
+ fields[field.name] = {
108
+ type: namedType(field.type),
109
+ personalizable: field.args.some((arg) => arg.name === "personalize")
110
+ };
111
+ }
112
+ types[type.name] = {
113
+ fields,
114
+ possibleTypes: (type.possibleTypes ?? []).map((t) => t.name),
115
+ hasContext: "_context" in fields
116
+ };
117
+ }
118
+ return { queryType: introspection.__schema.queryType.name, types };
119
+ }
120
+
121
+ // src/core/graphql/document.ts
122
+ import { print } from "graphql/language/printer.js";
123
+ import { stripIgnoredCharacters } from "graphql/utilities/stripIgnoredCharacters.js";
124
+ var printed = /* @__PURE__ */ new WeakMap();
125
+ function isDocumentNode(value) {
126
+ return typeof value === "object" && value !== null && value.kind === "Document" && Array.isArray(value.definitions);
127
+ }
128
+ function invalidDocument(originalError) {
129
+ return new PreprError(
130
+ "Expected a GraphQL query string, TypedDocumentString or DocumentNode",
131
+ "INVALID_DOCUMENT",
132
+ "createPreprClient",
133
+ originalError
134
+ );
135
+ }
136
+ var minified = /* @__PURE__ */ new Map();
137
+ var MINIFIED_CACHE_LIMIT = 500;
138
+ function minify(query) {
139
+ const cached = minified.get(query);
140
+ if (cached !== void 0) return cached;
141
+ let result = query;
142
+ try {
143
+ result = stripIgnoredCharacters(query);
144
+ } catch {
145
+ }
146
+ if (minified.size < MINIFIED_CACHE_LIMIT) minified.set(query, result);
147
+ return result;
148
+ }
149
+ function resolveQuery(document) {
150
+ if (typeof document === "string") return minify(document);
151
+ if (document instanceof String) return minify(document.toString());
152
+ if (!isDocumentNode(document)) throw invalidDocument();
153
+ let query = printed.get(document);
154
+ if (query === void 0) {
155
+ try {
156
+ query = minify(print(document));
157
+ } catch (error) {
158
+ throw invalidDocument(error instanceof Error ? error : void 0);
159
+ }
160
+ printed.set(document, query);
161
+ }
162
+ return query;
163
+ }
164
+
165
+ // src/core/variants/ast.ts
166
+ import { parse } from "graphql/language/parser.js";
167
+ var name = (value) => ({ kind: "Name", value });
168
+ function selectionSet(selections) {
169
+ return { kind: "SelectionSet", selections };
170
+ }
171
+ function contextField(withKey) {
172
+ const fields = ["kind", "group_id", "variant_id"];
173
+ if (withKey) fields.push("variant_key");
174
+ return {
175
+ kind: "Field",
176
+ alias: name(PREPR_CONTEXT_ALIAS),
177
+ name: name("_context"),
178
+ selectionSet: selectionSet(
179
+ fields.map(
180
+ (field) => ({ kind: "Field", name: name(field) })
181
+ )
182
+ )
183
+ };
184
+ }
185
+ function typenameField() {
186
+ return {
187
+ kind: "Field",
188
+ alias: name(PREPR_TYPE_ALIAS),
189
+ name: name("__typename")
190
+ };
191
+ }
192
+ function inlineFragment(typeName, selections) {
193
+ return {
194
+ kind: "InlineFragment",
195
+ typeCondition: { kind: "NamedType", name: name(typeName) },
196
+ selectionSet: selectionSet(selections)
197
+ };
198
+ }
199
+ function contextSelections(typeName, schema, onlyTypes, withType = true) {
200
+ const fields = () => withType ? [contextField(true), typenameField()] : [contextField(false)];
201
+ const type = schema.types[typeName];
202
+ if (!type) return [];
203
+ if (type.possibleTypes.length > 0) {
204
+ return type.possibleTypes.filter(
205
+ (member) => schema.types[member]?.hasContext && (!onlyTypes || onlyTypes.has(member))
206
+ ).map((member) => inlineFragment(member, fields()));
207
+ }
208
+ return type.hasContext ? fields() : [];
209
+ }
210
+ function withoutPersonalizeArg(args = []) {
211
+ return args.filter((arg) => arg.name.value !== "personalize");
212
+ }
213
+ function documentOf(document) {
214
+ if (typeof document === "string") return parse(document);
215
+ if (document instanceof String) return parse(document.toString());
216
+ return document;
217
+ }
218
+ function collectVariables(node, used) {
219
+ if (!node || typeof node !== "object") return;
220
+ if (Array.isArray(node)) {
221
+ for (const item of node) collectVariables(item, used);
222
+ return;
223
+ }
224
+ const value = node;
225
+ if (value.kind === "Variable") {
226
+ used.add(value.name.value);
227
+ return;
228
+ }
229
+ for (const key of Object.keys(value)) {
230
+ if (key !== "loc") collectVariables(value[key], used);
231
+ }
232
+ }
233
+
234
+ // src/core/variants/decision-document.ts
235
+ function decisionSelectionSet(set, typeName, path, ctx) {
236
+ const selections = [];
237
+ for (const selection of set.selections) {
238
+ if (selection.kind === "FragmentSpread") {
239
+ const fragment = ctx.fragments[selection.name.value];
240
+ if (!fragment) continue;
241
+ const pruned = decisionSelectionSet(
242
+ fragment.selectionSet,
243
+ fragment.typeCondition.name.value,
244
+ path,
245
+ ctx
246
+ );
247
+ if (pruned) {
248
+ selections.push(
249
+ inlineFragment(fragment.typeCondition.name.value, [
250
+ ...pruned.selections
251
+ ])
252
+ );
253
+ }
254
+ continue;
255
+ }
256
+ if (selection.kind === "InlineFragment") {
257
+ const pruned = decisionSelectionSet(
258
+ selection.selectionSet,
259
+ selection.typeCondition?.name.value ?? typeName,
260
+ path,
261
+ ctx
262
+ );
263
+ if (pruned) selections.push({ ...selection, selectionSet: pruned });
264
+ continue;
265
+ }
266
+ if (!selection.selectionSet) continue;
267
+ const field = ctx.schema.types[typeName]?.fields[selection.name.value];
268
+ if (!field) continue;
269
+ const key = selection.alias?.value ?? selection.name.value;
270
+ const fieldPath = path ? `${path}.${key}` : key;
271
+ if (ctx.paths && !ctx.paths.prefixes.has(fieldPath)) continue;
272
+ const child = decisionSelectionSet(
273
+ selection.selectionSet,
274
+ field.type,
275
+ fieldPath,
276
+ ctx
277
+ );
278
+ const groupTypes = ctx.paths?.groups.get(fieldPath);
279
+ const holdsGroups = field.personalizable && (!ctx.paths || groupTypes !== void 0);
280
+ if (holdsGroups) {
281
+ selections.push({
282
+ ...selection,
283
+ arguments: withoutPersonalizeArg(selection.arguments),
284
+ selectionSet: selectionSet([
285
+ ...ctx.topLevelOnly ? [] : child?.selections ?? [],
286
+ ...contextSelections(
287
+ field.type,
288
+ ctx.schema,
289
+ groupTypes && !groupTypes.has("*") ? groupTypes : void 0,
290
+ false
291
+ )
292
+ ])
293
+ });
294
+ } else if (child) {
295
+ selections.push({
296
+ ...selection,
297
+ arguments: field.personalizable ? withoutPersonalizeArg(selection.arguments) : selection.arguments,
298
+ selectionSet: child
299
+ });
300
+ }
301
+ }
302
+ return selections.length > 0 ? selectionSet(selections) : null;
303
+ }
304
+ function toDecisionDocument(document, schema, groupPaths, options) {
305
+ const doc = documentOf(document);
306
+ const fragments = {};
307
+ for (const definition of doc.definitions) {
308
+ if (definition.kind === "FragmentDefinition") {
309
+ fragments[definition.name.value] = definition;
310
+ }
311
+ }
312
+ const operation = doc.definitions.find(
313
+ (definition) => definition.kind === "OperationDefinition"
314
+ );
315
+ if (!operation) return null;
316
+ let paths;
317
+ if (groupPaths) {
318
+ const prefixes = /* @__PURE__ */ new Set();
319
+ for (const groupPath of groupPaths.keys()) {
320
+ const parts = groupPath.split(".");
321
+ for (let i = 1; i <= parts.length; i++) {
322
+ prefixes.add(parts.slice(0, i).join("."));
323
+ }
324
+ }
325
+ paths = { groups: groupPaths, prefixes };
326
+ }
327
+ const pruned = decisionSelectionSet(
328
+ operation.selectionSet,
329
+ schema.queryType,
330
+ "",
331
+ { schema, fragments, paths, topLevelOnly: options?.topLevelOnly }
332
+ );
333
+ if (!pruned) return null;
334
+ const used = /* @__PURE__ */ new Set();
335
+ collectVariables(pruned, used);
336
+ collectVariables(operation.directives, used);
337
+ return {
338
+ kind: "Document",
339
+ definitions: [
340
+ {
341
+ ...operation,
342
+ name: operation.name && name(`${operation.name.value}Decision`),
343
+ variableDefinitions: (operation.variableDefinitions ?? []).filter(
344
+ (definition) => used.has(definition.variable.name.value)
345
+ ),
346
+ selectionSet: pruned
347
+ }
348
+ ]
349
+ };
350
+ }
351
+
352
+ // src/core/variants/variants-document.ts
353
+ function variantsSelectionSet(set, typeName, schema) {
354
+ const selections = set.selections.map((selection) => {
355
+ if (selection.kind === "InlineFragment") {
356
+ return {
357
+ ...selection,
358
+ selectionSet: variantsSelectionSet(
359
+ selection.selectionSet,
360
+ selection.typeCondition?.name.value ?? typeName,
361
+ schema
362
+ )
363
+ };
364
+ }
365
+ if (selection.kind !== "Field" || !selection.selectionSet) return selection;
366
+ const field = schema.types[typeName]?.fields[selection.name.value];
367
+ if (!field) return selection;
368
+ const child = variantsSelectionSet(
369
+ selection.selectionSet,
370
+ field.type,
371
+ schema
372
+ );
373
+ if (!field.personalizable) return { ...selection, selectionSet: child };
374
+ return {
375
+ ...selection,
376
+ arguments: [
377
+ ...withoutPersonalizeArg(selection.arguments),
378
+ {
379
+ kind: "Argument",
380
+ name: name("personalize"),
381
+ value: { kind: "BooleanValue", value: false }
382
+ }
383
+ ],
384
+ selectionSet: selectionSet([
385
+ ...child.selections,
386
+ ...contextSelections(field.type, schema)
387
+ ])
388
+ };
389
+ });
390
+ return { ...set, selections };
391
+ }
392
+ function toVariantsDocument(document, schema) {
393
+ const doc = documentOf(document);
394
+ return {
395
+ ...doc,
396
+ definitions: doc.definitions.map((definition) => {
397
+ if (definition.kind === "OperationDefinition") {
398
+ return {
399
+ ...definition,
400
+ selectionSet: variantsSelectionSet(
401
+ definition.selectionSet,
402
+ schema.queryType,
403
+ schema
404
+ )
405
+ };
406
+ }
407
+ if (definition.kind === "FragmentDefinition") {
408
+ return {
409
+ ...definition,
410
+ selectionSet: variantsSelectionSet(
411
+ definition.selectionSet,
412
+ definition.typeCondition.name.value,
413
+ schema
414
+ )
415
+ };
416
+ }
417
+ return definition;
418
+ })
419
+ };
420
+ }
421
+
422
+ // src/core/variants/queries.ts
423
+ function toVariantsQuery(document, schema) {
424
+ return resolveQuery(toVariantsDocument(document, schema));
425
+ }
426
+ function toDecisionQuery(document, schema, options = {}) {
427
+ const paths = options.from === void 0 ? void 0 : personalizationPaths(options.from);
428
+ if (paths && paths.size === 0) return null;
429
+ const decision = toDecisionDocument(document, schema, paths, {
430
+ topLevelOnly: !paths && options.topLevelOnly
431
+ });
432
+ return decision ? resolveQuery(decision) : null;
433
+ }
434
+
435
+ export {
436
+ PREPR_DEFAULT_VARIANT,
437
+ name,
438
+ selectionSet,
439
+ documentOf,
440
+ collectVariables,
441
+ toDecisionDocument,
442
+ personalizationPaths,
443
+ contextOf,
444
+ decisionsFrom,
445
+ resolveVariants,
446
+ PREPR_SCHEMA_QUERY,
447
+ toSchemaInfo,
448
+ toVariantsDocument,
449
+ resolveQuery,
450
+ toVariantsQuery,
451
+ toDecisionQuery
452
+ };