@preprio/toolkit 0.6.0-alpha.0 → 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 (52) hide show
  1. package/README.md +107 -0
  2. package/dist/astro/{chunk-3UVUDDNY.js → chunk-O7N6GPAH.js} +1 -1
  3. package/dist/astro/graphql.cjs +96 -49
  4. package/dist/astro/graphql.d.cts +75 -1
  5. package/dist/astro/graphql.d.ts +75 -1
  6. package/dist/astro/graphql.js +92 -49
  7. package/dist/astro/index.cjs +1 -1
  8. package/dist/astro/index.js +1 -1
  9. package/dist/{chunk-YR7P4IWE.js → chunk-3JLWZQ37.js} +1 -9
  10. package/dist/chunk-3NCEVJJ3.js +10 -0
  11. package/dist/{chunk-DGZA7KX7.js → chunk-S5RWVMDJ.js} +0 -24
  12. package/dist/chunk-TBKSBXGH.js +452 -0
  13. package/dist/{chunk-BJ2LBLWO.js → chunk-VI6QZTBM.js} +6 -4
  14. package/dist/chunk-YWIKKWBR.js +26 -0
  15. package/dist/graphql/codegen.cjs +510 -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 +96 -49
  20. package/dist/graphql.d.cts +75 -1
  21. package/dist/graphql.d.ts +75 -1
  22. package/dist/graphql.js +60 -428
  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-U4HXENF7.js → chunk-BQ6IGSH6.js} +1 -1
  28. package/dist/nextjs/components.cjs +1 -1
  29. package/dist/nextjs/components.js +3 -2
  30. package/dist/nextjs/graphql.cjs +96 -49
  31. package/dist/nextjs/graphql.d.cts +75 -1
  32. package/dist/nextjs/graphql.d.ts +75 -1
  33. package/dist/nextjs/graphql.js +92 -49
  34. package/dist/nextjs/index.cjs +1 -1
  35. package/dist/nextjs/index.js +1 -1
  36. package/dist/nuxt/{chunk-P6ENNAEE.js → chunk-IQDSBPDN.js} +1 -1
  37. package/dist/nuxt/graphql.cjs +96 -49
  38. package/dist/nuxt/graphql.d.cts +75 -1
  39. package/dist/nuxt/graphql.d.ts +75 -1
  40. package/dist/nuxt/graphql.js +92 -49
  41. package/dist/nuxt/index.cjs +1 -1
  42. package/dist/nuxt/index.js +1 -1
  43. package/dist/react/components.cjs +1 -1
  44. package/dist/react/components.js +1 -1
  45. package/dist/sveltekit/{chunk-FSARVMG5.js → chunk-OKYZ6HPH.js} +1 -1
  46. package/dist/sveltekit/graphql.cjs +96 -49
  47. package/dist/sveltekit/graphql.d.cts +75 -1
  48. package/dist/sveltekit/graphql.d.ts +75 -1
  49. package/dist/sveltekit/graphql.js +92 -49
  50. package/dist/sveltekit/index.cjs +1 -1
  51. package/dist/sveltekit/index.js +1 -1
  52. package/package.json +6 -1
@@ -3,7 +3,7 @@ import {
3
3
  getPreprHeadersFromHeaders,
4
4
  isPersonalizedResponse,
5
5
  validatePreprToken
6
- } from "./chunk-3UVUDDNY.js";
6
+ } from "./chunk-O7N6GPAH.js";
7
7
 
8
8
  // src/core/variants/constants.ts
9
9
  var PREPR_CONTEXT_ALIAS = "_preprContext";
@@ -482,6 +482,63 @@ function toVariantsDocument(document2, schema) {
482
482
  };
483
483
  }
484
484
 
