prismic-ts-codegen 0.1.6 → 0.1.8

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 (175) hide show
  1. package/README.md +5 -5
  2. package/bin/prismic-ts-codegen.mjs +3 -0
  3. package/dist/cli/configSchema.cjs +38 -0
  4. package/dist/cli/configSchema.cjs.map +1 -0
  5. package/dist/cli/configSchema.d.ts +3 -0
  6. package/dist/cli/configSchema.js +38 -0
  7. package/dist/cli/configSchema.js.map +1 -0
  8. package/dist/cli/constants.cjs +5 -0
  9. package/dist/cli/constants.cjs.map +1 -0
  10. package/dist/cli/constants.d.ts +1 -0
  11. package/dist/cli/constants.js +5 -0
  12. package/dist/cli/constants.js.map +1 -0
  13. package/dist/cli/index.d.ts +1 -0
  14. package/dist/cli/loadConfig.cjs +32 -0
  15. package/dist/cli/loadConfig.cjs.map +1 -0
  16. package/dist/cli/loadConfig.d.ts +6 -0
  17. package/dist/cli/loadConfig.js +32 -0
  18. package/dist/cli/loadConfig.js.map +1 -0
  19. package/dist/cli/loadLocaleIDs.cjs +37 -0
  20. package/dist/cli/loadLocaleIDs.cjs.map +1 -0
  21. package/dist/cli/loadLocaleIDs.d.ts +10 -0
  22. package/dist/cli/loadLocaleIDs.js +20 -0
  23. package/dist/cli/loadLocaleIDs.js.map +1 -0
  24. package/dist/cli/loadModels.cjs +79 -0
  25. package/dist/cli/loadModels.cjs.map +1 -0
  26. package/dist/cli/loadModels.d.ts +15 -0
  27. package/dist/cli/loadModels.js +62 -0
  28. package/dist/cli/loadModels.js.map +1 -0
  29. package/dist/cli/types.d.ts +175 -0
  30. package/dist/cli.cjs +119 -0
  31. package/dist/cli.cjs.map +1 -0
  32. package/dist/cli.js +118 -0
  33. package/dist/cli.js.map +1 -0
  34. package/dist/constants.cjs +29 -0
  35. package/dist/constants.cjs.map +1 -0
  36. package/dist/constants.d.ts +5 -0
  37. package/dist/constants.js +29 -0
  38. package/dist/constants.js.map +1 -0
  39. package/dist/detectTypesProvider.cjs +19 -0
  40. package/dist/detectTypesProvider.cjs.map +1 -0
  41. package/dist/detectTypesProvider.d.ts +13 -0
  42. package/dist/detectTypesProvider.js +19 -0
  43. package/dist/detectTypesProvider.js.map +1 -0
  44. package/dist/generateTypes.cjs +109 -0
  45. package/dist/generateTypes.cjs.map +1 -0
  46. package/dist/generateTypes.d.ts +15 -0
  47. package/dist/generateTypes.js +109 -0
  48. package/dist/generateTypes.js.map +1 -0
  49. package/dist/index.cjs +6 -1086
  50. package/dist/index.cjs.map +1 -1
  51. package/dist/index.d.ts +5 -192
  52. package/dist/index.js +5 -1081
  53. package/dist/index.js.map +1 -1
  54. package/dist/lib/addInterfacePropertiesForFields.cjs +490 -0
  55. package/dist/lib/addInterfacePropertiesForFields.cjs.map +1 -0
  56. package/dist/lib/addInterfacePropertiesForFields.d.ts +21 -0
  57. package/dist/lib/addInterfacePropertiesForFields.js +490 -0
  58. package/dist/lib/addInterfacePropertiesForFields.js.map +1 -0
  59. package/dist/lib/addTypeAliasForCustomType.cjs +81 -0
  60. package/dist/lib/addTypeAliasForCustomType.cjs.map +1 -0
  61. package/dist/lib/addTypeAliasForCustomType.d.ts +11 -0
  62. package/dist/lib/addTypeAliasForCustomType.js +81 -0
  63. package/dist/lib/addTypeAliasForCustomType.js.map +1 -0
  64. package/dist/lib/addTypeAliasForSharedSlice.cjs +149 -0
  65. package/dist/lib/addTypeAliasForSharedSlice.cjs.map +1 -0
  66. package/dist/lib/addTypeAliasForSharedSlice.d.ts +10 -0
  67. package/dist/lib/addTypeAliasForSharedSlice.js +149 -0
  68. package/dist/lib/addTypeAliasForSharedSlice.js.map +1 -0
  69. package/dist/lib/buildFieldDocs.cjs +43 -0
  70. package/dist/lib/buildFieldDocs.cjs.map +1 -0
  71. package/dist/lib/buildFieldDocs.d.ts +14 -0
  72. package/dist/lib/buildFieldDocs.js +43 -0
  73. package/dist/lib/buildFieldDocs.js.map +1 -0
  74. package/dist/lib/buildSharedSliceInterfaceNamePart.cjs +8 -0
  75. package/dist/lib/buildSharedSliceInterfaceNamePart.cjs.map +1 -0
  76. package/dist/lib/buildSharedSliceInterfaceNamePart.d.ts +5 -0
  77. package/dist/lib/buildSharedSliceInterfaceNamePart.js +8 -0
  78. package/dist/lib/buildSharedSliceInterfaceNamePart.js.map +1 -0
  79. package/dist/lib/buildTypeName.cjs +12 -0
  80. package/dist/lib/buildTypeName.cjs.map +1 -0
  81. package/dist/lib/buildTypeName.d.ts +1 -0
  82. package/dist/lib/buildTypeName.js +12 -0
  83. package/dist/lib/buildTypeName.js.map +1 -0
  84. package/dist/lib/getAPIIDPath.cjs +23 -0
  85. package/dist/lib/getAPIIDPath.cjs.map +1 -0
  86. package/dist/lib/getAPIIDPath.d.ts +10 -0
  87. package/dist/lib/getAPIIDPath.js +23 -0
  88. package/dist/lib/getAPIIDPath.js.map +1 -0
  89. package/dist/lib/getFieldDocumentationURL.cjs +8 -0
  90. package/dist/lib/getFieldDocumentationURL.cjs.map +1 -0
  91. package/dist/lib/getFieldDocumentationURL.d.ts +6 -0
  92. package/dist/lib/getFieldDocumentationURL.js +8 -0
  93. package/dist/lib/getFieldDocumentationURL.js.map +1 -0
  94. package/dist/lib/getHumanReadableFieldPath.cjs +19 -0
  95. package/dist/lib/getHumanReadableFieldPath.cjs.map +1 -0
  96. package/dist/lib/getHumanReadableFieldPath.d.ts +10 -0
  97. package/dist/lib/getHumanReadableFieldPath.js +19 -0
  98. package/dist/lib/getHumanReadableFieldPath.js.map +1 -0
  99. package/dist/lib/getHumanReadableFieldType.cjs +37 -0
  100. package/dist/lib/getHumanReadableFieldType.cjs.map +1 -0
  101. package/dist/lib/getHumanReadableFieldType.d.ts +6 -0
  102. package/dist/lib/getHumanReadableFieldType.js +37 -0
  103. package/dist/lib/getHumanReadableFieldType.js.map +1 -0
  104. package/dist/lib/getHumanReadableModelName.cjs +24 -0
  105. package/dist/lib/getHumanReadableModelName.cjs.map +1 -0
  106. package/dist/lib/getHumanReadableModelName.d.ts +7 -0
  107. package/dist/lib/getHumanReadableModelName.js +24 -0
  108. package/dist/lib/getHumanReadableModelName.js.map +1 -0
  109. package/dist/lib/getSourceFileText.cjs +9 -0
  110. package/dist/lib/getSourceFileText.cjs.map +1 -0
  111. package/dist/lib/getSourceFileText.d.ts +2 -0
  112. package/dist/lib/getSourceFileText.js +9 -0
  113. package/dist/lib/getSourceFileText.js.map +1 -0
  114. package/dist/lib/hasOwnProperty.cjs +7 -0
  115. package/dist/lib/hasOwnProperty.cjs.map +1 -0
  116. package/dist/lib/hasOwnProperty.d.ts +10 -0
  117. package/dist/lib/hasOwnProperty.js +7 -0
  118. package/dist/lib/hasOwnProperty.js.map +1 -0
  119. package/dist/lib/isCustomTypeModel.cjs +7 -0
  120. package/dist/lib/isCustomTypeModel.cjs.map +1 -0
  121. package/dist/lib/isCustomTypeModel.d.ts +2 -0
  122. package/dist/lib/isCustomTypeModel.js +7 -0
  123. package/dist/lib/isCustomTypeModel.js.map +1 -0
  124. package/dist/lib/isCustomTypeModelField.cjs +7 -0
  125. package/dist/lib/isCustomTypeModelField.cjs.map +1 -0
  126. package/dist/lib/isCustomTypeModelField.d.ts +2 -0
  127. package/dist/lib/isCustomTypeModelField.js +7 -0
  128. package/dist/lib/isCustomTypeModelField.js.map +1 -0
  129. package/dist/lib/isCustomTypeModelSlice.cjs +9 -0
  130. package/dist/lib/isCustomTypeModelSlice.cjs.map +1 -0
  131. package/dist/lib/isCustomTypeModelSlice.d.ts +2 -0
  132. package/dist/lib/isCustomTypeModelSlice.js +9 -0
  133. package/dist/lib/isCustomTypeModelSlice.js.map +1 -0
  134. package/dist/lib/isSharedSliceModel.cjs +9 -0
  135. package/dist/lib/isSharedSliceModel.cjs.map +1 -0
  136. package/dist/lib/isSharedSliceModel.d.ts +2 -0
  137. package/dist/lib/isSharedSliceModel.js +9 -0
  138. package/dist/lib/isSharedSliceModel.js.map +1 -0
  139. package/dist/lib/pascalCase.cjs +8 -0
  140. package/dist/lib/pascalCase.cjs.map +1 -0
  141. package/dist/lib/pascalCase.d.ts +1 -0
  142. package/dist/lib/pascalCase.js +8 -0
  143. package/dist/lib/pascalCase.js.map +1 -0
  144. package/dist/lib/writeFieldDocsDescription.cjs +29 -0
  145. package/dist/lib/writeFieldDocsDescription.cjs.map +1 -0
  146. package/dist/lib/writeFieldDocsDescription.d.ts +13 -0
  147. package/dist/lib/writeFieldDocsDescription.js +29 -0
  148. package/dist/lib/writeFieldDocsDescription.js.map +1 -0
  149. package/dist/types.d.ts +14 -0
  150. package/package.json +45 -36
  151. package/src/cli/configSchema.ts +2 -0
  152. package/src/cli/index.ts +9 -5
  153. package/src/cli/loadConfig.ts +4 -2
  154. package/src/cli/loadModels.ts +3 -3
  155. package/src/cli/types.ts +10 -0
  156. package/src/constants.ts +6 -3
  157. package/src/detectTypesProvider.ts +41 -0
  158. package/src/generateTypes.ts +11 -6
  159. package/src/index.ts +4 -1
  160. package/src/lib/addInterfacePropertiesForFields.ts +27 -27
  161. package/src/lib/addTypeAliasForCustomType.ts +7 -5
  162. package/src/lib/addTypeAliasForSharedSlice.ts +5 -4
  163. package/src/lib/buildFieldDocs.ts +2 -1
  164. package/src/lib/getAPIIDPath.ts +3 -2
  165. package/src/lib/getFieldDocumentationURL.ts +1 -1
  166. package/src/lib/getHumanReadableFieldPath.ts +2 -1
  167. package/src/lib/getHumanReadableFieldType.ts +2 -2
  168. package/src/lib/getHumanReadableModelName.ts +1 -1
  169. package/src/lib/getSourceFileText.ts +1 -1
  170. package/src/lib/isCustomTypeModel.ts +1 -1
  171. package/src/lib/isCustomTypeModelField.ts +1 -1
  172. package/src/lib/isCustomTypeModelSlice.ts +1 -1
  173. package/src/lib/isSharedSliceModel.ts +1 -1
  174. package/src/types.ts +1 -1
  175. package/bin/cli.mjs +0 -1342
