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