@preprio/toolkit 0.6.0-alpha.9 → 1.0.0-rc.2

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 (116) hide show
  1. package/README.md +111 -1363
  2. package/dist/astro/index.cjs +1555 -182
  3. package/dist/astro/index.d.cts +201 -1
  4. package/dist/astro/index.d.ts +201 -1
  5. package/dist/astro/index.js +1557 -29
  6. package/dist/chunk-A2J7R4LR.js +256 -0
  7. package/dist/{chunk-5QIWVDP2.js → chunk-DBKZWOXA.js} +1 -1
  8. package/dist/{chunk-WQBKQG4R.js → chunk-EWXBE3JD.js} +6 -1
  9. package/dist/chunk-KM2GME62.js +1255 -0
  10. package/dist/{chunk-J66UOCK6.js → chunk-LJA5K4KM.js} +1680 -579
  11. package/dist/chunk-UTKH7DOI.js +58 -0
  12. package/dist/chunk-V7MCHPE7.js +85 -0
  13. package/dist/chunk-Y7JWV7WV.js +374 -0
  14. package/dist/codegen/index.cjs +82 -0
  15. package/dist/codegen/index.d.cts +70 -0
  16. package/dist/codegen/index.d.ts +70 -0
  17. package/dist/codegen/index.js +64 -0
  18. package/dist/index.cjs +3729 -553
  19. package/dist/index.d.cts +403 -19
  20. package/dist/index.d.ts +403 -19
  21. package/dist/index.js +345 -24
  22. package/dist/live-preview-HCBNBC6C.js +19 -0
  23. package/dist/nextjs/components.cjs +1872 -754
  24. package/dist/nextjs/components.d.cts +5 -51
  25. package/dist/nextjs/components.d.ts +5 -51
  26. package/dist/nextjs/components.js +8 -299
  27. package/dist/nextjs/index.cjs +1228 -181
  28. package/dist/nextjs/index.d.cts +247 -92
  29. package/dist/nextjs/index.d.ts +247 -92
  30. package/dist/nextjs/index.js +1629 -111
  31. package/dist/nextjs/live-action.cjs +298 -0
  32. package/dist/nextjs/live-action.d.cts +12 -0
  33. package/dist/nextjs/live-action.d.ts +12 -0
  34. package/dist/nextjs/live-action.js +68 -0
  35. package/dist/nextjs/live.cjs +2010 -0
  36. package/dist/nextjs/live.d.cts +54 -0
  37. package/dist/nextjs/live.d.ts +54 -0
  38. package/dist/nextjs/live.js +287 -0
  39. package/dist/nuxt/chunk-5RBZPRQN.js +350 -0
  40. package/dist/nuxt/chunk-LUKVPOJ2.js +32 -0
  41. package/dist/nuxt/index.cjs +15 -16
  42. package/dist/nuxt/index.d.cts +167 -6
  43. package/dist/nuxt/index.d.ts +167 -6
  44. package/dist/nuxt/index.js +402 -15
  45. package/dist/nuxt/live-preview-KO4IKWTX.js +1234 -0
  46. package/dist/nuxt/live.cjs +1928 -0
  47. package/dist/nuxt/live.d.cts +45 -0
  48. package/dist/nuxt/live.d.ts +45 -0
  49. package/dist/nuxt/live.js +216 -0
  50. package/dist/{types-BYH9XqmN.d.cts → pixel-DqU7NvgW.d.cts} +102 -5
  51. package/dist/{types-BYH9XqmN.d.ts → pixel-DqU7NvgW.d.ts} +102 -5
  52. package/dist/react/components.cjs +1865 -480
  53. package/dist/react/components.d.cts +43 -8
  54. package/dist/react/components.d.ts +43 -8
  55. package/dist/react/components.js +1865 -480
  56. package/dist/sveltekit/chunk-5RBZPRQN.js +350 -0
  57. package/dist/sveltekit/chunk-LUKVPOJ2.js +32 -0
  58. package/dist/sveltekit/index.cjs +15 -16
  59. package/dist/sveltekit/index.d.cts +111 -10
  60. package/dist/sveltekit/index.d.ts +111 -10
  61. package/dist/sveltekit/index.js +398 -15
  62. package/dist/sveltekit/live-preview-KO4IKWTX.js +1234 -0
  63. package/dist/sveltekit/live.cjs +1940 -0
  64. package/dist/sveltekit/live.d.cts +61 -0
  65. package/dist/sveltekit/live.d.ts +61 -0
  66. package/dist/sveltekit/live.js +220 -0
  67. package/package.json +132 -71
  68. package/src/astro/PreprToolbar.astro +7 -1
  69. package/src/astro/PreprTrackingPixel.astro +2 -4
  70. package/src/nuxt/PreprToolbar.vue +12 -1
  71. package/src/nuxt/PreprTrackingPixel.vue +2 -4
  72. package/src/sveltekit/PreprToolbar.svelte +17 -2
  73. package/src/sveltekit/PreprTrackingPixel.svelte +2 -4
  74. package/dist/astro/chunk-UN4JADNU.js +0 -173
  75. package/dist/astro/graphql.cjs +0 -1078
  76. package/dist/astro/graphql.d.cts +0 -300
  77. package/dist/astro/graphql.d.ts +0 -300
  78. package/dist/astro/graphql.js +0 -984
  79. package/dist/astro/index-XT_z-RYT.d.cts +0 -130
  80. package/dist/astro/index-XT_z-RYT.d.ts +0 -130
  81. package/dist/chunk-3NCEVJJ3.js +0 -10
  82. package/dist/chunk-S5RWVMDJ.js +0 -148
  83. package/dist/chunk-XL4Y7FGW.js +0 -499
  84. package/dist/graphql/codegen.cjs +0 -557
  85. package/dist/graphql/codegen.d.cts +0 -23
  86. package/dist/graphql/codegen.d.ts +0 -23
  87. package/dist/graphql/codegen.js +0 -99
  88. package/dist/graphql.cjs +0 -1020
  89. package/dist/graphql.d.cts +0 -335
  90. package/dist/graphql.d.ts +0 -335
  91. package/dist/graphql.js +0 -448
  92. package/dist/nextjs/chunk-CMV6ZP6E.js +0 -549
  93. package/dist/nextjs/graphql.cjs +0 -1836
  94. package/dist/nextjs/graphql.d.cts +0 -498
  95. package/dist/nextjs/graphql.d.ts +0 -498
  96. package/dist/nextjs/graphql.js +0 -1422
  97. package/dist/nextjs/server-BL-sqZvu.d.cts +0 -156
  98. package/dist/nextjs/server-BL-sqZvu.d.ts +0 -156
  99. package/dist/nuxt/chunk-XWOABVMW.js +0 -417
  100. package/dist/nuxt/graphql.cjs +0 -1085
  101. package/dist/nuxt/graphql.d.cts +0 -297
  102. package/dist/nuxt/graphql.d.ts +0 -297
  103. package/dist/nuxt/graphql.js +0 -985
  104. package/dist/nuxt/server-8v6OHbsw.d.cts +0 -158
  105. package/dist/nuxt/server-8v6OHbsw.d.ts +0 -158
  106. package/dist/pixel-DwW7cXJm.d.cts +0 -49
  107. package/dist/pixel-DwW7cXJm.d.ts +0 -49
  108. package/dist/server-Brr1UgtW.d.ts +0 -59
  109. package/dist/server-DJuS3YU0.d.cts +0 -59
  110. package/dist/sveltekit/chunk-3S5RA6G5.js +0 -413
  111. package/dist/sveltekit/graphql.cjs +0 -1091
  112. package/dist/sveltekit/graphql.d.cts +0 -302
  113. package/dist/sveltekit/graphql.d.ts +0 -302
  114. package/dist/sveltekit/graphql.js +0 -991
  115. package/dist/sveltekit/server-CO847mEV.d.cts +0 -98
  116. package/dist/sveltekit/server-CO847mEV.d.ts +0 -98