485
+ // src/core/graphql/document.ts
486
+ import { print } from "graphql/language/printer.js";
487
+ import { stripIgnoredCharacters } from "graphql/utilities/stripIgnoredCharacters.js";
488
+ var printed = /* @__PURE__ */ new WeakMap();
489
+ function isDocumentNode(value) {
490
+ return typeof value === "object" && value !== null && value.kind === "Document" && Array.isArray(value.definitions);
491
+ }
492
+ function invalidDocument(originalError) {
493
+ return new PreprError(
494
+ "Expected a GraphQL query string, TypedDocumentString or DocumentNode",
495
+ "INVALID_DOCUMENT",
496
+ "createPreprClient",
497
+ originalError
498
+ );
499
+ }
500
+ var minified = /* @__PURE__ */ new Map();
501
+ var MINIFIED_CACHE_LIMIT = 500;
502
+ function minify(query) {
503
+ const cached = minified.get(query);
504
+ if (cached !== void 0) return cached;
505
+ let result = query;
506
+ try {
507
+ result = stripIgnoredCharacters(query);
508
+ } catch {
509
+ }
510
+ if (minified.size < MINIFIED_CACHE_LIMIT) minified.set(query, result);
511
+ return result;
512
+ }
513
+ function resolveQuery(document2) {
514
+ if (typeof document2 === "string") return minify(document2);
515
+ if (document2 instanceof String) return minify(document2.toString());
516
+ if (!isDocumentNode(document2)) throw invalidDocument();
517
+ let query = printed.get(document2);
518
+ if (query === void 0) {
519
+ try {
520
+ query = minify(print(document2));
521
+ } catch (error) {
522
+ throw invalidDocument(error instanceof Error ? error : void 0);
523
+ }
524
+ printed.set(document2, query);
525
+ }
526
+ return query;
527
+ }
528
+
529
+ // src/core/variants/queries.ts
530
+ function toVariantsQuery(document2, schema) {
531
+ return resolveQuery(toVariantsDocument(document2, schema));
532
+ }
533
+ function toDecisionQuery(document2, schema, options = {}) {
534
+ const paths = options.from === void 0 ? void 0 : personalizationPaths(options.from);
535
+ if (paths && paths.size === 0) return null;
536
+ const decision = toDecisionDocument(document2, schema, paths, {
537
+ topLevelOnly: !paths && options.topLevelOnly
538
+ });
539
+ return decision ? resolveQuery(decision) : null;
540
+ }
541
+
485
542
  // src/core/graphql/transport.ts
486
543
  import { VERCEL_STEGA_REGEX } from "@vercel/stega";
487
544
 
@@ -534,50 +591,6 @@ function stegaClean(text) {
534
591
  return vercelStegaClean(text);
535
592
  }
536
593
 
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
594
  // src/core/graphql/transport.ts
582
595
  var OPERATION_NAME = /\b(?:query|mutation|subscription)\s+([_A-Za-z][_0-9A-Za-z]*)/;
583
596
  var STEGA_RAW = new RegExp(
@@ -764,16 +777,18 @@ function isParseableUrl(url) {
764
777
  return false;
765
778
  }
766
779
  }
