@preprio/toolkit 0.5.0-rc.6 → 0.5.0-rc.7

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 CHANGED
@@ -737,12 +737,13 @@ There are three ways to get them there. Pick the first that fits your setup.
737
737
 
738
738
  #### Option 1: GraphQL Codegen (recommended)
739
739
 
740
- Add the `preprIds` document transform to your Codegen output. `_id` and
741
- `__typename` are added wherever your schema has them: on models and
742
- components, and inside each member of a dynamic content field. Collections
743
- like `Pages` and unions themselves never get an `_id`, so the queries stay
744
- valid. Your `.graphql` files stay as they are; the generated documents and
745
- types include the fields, at no runtime cost.
740
+ Add the `preprIds` document transform to your Codegen output. `_id` is
741
+ added wherever your schema has one: on models and components, and inside
742
+ each member of a dynamic content field. Collections like `Pages` and unions
743
+ themselves never get an `_id`, so the queries stay valid. Your `.graphql`
744
+ files stay as they are; the generated documents and types include the field,
745
+ at no runtime cost. Adding a field never breaks code that compiles today, so
746
+ this is safe to turn on in an existing project.
746
747
 
747
748
  ```ts
748
749
  // codegen.ts
@@ -766,6 +767,13 @@ export default config;
766
767
  Run Codegen again after adding it. Needs `graphql` 16 or later, which a
767
768
  Codegen setup already has.
768
769
 
770
+ To add `__typename` everywhere too, use `createPreprIds({ typename: true })`
771
+ instead. It is off by default because a selected `__typename` changes the
772
+ generated types: each member of a union is typed on its own, so code that
773
+ reads a union's fields without checking `__typename` first stops compiling.
774
+ Live preview only needs it on dynamic content entries, where you select it
775
+ already to render them.
776
+
769
777
  #### Option 2: at runtime, without Codegen
770
778
 
771
779
  `addPreprIds(document, schema)` does the same to a parsed query. It needs
@@ -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.5.0-rc.6";
34
+ var VERSION = "0.5.0-rc.7";
35
35
 
36
36
  // src/core/features.ts
37
37
  function isEnabled(config) {
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "0.5.0-rc.6";
2
+ var VERSION = "0.5.0-rc.7";
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-WHZNCYUS.js";
4
+ } from "./chunk-JUXP5KRX.js";
5
5
 
6
6
  // src/core/server.ts
7
7
  var PreprError = class extends Error {
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "0.5.0-rc.6";
2
+ var VERSION = "0.5.0-rc.7";
3
3
 
4
4
  // src/core/features.ts
5
5
  function isEnabled(config) {
@@ -2,7 +2,7 @@ import {
2
2
  ALL_FEATURES_ENABLED,
3
3
  VERSION,
4
4
  resolveFeatures
5
- } from "./chunk-WHZNCYUS.js";
5
+ } from "./chunk-JUXP5KRX.js";
6
6
  import {
7
7
  createIframeBridge,
8
8
  isLocale,
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var codegen_exports = {};
22
22
  __export(codegen_exports, {
23
23
  addPreprIds: () => addPreprIds,
24
+ createPreprIds: () => createPreprIds,
24
25
  preprIds: () => preprIds
25
26
  });
26
27
  module.exports = __toCommonJS(codegen_exports);
@@ -33,7 +34,8 @@ function hasResponseKey(set, key) {
33
34
  (s) => s.kind === import_graphql.Kind.FIELD && (s.alias ?? s.name).value === key
34
35
  );
35
36
  }
36
- function addPreprIds(document, schema) {
37
+ function addPreprIds(document, schema, options = {}) {
38
+ const typename = options.typename ?? false;
37
39
  const roots = new Set(
38
40
  [
39
41
  schema.getQueryType(),
@@ -52,7 +54,7 @@ function addPreprIds(document, schema) {
52
54
  if (((0, import_graphql.isObjectType)(type) || (0, import_graphql.isInterfaceType)(type)) && "_id" in type.getFields() && !hasResponseKey(node, "_id")) {
53
55
  added.push(field("_id"));
54
56
  }
55
- if (!hasResponseKey(node, "__typename")) {
57
+ if (typename && !hasResponseKey(node, "__typename")) {
56
58
  added.push(field("__typename"));
57
59
  }
58
60
  if (!added.length) return;
@@ -61,19 +63,20 @@ function addPreprIds(document, schema) {
61
63
  })
62
64
  );
63
65
  }
64
- var preprIds = {
65
- transform({
66
- documents,
67
- schema
68
- }) {
69
- const built = (0, import_graphql.buildASTSchema)(schema, { assumeValidSDL: true });
70
- return documents.map(
71
- (file) => file.document ? { ...file, document: addPreprIds(file.document, built) } : file
72
- );
73
- }
74
- };
66
+ function createPreprIds(options = {}) {
67
+ return {
68
+ transform({ documents, schema }) {
69
+ const built = (0, import_graphql.buildASTSchema)(schema, { assumeValidSDL: true });
70
+ return documents.map(
71
+ (file) => file.document ? { ...file, document: addPreprIds(file.document, built, options) } : file
72
+ );
73
+ }
74
+ };
75
+ }
76
+ var preprIds = createPreprIds();
75
77
  // Annotate the CommonJS export names for ESM import in node:
76
78
  0 && (module.exports = {
77
79
  addPreprIds,
80
+ createPreprIds,
78
81
  preprIds
79
82
  });
@@ -6,23 +6,50 @@ import { DocumentNode, GraphQLSchema } from 'graphql';
6
6
  */
7
7
  interface CodegenDocumentFile {
8
8
  document?: DocumentNode;
9
- [key: string]: unknown;
9
+ }
10
+ interface PreprIdsOptions {
11
+ /**
12
+ * Also select `__typename` on every object, interface and union below the
13
+ * root. Default `false`: a selected `__typename` changes the types Codegen
14
+ * generates (each union member is typed separately, and objects built by
15
+ * hand must carry one), which can break code that compiles today. Live
16
+ * preview only needs it on dynamic content entries, where you select it
17
+ * already to render them.
18
+ */
19
+ typename?: boolean;
10
20
  }
11
21
  /**
12
- * Select `_id` and `__typename` on every level of a query that has them, so
13
- * live preview and click-to-edit can find each item without you selecting
14
- * them by hand.
22
+ * Select `_id` on every level of a query that has one, so live preview and
23
+ * click-to-edit can find each item without you selecting it by hand.
15
24
  *
16
- * The schema decides where they go: `_id` only on types that have one
17
- * (models and components, not collections like `Pages` or unions), and
18
- * `__typename` on every object, interface and union below the root.
19
- * Fields already selected are left as they are.
25
+ * The schema decides where it goes: only on types that have an `_id`
26
+ * (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,
29
+ * interface and union below the root too. Fields already selected are left
30
+ * as they are.
20
31
  */
21
- declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema): DocumentNode;
32
+ declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema, options?: PreprIdsOptions): DocumentNode;
33
+ /** A GraphQL Codegen document transform. */
34
+ interface CodegenDocumentTransform {
35
+ transform<File extends CodegenDocumentFile>(options: {
36
+ documents: File[];
37
+ schema: DocumentNode;
38
+ }): File[];
39
+ }
22
40
  /**
23
41
  * GraphQL Codegen document transform that runs `addPreprIds` on every
24
- * document, so the generated documents and types include `_id` and
25
- * `__typename` wherever the schema has them:
42
+ * document, with options:
43
+ *
44
+ * ```ts
45
+ * documentTransforms: [createPreprIds({ typename: true })],
46
+ * ```
47
+ */
48
+ declare function createPreprIds(options?: PreprIdsOptions): CodegenDocumentTransform;
49
+ /**
50
+ * GraphQL Codegen document transform that runs `addPreprIds` on every
51
+ * document, so the generated documents and types include `_id` wherever the
52
+ * schema has one:
26
53
  *
27
54
  * ```ts
