prismic-ts-codegen 0.0.2 → 0.0.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.
- package/README.md +51 -2
- package/bin/cli.mjs +19 -36
- package/dist/index.cjs +19 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +104 -0
- package/dist/index.js +12 -33
- package/dist/index.js.map +1 -1
- package/package.json +21 -18
- package/src/cli/loadModels.ts +7 -3
- package/src/cli/types.ts +109 -0
- package/src/lib/addInterfacePropertiesForFields.ts +5 -2
- package/src/lib/addTypeAliasForSharedSlice.ts +2 -0
- package/src/lib/buildFieldDocs.ts +1 -1
- package/src/lib/getAPIIDPath.ts +1 -1
- package/src/lib/getHumanReadableFieldPath.ts +1 -1
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ An experimental Prismic model-to-TypeScript-type generator.
|
|
|
20
20
|
## Install
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
|
-
npm install --save-dev prismic-ts-codegen
|
|
23
|
+
npm install --save-dev prismic-ts-codegen @prismicio/types
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
## Usage
|
|
@@ -62,7 +62,56 @@ npm install --save-dev prismic-ts-codegen
|
|
|
62
62
|
|
|
63
63
|
## Documentation
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
### Configuration
|
|
66
|
+
|
|
67
|
+
`prismic-ts-codegen` is configured using a `prismicCodegen.config.ts` file. It must export a configuration object as the default export.
|
|
68
|
+
|
|
69
|
+
#### Interface
|
|
70
|
+
|
|
71
|
+
See the [`Config` TypeScript interface](./src/cli/types.ts) for a complete reference of each configuration property.
|
|
72
|
+
|
|
73
|
+
#### Full example
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
// prismicCodegen.config.ts
|
|
77
|
+
|
|
78
|
+
import type { Config } from "prismic-ts-codegen";
|
|
79
|
+
|
|
80
|
+
const config: Config = {
|
|
81
|
+
repositoryName: "nextjs-blog-demo",
|
|
82
|
+
accessToken: "abc123xyz345",
|
|
83
|
+
customTypesAPIToken: "abc123xyz345",
|
|
84
|
+
|
|
85
|
+
output: "./types.generated.ts",
|
|
86
|
+
|
|
87
|
+
locales: {
|
|
88
|
+
ids: ["en-us", "fr-fr", "en-gb"],
|
|
89
|
+
fetchFromRepository: true,
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
models: {
|
|
93
|
+
files: ["./customtypes/**/index.json", "./slices/**/model.json"],
|
|
94
|
+
fetchFromRepository: true,
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
fields: {
|
|
98
|
+
embed: {
|
|
99
|
+
providerTypes: {
|
|
100
|
+
YouTube: 'import("./types").OEmbedYouTube',
|
|
101
|
+
Twitter: 'import("./types").OEmbedTwitter',
|
|
102
|
+
Vimeo: 'import("./types").OEmbedVimeo',
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
integrationFields: {
|
|
106
|
+
catalogTypes: {
|
|
107
|
+
shop: 'import("./types").IntegrationFieldShop',
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export default config;
|
|
114
|
+
```
|
|
66
115
|
|
|
67
116
|
## Contributing
|
|
68
117
|
|
package/bin/cli.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import { resolve } from 'path';
|
|
|
5
5
|
import meow from 'meow';
|
|
6
6
|
import { stripIndent } from 'common-tags';
|
|
7
7
|
import { Project } from 'ts-morph';
|
|
8
|
+
import { CustomTypeModelFieldType, CustomTypeModelLinkSelectType, CustomTypeModelSliceType } from '@prismicio/types';
|
|
8
9
|
import { pascalCase as pascalCase$1, pascalCaseTransformMerge } from 'pascal-case';
|
|
9
10
|
import Joi from 'joi';
|
|
10
11
|
import * as prismic from '@prismicio/client';
|
|
@@ -39,33 +40,6 @@ const FIELD_DOCUMENTATION_URLS = {
|
|
|
39
40
|
Slices: "https://prismic.io/docs/core-concepts/slices"
|
|
40
41
|
};
|
|
41
42
|
|
|
42
|
-
const CustomTypeModelFieldType = {
|
|
43
|
-
Boolean: "Boolean",
|
|
44
|
-
Color: "Color",
|
|
45
|
-
Date: "Date",
|
|
46
|
-
Embed: "Embed",
|
|
47
|
-
GeoPoint: "GeoPoint",
|
|
48
|
-
Group: "Group",
|
|
49
|
-
Image: "Image",
|
|
50
|
-
IntegrationFields: "IntegrationFields",
|
|
51
|
-
Link: "Link",
|
|
52
|
-
Number: "Number",
|
|
53
|
-
Select: "Select",
|
|
54
|
-
Slices: "Slices",
|
|
55
|
-
StructuredText: "StructuredText",
|
|
56
|
-
Text: "Text",
|
|
57
|
-
Timestamp: "Timestamp",
|
|
58
|
-
UID: "UID"
|
|
59
|
-
};
|
|
60
|
-
const CustomTypeModelLinkSelectType = {
|
|
61
|
-
Document: "document",
|
|
62
|
-
Media: "media"
|
|
63
|
-
};
|
|
64
|
-
const CustomTypeModelSliceType = {
|
|
65
|
-
Slice: "Slice",
|
|
66
|
-
SharedSlice: "SharedSlice"
|
|
67
|
-
};
|
|
68
|
-
|
|
69
43
|
const isCustomTypeModelField = (model) => {
|
|
70
44
|
return typeof model === "object" && model !== null && "type" in model && "config" in model;
|
|
71
45
|
};
|
|
@@ -481,7 +455,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
481
455
|
writer.writeLine(`Item in ${humanReadablePath}`);
|
|
482
456
|
}
|
|
483
457
|
}
|
|
484
|
-
]
|
|
458
|
+
],
|
|
459
|
+
isExported: true
|
|
485
460
|
});
|
|
486
461
|
addInterfacePropertiesForFields({
|
|
487
462
|
interface: itemInterface,
|
|
@@ -592,7 +567,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
592
567
|
writer.writeLine(`Item in ${humanReadablePath}`);
|
|
593
568
|
}
|
|
594
569
|
}
|
|
595
|
-
]
|
|
570
|
+
],
|
|
571
|
+
isExported: true
|
|
596
572
|
});
|
|
597
573
|
addInterfacePropertiesForFields({
|
|
598
574
|
interface: itemInterface,
|
|
@@ -618,7 +594,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
618
594
|
}
|
|
619
595
|
const sliceType = config.sourceFile.addTypeAlias({
|
|
620
596
|
name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
|
|
621
|
-
type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}
|
|
597
|
+
type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
|
|
598
|
+
isExported: true
|
|
622
599
|
});
|
|
623
600
|
choiceInterfaceNames.push(sliceType.getName());
|
|
624
601
|
}
|
|
@@ -671,7 +648,7 @@ const addInterfacePropertiesForFields = (config) => {
|
|
|
671
648
|
for (const name in config.fields) {
|
|
672
649
|
addInterfacePropertyFromField({
|
|
673
650
|
...config,
|
|
674
|
-
id: name,
|
|
651
|
+
id: name.includes("-") ? `"${name}"` : name,
|
|
675
652
|
model: config.fields[name]
|
|
676
653
|
});
|
|
677
654
|
}
|
|
@@ -819,7 +796,8 @@ const addTypeAliasForSharedSlice = (config) => {
|
|
|
819
796
|
writer.writeLine(`Item in ${humanReadablePath}`);
|
|
820
797
|
}
|
|
821
798
|
}
|
|
822
|
-
]
|
|
799
|
+
],
|
|
800
|
+
isExported: true
|
|
823
801
|
});
|
|
824
802
|
addInterfacePropertiesForFields({
|
|
825
803
|
interface: itemInterface,
|
|
@@ -853,7 +831,8 @@ const addTypeAliasForSharedSlice = (config) => {
|
|
|
853
831
|
writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
|
|
854
832
|
}
|
|
855
833
|
}
|
|
856
|
-
]
|
|
834
|
+
],
|
|
835
|
+
isExported: true
|
|
857
836
|
});
|
|
858
837
|
variationTypeNames.push(variationType.getName());
|
|
859
838
|
}
|
|
@@ -1023,7 +1002,7 @@ const loadModels = async (config) => {
|
|
|
1023
1002
|
});
|
|
1024
1003
|
if (config.fetchFromRepository) {
|
|
1025
1004
|
const [remoteCustomTypeModels, remoteSharedSliceModels] = await Promise.all([
|
|
1026
|
-
customTypesClient.
|
|
1005
|
+
customTypesClient.getAllCustomTypes(),
|
|
1027
1006
|
customTypesClient.getAllSharedSlices()
|
|
1028
1007
|
]);
|
|
1029
1008
|
for (const customTypeModel of remoteCustomTypeModels) {
|
|
@@ -1045,8 +1024,12 @@ const loadModels = async (config) => {
|
|
|
1045
1024
|
}
|
|
1046
1025
|
}
|
|
1047
1026
|
return {
|
|
1048
|
-
customTypeModels: Object.values(customTypeModels),
|
|
1049
|
-
|
|
1027
|
+
customTypeModels: Object.values(customTypeModels).sort((a, b) => {
|
|
1028
|
+
return a.id.localeCompare(b.id);
|
|
1029
|
+
}),
|
|
1030
|
+
sharedSliceModels: Object.values(sharedSliceModels).sort((a, b) => {
|
|
1031
|
+
return a.id.localeCompare(b.id);
|
|
1032
|
+
})
|
|
1050
1033
|
};
|
|
1051
1034
|
};
|
|
1052
1035
|
|
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
const tsMorph = require('ts-morph');
|
|
6
6
|
const commonTags = require('common-tags');
|
|
7
|
+
const types = require('@prismicio/types');
|
|
7
8
|
const pascalCase$1 = require('pascal-case');
|
|
8
9
|
|
|
9
10
|
const BLANK_LINE_IDENTIFIER = "// ___BLANK_LINE_TO_BE_REPLACED___";
|
|
@@ -32,33 +33,6 @@ const FIELD_DOCUMENTATION_URLS = {
|
|
|
32
33
|
Slices: "https://prismic.io/docs/core-concepts/slices"
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
const CustomTypeModelFieldType = {
|
|
36
|
-
Boolean: "Boolean",
|
|
37
|
-
Color: "Color",
|
|
38
|
-
Date: "Date",
|
|
39
|
-
Embed: "Embed",
|
|
40
|
-
GeoPoint: "GeoPoint",
|
|
41
|
-
Group: "Group",
|
|
42
|
-
Image: "Image",
|
|
43
|
-
IntegrationFields: "IntegrationFields",
|
|
44
|
-
Link: "Link",
|
|
45
|
-
Number: "Number",
|
|
46
|
-
Select: "Select",
|
|
47
|
-
Slices: "Slices",
|
|
48
|
-
StructuredText: "StructuredText",
|
|
49
|
-
Text: "Text",
|
|
50
|
-
Timestamp: "Timestamp",
|
|
51
|
-
UID: "UID"
|
|
52
|
-
};
|
|
53
|
-
const CustomTypeModelLinkSelectType = {
|
|
54
|
-
Document: "document",
|
|
55
|
-
Media: "media"
|
|
56
|
-
};
|
|
57
|
-
const CustomTypeModelSliceType = {
|
|
58
|
-
Slice: "Slice",
|
|
59
|
-
SharedSlice: "SharedSlice"
|
|
60
|
-
};
|
|
61
|
-
|
|
62
36
|
const isCustomTypeModelField = (model) => {
|
|
63
37
|
return typeof model === "object" && model !== null && "type" in model && "config" in model;
|
|
64
38
|
};
|
|
@@ -73,7 +47,7 @@ const isSharedSliceModel = (model) => {
|
|
|
73
47
|
|
|
74
48
|
const getAPIIDPath = (config) => {
|
|
75
49
|
return config.path.map((element, i) => {
|
|
76
|
-
if (isCustomTypeModelField(element.model) && (element.model.type === CustomTypeModelFieldType.Group || element.model.type === CustomTypeModelFieldType.Slices)) {
|
|
50
|
+
if (isCustomTypeModelField(element.model) && (element.model.type === types.CustomTypeModelFieldType.Group || element.model.type === types.CustomTypeModelFieldType.Slices)) {
|
|
77
51
|
return `${element.id}[]`;
|
|
78
52
|
} else {
|
|
79
53
|
if (element.id === "items") {
|
|
@@ -135,10 +109,10 @@ const getHumanReadableFieldType = (config) => {
|
|
|
135
109
|
}
|
|
136
110
|
case "Link": {
|
|
137
111
|
switch (config.model.config.select) {
|
|
138
|
-
case CustomTypeModelLinkSelectType.Document: {
|
|
112
|
+
case types.CustomTypeModelLinkSelectType.Document: {
|
|
139
113
|
return "Content Relationship";
|
|
140
114
|
}
|
|
141
|
-
case CustomTypeModelLinkSelectType.Media: {
|
|
115
|
+
case types.CustomTypeModelLinkSelectType.Media: {
|
|
142
116
|
return "Link to Media";
|
|
143
117
|
}
|
|
144
118
|
default: {
|
|
@@ -331,7 +305,7 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
331
305
|
}
|
|
332
306
|
case "Link": {
|
|
333
307
|
switch (config.model.config.select) {
|
|
334
|
-
case CustomTypeModelLinkSelectType.Document: {
|
|
308
|
+
case types.CustomTypeModelLinkSelectType.Document: {
|
|
335
309
|
config.interface.addProperty({
|
|
336
310
|
name: config.id,
|
|
337
311
|
type: config.model.config.customtypes && config.model.config.customtypes.length > 0 ? `prismicT.RelationField<${config.model.config.customtypes.map((type) => `"${type}"`).join(" | ")}>` : "prismicT.RelationField",
|
|
@@ -343,7 +317,7 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
343
317
|
});
|
|
344
318
|
break;
|
|
345
319
|
}
|
|
346
|
-
case CustomTypeModelLinkSelectType.Media: {
|
|
320
|
+
case types.CustomTypeModelLinkSelectType.Media: {
|
|
347
321
|
config.interface.addProperty({
|
|
348
322
|
name: config.id,
|
|
349
323
|
type: "prismicT.LinkToMediaField",
|
|
@@ -474,7 +448,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
474
448
|
writer.writeLine(`Item in ${humanReadablePath}`);
|
|
475
449
|
}
|
|
476
450
|
}
|
|
477
|
-
]
|
|
451
|
+
],
|
|
452
|
+
isExported: true
|
|
478
453
|
});
|
|
479
454
|
addInterfacePropertiesForFields({
|
|
480
455
|
interface: itemInterface,
|
|
@@ -504,9 +479,9 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
504
479
|
const choiceInterfaceNames = [];
|
|
505
480
|
for (const choiceId in config.model.config.choices) {
|
|
506
481
|
const choice = config.model.config.choices[choiceId];
|
|
507
|
-
if (choice.type === CustomTypeModelSliceType.SharedSlice) {
|
|
482
|
+
if (choice.type === types.CustomTypeModelSliceType.SharedSlice) {
|
|
508
483
|
choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
|
|
509
|
-
} else if (choice.type === CustomTypeModelSliceType.Slice) {
|
|
484
|
+
} else if (choice.type === types.CustomTypeModelSliceType.Slice) {
|
|
510
485
|
let primaryInterface;
|
|
511
486
|
if (Object.keys(choice["non-repeat"]).length > 0) {
|
|
512
487
|
primaryInterface = config.sourceFile.addInterface({
|
|
@@ -585,7 +560,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
585
560
|
writer.writeLine(`Item in ${humanReadablePath}`);
|
|
586
561
|
}
|
|
587
562
|
}
|
|
588
|
-
]
|
|
563
|
+
],
|
|
564
|
+
isExported: true
|
|
589
565
|
});
|
|
590
566
|
addInterfacePropertiesForFields({
|
|
591
567
|
interface: itemInterface,
|
|
@@ -611,7 +587,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
611
587
|
}
|
|
612
588
|
const sliceType = config.sourceFile.addTypeAlias({
|
|
613
589
|
name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
|
|
614
|
-
type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}
|
|
590
|
+
type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
|
|
591
|
+
isExported: true
|
|
615
592
|
});
|
|
616
593
|
choiceInterfaceNames.push(sliceType.getName());
|
|
617
594
|
}
|
|
@@ -664,7 +641,7 @@ const addInterfacePropertiesForFields = (config) => {
|
|
|
664
641
|
for (const name in config.fields) {
|
|
665
642
|
addInterfacePropertyFromField({
|
|
666
643
|
...config,
|
|
667
|
-
id: name,
|
|
644
|
+
id: name.includes("-") ? `"${name}"` : name,
|
|
668
645
|
model: config.fields[name]
|
|
669
646
|
});
|
|
670
647
|
}
|
|
@@ -812,7 +789,8 @@ const addTypeAliasForSharedSlice = (config) => {
|
|
|
812
789
|
writer.writeLine(`Item in ${humanReadablePath}`);
|
|
813
790
|
}
|
|
814
791
|
}
|
|
815
|
-
]
|
|
792
|
+
],
|
|
793
|
+
isExported: true
|
|
816
794
|
});
|
|
817
795
|
addInterfacePropertiesForFields({
|
|
818
796
|
interface: itemInterface,
|
|
@@ -846,7 +824,8 @@ const addTypeAliasForSharedSlice = (config) => {
|
|
|
846
824
|
writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
|
|
847
825
|
}
|
|
848
826
|
}
|
|
849
|
-
]
|
|
827
|
+
],
|
|
828
|
+
isExported: true
|
|
850
829
|
});
|
|
851
830
|
variationTypeNames.push(variationType.getName());
|
|
852
831
|
}
|