package/dist/index.js CHANGED
@@ -1,1083 +1,7 @@
1
- import { Project, ModuleDeclarationKind } from 'ts-morph';
2
- import { CustomTypeModelSliceType, CustomTypeModelFieldType, CustomTypeModelLinkSelectType } from '@prismicio/types';
3
- import { pascalCase as pascalCase$1, pascalCaseTransformMerge } from 'pascal-case';
4
-
5
- const BLANK_LINE_IDENTIFIER = "// ___BLANK_LINE_TO_BE_REPLACED___";
6
- const CUSTOM_TYPES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/custom-types";
7
- const SHARED_SLICES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/reusing-slices";
8
- const FIELD_DOCUMENTATION_URLS = {
9
- UID: "https://prismic.io/docs/core-concepts/uid",
10
- Boolean: "https://prismic.io/docs/core-concepts/boolean",
11
- Color: "https://prismic.io/docs/core-concepts/color",
12
- Date: "https://prismic.io/docs/core-concepts/date",
13
- Timestamp: "https://prismic.io/docs/core-concepts/timestamp",
14
- Number: "https://prismic.io/docs/core-concepts/number",
15
- Text: "https://prismic.io/docs/core-concepts/key-text",
16
- Select: "https://prismic.io/docs/core-concepts/select",
17
- StructuredText: "https://prismic.io/docs/core-concepts/rich-text-title",
18
- Image: "https://prismic.io/docs/core-concepts/image",
19
- Link: "https://prismic.io/docs/core-concepts/link-content-relationship",
20
- Embed: "https://prismic.io/docs/core-concepts/embed",
21
- GeoPoint: "https://prismic.io/docs/core-concepts/geopoint",
22
- Group: "https://prismic.io/docs/core-concepts/group",
23
- IntegrationFields: "https://prismic.io/docs/core-concepts/integration-fields",
24
- Slices: "https://prismic.io/docs/core-concepts/slices",
25
- LegacySlices: "https://prismic.io/docs/core-concepts/slices"
1
+ import { generateTypes } from "./generateTypes.js";
2
+ import { detectTypesProvider } from "./detectTypesProvider.js";
3
+ export {
4
+ detectTypesProvider,
5
+ generateTypes
26
6
  };
27
-
28
- const isCustomTypeModelField = (model) => {
29
- return typeof model === "object" && model !== null && "type" in model;
30
- };
31
-
32
- const hasOwnProperty = (obj, prop) => {
33
- return obj.hasOwnProperty(prop);
34
- };
35
-
36
- const isCustomTypeModelSlice = (model) => {
37
- return typeof model === "object" && model !== null && hasOwnProperty(model, "type") && model.type === CustomTypeModelSliceType.Slice;
38
- };
39
-
40
- const isSharedSliceModel = (model) => {
41
- return typeof model === "object" && model !== null && hasOwnProperty(model, "type") && model.type === CustomTypeModelSliceType.SharedSlice;
42
- };
43
-
44
- const getAPIIDPath = (config) => {
45
- return config.path.map((element, i) => {
46
- if (isCustomTypeModelField(element.model) && (element.model.type === CustomTypeModelFieldType.Group || element.model.type === CustomTypeModelFieldType.Slices)) {
47
- return `${element.id}[]`;
48
- } else {
49
- if (element.id === "items") {
50
- const previousElement = config.path[i - 1];
51
- if (isCustomTypeModelSlice(previousElement.model) || isSharedSliceModel(previousElement.model)) {
52
- return `${element.id}[]`;
53
- }
54
- }
55
- return element.id;
56
- }
57
- }).join(".");
58
- };
59
-
60
- const getFieldDocumentationURL = (config) => {
61
- return FIELD_DOCUMENTATION_URLS[config.model.type];
62
- };
63
-
64
- const isCustomTypeModel = (model) => {
65
- return typeof model === "object" && model !== null && "json" in model;
66
- };
67
-
68
- const getHumanReadableModelName = (config) => {
69
- if (isCustomTypeModel(config.model)) {
70
- return config.model.label || config.model.id;
71
- } else if (isSharedSliceModel(config.model)) {
72
- return config.model.name;
73
- } else if (isCustomTypeModelField(config.model)) {
74
- if (config.model.config && "label" in config.model.config) {
75
- return config.model.config.label || config.id;
76
- } else if (config.model.config && "fieldset" in config.model) {
77
- return config.model.fieldset || config.id;
78
- }
79
- } else if (isCustomTypeModelSlice(config.model)) {
80
- return config.model.fieldset || config.id;
81
- }
82
- return `\`${config.id}\``;
83
- };
84
-
85
- const getHumanReadableFieldPath = (config) => {
86
- return config.path.map((element) => {
87
- if (element.label) {
88
- return element.label;
89
- } else if (element.model) {
90
- return getHumanReadableModelName({
91
- id: element.id,
92
- model: element.model
93
- });
94
- } else {
95
- return element.id;
96
- }
97
- }).join(" \u2192 ");
98
- };
99
-
100
- const getHumanReadableFieldType = (config) => {
101
- switch (config.model.type) {
102
- case "StructuredText": {
103
- const isTitleField = config.model.config && "single" in config.model.config && config.model.config.single && config.model.config.single.split(",").every((blockType) => /heading/.test(blockType));
104
- return isTitleField ? "Title" : "Rich Text";
105
- }
106
- case "IntegrationFields": {
107
- const catalog = config.model.config?.catalog;
108
- return `Integration Fields (Catalog: ${catalog ? `\`${catalog}\`` : "*unknown*"})`;
109
- }
110
- case "Link": {
111
- switch (config.model.config?.select) {
112
- case CustomTypeModelLinkSelectType.Document: {
113
- return "Content Relationship";
114
- }
115
- case CustomTypeModelLinkSelectType.Media: {
116
- return "Link to Media";
117
- }
118
- default: {
119
- return "Link";
120
- }
121
- }
122
- }
123
- case "Slices": {
124
- return "Slice Zone";
125
- }
126
- default: {
127
- return config.model.type;
128
- }
129
- }
130
- };
131
-
132
- const writeFieldDocsDescription = (config) => {
133
- if (config.description) {
134
- config.writer.writeLine(config.description);
135
- }
136
- config.writer.blankLineIfLastNot();
137
- if (config.fieldType) {
138
- config.writer.writeLine(`- **Field Type**: ${config.fieldType}`);
139
- }
140
- if (config.placeholder) {
141
- config.writer.writeLine(`- **Placeholder**: ${config.placeholder}`);
142
- }
143
- if (config.defaultValue !== void 0) {
144
- const stringifiedDefaultValue = typeof config.defaultValue === "boolean" ? `${config.defaultValue}` : config.defaultValue;
145
- config.writer.writeLine(`- **Default Value**: ${stringifiedDefaultValue}`);
146
- }
147
- if (config.apiIDPath) {
148
- config.writer.writeLine(`- **API ID Path**: ${config.apiIDPath}`);
149
- }
150
- if (config.tabName) {
151
- config.writer.writeLine(`- **Tab**: ${config.tabName}`);
152
- }
153
- if (config.documentationURL) {
154
- config.writer.writeLine(`- **Documentation**: ${config.documentationURL}`);
155
- }
156
- };
157
-
158
- const buildFieldDocs = (config) => {
159
- return [
160
- {
161
- description: (writer) => {
162
- const name = getHumanReadableModelName({
163
- id: config.id,
164
- model: config.model
165
- });
166
- const humanReadablePath = getHumanReadableFieldPath({
167
- path: config.path
168
- });
169
- const fieldType = getHumanReadableFieldType({
170
- model: config.model
171
- });
172
- const placeholder = config.model.config && "placeholder" in config.model.config && config.model.config.placeholder ? config.model.config.placeholder : `*None*`;
173
- const defaultValue = config.model.config && "default_value" in config.model.config ? config.model.config.default_value : void 0;
174
- const apiIDPath = getAPIIDPath({
175
- path: [...config.path, { id: config.id, model: config.model }]
176
- });
177
- writeFieldDocsDescription({
178
- writer,
179
- description: `${name} field in *${humanReadablePath}*`,
180
- fieldType,
181
- placeholder,
182
- defaultValue,
183
- apiIDPath,
184
- tabName: config.tabName,
185
- documentationURL: getFieldDocumentationURL({ model: config.model })
186
- });
187
- }
188
- }
189
- ];
190
- };
191
-
192
- const pascalCase = (input) => pascalCase$1(input, {
193
- transform: pascalCaseTransformMerge
194
- });
195
-
196
- const buildSharedSliceInterfaceNamePart = (config) => {
197
- return pascalCase(`${config.id} Slice`);
198
- };
199
-
200
- const buildTypeName = (...parts) => {
201
- let name = pascalCase(parts.filter(Boolean).join(" "));
202
- if (/^[0-9]/.test(name) ? "_" : "") {
203
- name = `_${name}`;
204
- }
205
- return name;
206
- };
207
-
208
- const addInterfacePropertyForField = (config) => {
209
- switch (config.model.type) {
210
- case "UID": {
211
- break;
212
- }
213
- case "Boolean": {
214
- config.interface.addProperty({
215
- name: config.id,
216
- type: "prismicT.BooleanField",
217
- docs: buildFieldDocs({
218
- id: config.id,
219
- model: config.model,
220
- path: config.path,
221
- tabName: config.tabName
222
- })
223
- });
224
- break;
225
- }
226
- case "Color": {
227
- config.interface.addProperty({
228
- name: config.id,
229
- type: "prismicT.ColorField",
230
- docs: buildFieldDocs({
231
- id: config.id,
232
- model: config.model,
233
- path: config.path,
234
- tabName: config.tabName
235
- })
236
- });
237
- break;
238
- }
239
- case "Date": {
240
- config.interface.addProperty({
241
- name: config.id,
242
- type: "prismicT.DateField",
243
- docs: buildFieldDocs({
244
- id: config.id,
245
- model: config.model,
246
- path: config.path,
247
- tabName: config.tabName
248
- })
249
- });
250
- break;
251
- }
252
- case "Embed": {
253
- const providerTypes = [];
254
- if (config.fieldConfigs.embed?.providerTypes) {
255
- for (const providerType in config.fieldConfigs.embed?.providerTypes) {
256
- const configuredProviderType = config.fieldConfigs.embed?.providerTypes[providerType];
257
- providerTypes.push(
258
- `({ provider_name: "${providerType}" } & ${configuredProviderType})`
259
- );
260
- }
261
- }
262
- config.interface.addProperty({
263
- name: config.id,
264
- type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(
265
- " | "
266
- )})>` : "prismicT.EmbedField",
267
- docs: buildFieldDocs({
268
- id: config.id,
269
- model: config.model,
270
- path: config.path,
271
- tabName: config.tabName
272
- })
273
- });
274
- break;
275
- }
276
- case "GeoPoint": {
277
- config.interface.addProperty({
278
- name: config.id,
279
- type: "prismicT.GeoPointField",
280
- docs: buildFieldDocs({
281
- id: config.id,
282
- model: config.model,
283
- path: config.path,
284
- tabName: config.tabName
285
- })
286
- });
287
- break;
288
- }
289
- case "Image": {
290
- if (config.model.config?.thumbnails && config.model.config.thumbnails.length > 0) {
291
- const thumbnailNames = config.model.config.thumbnails.map((thumbnail) => `"${thumbnail.name}"`).join(" | ");
292
- config.interface.addProperty({
293
- name: config.id,
294
- type: `prismicT.ImageField<${thumbnailNames}>`,
295
- docs: buildFieldDocs({
296
- id: config.id,
297
- model: config.model,
298
- path: config.path,
299
- tabName: config.tabName
300
- })
301
- });
302
- } else {
303
- config.interface.addProperty({
304
- name: config.id,
305
- type: "prismicT.ImageField<never>",
306
- docs: buildFieldDocs({
307
- id: config.id,
308
- model: config.model,
309
- path: config.path,
310
- tabName: config.tabName
311
- })
312
- });
313
- }
314
- break;
315
- }
316
- case "IntegrationFields": {
317
- const catalogType = config.model.config?.catalog ? config.fieldConfigs.integrationFields?.catalogTypes?.[config.model.config.catalog] : void 0;
318
- config.interface.addProperty({
319
- name: config.id,
320
- type: catalogType ? `prismicT.IntegrationFields<${catalogType}>` : "prismicT.IntegrationFields",
321
- docs: buildFieldDocs({
322
- id: config.id,
323
- model: config.model,
324
- path: config.path,
325
- tabName: config.tabName
326
- })
327
- });
328
- break;
329
- }
330
- case "Link": {
331
- switch (config.model.config?.select) {
332
- case CustomTypeModelLinkSelectType.Document: {
333
- config.model.config;
334
- config.interface.addProperty({
335
- name: config.id,
336
- type: "customtypes" in config.model.config && config.model.config.customtypes && config.model.config.customtypes.length > 0 ? `prismicT.RelationField<${config.model.config.customtypes.map((type) => `"${type}"`).join(" | ")}>` : "prismicT.RelationField",
337
- docs: buildFieldDocs({
338
- id: config.id,
339
- model: config.model,
340
- path: config.path,
341
- tabName: config.tabName
342
- })
343
- });
344
- break;
345
- }
346
- case CustomTypeModelLinkSelectType.Media: {
347
- config.interface.addProperty({
348
- name: config.id,
349
- type: "prismicT.LinkToMediaField",
350
- docs: buildFieldDocs({
351
- id: config.id,
352
- model: config.model,
353
- path: config.path,
354
- tabName: config.tabName
355
- })
356
- });
357
- break;
358
- }
359
- default: {
360
- config.interface.addProperty({
361
- name: config.id,
362
- type: "prismicT.LinkField",
363
- docs: buildFieldDocs({
364
- id: config.id,
365
- model: config.model,
366
- path: config.path,
367
- tabName: config.tabName
368
- })
369
- });
370
- }
371
- }
372
- break;
373
- }
374
- case "Number": {
375
- config.interface.addProperty({
376
- name: config.id,
377
- type: "prismicT.NumberField",
378
- docs: buildFieldDocs({
379
- id: config.id,
380
- model: config.model,
381
- path: config.path,
382
- tabName: config.tabName
383
- })
384
- });
385
- break;
386
- }
387
- case "StructuredText": {
388
- const isTitleField = config.model.config && "single" in config.model.config && config.model.config.single && config.model.config.single.split(",").every((blockType) => /heading/.test(blockType));
389
- if (isTitleField) {
390
- config.interface.addProperty({
391
- name: config.id,
392
- type: "prismicT.TitleField",
393
- docs: buildFieldDocs({
394
- id: config.id,
395
- model: config.model,
396
- path: config.path,
397
- tabName: config.tabName
398
- })
399
- });
400
- } else {
401
- config.interface.addProperty({
402
- name: config.id,
403
- type: "prismicT.RichTextField",
404
- docs: buildFieldDocs({
405
- id: config.id,
406
- model: config.model,
407
- path: config.path,
408
- tabName: config.tabName
409
- })
410
- });
411
- }
412
- break;
413
- }
414
- case "Select": {
415
- const options = config.model.config?.options?.map((option) => `"${option}"`).join(" | ");
416
- const hasDefault = Boolean(config.model.config?.default_value);
417
- if (hasDefault) {
418
- config.interface.addProperty({
419
- name: config.id,
420
- type: `prismicT.SelectField<${options || "string"}, "filled">`,
421
- docs: buildFieldDocs({
422
- id: config.id,
423
- model: config.model,
424
- path: config.path,
425
- tabName: config.tabName
426
- })
427
- });
428
- } else {
429
- config.interface.addProperty({
430
- name: config.id,
431
- type: `prismicT.SelectField<${options}>`,
432
- docs: buildFieldDocs({
433
- id: config.id,
434
- model: config.model,
435
- path: config.path,
436
- tabName: config.tabName
437
- })
438
- });
439
- }
440
- break;
441
- }
442
- case "Text": {
443
- config.interface.addProperty({
444
- name: config.id,
445
- type: "prismicT.KeyTextField",
446
- docs: buildFieldDocs({
447
- id: config.id,
448
- model: config.model,
449
- path: config.path,
450
- tabName: config.tabName
451
- })
452
- });
453
- break;
454
- }
455
- case "Timestamp": {
456
- config.interface.addProperty({
457
- name: config.id,
458
- type: "prismicT.TimestampField",
459
- docs: buildFieldDocs({
460
- id: config.id,
461
- model: config.model,
462
- path: config.path,
463
- tabName: config.tabName
464
- })
465
- });
466
- break;
467
- }
468
- case "Group": {
469
- const itemInterface = config.sourceFile.addInterface({
470
- name: buildTypeName(
471
- config.path[0].id,
472
- "Document",
473
- "Data",
474
- config.id,
475
- "Item"
476
- ),
477
- docs: [
478
- {
479
- description: (writer) => {
480
- const humanReadablePath = getHumanReadableFieldPath({
481
- path: [
482
- ...config.path,
483
- {
484
- id: config.id,
485
- model: config.model
486
- }
487
- ]
488
- });
489
- writer.writeLine(`Item in ${humanReadablePath}`);
490
- }
491
- }
492
- ],
493
- isExported: true
494
- });
495
- if (config.model.config?.fields) {
496
- addInterfacePropertiesForFields({
497
- interface: itemInterface,
498
- sourceFile: config.sourceFile,
499
- fields: config.model.config.fields,
500
- path: [
501
- ...config.path,
502
- {
503
- id: config.id,
504
- model: config.model
505
- }
506
- ],
507
- fieldConfigs: config.fieldConfigs
508
- });
509
- }
510
- config.interface.addProperty({
511
- name: config.id,
512
- type: `prismicT.GroupField<Simplify<${itemInterface.getName()}>>`,
513
- docs: buildFieldDocs({
514
- id: config.id,
515
- model: config.model,
516
- path: config.path,
517
- tabName: config.tabName
518
- })
519
- });
520
- break;
521
- }
522
- case "Slices": {
523
- const choiceInterfaceNames = [];
524
- if (config.model.config?.choices) {
525
- for (const choiceId in config.model.config.choices) {
526
- const choice = config.model.config.choices[choiceId];
527
- if (choice.type === CustomTypeModelSliceType.SharedSlice) {
528
- choiceInterfaceNames.push(
529
- buildSharedSliceInterfaceNamePart({ id: choiceId })
530
- );
531
- } else if (choice.type === CustomTypeModelSliceType.Slice) {
532
- let primaryInterface;
533
- if (choice["non-repeat"] && Object.keys(choice["non-repeat"]).length > 0) {
534
- primaryInterface = config.sourceFile.addInterface({
535
- name: buildTypeName(
536
- config.path[0].id,
537
- "Document",
538
- "Data",
539
- config.id,
540
- choiceId,
541
- "Slice",
542
- "Primary"
543
- ),
544
- docs: [
545
- {
546
- description: (writer) => {
547
- const humanReadablePath = getHumanReadableFieldPath({
548
- path: [
549
- ...config.path,
550
- {
551
- id: config.id,
552
- model: config.model
553
- },
554
- {
555
- id: choiceId,
556
- model: choice
557
- },
558
- {
559
- id: "primary",
560
- label: "Primary"
561
- }
562
- ]
563
- });
564
- writer.writeLine(
565
- `Primary content in ${humanReadablePath}`
566
- );
567
- }
568
- }
569
- ]
570
- });
571
- addInterfacePropertiesForFields({
572
- interface: primaryInterface,
573
- sourceFile: config.sourceFile,
574
- fields: choice["non-repeat"],
575
- path: [
576
- ...config.path,
577
- {
578
- id: config.id,
579
- model: config.model
580
- },
581
- {
582
- id: choiceId,
583
- model: choice
584
- },
585
- {
586
- id: "primary",
587
- label: "Primary"
588
- }
589
- ],
590
- fieldConfigs: config.fieldConfigs
591
- });
592
- }
593
- let itemInterface;
594
- if (choice.repeat && Object.keys(choice.repeat).length > 0) {
595
- itemInterface = config.sourceFile.addInterface({
596
- name: buildTypeName(
597
- config.path[0].id,
598
- "Document",
599
- "Data",
600
- config.id,
601
- choiceId,
602
- "Slice",
603
- "Item"
604
- ),
605
- docs: [
606
- {
607
- description: (writer) => {
608
- const humanReadablePath = getHumanReadableFieldPath({
609
- path: [
610
- ...config.path,
611
- {
612
- id: config.id,
613
- model: config.model
614
- },
615
- {
616
- id: choiceId,
617
- model: choice
618
- },
619
- {
620
- id: "items",
621
- label: "Items"
622
- }
623
- ]
624
- });
625
- writer.writeLine(`Item in ${humanReadablePath}`);
626
- }
627
- }
628
- ],
629
- isExported: true
630
- });
631
- addInterfacePropertiesForFields({
632
- interface: itemInterface,
633
- sourceFile: config.sourceFile,
634
- fields: choice.repeat,
635
- path: [
636
- ...config.path,
637
- {
638
- id: config.id,
639
- model: config.model
640
- },
641
- {
642
- id: choiceId,
643
- model: choice
644
- },
645
- {
646
- id: "items",
647
- label: "Items"
648
- }
649
- ],
650
- fieldConfigs: config.fieldConfigs
651
- });
652
- }
653
- const sliceType = config.sourceFile.addTypeAlias({
654
- name: buildTypeName(
655
- config.path[0].id,
656
- "Document",
657
- "Data",
658
- config.id,
659
- choiceId,
660
- "Slice"
661
- ),
662
- type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
663
- isExported: true
664
- });
665
- choiceInterfaceNames.push(sliceType.getName());
666
- }
667
- }
668
- }
669
- const slicesType = config.sourceFile.addTypeAlias({
670
- name: buildTypeName(
671
- config.path[0].id,
672
- "Document",
673
- "Data",
674
- config.id,
675
- "Slice"
676
- ),
677
- type: choiceInterfaceNames.length > 0 ? choiceInterfaceNames.join(" | ") : "never",
678
- docs: [
679
- {
680
- description: (writer) => {
681
- const humanReadablePath = getHumanReadableFieldPath({
682
- path: [
683
- ...config.path,
684
- {
685
- id: config.id,
686
- model: config.model
687
- }
688
- ]
689
- });
690
- writer.writeLine(`Slice for *${humanReadablePath}*`);
691
- }
692
- }
693
- ]
694
- });
695
- config.interface.addProperty({
696
- name: config.id,
697
- type: `prismicT.SliceZone<${slicesType.getName()}>`,
698
- docs: buildFieldDocs({
699
- id: config.id,
700
- model: config.model,
701
- path: config.path,
702
- tabName: config.tabName
703
- })
704
- });
705
- break;
706
- }
707
- default: {
708
- config.interface.addProperty({
709
- name: config.id,
710
- type: "unknown",
711
- docs: buildFieldDocs({
712
- id: config.id,
713
- model: config.model,
714
- path: config.path,
715
- tabName: config.tabName
716
- })
717
- });
718
- }
719
- }
720
- };
721
- const addInterfacePropertiesForFields = (config) => {
722
- for (const name in config.fields) {
723
- addInterfacePropertyForField({
724
- ...config,
725
- id: name.includes("-") || name.includes(":") || /^[0-9]/.test(name) ? `"${name}"` : name,
726
- model: config.fields[name],
727
- tabName: config.tabName
728
- });
729
- }
730
- };
731
-
732
- const collectCustomTypeFields = (model) => {
733
- return Object.assign({}, ...Object.values(model.json));
734
- };
735
- const addTypeAliasForCustomType = ({
736
- model,
737
- sourceFile,
738
- localeIDs,
739
- fieldConfigs
740
- }) => {
741
- const { uid: uidField, ...fields } = collectCustomTypeFields(model);
742
- const hasDataFields = Object.keys(fields).length > 0;
743
- const hasUIDField = Boolean(uidField);
744
- let dataInterface;
745
- if (hasDataFields) {
746
- dataInterface = sourceFile.addInterface({
747
- name: buildTypeName(model.id, "Document", "Data"),
748
- docs: [
749
- {
750
- description: `Content for ${model.label} documents`
751
- }
752
- ]
753
- });
754
- for (const tabName in model.json) {
755
- const { uid: _uidField, ...tabFields } = model.json[tabName];
756
- addInterfacePropertiesForFields({
757
- fields: tabFields,
758
- interface: dataInterface,
759
- sourceFile,
760
- path: [
761
- {
762
- id: model.id,
763
- model
764
- }
765
- ],
766
- fieldConfigs,
767
- tabName
768
- });
769
- }
770
- } else {
771
- dataInterface = sourceFile.addTypeAlias({
772
- name: buildTypeName(model.id, "Document", "Data"),
773
- type: `Record<string, never>`,
774
- docs: [
775
- {
776
- description: `Content for ${model.label} documents`
777
- }
778
- ]
779
- });
780
- }
781
- return sourceFile.addTypeAlias({
782
- name: buildTypeName(model.id, "Document"),
783
- typeParameters: [
784
- {
785
- name: "Lang",
786
- constraint: "string",
787
- default: localeIDs.length > 0 ? localeIDs.map((localeID) => `"${localeID}"`).join(" | ") : "string"
788
- }
789
- ],
790
- type: hasUIDField ? `prismicT.PrismicDocumentWithUID<Simplify<${dataInterface.getName()}>, "${model.id}", Lang>` : `prismicT.PrismicDocumentWithoutUID<Simplify<${dataInterface.getName()}>, "${model.id}", Lang>`,
791
- docs: [
792
- {
793
- description: (writer) => {
794
- writer.writeLine(`${model.label} document from Prismic`);
795
- writer.blankLine();
796
- writer.writeLine(`- **API ID**: \`${model.id}\``);
797
- writer.writeLine(`- **Repeatable**: \`${model.repeatable}\``);
798
- writer.writeLine(
799
- `- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`
800
- );
801
- },
802
- tags: [
803
- {
804
- tagName: "typeParam",
805
- text: "Lang - Language API ID of the document."
806
- }
807
- ]
808
- }
809
- ],
810
- isExported: true
811
- });
812
- };
813
-
814
- const addTypeAliasForSharedSlice = (config) => {
815
- const variationTypeNames = [];
816
- for (const variation of config.model.variations) {
817
- let primaryInterface;
818
- if (variation.primary && Object.keys(variation.primary).length > 0) {
819
- primaryInterface = config.sourceFile.addInterface({
820
- name: buildTypeName(
821
- buildSharedSliceInterfaceNamePart({ id: config.model.id }),
822
- variation.id,
823
- "Primary"
824
- ),
825
- docs: [
826
- {
827
- description: (writer) => {
828
- const humanReadablePath = getHumanReadableFieldPath({
829
- path: [
830
- {
831
- id: config.model.id,
832
- model: config.model
833
- },
834
- {
835
- id: "primary",
836
- label: "Primary"
837
- }
838
- ]
839
- });
840
- writer.writeLine(`Primary content in ${humanReadablePath}`);
841
- }
842
- }
843
- ]
844
- });
845
- addInterfacePropertiesForFields({
846
- interface: primaryInterface,
847
- sourceFile: config.sourceFile,
848
- fields: variation.primary,
849
- path: [
850
- {
851
- id: config.model.id,
852
- model: config.model
853
- },
854
- {
855
- id: "primary",
856
- label: "Primary"
857
- }
858
- ],
859
- fieldConfigs: config.fieldConfigs
860
- });
861
- }
862
- let itemInterface;
863
- if (variation.items && Object.keys(variation.items).length > 0) {
864
- itemInterface = config.sourceFile.addInterface({
865
- name: buildTypeName(
866
- buildSharedSliceInterfaceNamePart({ id: config.model.id }),
867
- variation.id,
868
- "Item"
869
- ),
870
- docs: [
871
- {
872
- description: (writer) => {
873
- const humanReadablePath = getHumanReadableFieldPath({
874
- path: [
875
- {
876
- id: config.model.id,
877
- model: config.model
878
- },
879
- {
880
- id: "items",
881
- label: "Items"
882
- }
883
- ]
884
- });
885
- writer.writeLine(`Item in ${humanReadablePath}`);
886
- }
887
- }
888
- ],
889
- isExported: true
890
- });
891
- addInterfacePropertiesForFields({
892
- interface: itemInterface,
893
- sourceFile: config.sourceFile,
894
- fields: variation.items,
895
- path: [
896
- {
897
- id: config.model.id,
898
- model: config.model
899
- },
900
- {
901
- id: "items",
902
- label: "Items"
903
- }
904
- ],
905
- fieldConfigs: config.fieldConfigs
906
- });
907
- }
908
- const variationType = config.sourceFile.addTypeAlias({
909
- name: buildTypeName(
910
- buildSharedSliceInterfaceNamePart({ id: config.model.id }),
911
- variation.id
912
- ),
913
- type: `prismicT.SharedSliceVariation<"${variation.id}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
914
- docs: [
915
- {
916
- description: (writer) => {
917
- writer.writeLine(
918
- `${variation.name} variation for ${config.model.name} Slice`
919
- );
920
- writer.blankLine();
921
- writer.writeLine(`- **API ID**: \`${variation.id}\``);
922
- writer.writeLine(`- **Description**: \`${variation.description}\``);
923
- writer.writeLine(
924
- `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
925
- );
926
- }
927
- }
928
- ],
929
- isExported: true
930
- });
931
- variationTypeNames.push(variationType.getName());
932
- }
933
- const variationsType = config.sourceFile.addTypeAlias({
934
- name: buildTypeName(config.model.id, "Slice", "Variation"),
935
- type: variationTypeNames.length > 0 ? variationTypeNames.join(" | ") : "never",
936
- docs: [
937
- {
938
- description: (writer) => {
939
- const humanReadablePath = getHumanReadableFieldPath({
940
- path: [
941
- {
942
- id: config.model.id,
943
- model: config.model
944
- }
945
- ]
946
- });
947
- writer.writeLine(`Slice variation for *${humanReadablePath}*`);
948
- }
949
- }
950
- ]
951
- });
952
- return config.sourceFile.addTypeAlias({
953
- name: buildTypeName(
954
- buildSharedSliceInterfaceNamePart({ id: config.model.id })
955
- ),
956
- type: `prismicT.SharedSlice<"${config.model.id}", ${variationsType.getName()}>`,
957
- docs: [
958
- {
959
- description: (writer) => {
960
- writer.writeLine(`${config.model.name} Shared Slice`);
961
- writer.blankLine();
962
- writer.writeLine(`- **API ID**: \`${config.model.id}\``);
963
- writer.writeLine(
964
- `- **Description**: \`${config.model.description}\``
965
- );
966
- writer.writeLine(
967
- `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
968
- );
969
- }
970
- }
971
- ],
972
- isExported: true
973
- });
974
- };
975
-
976
- const getSourceFileText = (sourceFile) => {
977
- const text = sourceFile.print().replace(new RegExp(BLANK_LINE_IDENTIFIER, "g"), "");
978
- return text;
979
- };
980
-
981
- const generateTypes = (config = {}) => {
982
- const project = new Project({
983
- useInMemoryFileSystem: true
984
- });
985
- const sourceFile = project.createSourceFile("types.d.ts");
986
- sourceFile.addImportDeclaration({
987
- moduleSpecifier: "@prismicio/types",
988
- namespaceImport: "prismicT",
989
- isTypeOnly: true
990
- });
991
- sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
992
- const simplifyTypeAlias = sourceFile.addTypeAlias({
993
- name: "Simplify",
994
- typeParameters: [
995
- {
996
- name: "T"
997
- }
998
- ],
999
- type: `{ [KeyType in keyof T]: T[KeyType] }`
1000
- });
1001
- if (config.customTypeModels) {
1002
- for (const model of config.customTypeModels) {
1003
- addTypeAliasForCustomType({
1004
- model,
1005
- sourceFile,
1006
- localeIDs: config.localeIDs || [],
1007
- fieldConfigs: config.fieldConfigs || {}
1008
- });
1009
- }
1010
- if (config.customTypeModels.length > 0) {
1011
- sourceFile.addTypeAlias({
1012
- name: "AllDocumentTypes",
1013
- type: config.customTypeModels.map(
1014
- (customTypeModel) => buildTypeName(customTypeModel.id, "Document")
1015
- ).join(" | "),
1016
- isExported: true
1017
- });
1018
- }
1019
- }
1020
- if (config.sharedSliceModels) {
1021
- for (const model of config.sharedSliceModels) {
1022
- addTypeAliasForSharedSlice({
1023
- model,
1024
- sourceFile,
1025
- fieldConfigs: config.fieldConfigs || {}
1026
- });
1027
- }
1028
- }
1029
- if (config.clientIntegration?.includeCreateClientInterface || config.clientIntegration?.includeContentNamespace) {
1030
- sourceFile.addImportDeclaration({
1031
- moduleSpecifier: "@prismicio/client",
1032
- namespaceImport: "prismic",
1033
- isTypeOnly: true
1034
- });
1035
- const clientModuleDeclaration = sourceFile.addModule({
1036
- name: '"@prismicio/client"',
1037
- hasDeclareKeyword: true,
1038
- declarationKind: ModuleDeclarationKind.Module
1039
- });
1040
- if (config.clientIntegration.includeCreateClientInterface) {
1041
- clientModuleDeclaration.addInterface({
1042
- name: "CreateClient",
1043
- callSignatures: [
1044
- {
1045
- parameters: [
1046
- {
1047
- name: "repositoryNameOrEndpoint",
1048
- type: "string"
1049
- },
1050
- {
1051
- name: "options",
1052
- type: "prismic.ClientConfig",
1053
- hasQuestionToken: true
1054
- }
1055
- ],
1056
- returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
1057
- }
1058
- ]
1059
- });
1060
- }
1061
- if (config.clientIntegration.includeContentNamespace) {
1062
- const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
1063
- name: "Content",
1064
- declarationKind: ModuleDeclarationKind.Namespace
1065
- });
1066
- const exportSymbols = sourceFile.getExportSymbols().filter((exportSymbol) => {
1067
- return exportSymbol.getName() !== simplifyTypeAlias.getName();
1068
- });
1069
- contentNamespaceDeclaration.addExportDeclaration({
1070
- isTypeOnly: true,
1071
- namedExports: exportSymbols.map((exportSymbol) => {
1072
- return {
1073
- name: exportSymbol.getName()
1074
- };
1075
- })
1076
- });
1077
- }
1078
- }
1079
- return getSourceFileText(sourceFile);
1080
- };
1081
-
1082
- export { generateTypes };
1083
7
  //# sourceMappingURL=index.js.map