@preprio/toolkit 0.6.0-alpha.0 → 0.6.0-alpha.10

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 (59) hide show
  1. package/README.md +268 -59
  2. package/dist/astro/{chunk-3UVUDDNY.js → chunk-CG2QBHNU.js} +1 -1
  3. package/dist/astro/graphql.cjs +154 -59
  4. package/dist/astro/graphql.d.cts +75 -6
  5. package/dist/astro/graphql.d.ts +75 -6
  6. package/dist/astro/graphql.js +150 -59
  7. package/dist/astro/index.cjs +1 -1
  8. package/dist/astro/index.js +1 -1
  9. package/dist/chunk-3NCEVJJ3.js +10 -0
  10. package/dist/{chunk-BJ2LBLWO.js → chunk-LIDUJWWJ.js} +6 -4
  11. package/dist/chunk-M3RBYJ5J.js +499 -0
  12. package/dist/{chunk-DGZA7KX7.js → chunk-S5RWVMDJ.js} +0 -24
  13. package/dist/{chunk-YR7P4IWE.js → chunk-SF6TN4JR.js} +1 -9
  14. package/dist/chunk-TN6SNAR2.js +26 -0
  15. package/dist/graphql/codegen.cjs +557 -0
  16. package/dist/graphql/codegen.d.cts +23 -0
  17. package/dist/graphql/codegen.d.ts +23 -0
  18. package/dist/graphql/codegen.js +99 -0
  19. package/dist/graphql.cjs +185 -59
  20. package/dist/graphql.d.cts +110 -6
  21. package/dist/graphql.d.ts +110 -6
  22. package/dist/graphql.js +91 -430
  23. package/dist/index.cjs +1 -1
  24. package/dist/index.d.cts +1 -1
  25. package/dist/index.d.ts +1 -1
  26. package/dist/index.js +10 -6
  27. package/dist/nextjs/chunk-B4GVZEOL.js +549 -0
  28. package/dist/nextjs/components.cjs +297 -81
  29. package/dist/nextjs/components.d.cts +26 -11
  30. package/dist/nextjs/components.d.ts +26 -11
  31. package/dist/nextjs/components.js +289 -73
  32. package/dist/nextjs/graphql.cjs +952 -315
  33. package/dist/nextjs/graphql.d.cts +225 -83
  34. package/dist/nextjs/graphql.d.ts +225 -83
  35. package/dist/nextjs/graphql.js +634 -314
  36. package/dist/nextjs/index.cjs +116 -54
  37. package/dist/nextjs/index.d.cts +61 -23
  38. package/dist/nextjs/index.d.ts +61 -23
  39. package/dist/nextjs/index.js +97 -299
  40. package/dist/nextjs/{server-CjpELYvq.d.ts → server-BL-sqZvu.d.cts} +1 -1
  41. package/dist/nextjs/{server-CjpELYvq.d.cts → server-BL-sqZvu.d.ts} +1 -1
  42. package/dist/nuxt/{chunk-P6ENNAEE.js → chunk-FMC3DP7N.js} +1 -1
  43. package/dist/nuxt/graphql.cjs +154 -59
  44. package/dist/nuxt/graphql.d.cts +75 -6
  45. package/dist/nuxt/graphql.d.ts +75 -6
  46. package/dist/nuxt/graphql.js +150 -59
  47. package/dist/nuxt/index.cjs +1 -1
  48. package/dist/nuxt/index.js +1 -1
  49. package/dist/react/components.cjs +1 -1
  50. package/dist/react/components.js +1 -1
  51. package/dist/sveltekit/{chunk-FSARVMG5.js → chunk-AJPKBAHO.js} +1 -1
  52. package/dist/sveltekit/graphql.cjs +154 -59
  53. package/dist/sveltekit/graphql.d.cts +75 -6
  54. package/dist/sveltekit/graphql.d.ts +75 -6
  55. package/dist/sveltekit/graphql.js +150 -59
  56. package/dist/sveltekit/index.cjs +1 -1
  57. package/dist/sveltekit/index.js +1 -1
  58. package/package.json +6 -1
  59. package/dist/nextjs/chunk-U4HXENF7.js +0 -231
