prismic-ts-codegen 0.0.2 → 0.0.5

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/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: {
@@ -173,6 +147,9 @@ const writeFieldDocsDescription = (config) => {
173
147
  if (config.apiIDPath) {
174
148
  config.writer.writeLine(`- **API ID Path**: ${config.apiIDPath}`);
175
149
  }
150
+ if (config.tabName) {
151
+ config.writer.writeLine(`- **Tab**: ${config.tabName}`);
152
+ }
176
153
  if (config.documentationURL) {
177
154
  config.writer.writeLine(`- **Documentation**: ${config.documentationURL}`);
178
155
  }
@@ -204,6 +181,7 @@ const buildFieldDocs = (config) => {
204
181
  placeholder,
205
182
  defaultValue,
206
183
  apiIDPath,
184
+ tabName: config.tabName,
207
185
  documentationURL: getFieldDocumentationURL({ model: config.model })
208
186
  });
209
187
  }
@@ -219,7 +197,7 @@ const buildSharedSliceInterfaceName = (config) => {
219
197
  return pascalCase(`${config.id} Slice`);
220
198
  };
221
199
 
222
- const addInterfacePropertyFromField = (config) => {
200
+ const addInterfacePropertyForField = (config) => {
223
201
  switch (config.model.type) {
224
202
  case "UID": {
225
203
  break;
@@ -231,7 +209,8 @@ const addInterfacePropertyFromField = (config) => {
231
209
  docs: buildFieldDocs({
232
210
  id: config.id,
233
211
  model: config.model,
234
- path: config.path
212
+ path: config.path,
213
+ tabName: config.tabName
235
214
  })
236
215
  });
237
216
  break;
@@ -243,7 +222,8 @@ const addInterfacePropertyFromField = (config) => {
243
222
  docs: buildFieldDocs({
244
223
  id: config.id,
245
224
  model: config.model,
246
- path: config.path
225
+ path: config.path,
226
+ tabName: config.tabName
247
227
  })
248
228
  });
249
229
  break;
@@ -255,7 +235,8 @@ const addInterfacePropertyFromField = (config) => {
255
235
  docs: buildFieldDocs({
256
236
  id: config.id,
257
237
  model: config.model,
258
- path: config.path
238
+ path: config.path,
239
+ tabName: config.tabName
259
240
  })
260
241
  });
261
242
  break;
@@ -274,7 +255,8 @@ const addInterfacePropertyFromField = (config) => {
274
255
  docs: buildFieldDocs({
275
256
  id: config.id,
276
257
  model: config.model,
277
- path: config.path
258
+ path: config.path,
259
+ tabName: config.tabName
278
260
  })
279
261
  });
280
262
  break;
@@ -286,7 +268,8 @@ const addInterfacePropertyFromField = (config) => {
286
268
  docs: buildFieldDocs({
287
269
  id: config.id,
288
270
  model: config.model,
289
- path: config.path
271
+ path: config.path,
272
+ tabName: config.tabName
290
273
  })
291
274
  });
292
275
  break;
@@ -300,7 +283,8 @@ const addInterfacePropertyFromField = (config) => {
300
283
  docs: buildFieldDocs({
301
284
  id: config.id,
302
285
  model: config.model,
303
- path: config.path
286
+ path: config.path,
287
+ tabName: config.tabName
304
288
  })
305
289
  });
306
290
  } else {
@@ -310,7 +294,8 @@ const addInterfacePropertyFromField = (config) => {
310
294
  docs: buildFieldDocs({
311
295
  id: config.id,
312
296
  model: config.model,
313
- path: config.path
297
+ path: config.path,
298
+ tabName: config.tabName
314
299
  })
315
300
  });
316
301
  }
@@ -324,33 +309,36 @@ const addInterfacePropertyFromField = (config) => {
324
309
  docs: buildFieldDocs({
325
310
  id: config.id,
326
311
  model: config.model,
327
- path: config.path
312
+ path: config.path,
313
+ tabName: config.tabName
328
314
  })
329
315
  });
330
316
  break;
331
317
  }