@@ -0,0 +1,70 @@
1
+ import { DocumentNode, GraphQLSchema } from 'graphql';
2
+
3
+ /**
4
+ * The slice of a GraphQL Codegen `DocumentFile` this transform touches,
5
+ * written out so the package needs no dependency on `@graphql-codegen/*`.
6
+ */
7
+ interface CodegenDocumentFile {
8
+ document?: DocumentNode;
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;
20
+ }
21
+ /**
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.
24
+ *
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.
31
+ */
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
+ }
40
+ /**
41
+ * GraphQL Codegen document transform that runs `addPreprIds` on every
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:
53
+ *
54
+ * ```ts
55
+ * // codegen.ts
56
+ * import { preprIds } from '@preprio/toolkit/codegen';
57
+ *
58
+ * generates: {
59
+ * 'src/gql/': {
60
+ * preset: 'client',
61
+ * documentTransforms: [preprIds],
62
+ * },
63
+ * },
64
+ * ```
65
+ *
66
+ * `createPreprIds({ typename: true })` adds `__typename` as well.
67
+ */
68
+ declare const preprIds: CodegenDocumentTransform;
69
+
70
+ export { type CodegenDocumentFile, type CodegenDocumentTransform, type PreprIdsOptions, addPreprIds, createPreprIds, preprIds };
@@ -0,0 +1,64 @@
1
+ // src/codegen/index.ts
2
+ import {
3
+ buildASTSchema,
4
+ isCompositeType,
5
+ isInterfaceType,
6
+ isObjectType,
7
+ Kind,
8
+ TypeInfo,
9
+ visit,
10
+ visitWithTypeInfo
11
+ } from "graphql";
12
+ function field(name) {
13
+ return { kind: Kind.FIELD, name: { kind: Kind.NAME, value: name } };
14
+ }
15
+ function hasResponseKey(set, key) {
16
+ return set.selections.some(
17
+ (s) => s.kind === Kind.FIELD && (s.alias ?? s.name).value === key
18
+ );
19
+ }
20
+ function addPreprIds(document, schema, options = {}) {
21
+ const typename = options.typename ?? false;
22
+ const roots = new Set(
23
+ [
24
+ schema.getQueryType(),
25
+ schema.getMutationType(),
26
+ schema.getSubscriptionType()
27
+ ].filter(Boolean)
28
+ );
29
+ const typeInfo = new TypeInfo(schema);
30
+ return visit(
31
+ document,
32
+ visitWithTypeInfo(typeInfo, {
33
+ SelectionSet(node) {
34
+ const type = typeInfo.getParentType();
35
+ if (!type || !isCompositeType(type) || roots.has(type)) return;
36
+ const added = [];
37
+ if ((isObjectType(type) || isInterfaceType(type)) && "_id" in type.getFields() && !hasResponseKey(node, "_id")) {
38
+ added.push(field("_id"));
39
+ }
40
+ if (typename && !hasResponseKey(node, "__typename")) {
41
+ added.push(field("__typename"));
42
+ }
43
+ if (!added.length) return;
44
+ return { ...node, selections: [...added, ...node.selections] };
45
+ }
46
+ })
47
+ );
48
+ }
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();
60
+ export {
61
+ addPreprIds,
62
+ createPreprIds,
63
+ preprIds
64
+ };