@@ -4,7 +4,7 @@ import {
4
4
  getPreprHeadersFromHeaders,
5
5
  isPersonalizedResponse,
6
6
  validatePreprToken
7
- } from "./chunk-P6ENNAEE.js";
7
+ } from "./chunk-FMC3DP7N.js";
8
8
 
9
9
  // src/core/variants/constants.ts
10
10
  var PREPR_CONTEXT_ALIAS = "_preprContext";
@@ -93,7 +93,10 @@ function decisionSelectionSet(set, typeName, path, ctx) {
93
93
  path,
94
94
  ctx
95
95
  );
96
- if (pruned) {
96
+ if (!pruned) continue;
97
+ if (fragment.typeCondition.name.value === typeName) {
98
+ selections.push(...pruned.selections);
99
+ } else {
97
100
  selections.push(
98
101
  inlineFragment(fragment.typeCondition.name.value, [
99
102
  ...pruned.selections
@@ -103,13 +106,19 @@ function decisionSelectionSet(set, typeName, path, ctx) {
103
106
  continue;
104
107
  }
105
108
  if (selection.kind === "InlineFragment") {
109
+ const condition = selection.typeCondition?.name.value ?? typeName;
106
110
  const pruned = decisionSelectionSet(
107
111
  selection.selectionSet,
108
- selection.typeCondition?.name.value ?? typeName,
112
+ condition,
109
113
  path,
110
114
  ctx
111
115
  );
112
- if (pruned) selections.push({ ...selection, selectionSet: pruned });
116
+ if (!pruned) continue;
117
+ if (condition === typeName && !selection.directives?.length) {
118
+ selections.push(...pruned.selections);
119
+ } else {
120
+ selections.push({ ...selection, selectionSet: pruned });
121
+ }
113
122
  continue;
114
123
  }
115
124
  if (!selection.selectionSet) continue;
@@ -183,20 +192,58 @@ function toDecisionDocument(document2, schema, groupPaths, options) {
183
192
  const used = /* @__PURE__ */ new Set();
184
193
  collectVariables(pruned, used);
185
194
  collectVariables(operation.directives, used);
195
+ const shared = sharedContextFragment(schema);
186
196
  return {
187
197
  kind: "Document",
188
198
  definitions: [
199
+ ...shared ? [shared.definition] : [],
189
200
  {
190
201
  ...operation,
191
202
  name: operation.name && name(`${operation.name.value}Decision`),
192
203
  variableDefinitions: (operation.variableDefinitions ?? []).filter(
193
204
  (definition) => used.has(definition.variable.name.value)
194
205
  ),
195
- selectionSet: pruned
206
+ selectionSet: shared ? shared.apply(pruned) : pruned
196
207
  }
197
208
  ]
198
209
  };
199
210
  }
211
+ function sharedContextFragment(schema) {
212
+ const contextTypes = new Set(
213
+ Object.values(schema.types).flatMap(
214
+ (type) => type.hasContext ? [type.fields._context.type] : []
215
+ )
216
+ );
217
+ if (contextTypes.size !== 1) return null;
218
+ const [contextType] = contextTypes;
219
+ const fragmentName = "c";
220
+ const definition = {
221
+ kind: "FragmentDefinition",
222
+ name: name(fragmentName),
223
+ typeCondition: { kind: "NamedType", name: name(contextType) },
224
+ selectionSet: selectionSet(
225
+ ["kind", "group_id", "variant_id"].map(
226
+ (field) => ({ kind: "Field", name: name(field) })
227
+ )
228
+ )
229
+ };
230
+ const spread = {
231
+ kind: "FragmentSpread",
232
+ name: name(fragmentName)
233
+ };
234
+ const apply = (set) => selectionSet(
235
+ set.selections.map((selection) => {
236
+ if (selection.kind === "Field" && selection.alias?.value === PREPR_CONTEXT_ALIAS) {
237
+ return { ...selection, selectionSet: selectionSet([spread]) };
238
+ }
239
+ return "selectionSet" in selection && selection.selectionSet ? {
240
+ ...selection,
241
+ selectionSet: apply(selection.selectionSet)
242
+ } : selection;
243
+ })
244
+ );
245
+ return { definition, apply };
246
+ }
200
247
 
201
248
  // src/core/variants/probe-document.ts
202
249
  function probeSelectionSet(set, fragments) {
@@ -483,6 +530,63 @@ function toVariantsDocument(document2, schema) {
483
530
  };
484
531
  }
485
532
 
533
+ // src/core/graphql/document.ts
534
+ import { print } from "graphql/language/printer.js";
535
+ import { stripIgnoredCharacters } from "graphql/utilities/stripIgnoredCharacters.js";
536
+ var printed = /* @__PURE__ */ new WeakMap();
537
+ function isDocumentNode(value) {
538
+ return typeof value === "object" && value !== null && value.kind === "Document" && Array.isArray(value.definitions);
539
+ }
540
+ function invalidDocument(originalError) {
541
+ return new PreprError(
542
+ "Expected a GraphQL query string, TypedDocumentString or DocumentNode",
543
+ "INVALID_DOCUMENT",
544
+ "createPreprClient",
545
+ originalError
546
+ );
547
+ }
548
+ var minified = /* @__PURE__ */ new Map();
549
+ var MINIFIED_CACHE_LIMIT = 500;
550
+ function minify(query) {
551
+ const cached = minified.get(query);
552
+ if (cached !== void 0) return cached;
553
+ let result = query;
554
+ try {
555
+ result = stripIgnoredCharacters(query);
556
+ } catch {
557
+ }
558
+ if (minified.size < MINIFIED_CACHE_LIMIT) minified.set(query, result);
559
+ return result;
560
+ }
561
+ function resolveQuery(document2) {
562
+ if (typeof document2 === "string") return minify(document2);
563
+ if (document2 instanceof String) return minify(document2.toString());
564
+ if (!isDocumentNode(document2)) throw invalidDocument();
565
+ let query = printed.get(document2);
566
+ if (query === void 0) {
567
+ try {
568
+ query = minify(print(document2));
569
+ } catch (error) {
570
+ throw invalidDocument(error instanceof Error ? error : void 0);
571
+ }
572
+ printed.set(document2, query);
573
+ }
574
+ return query;
575
+ }
576
+
577
+ // src/core/variants/queries.ts
578
+ function toVariantsQuery(document2, schema) {
579
+ return resolveQuery(toVariantsDocument(document2, schema));
580
+ }
581
+ function toDecisionQuery(document2, schema, options = {}) {
582
+ const paths = options.from === void 0 ? void 0 : personalizationPaths(options.from);
583
+ if (paths && paths.size === 0) return null;
584
+ const decision = toDecisionDocument(document2, schema, paths, {
585
+ topLevelOnly: !paths && options.topLevelOnly
586
+ });
587
+ return decision ? resolveQuery(decision) : null;
588
+ }
589
+
486
590
  // src/core/graphql/transport.ts
487
591
  import { VERCEL_STEGA_REGEX } from "@vercel/stega";
488
592
 
@@ -535,50 +639,6 @@ function stegaClean(text) {
535
639
  return vercelStegaClean(text);
536
640
  }
537
641
 
538
- // src/core/graphql/document.ts
539
- import { print } from "graphql/language/printer.js";
540
- import { stripIgnoredCharacters } from "graphql/utilities/stripIgnoredCharacters.js";
541
- var printed = /* @__PURE__ */ new WeakMap();
542
- function isDocumentNode(value) {
543
- return typeof value === "object" && value !== null && value.kind === "Document" && Array.isArray(value.definitions);
544
- }
545
- function invalidDocument(originalError) {
546
- return new PreprError(
547
- "Expected a GraphQL query string, TypedDocumentString or DocumentNode",
548
- "INVALID_DOCUMENT",
549
- "createPreprClient",
550
- originalError
551
- );
552
- }
553
- var minified = /* @__PURE__ */ new Map();
554
- var MINIFIED_CACHE_LIMIT = 500;
555
- function minify(query) {
556
- const cached = minified.get(query);
557
- if (cached !== void 0) return cached;
558
- let result = query;
559
- try {
560
- result = stripIgnoredCharacters(query);
561
- } catch {
562
- }
563
- if (minified.size < MINIFIED_CACHE_LIMIT) minified.set(query, result);
564
- return result;
565
- }
566
- function resolveQuery(document2) {
567
- if (typeof document2 === "string") return minify(document2);
568
- if (document2 instanceof String) return minify(document2.toString());
569
- if (!isDocumentNode(document2)) throw invalidDocument();
570
- let query = printed.get(document2);
571
- if (query === void 0) {
572
- try {
573
- query = minify(print(document2));
574
- } catch (error) {
575
- throw invalidDocument(error instanceof Error ? error : void 0);
576
- }
577
- printed.set(document2, query);
578
- }
579
- return query;
580
- }
581
-
582
642
  // src/core/graphql/transport.ts
583
643
  var OPERATION_NAME = /\b(?:query|mutation|subscription)\s+([_A-Za-z][_0-9A-Za-z]*)/;
584
644
  var STEGA_RAW = new RegExp(
@@ -757,6 +817,7 @@ function createVaryProbe(run, options) {
757
817
  }
758
818
 
759
819
  // src/core/graphql/client.ts
820
+ var clientSchemas = /* @__PURE__ */ new WeakMap();
760
821
  function isParseableUrl(url) {
761
822
  try {
762
823
  new URL(url);
@@ -765,16 +826,18 @@ function isParseableUrl(url) {
765
826
  return false;
766
827
  }
767
828
  }
768
- function createPreprClient(options) {
769
- const { url } = options;
829
+ function assertPreprUrl(url, source) {
770
830
  validatePreprToken(url);
771
831
  if (!isParseableUrl(url)) {
772
832
  throw new PreprError(
773
833
  "Prepr GraphQL URL is not a valid URL",
774
834
  "INVALID_TOKEN",
775
- "createPreprClient"
835
+ source
776
836
  );
777
837
  }
838
+ }
839
+ function createPreprClient(options) {
840
+ assertPreprUrl(options.url, "createPreprClient");
778
841
  const run = createTransport(options);
779
842
  const probeVary = createVaryProbe(run, options);
780
843
  let schema;
@@ -791,7 +854,7 @@ function createPreprClient(options) {
791
854
  ));
792
855
  const variantsDocuments = /* @__PURE__ */ new Map();
793
856
  const decisionDocuments = /* @__PURE__ */ new Map();
794
- return {
857
+ const client = {
795
858
  async query(document2, variables, queryOptions = {}) {
796
859
  const { variants, ...rest } = queryOptions;
797
860
  if (!variants) return run(document2, variables, rest);
@@ -803,10 +866,10 @@ function createPreprClient(options) {
803
866
  return run(derived, variables, rest);
804
867
  },
805
868
  async decide(document2, variables, queryOptions = {}) {
806
- const { from, topLevelOnly, ...rest } = queryOptions;
869
+ const { from, ...rest } = queryOptions;
807
870
  const paths = from === void 0 ? void 0 : personalizationPaths(from);
808
871
  if (paths && paths.size === 0) return /* @__PURE__ */ new Map();
809
- const signature = paths ? [...paths].map(([path, types]) => `${path}:${[...types].sort().join(",")}`).sort().join("|") : topLevelOnly ? "top" : "*";
872
+ const signature = paths ? [...paths].map(([path, types]) => `${path}:${[...types].sort().join(",")}`).sort().join("|") : "*";
810
873
  let byPaths = decisionDocuments.get(document2);
811
874
  if (!byPaths) {
812
875
  byPaths = /* @__PURE__ */ new Map();
@@ -814,9 +877,7 @@ function createPreprClient(options) {
814
877
  }
815
878
  let derived = byPaths.get(signature);
816
879
  if (derived === void 0) {
817
- derived = toDecisionDocument(document2, await loadSchema(), paths, {
818
- topLevelOnly: !paths && topLevelOnly
819
- });
880
+ derived = toDecisionDocument(document2, await loadSchema(), paths);
820
881
  byPaths.set(signature, derived);
821
882
  }
822
883
  if (!derived) return /* @__PURE__ */ new Map();
@@ -830,6 +891,8 @@ function createPreprClient(options) {
830
891
  (vary) => vary === null || SEGMENTS_VARY.test(vary)
831
892
  )
832
893
  };
894
+ clientSchemas.set(client, loadSchema);
895
+ return client;
833
896
  }
834
897
 
835
898
  // src/core/graphql/event-client.ts
@@ -877,6 +940,30 @@ function createEventClient(options, adapter) {
877
940
  };
878
941
  }
879
942
 
943
+ // src/core/graphql/schema-loader.ts
944
+ var schemas = /* @__PURE__ */ new Map();
945
+ async function loadPreprSchema(url, options = {}) {
946
+ assertPreprUrl(url, "loadPreprSchema");
947
+ let schema = schemas.get(url);
948
+ if (!schema) {
949
+ const run = createTransport({ url, fetch: options.fetch, stega: "none" });
950
+ schema = run(
951
+ PREPR_SCHEMA_QUERY
952
+ ).then((result) => toSchemaInfo(result.data)).catch((error) => {
953
+ schemas.delete(url);
954
+ if (error instanceof PreprError) throw error;
955
+ throw new PreprError(
956
+ "Prepr schema response is not an introspection result",
957
+ "INVALID_RESPONSE",
958
+ "loadPreprSchema",
959
+ error instanceof Error ? error : void 0
960
+ );
961
+ });
962
+ schemas.set(url, schema);
963
+ }
964
+ return schema;
965
+ }
966
+
880
967
  // src/nuxt/graphql.ts
881
968
  function createPreprClient2(options) {
882
969
  return createEventClient(options, {
@@ -887,8 +974,12 @@ export {
887
974
  PREPR_DEFAULT_VARIANT,
888
975
  PreprError,
889
976
  createPreprClient2 as createPreprClient,
977
+ decisionsFrom,
890
978
  isPersonalizedResponse,
979
+ loadPreprSchema,
891
980
  preprVariantProps,
892
981
  resolveVariants,
893
- splitStack
982
+ splitStack,
983
+ toDecisionQuery,
984
+ toVariantsQuery
894
985
  };
@@ -36,7 +36,7 @@ __export(nuxt_exports, {
36
36
  module.exports = __toCommonJS(nuxt_exports);
37
37
 
38
38
  // src/version.ts
39
- var VERSION = "0.6.0-alpha.0";
39
+ var VERSION = "0.6.0-alpha.10";
40
40
 
41
41
  // src/core/constants.ts
42
42
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -11,7 +11,7 @@ import {
11
11
  handlePreprRequest,
12
12
  isPersonalizedResponse,
13
13
  validatePreprToken
14
- } from "./chunk-P6ENNAEE.js";
14
+ } from "./chunk-FMC3DP7N.js";
15
15
 
16
16
  // src/nuxt/server.ts
17
17
  function getPreprUUID(headers) {
@@ -2313,7 +2313,7 @@ function isReactHydrated(text) {
2313
2313
  }
2314
2314
 
2315
2315
  // src/version.ts
2316
- var VERSION = "0.6.0-alpha.0";
2316
+ var VERSION = "0.6.0-alpha.10";
2317
2317
 
2318
2318
  // src/core/pixel.ts
2319
2319
  var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
@@ -2289,7 +2289,7 @@ function isReactHydrated(text) {
2289
2289
  }
2290
2290
 
2291
2291
  // src/version.ts
2292
- var VERSION = "0.6.0-alpha.0";
2292
+ var VERSION = "0.6.0-alpha.10";
2293
2293
 
2294
2294
  // src/core/pixel.ts
2295
2295
  var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "0.6.0-alpha.0";
2
+ var VERSION = "0.6.0-alpha.10";
3
3
 
4
4
  // src/core/constants.ts
5
5
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";