332
318
  case "Link": {
333
319
  switch (config.model.config.select) {
334
- case CustomTypeModelLinkSelectType.Document: {
320
+ case types.CustomTypeModelLinkSelectType.Document: {
335
321
  config.interface.addProperty({
336
322
  name: config.id,
337
323
  type: config.model.config.customtypes && config.model.config.customtypes.length > 0 ? `prismicT.RelationField<${config.model.config.customtypes.map((type) => `"${type}"`).join(" | ")}>` : "prismicT.RelationField",
338
324
  docs: buildFieldDocs({
339
325
  id: config.id,
340
326
  model: config.model,
341
- path: config.path
327
+ path: config.path,
328
+ tabName: config.tabName
342
329
  })
343
330
  });
344
331
  break;
345
332
  }
346
- case CustomTypeModelLinkSelectType.Media: {
333
+ case types.CustomTypeModelLinkSelectType.Media: {
347
334
  config.interface.addProperty({
348
335
  name: config.id,
349
336
  type: "prismicT.LinkToMediaField",
350
337
  docs: buildFieldDocs({
351
338
  id: config.id,
352
339
  model: config.model,
353
- path: config.path
340
+ path: config.path,
341
+ tabName: config.tabName
354
342
  })
355
343
  });
356
344
  break;
@@ -362,7 +350,8 @@ const addInterfacePropertyFromField = (config) => {
362
350
  docs: buildFieldDocs({
363
351
  id: config.id,
364
352
  model: config.model,
365
- path: config.path
353
+ path: config.path,
354
+ tabName: config.tabName
366
355
  })
367
356
  });
368
357
  }
@@ -376,7 +365,8 @@ const addInterfacePropertyFromField = (config) => {
376
365
  docs: buildFieldDocs({
377
366
  id: config.id,
378
367
  model: config.model,
379
- path: config.path
368
+ path: config.path,
369
+ tabName: config.tabName
380
370
  })
381
371
  });
382
372
  break;
@@ -390,7 +380,8 @@ const addInterfacePropertyFromField = (config) => {
390
380
  docs: buildFieldDocs({
391
381
  id: config.id,
392
382
  model: config.model,
393
- path: config.path
383
+ path: config.path,
384
+ tabName: config.tabName
394
385
  })
395
386
  });
396
387
  } else {
@@ -400,7 +391,8 @@ const addInterfacePropertyFromField = (config) => {
400
391
  docs: buildFieldDocs({
401
392
  id: config.id,
402
393
  model: config.model,
403
- path: config.path
394
+ path: config.path,
395
+ tabName: config.tabName
404
396
  })
405
397
  });
406
398
  }
@@ -416,7 +408,8 @@ const addInterfacePropertyFromField = (config) => {
416
408
  docs: buildFieldDocs({
417
409
  id: config.id,
418
410
  model: config.model,
419
- path: config.path
411
+ path: config.path,
412
+ tabName: config.tabName
420
413
  })
421
414
  });
422
415
  } else {
@@ -426,7 +419,8 @@ const addInterfacePropertyFromField = (config) => {
426
419
  docs: buildFieldDocs({
427
420
  id: config.id,
428
421
  model: config.model,
429
- path: config.path
422
+ path: config.path,
423
+ tabName: config.tabName
430
424
  })
431
425
  });
432
426
  }
@@ -439,7 +433,8 @@ const addInterfacePropertyFromField = (config) => {
439
433
  docs: buildFieldDocs({
440
434
  id: config.id,
441
435
  model: config.model,
442
- path: config.path
436
+ path: config.path,
437
+ tabName: config.tabName
443
438
  })
444
439
  });
445
440
  break;
@@ -451,7 +446,8 @@ const addInterfacePropertyFromField = (config) => {
451
446
  docs: buildFieldDocs({
452
447
  id: config.id,
453
448
  model: config.model,
454
- path: config.path
449
+ path: config.path,
450
+ tabName: config.tabName
455
451
  })
456
452
  });
457
453
  break;
@@ -474,7 +470,8 @@ const addInterfacePropertyFromField = (config) => {
474
470
  writer.writeLine(`Item in ${humanReadablePath}`);
475
471
  }
476
472
  }
477
- ]
473
+ ],
474
+ isExported: true
478
475
  });