28
55
  * // codegen.ts
@@ -35,12 +62,9 @@ declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema): Doc
35
62
  * },
36
63
  * },
37
64
  * ```
65
+ *
66
+ * `createPreprIds({ typename: true })` adds `__typename` as well.
38
67
  */
39
- declare const preprIds: {
40
- transform({ documents, schema, }: {
41
- documents: CodegenDocumentFile[];
42
- schema: DocumentNode;
43
- }): CodegenDocumentFile[];
44
- };
68
+ declare const preprIds: CodegenDocumentTransform;
45
69
 
46
- export { type CodegenDocumentFile, addPreprIds, preprIds };
70
+ export { type CodegenDocumentFile, type CodegenDocumentTransform, type PreprIdsOptions, addPreprIds, createPreprIds, preprIds };
@@ -6,23 +6,50 @@ import { DocumentNode, GraphQLSchema } from 'graphql';
6
6
  */
7
7
  interface CodegenDocumentFile {
8
8
  document?: DocumentNode;
9
- [key: string]: unknown;
9
+ }
10
+ interface PreprIdsOptions {
11
+ /**
12
+ * Also select `__typename` on every object, interface and union below the
13
+ * root. Default `false`: a selected `__typename` changes the types Codegen
14
+ * generates (each union member is typed separately, and objects built by
15
+ * hand must carry one), which can break code that compiles today. Live
16
+ * preview only needs it on dynamic content entries, where you select it
17
+ * already to render them.
18
+ */
19
+ typename?: boolean;
10
20
  }
11
21
  /**
12
- * Select `_id` and `__typename` on every level of a query that has them, so
13
- * live preview and click-to-edit can find each item without you selecting
14
- * them by hand.
22
+ * Select `_id` on every level of a query that has one, so live preview and
23
+ * click-to-edit can find each item without you selecting it by hand.
15
24
  *
16
- * The schema decides where they go: `_id` only on types that have one
17
- * (models and components, not collections like `Pages` or unions), and
18
- * `__typename` on every object, interface and union below the root.
19
- * Fields already selected are left as they are.
25
+ * The schema decides where it goes: only on types that have an `_id`
26
+ * (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,
29
+ * interface and union below the root too. Fields already selected are left
30
+ * as they are.
20
31
  */
21
- declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema): DocumentNode;
32
+ declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema, options?: PreprIdsOptions): DocumentNode;
33
+ /** A GraphQL Codegen document transform. */
34
+ interface CodegenDocumentTransform {
35
+ transform<File extends CodegenDocumentFile>(options: {
36
+ documents: File[];
37
+ schema: DocumentNode;
38
+ }): File[];
39
+ }
22
40
  /**
23
41
  * GraphQL Codegen document transform that runs `addPreprIds` on every
24
- * document, so the generated documents and types include `_id` and
25
- * `__typename` wherever the schema has them:
42
+ * document, with options:
43
+ *
44
+ * ```ts
45
+ * documentTransforms: [createPreprIds({ typename: true })],
46
+ * ```
47
+ */
48
+ declare function createPreprIds(options?: PreprIdsOptions): CodegenDocumentTransform;
49
+ /**
50
+ * GraphQL Codegen document transform that runs `addPreprIds` on every
51
+ * document, so the generated documents and types include `_id` wherever the
52
+ * schema has one:
26
53
  *
27
54
  * ```ts