767
- function createPreprClient(options) {
768
- const { url } = options;
780
+ function assertPreprUrl(url, source) {
769
781
  validatePreprToken(url);
770
782
  if (!isParseableUrl(url)) {
771
783
  throw new PreprError(
772
784
  "Prepr GraphQL URL is not a valid URL",
773
785
  "INVALID_TOKEN",
774
- "createPreprClient"
786
+ source
775
787
  );
776
788
  }
789
+ }
790
+ function createPreprClient(options) {
791
+ assertPreprUrl(options.url, "createPreprClient");
777
792
  const run = createTransport(options);
778
793
  const probeVary = createVaryProbe(run, options);
779
794
  let schema;
@@ -876,6 +891,30 @@ function createEventClient(options, adapter) {
876
891
  };
877
892
  }
878
893
 
894
+ // src/core/graphql/schema-loader.ts
895
+ var schemas = /* @__PURE__ */ new Map();
896
+ async function loadPreprSchema(url, options = {}) {
897
+ assertPreprUrl(url, "loadPreprSchema");
898
+ let schema = schemas.get(url);
899
+ if (!schema) {
900
+ const run = createTransport({ url, fetch: options.fetch, stega: "none" });
901
+ schema = run(
902
+ PREPR_SCHEMA_QUERY
903
+ ).then((result) => toSchemaInfo(result.data)).catch((error) => {
904
+ schemas.delete(url);
905
+ if (error instanceof PreprError) throw error;
906
+ throw new PreprError(
907
+ "Prepr schema response is not an introspection result",
908
+ "INVALID_RESPONSE",
909
+ "loadPreprSchema",
910
+ error instanceof Error ? error : void 0
911
+ );
912
+ });
913
+ schemas.set(url, schema);
914
+ }
915
+ return schema;
916
+ }
917
+
879
918
  // src/astro/graphql.ts
880
919
  function createPreprClient2(options) {
881
920
  return createEventClient(options, {
@@ -886,8 +925,12 @@ export {
886
925
  PREPR_DEFAULT_VARIANT,
887
926
  PreprError,
888
927
  createPreprClient2 as createPreprClient,
928
+ decisionsFrom,
889
929
  isPersonalizedResponse,
930
+ loadPreprSchema,
890
931
  preprVariantProps,
891
932
  resolveVariants,
892
- splitStack
933
+ splitStack,
934
+ toDecisionQuery,
935
+ toVariantsQuery
893
936
  };
@@ -31,7 +31,7 @@ __export(astro_exports, {
31
31
  module.exports = __toCommonJS(astro_exports);
32
32
 
33
33
  // src/version.ts
34
- var VERSION = "0.6.0-alpha.0";
34
+ var VERSION = "0.6.0-alpha.1";
35
35
 
36
36
  // src/core/features.ts
37
37
  function isEnabled(config) {
@@ -7,7 +7,7 @@ import {
7
7
  getToolbarPropsFromHeaders,
8
8
  isPersonalizedResponse,
9
9
  resolveFeatures
10
- } from "./chunk-3UVUDDNY.js";
10
+ } from "./chunk-O7N6GPAH.js";
11
11
 
12
12
  // src/core/constants.ts
13
13
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1,14 +1,7 @@
1
1
  import {
2
2
  VERSION,
3
3
  resolveFeatures
4
- } from "./chunk-DGZA7KX7.js";
5
-
6
- // src/core/personalization.ts
7
- var PERSONALIZED_VARY = /\bPrepr-(Segments|ABTesting)/i;
8
- function isPersonalizedResponse(source) {
9
- const headers = "headers" in source ? source.headers : source;
10
- return PERSONALIZED_VARY.test(headers.get("vary") ?? "");
11
- }
4
+ } from "./chunk-YWIKKWBR.js";
12
5
 
13
6
  // src/core/server.ts
14
7
  var PreprError = class extends Error {
@@ -174,7 +167,6 @@ async function getToolbarPropsFromHeaders(headers, token, features) {
174
167
  }
175
168
 
176
169
  export {
177
- isPersonalizedResponse,
178
170
  PreprError,
179
171
  getPreprUUIDFromHeaders,
180
172
  getActiveSegmentFromHeaders,
@@ -0,0 +1,10 @@
1
+ // src/core/personalization.ts
2
+ var PERSONALIZED_VARY = /\bPrepr-(Segments|ABTesting)/i;
3
+ function isPersonalizedResponse(source) {
4
+ const headers = "headers" in source ? source.headers : source;
5
+ return PERSONALIZED_VARY.test(headers.get("vary") ?? "");
6
+ }
7
+
8
+ export {
9
+ isPersonalizedResponse
10
+ };
@@ -1,24 +1,3 @@
1
- // src/version.ts
2
- var VERSION = "0.6.0-alpha.0";
3
-
4
- // src/core/features.ts
5
- function isEnabled(config) {
6
- if (typeof config === "boolean") return config;
7
- return config?.enabled ?? true;
8
- }
9
- function resolveFeatures(features) {
10
- return {
11
- segments: isEnabled(features?.segments),
12
- abTesting: isEnabled(features?.abTesting),
13
- editMode: isEnabled(features?.editMode)
14
- };
15
- }
16
- var ALL_FEATURES_ENABLED = Object.freeze({
17
- segments: true,
18
- abTesting: true,
19
- editMode: true
20
- });
21
-
22
1
  // src/core/stega/clean.ts
23
2
  import { vercelStegaClean, vercelStegaDecode } from "@vercel/stega";
24
3
 
@@ -158,9 +137,6 @@ function decodeStega(str) {
158
137
  }
159
138
 
160
139
  export {
161
- VERSION,
162
- resolveFeatures,
163
- ALL_FEATURES_ENABLED,
164
140
  setTrustedParentOrigin,
165
141
  sendPreprEvent,
166
142
  initDebugLogger,