479
476
  addInterfacePropertiesForFields({
480
477
  interface: itemInterface,
@@ -495,7 +492,8 @@ const addInterfacePropertyFromField = (config) => {
495
492
  docs: buildFieldDocs({
496
493
  id: config.id,
497
494
  model: config.model,
498
- path: config.path
495
+ path: config.path,
496
+ tabName: config.tabName
499
497
  })
500
498
  });
501
499
  break;
@@ -504,9 +502,9 @@ const addInterfacePropertyFromField = (config) => {
504
502
  const choiceInterfaceNames = [];
505
503
  for (const choiceId in config.model.config.choices) {
506
504
  const choice = config.model.config.choices[choiceId];
507
- if (choice.type === CustomTypeModelSliceType.SharedSlice) {
505
+ if (choice.type === types.CustomTypeModelSliceType.SharedSlice) {
508
506
  choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
509
- } else if (choice.type === CustomTypeModelSliceType.Slice) {
507
+ } else if (choice.type === types.CustomTypeModelSliceType.Slice) {
510
508
  let primaryInterface;
511
509
  if (Object.keys(choice["non-repeat"]).length > 0) {
512
510
  primaryInterface = config.sourceFile.addInterface({
@@ -585,7 +583,8 @@ const addInterfacePropertyFromField = (config) => {
585
583
  writer.writeLine(`Item in ${humanReadablePath}`);
586
584
  }
587
585
  }
588
- ]
586
+ ],
587
+ isExported: true
589
588
  });
590
589
  addInterfacePropertiesForFields({
591
590
  interface: itemInterface,
@@ -611,7 +610,8 @@ const addInterfacePropertyFromField = (config) => {
611
610
  }
612
611
  const sliceType = config.sourceFile.addTypeAlias({
613
612
  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"}>`
613
+ type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
614
+ isExported: true
615
615
  });
616
616
  choiceInterfaceNames.push(sliceType.getName());
617
617
  }
@@ -642,7 +642,8 @@ const addInterfacePropertyFromField = (config) => {
642
642
  docs: buildFieldDocs({
643
643
  id: config.id,
644
644
  model: config.model,
645
- path: config.path
645
+ path: config.path,
646
+ tabName: config.tabName
646
647
  })
647
648
  });
648
649
  break;
@@ -654,7 +655,8 @@ const addInterfacePropertyFromField = (config) => {
654
655
  docs: buildFieldDocs({
655
656
  id: config.id,
656
657
  model: config.model,
657
- path: config.path
658
+ path: config.path,
659
+ tabName: config.tabName
658
660
  })
659
661
  });
660
662
  }
@@ -662,10 +664,11 @@ const addInterfacePropertyFromField = (config) => {
662
664
  };
663
665
  const addInterfacePropertiesForFields = (config) => {
664
666
  for (const name in config.fields) {
665
- addInterfacePropertyFromField({
667
+ addInterfacePropertyForField({
666
668
  ...config,
667
- id: name,
668
- model: config.fields[name]
669
+ id: name.includes("-") ? `"${name}"` : name,
670
+ model: config.fields[name],
671
+ tabName: config.tabName
669
672
  });
670
673
  }
671
674
  };
@@ -692,18 +695,22 @@ const addTypeAliasForCustomType = ({
692
695
  }
693
696
  ]
694
697
  });
695
- addInterfacePropertiesForFields({
696
- fields,
697
- interface: dataInterface,
698
- sourceFile,
699
- path: [
700
- {
701
- id: model.id,
702
- model
703
- }
704
- ],
705
- fieldConfigs
706
- });
698
+ for (const tabName in model.json) {
699
+ const { uid: _uidField, ...tabFields } = model.json[tabName];
700
+ addInterfacePropertiesForFields({
701
+ fields: tabFields,
702
+ interface: dataInterface,
703
+ sourceFile,
704
+ path: [
705
+ {
706
+ id: model.id,
707
+ model
708
+ }
709
+ ],
710
+ fieldConfigs,
711
+ tabName
712
+ });
713
+ }
707
714
  } else {
708
715
  dataInterface = sourceFile.addTypeAlias({
709
716
  name: pascalCase(`${model.id} Document Data`),
@@ -812,7 +819,8 @@ const addTypeAliasForSharedSlice = (config) => {
812
819
  writer.writeLine(`Item in ${humanReadablePath}`);
813
820
  }
814
821
  }
815
- ]
822
+ ],
823
+ isExported: true
816
824
  });
817
825
  addInterfacePropertiesForFields({
818
826
  interface: itemInterface,
@@ -846,7 +854,8 @@ const addTypeAliasForSharedSlice = (config) => {
846
854
  writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
847
855
  }
848
856
  }
849
- ]
857
+ ],
858
+ isExported: true
850
859
  });
851
860
  variationTypeNames.push(variationType.getName());
852
861
  }
@@ -924,6 +933,13 @@ const generateTypes = (config = {}) => {
924
933
  fieldConfigs: config.fieldConfigs || {}
925
934
  });
926
935
  }
936
+ if (config.customTypeModels.length > 0) {
937
+ sourceFile.addTypeAlias({
938
+ name: "AllDocumentTypes",
939
+ type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
940
+ isExported: true
941
+ });
942
+ }
927
943
  }
928
944
  if (config.sharedSliceModels) {
929
945
  for (const model of config.sharedSliceModels) {
@@ -934,6 +950,37 @@ const generateTypes = (config = {}) => {
934
950
  });
935
951
  }
936
952
  }
953
+ if (config.clientIntegration?.includeCreateClientInterface) {
954
+ sourceFile.addImportDeclaration({
955
+ moduleSpecifier: "@prismicio/client",
956
+ namespaceImport: "prismic",
957
+ isTypeOnly: true
958
+ });
959
+ const clientModuleDeclaration = sourceFile.addModule({
960
+ name: '"@prismicio/client"',
961
+ hasDeclareKeyword: true,
962
+ declarationKind: tsMorph.ModuleDeclarationKind.Module
963
+ });
964
+ clientModuleDeclaration.addInterface({
965
+ name: "CreateClient",
966
+ callSignatures: [
967
+ {
968
+ parameters: [
969
+ {
970
+ name: "repositoryNameOrEndpoint",
971
+ type: "string"
972
+ },
973
+ {
974
+ name: "options",
975
+ type: "prismic.ClientConfig",
976
+ hasQuestionToken: true
977
+ }
978
+ ],
979
+ returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
980
+ }
981
+ ]
982
+ });
983
+ }
937
984
  return getSourceFileText(sourceFile);
938
985
  };
939
986