28
55
  * // codegen.ts
@@ -35,12 +62,9 @@ declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema): Doc
35
62
  * },
36
63
  * },
37
64
  * ```
65
+ *
66
+ * `createPreprIds({ typename: true })` adds `__typename` as well.
38
67
  */
39
- declare const preprIds: {
40
- transform({ documents, schema, }: {
41
- documents: CodegenDocumentFile[];
42
- schema: DocumentNode;
43
- }): CodegenDocumentFile[];
44
- };
68
+ declare const preprIds: CodegenDocumentTransform;
45
69
 
46
- export { type CodegenDocumentFile, addPreprIds, preprIds };
70
+ export { type CodegenDocumentFile, type CodegenDocumentTransform, type PreprIdsOptions, addPreprIds, createPreprIds, preprIds };
@@ -17,7 +17,8 @@ function hasResponseKey(set, key) {
17
17
  (s) => s.kind === Kind.FIELD && (s.alias ?? s.name).value === key
18
18
  );
19
19
  }
20
- function addPreprIds(document, schema) {
20
+ function addPreprIds(document, schema, options = {}) {
21
+ const typename = options.typename ?? false;
21
22
  const roots = new Set(
22
23
  [
23
24
  schema.getQueryType(),
@@ -36,7 +37,7 @@ function addPreprIds(document, schema) {
36
37
  if ((isObjectType(type) || isInterfaceType(type)) && "_id" in type.getFields() && !hasResponseKey(node, "_id")) {
37
38
  added.push(field("_id"));
38
39
  }
39
- if (!hasResponseKey(node, "__typename")) {
40
+ if (typename && !hasResponseKey(node, "__typename")) {
40
41
  added.push(field("__typename"));
41
42
  }
42
43
  if (!added.length) return;
@@ -45,18 +46,19 @@ function addPreprIds(document, schema) {
45
46
  })
46
47
  );
47
48
  }
48
- var preprIds = {
49
- transform({
50
- documents,
51
- schema
52
- }) {
53
- const built = buildASTSchema(schema, { assumeValidSDL: true });
54
- return documents.map(
55
- (file) => file.document ? { ...file, document: addPreprIds(file.document, built) } : file
56
- );
57
- }
58
- };
49
+ function createPreprIds(options = {}) {
50
+ return {
51
+ transform({ documents, schema }) {
52
+ const built = buildASTSchema(schema, { assumeValidSDL: true });
53
+ return documents.map(
54
+ (file) => file.document ? { ...file, document: addPreprIds(file.document, built, options) } : file
55
+ );
56
+ }
57
+ };
58
+ }
59
+ var preprIds = createPreprIds();
59
60
  export {
60
61
  addPreprIds,
62
+ createPreprIds,
61
63
  preprIds
62
64
  };
package/dist/index.cjs CHANGED
@@ -50,7 +50,7 @@ __export(src_exports, {
50
50
  module.exports = __toCommonJS(src_exports);
51
51
 
52
52
  // src/version.ts
53
- var VERSION = "0.5.0-rc.6";
53
+ var VERSION = "0.5.0-rc.7";
54
54
 
55
55
  // src/core/features.ts
56
56
  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 = "0.5.0-rc.6";
8
+ declare const VERSION = "0.5.0-rc.7";
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 = "0.5.0-rc.6";
8
+ declare const VERSION = "0.5.0-rc.7";
9
9
 
10
10
  /**
11
11
  * Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ import {
17
17
  setTrackingParam,
18
18
  stegaClean,
19
19
  trackEvent
20
- } from "./chunk-L3SDL6MP.js";
20
+ } from "./chunk-SUAFASBB.js";
21
21
  import {
22
22
  PreprError,
23
23
  extractAccessToken,
@@ -28,11 +28,11 @@ import {
28
28
  getPreprUUIDFromHeaders,
29
29
  getToolbarPropsFromHeaders,
30
30
  validatePreprToken
31
- } from "./chunk-KZIRM54F.js";
31
+ } from "./chunk-HVS5QLWG.js";
32
32
  import {
33
33
  VERSION,
34
34
  resolveFeatures
35
- } from "./chunk-WHZNCYUS.js";
35
+ } from "./chunk-JUXP5KRX.js";
36
36
  import {
37
37
  createLiveBridge,
38
38
  createLiveStore
@@ -3525,7 +3525,7 @@ function createPreprPreview(opts = {}) {
3525
3525
  var import_react = require("react");
3526
3526
 
3527
3527
  // src/version.ts
3528
- var VERSION = "0.5.0-rc.6";
3528
+ var VERSION = "0.5.0-rc.7";
3529
3529
 
3530
3530
  // src/core/pixel.ts
3531
3531
  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-L3SDL6MP.js";
6
- import "../chunk-WHZNCYUS.js";
5
+ } from "../chunk-SUAFASBB.js";
6
+ import "../chunk-JUXP5KRX.js";
7
7
  import "../chunk-URXSDJOU.js";
8
8
  import "../chunk-S3VT4UWM.js";
9
9
 
@@ -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 = "0.5.0-rc.6";
52
+ var VERSION = "0.5.0-rc.7";
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 = "0.5.0-rc.6";
5
+ var VERSION = "0.5.0-rc.7";
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-KZIRM54F.js";
5
- import "../chunk-WHZNCYUS.js";
4
+ } from "../chunk-HVS5QLWG.js";
5
+ import "../chunk-JUXP5KRX.js";
6
6
  import {
7
7
  parsePatchLog
8
8
  } from "../chunk-KD3GK6TO.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 = "0.5.0-rc.6";
38
+ var VERSION = "0.5.0-rc.7";
39
39
 
40
40
  // src/core/constants.ts
41
41
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "0.5.0-rc.6";
2
+ var VERSION = "0.5.0-rc.7";
3
3
 
4
4
  // src/core/constants.ts
5
5
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -3521,7 +3521,7 @@ function createPreprPreview(opts = {}) {
3521
3521
  }
3522
3522
 
3523
3523
  // src/version.ts
3524
- var VERSION = "0.5.0-rc.6";
3524
+ var VERSION = "0.5.0-rc.7";
3525
3525
 
3526
3526
  // src/core/pixel.ts
3527
3527
  var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
@@ -3497,7 +3497,7 @@ function createPreprPreview(opts = {}) {
3497
3497
  }
3498
3498
 
3499
3499
  // src/version.ts
3500
- var VERSION = "0.5.0-rc.6";
3500
+ var VERSION = "0.5.0-rc.7";
3501
3501
 
3502
3502
  // src/core/pixel.ts
3503
3503
  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 = "0.5.0-rc.6";
38
+ var VERSION = "0.5.0-rc.7";
39
39
 
40
40
  // src/core/constants.ts
41
41
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "0.5.0-rc.6";
2
+ var VERSION = "0.5.0-rc.7";
3
3
 
4
4
  // src/core/constants.ts
5
5
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preprio/toolkit",
3
- "version": "0.5.0-rc.6",
3
+ "version": "0.5.0-rc.7",
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)",