@preprio/toolkit 0.4.3 → 0.5.0-rc.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.
- package/README.md +416 -23
- package/dist/astro/index.cjs +1290 -1
- package/dist/astro/index.d.cts +47 -1
- package/dist/astro/index.d.ts +47 -1
- package/dist/astro/index.js +1297 -1
- package/dist/chunk-F46GJS6Y.js +26 -0
- package/dist/{chunk-H7VA7NGY.js → chunk-IDEC77LZ.js} +1480 -621
- package/dist/chunk-IPMB32AZ.js +246 -0
- package/dist/chunk-KD3GK6TO.js +1221 -0
- package/dist/chunk-S3VT4UWM.js +75 -0
- package/dist/chunk-TBBU3LDD.js +184 -0
- package/dist/chunk-URXSDJOU.js +356 -0
- package/dist/codegen/index.cjs +82 -0
- package/dist/codegen/index.d.cts +70 -0
- package/dist/codegen/index.d.ts +70 -0
- package/dist/codegen/index.js +64 -0
- package/dist/index.cjs +3412 -409
- package/dist/index.d.cts +287 -5
- package/dist/index.d.ts +287 -5
- package/dist/index.js +307 -144
- package/dist/live-preview-DLXGXRE5.js +18 -0
- package/dist/nextjs/components.cjs +1662 -388
- package/dist/nextjs/components.d.cts +1 -1
- package/dist/nextjs/components.d.ts +1 -1
- package/dist/nextjs/components.js +4 -1
- package/dist/nextjs/index.cjs +1181 -6
- package/dist/nextjs/index.d.cts +83 -3
- package/dist/nextjs/index.d.ts +83 -3
- package/dist/nextjs/index.js +1179 -7
- package/dist/nextjs/live-action.cjs +285 -0
- package/dist/nextjs/live-action.d.cts +12 -0
- package/dist/nextjs/live-action.d.ts +12 -0
- package/dist/nextjs/live-action.js +68 -0
- package/dist/nextjs/live.cjs +1899 -0
- package/dist/nextjs/live.d.cts +54 -0
- package/dist/nextjs/live.d.ts +54 -0
- package/dist/nextjs/live.js +268 -0
- package/dist/nuxt/chunk-BWPGO25E.js +340 -0
- package/dist/nuxt/index.cjs +1 -1
- package/dist/nuxt/index.js +1 -1
- package/dist/nuxt/live-preview-2RCU35W5.js +1189 -0
- package/dist/nuxt/live.cjs +1816 -0
- package/dist/nuxt/live.d.cts +43 -0
- package/dist/nuxt/live.d.ts +43 -0
- package/dist/nuxt/live.js +198 -0
- package/dist/{pixel-e1fUWl2Y.d.cts → pixel-CX422dCO.d.cts} +18 -3
- package/dist/{pixel-e1fUWl2Y.d.ts → pixel-CX422dCO.d.ts} +18 -3
- package/dist/react/components.cjs +1662 -388
- package/dist/react/components.d.cts +17 -2
- package/dist/react/components.d.ts +17 -2
- package/dist/react/components.js +1662 -388
- package/dist/sveltekit/chunk-BWPGO25E.js +340 -0
- package/dist/sveltekit/index.cjs +1 -1
- package/dist/sveltekit/index.js +1 -1
- package/dist/sveltekit/live-preview-2RCU35W5.js +1189 -0
- package/dist/sveltekit/live.cjs +1830 -0
- package/dist/sveltekit/live.d.cts +59 -0
- package/dist/sveltekit/live.d.ts +59 -0
- package/dist/sveltekit/live.js +204 -0
- package/package.json +37 -11
- package/src/astro/PreprToolbar.astro +7 -1
|
@@ -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,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
|
+
};
|