@preprio/toolkit 1.0.0-rc.2 → 1.0.0-rc.3

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.
@@ -34,7 +34,7 @@ __export(astro_exports, {
34
34
  module.exports = __toCommonJS(astro_exports);
35
35
 
36
36
  // src/version.ts
37
- var VERSION = "1.0.0-rc.2";
37
+ var VERSION = "1.0.0-rc.3";
38
38
 
39
39
  // src/core/constants.ts
40
40
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "1.0.0-rc.2";
2
+ var VERSION = "1.0.0-rc.3";
3
3
 
4
4
  // src/core/constants.ts
5
5
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "1.0.0-rc.2";
2
+ var VERSION = "1.0.0-rc.3";
3
3
 
4
4
  // src/core/features.ts
5
5
  function isEnabled(config) {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  VERSION,
3
3
  resolveFeatures
4
- } from "./chunk-DBKZWOXA.js";
4
+ } from "./chunk-EJDHVIFU.js";
5
5
 
6
6
  // src/core/server.ts
7
7
  var PreprError = class extends Error {
@@ -2,7 +2,7 @@ import {
2
2
  ALL_FEATURES_ENABLED,
3
3
  VERSION,
4
4
  resolveFeatures
5
- } from "./chunk-DBKZWOXA.js";
5
+ } from "./chunk-EJDHVIFU.js";
6
6
  import {
7
7
  createIframeBridge,
8
8
  isLocale,
@@ -29,13 +29,56 @@ var import_graphql = require("graphql");
29
29
  function field(name) {
30
30
  return { kind: import_graphql.Kind.FIELD, name: { kind: import_graphql.Kind.NAME, value: name } };
31
31
  }
32
+ function optionalField(name) {
33
+ return {
34
+ ...field(name),
35
+ directives: [
36
+ {
37
+ kind: import_graphql.Kind.DIRECTIVE,
38
+ name: { kind: import_graphql.Kind.NAME, value: "include" },
39
+ arguments: [
40
+ {
41
+ kind: import_graphql.Kind.ARGUMENT,
42
+ name: { kind: import_graphql.Kind.NAME, value: "if" },
43
+ value: { kind: import_graphql.Kind.BOOLEAN, value: true }
44
+ }
45
+ ]
46
+ }
47
+ ]
48
+ };
49
+ }
50
+ function collectFragmentTypes(documents) {
51
+ const types = /* @__PURE__ */ new Map();
52
+ for (const document of documents) {
53
+ for (const definition of document.definitions) {
54
+ if (definition.kind === import_graphql.Kind.FRAGMENT_DEFINITION) {
55
+ types.set(definition.name.value, definition.typeCondition.name.value);
56
+ }
57
+ }
58
+ }
59
+ return types;
60
+ }
61
+ function spreadCarriesId(set, type, schema, fragments) {
62
+ return set.selections.some((s) => {
63
+ if (s.kind !== import_graphql.Kind.FRAGMENT_SPREAD || s.directives?.length) return false;
64
+ const name = fragments.get(s.name.value);
65
+ const on = name ? schema.getType(name) : void 0;
66
+ if (!on || !((0, import_graphql.isObjectType)(on) || (0, import_graphql.isInterfaceType)(on))) return false;
67
+ if (!("_id" in on.getFields())) return false;
68
+ return on.name === type.name || (0, import_graphql.isInterfaceType)(on) && schema.isSubType(on, type);
69
+ });
70
+ }
32
71
  function hasResponseKey(set, key) {
33
72
  return set.selections.some(
34
73
  (s) => s.kind === import_graphql.Kind.FIELD && (s.alias ?? s.name).value === key
35
74
  );
36
75
  }
37
76
  function addPreprIds(document, schema, options = {}) {
77
+ return addIds(document, schema, options, collectFragmentTypes([document]));
78
+ }
79
+ function addIds(document, schema, options, fragments) {
38
80
  const typename = options.typename ?? false;
81
+ const idField = options.optionalInTypes ? optionalField("_id") : field("_id");
39
82
  const roots = new Set(
40
83
  [
41
84
  schema.getQueryType(),
@@ -51,8 +94,8 @@ function addPreprIds(document, schema, options = {}) {
51
94
  const type = typeInfo.getParentType();
52
95
  if (!type || !(0, import_graphql.isCompositeType)(type) || roots.has(type)) return;
53
96
  const added = [];
54
- if (((0, import_graphql.isObjectType)(type) || (0, import_graphql.isInterfaceType)(type)) && "_id" in type.getFields() && !hasResponseKey(node, "_id")) {
55
- added.push(field("_id"));
97
+ if (((0, import_graphql.isObjectType)(type) || (0, import_graphql.isInterfaceType)(type)) && "_id" in type.getFields() && !hasResponseKey(node, "_id") && !spreadCarriesId(node, type, schema, fragments)) {
98
+ added.push(idField);
56
99
  }
57
100
  if (typename && !hasResponseKey(node, "__typename")) {
58
101
  added.push(field("__typename"));
@@ -67,8 +110,14 @@ function createPreprIds(options = {}) {
67
110
  return {
68
111
  transform({ documents, schema }) {
69
112
  const built = (0, import_graphql.buildASTSchema)(schema, { assumeValidSDL: true });
113
+ const fragments = collectFragmentTypes(
114
+ documents.flatMap((file) => file.document ? [file.document] : [])
115
+ );
70
116
  return documents.map(
71
- (file) => file.document ? { ...file, document: addPreprIds(file.document, built, options) } : file
117
+ (file) => file.document ? {
118
+ ...file,
119
+ document: addIds(file.document, built, options, fragments)
120
+ } : file
72
121
  );
73
122
  }
74
123
  };
@@ -17,6 +17,17 @@ interface PreprIdsOptions {
17
17
  * already to render them.
18
18
  */
19
19
  typename?: boolean;
20
+ /**
21
+ * Type the added `_id` as optional (`_id?: string`), so objects written by
22
+ * hand against the generated types, such as test fixtures and mocks, keep
23
+ * compiling without one. Default `false`.
24
+ *
25
+ * The field is selected as `_id @include(if: true)`: the API always
26
+ * returns it, and Codegen types a field with `@include` as optional. Code
27
+ * that reads an added `_id` gets `string | undefined`. An `_id` you select
28
+ * yourself is left as written.
29
+ */
30
+ optionalInTypes?: boolean;
20
31
  }
21
32
  /**
22
33
  * Select `_id` on every level of a query that has one, so live preview and
@@ -24,10 +35,15 @@ interface PreprIdsOptions {
24
35
  *
25
36
  * The schema decides where it goes: only on types that have an `_id`
26
37
  * (models and components, not collections like `Pages` or unions; inside a
27
- * union, on each member). Adding a field never breaks the types Codegen
28
- * generates. With `typename: true`, `__typename` is added on every object,
38
+ * union, on each member). Code that reads the data keeps compiling; objects
39
+ * written by hand against the generated types need an `_id` too, unless
40
+ * `optionalInTypes` is set. With `typename: true`, `__typename` is added on every object,
29
41
  * interface and union below the root too. Fields already selected are left
30
42
  * as they are.
43
+ *
44
+ * A level that spreads a fragment on its own type gets no `_id` of its own:
45
+ * the fragment selects it, and a second one next to the spread would sit
46
+ * outside the fragment's masked type. Fragments are looked up in `document`.
31
47
  */
32
48
  declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema, options?: PreprIdsOptions): DocumentNode;
33
49
  /** A GraphQL Codegen document transform. */
@@ -63,7 +79,8 @@ declare function createPreprIds(options?: PreprIdsOptions): CodegenDocumentTrans
63
79
  * },
64
80
  * ```
65
81
  *
66
- * `createPreprIds({ typename: true })` adds `__typename` as well.
82
+ * `createPreprIds({ typename: true })` adds `__typename` as well, and
83
+ * `createPreprIds({ optionalInTypes: true })` types the added `_id` as optional.
67
84
  */
68
85
  declare const preprIds: CodegenDocumentTransform;
69
86
 
@@ -17,6 +17,17 @@ interface PreprIdsOptions {
17
17
  * already to render them.
18
18
  */
19
19
  typename?: boolean;
20
+ /**
21
+ * Type the added `_id` as optional (`_id?: string`), so objects written by
22
+ * hand against the generated types, such as test fixtures and mocks, keep
23
+ * compiling without one. Default `false`.
24
+ *
25
+ * The field is selected as `_id @include(if: true)`: the API always
26
+ * returns it, and Codegen types a field with `@include` as optional. Code
27
+ * that reads an added `_id` gets `string | undefined`. An `_id` you select
28
+ * yourself is left as written.
29
+ */
30
+ optionalInTypes?: boolean;
20
31
  }
21
32
  /**
22
33
  * Select `_id` on every level of a query that has one, so live preview and
@@ -24,10 +35,15 @@ interface PreprIdsOptions {
24
35
  *
25
36
  * The schema decides where it goes: only on types that have an `_id`
26
37
  * (models and components, not collections like `Pages` or unions; inside a
27
- * union, on each member). Adding a field never breaks the types Codegen
28
- * generates. With `typename: true`, `__typename` is added on every object,
38
+ * union, on each member). Code that reads the data keeps compiling; objects
39
+ * written by hand against the generated types need an `_id` too, unless
40
+ * `optionalInTypes` is set. With `typename: true`, `__typename` is added on every object,
29
41
  * interface and union below the root too. Fields already selected are left
30
42
  * as they are.
43
+ *
44
+ * A level that spreads a fragment on its own type gets no `_id` of its own:
45
+ * the fragment selects it, and a second one next to the spread would sit
46
+ * outside the fragment's masked type. Fragments are looked up in `document`.
31
47
  */
32
48
  declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema, options?: PreprIdsOptions): DocumentNode;
33
49
  /** A GraphQL Codegen document transform. */
@@ -63,7 +79,8 @@ declare function createPreprIds(options?: PreprIdsOptions): CodegenDocumentTrans
63
79
  * },
64
80
  * ```
65
81
  *
66
- * `createPreprIds({ typename: true })` adds `__typename` as well.
82
+ * `createPreprIds({ typename: true })` adds `__typename` as well, and
83
+ * `createPreprIds({ optionalInTypes: true })` types the added `_id` as optional.
67
84
  */
68
85
  declare const preprIds: CodegenDocumentTransform;
69
86
 
@@ -12,13 +12,56 @@ import {
12
12
  function field(name) {
13
13
  return { kind: Kind.FIELD, name: { kind: Kind.NAME, value: name } };
14
14
  }
15
+ function optionalField(name) {
16
+ return {
17
+ ...field(name),
18
+ directives: [
19
+ {
20
+ kind: Kind.DIRECTIVE,
21
+ name: { kind: Kind.NAME, value: "include" },
22
+ arguments: [
23
+ {
24
+ kind: Kind.ARGUMENT,
25
+ name: { kind: Kind.NAME, value: "if" },
26
+ value: { kind: Kind.BOOLEAN, value: true }
27
+ }
28
+ ]
29
+ }
30
+ ]
31
+ };
32
+ }
33
+ function collectFragmentTypes(documents) {
34
+ const types = /* @__PURE__ */ new Map();
35
+ for (const document of documents) {
36
+ for (const definition of document.definitions) {
37
+ if (definition.kind === Kind.FRAGMENT_DEFINITION) {
38
+ types.set(definition.name.value, definition.typeCondition.name.value);
39
+ }
40
+ }
41
+ }
42
+ return types;
43
+ }
44
+ function spreadCarriesId(set, type, schema, fragments) {
45
+ return set.selections.some((s) => {
46
+ if (s.kind !== Kind.FRAGMENT_SPREAD || s.directives?.length) return false;
47
+ const name = fragments.get(s.name.value);
48
+ const on = name ? schema.getType(name) : void 0;
49
+ if (!on || !(isObjectType(on) || isInterfaceType(on))) return false;
50
+ if (!("_id" in on.getFields())) return false;
51
+ return on.name === type.name || isInterfaceType(on) && schema.isSubType(on, type);
52
+ });
53
+ }
15
54
  function hasResponseKey(set, key) {
16
55
  return set.selections.some(
17
56
  (s) => s.kind === Kind.FIELD && (s.alias ?? s.name).value === key
18
57
  );
19
58
  }
20
59
  function addPreprIds(document, schema, options = {}) {
60
+ return addIds(document, schema, options, collectFragmentTypes([document]));
61
+ }
62
+ function addIds(document, schema, options, fragments) {
21
63
  const typename = options.typename ?? false;
64
+ const idField = options.optionalInTypes ? optionalField("_id") : field("_id");
22
65
  const roots = new Set(
23
66
  [
24
67
  schema.getQueryType(),
@@ -34,8 +77,8 @@ function addPreprIds(document, schema, options = {}) {
34
77
  const type = typeInfo.getParentType();
35
78
  if (!type || !isCompositeType(type) || roots.has(type)) return;
36
79
  const added = [];
37
- if ((isObjectType(type) || isInterfaceType(type)) && "_id" in type.getFields() && !hasResponseKey(node, "_id")) {
38
- added.push(field("_id"));
80
+ if ((isObjectType(type) || isInterfaceType(type)) && "_id" in type.getFields() && !hasResponseKey(node, "_id") && !spreadCarriesId(node, type, schema, fragments)) {
81
+ added.push(idField);
39
82
  }
40
83
  if (typename && !hasResponseKey(node, "__typename")) {
41
84
  added.push(field("__typename"));
@@ -50,8 +93,14 @@ function createPreprIds(options = {}) {
50
93
  return {
51
94
  transform({ documents, schema }) {
52
95
  const built = buildASTSchema(schema, { assumeValidSDL: true });
96
+ const fragments = collectFragmentTypes(
97
+ documents.flatMap((file) => file.document ? [file.document] : [])
98
+ );
53
99
  return documents.map(
54
- (file) => file.document ? { ...file, document: addPreprIds(file.document, built, options) } : file
100
+ (file) => file.document ? {
101
+ ...file,
102
+ document: addIds(file.document, built, options, fragments)
103
+ } : file
55
104
  );
56
105
  }
57
106
  };
package/dist/index.cjs CHANGED
@@ -51,7 +51,7 @@ __export(src_exports, {
51
51
  module.exports = __toCommonJS(src_exports);
52
52
 
53
53
  // src/version.ts
54
- var VERSION = "1.0.0-rc.2";
54
+ var VERSION = "1.0.0-rc.3";
55
55
 
56
56
  // src/core/features.ts
57
57
  function isEnabled(config) {
package/dist/index.d.cts CHANGED
@@ -5,7 +5,7 @@ export { g as PreprEnvironment, h as PreprEventType, i as PreprFeatureConfig, j
5
5
  * Package version; must match package.json. Lives in its own leaf module so
6
6
  * core can import it without cycling through the barrel.
7
7
  */
8
- declare const VERSION = "1.0.0-rc.2";
8
+ declare const VERSION = "1.0.0-rc.3";
9
9
 
10
10
  /**
11
11
  * Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export { g as PreprEnvironment, h as PreprEventType, i as PreprFeatureConfig, j
5
5
  * Package version; must match package.json. Lives in its own leaf module so
6
6
  * core can import it without cycling through the barrel.
7
7
  */
8
- declare const VERSION = "1.0.0-rc.2";
8
+ declare const VERSION = "1.0.0-rc.3";
9
9
 
10
10
  /**
11
11
  * Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  setTrackingParam,
9
9
  stegaClean,
10
10
  trackEvent
11
- } from "./chunk-LJA5K4KM.js";
11
+ } from "./chunk-PI5HUQ6G.js";
12
12
  import {
13
13
  PreprError,
14
14
  extractAccessToken,
@@ -19,11 +19,11 @@ import {
19
19
  getPreprUUIDFromHeaders,
20
20
  getToolbarPropsFromHeaders,
21
21
  validatePreprToken
22
- } from "./chunk-EWXBE3JD.js";
22
+ } from "./chunk-N6CXRQQP.js";
23
23
  import {
24
24
  VERSION,
25
25
  resolveFeatures
26
- } from "./chunk-DBKZWOXA.js";
26
+ } from "./chunk-EJDHVIFU.js";
27
27
  import {
28
28
  createLiveBridge,
29
29
  createLiveStore
@@ -3707,7 +3707,7 @@ function createPreprPreview(opts = {}) {
3707
3707
  var import_react = require("react");
3708
3708
 
3709
3709
  // src/version.ts
3710
- var VERSION = "1.0.0-rc.2";
3710
+ var VERSION = "1.0.0-rc.3";
3711
3711
 
3712
3712
  // src/core/pixel.ts
3713
3713
  var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
@@ -2,8 +2,8 @@
2
2
  import {
3
3
  createPreprPreview,
4
4
  loadTrackingPixel
5
- } from "../chunk-LJA5K4KM.js";
6
- import "../chunk-DBKZWOXA.js";
5
+ } from "../chunk-PI5HUQ6G.js";
6
+ import "../chunk-EJDHVIFU.js";
7
7
  import "../chunk-Y7JWV7WV.js";
8
8
  import "../chunk-UTKH7DOI.js";
9
9
  import "../chunk-V7MCHPE7.js";
@@ -49,7 +49,7 @@ module.exports = __toCommonJS(nextjs_exports);
49
49
  var import_server = require("next/server");
50
50
 
51
51
  // src/version.ts
52
- var VERSION = "1.0.0-rc.2";
52
+ var VERSION = "1.0.0-rc.3";
53
53
 
54
54
  // src/core/constants.ts
55
55
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -2,7 +2,7 @@
2
2
  import { NextResponse } from "next/server";
3
3
 
4
4
  // src/version.ts
5
- var VERSION = "1.0.0-rc.2";
5
+ var VERSION = "1.0.0-rc.3";
6
6
 
7
7
  // src/core/constants.ts
8
8
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1,8 +1,8 @@
1
1
  "use server";
2
2
  import {
3
3
  isPreviewFromHeaders
4
- } from "../chunk-EWXBE3JD.js";
5
- import "../chunk-DBKZWOXA.js";
4
+ } from "../chunk-N6CXRQQP.js";
5
+ import "../chunk-EJDHVIFU.js";
6
6
  import {
7
7
  parsePatchLog
8
8
  } from "../chunk-KM2GME62.js";
@@ -35,7 +35,7 @@ __export(nuxt_exports, {
35
35
  module.exports = __toCommonJS(nuxt_exports);
36
36
 
37
37
  // src/version.ts
38
- var VERSION = "1.0.0-rc.2";
38
+ var VERSION = "1.0.0-rc.3";
39
39
 
40
40
  // src/core/constants.ts
41
41
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -11,7 +11,7 @@ import {
11
11
  } from "./chunk-LUKVPOJ2.js";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = "1.0.0-rc.2";
14
+ var VERSION = "1.0.0-rc.3";
15
15
 
16
16
  // src/core/features.ts
17
17
  function isEnabled(config) {
@@ -3703,7 +3703,7 @@ function createPreprPreview(opts = {}) {
3703
3703
  }
3704
3704
 
3705
3705
  // src/version.ts
3706
- var VERSION = "1.0.0-rc.2";
3706
+ var VERSION = "1.0.0-rc.3";
3707
3707
 
3708
3708
  // src/core/pixel.ts
3709
3709
  var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
@@ -3679,7 +3679,7 @@ function createPreprPreview(opts = {}) {
3679
3679
  }
3680
3680
 
3681
3681
  // src/version.ts
3682
- var VERSION = "1.0.0-rc.2";
3682
+ var VERSION = "1.0.0-rc.3";
3683
3683
 
3684
3684
  // src/core/pixel.ts
3685
3685
  var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
@@ -35,7 +35,7 @@ __export(sveltekit_exports, {
35
35
  module.exports = __toCommonJS(sveltekit_exports);
36
36
 
37
37
  // src/version.ts
38
- var VERSION = "1.0.0-rc.2";
38
+ var VERSION = "1.0.0-rc.3";
39
39
 
40
40
  // src/core/constants.ts
41
41
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -11,7 +11,7 @@ import {
11
11
  } from "./chunk-LUKVPOJ2.js";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = "1.0.0-rc.2";
14
+ var VERSION = "1.0.0-rc.3";
15
15
 
16
16
  // src/core/features.ts
17
17
  function isEnabled(config) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preprio/toolkit",
3
- "version": "1.0.0-rc.2",
3
+ "version": "1.0.0-rc.3",
4
4
  "description": "Prepr personalization, A/B testing and preview toolkit for React, Next.js, Nuxt, Astro, SvelteKit and Express",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Prepr B.V. (https://prepr.io)",