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.js CHANGED
@@ -1,5 +1,6 @@
1
- import { Project } from 'ts-morph';
1
+ import { Project, ModuleDeclarationKind } from 'ts-morph';
2
2
  import { stripIndent } from 'common-tags';
3
+ import { CustomTypeModelFieldType, CustomTypeModelLinkSelectType, CustomTypeModelSliceType } from '@prismicio/types';
3
4
  import { pascalCase as pascalCase$1, pascalCaseTransformMerge } from 'pascal-case';
4
5
 
5
6
  const BLANK_LINE_IDENTIFIER = "// ___BLANK_LINE_TO_BE_REPLACED___";
@@ -28,33 +29,6 @@ const FIELD_DOCUMENTATION_URLS = {
28
29
  Slices: "https://prismic.io/docs/core-concepts/slices"
29
30
  };
30
31
 
31
- const CustomTypeModelFieldType = {
32
- Boolean: "Boolean",
33
- Color: "Color",
34
- Date: "Date",
35
- Embed: "Embed",
36
- GeoPoint: "GeoPoint",
37
- Group: "Group",
38
- Image: "Image",
39
- IntegrationFields: "IntegrationFields",
40
- Link: "Link",
41
- Number: "Number",
42
- Select: "Select",
43
- Slices: "Slices",
44
- StructuredText: "StructuredText",
45
- Text: "Text",
46
- Timestamp: "Timestamp",
47
- UID: "UID"
48
- };
49
- const CustomTypeModelLinkSelectType = {
50
- Document: "document",
51
- Media: "media"
52
- };
53
- const CustomTypeModelSliceType = {
54
- Slice: "Slice",
55
- SharedSlice: "SharedSlice"
56
- };
57
-
58
32
  const isCustomTypeModelField = (model) => {
59
33
  return typeof model === "object" && model !== null && "type" in model && "config" in model;
60
34
  };
@@ -169,6 +143,9 @@ const writeFieldDocsDescription = (config) => {
169
143
  if (config.apiIDPath) {
170
144
  config.writer.writeLine(`- **API ID Path**: ${config.apiIDPath}`);
171
145
  }
146
+ if (config.tabName) {
147
+ config.writer.writeLine(`- **Tab**: ${config.tabName}`);
148
+ }
172
149
  if (config.documentationURL) {
173
150
  config.writer.writeLine(`- **Documentation**: ${config.documentationURL}`);
174
151
  }
@@ -200,6 +177,7 @@ const buildFieldDocs = (config) => {
200
177
  placeholder,
201
178
  defaultValue,
202
179
  apiIDPath,
180
+ tabName: config.tabName,
203
181
  documentationURL: getFieldDocumentationURL({ model: config.model })
204
182
  });
205
183
  }
@@ -215,7 +193,7 @@ const buildSharedSliceInterfaceName = (config) => {
215
193
  return pascalCase(`${config.id} Slice`);
216
194
  };
217
195
 
218
- const addInterfacePropertyFromField = (config) => {
196
+ const addInterfacePropertyForField = (config) => {
219
197
  switch (config.model.type) {
220
198
  case "UID": {
221
199
  break;
@@ -227,7 +205,8 @@ const addInterfacePropertyFromField = (config) => {
227
205
  docs: buildFieldDocs({
228
206
  id: config.id,
229
207
  model: config.model,
230
- path: config.path
208
+ path: config.path,
209
+ tabName: config.tabName
231
210
  })
232
211
  });
233
212
  break;
@@ -239,7 +218,8 @@ const addInterfacePropertyFromField = (config) => {
239
218
  docs: buildFieldDocs({
240
219
  id: config.id,
241
220
  model: config.model,
242
- path: config.path
221
+ path: config.path,
222
+ tabName: config.tabName
243
223
  })
244
224
  });
245
225
  break;
@@ -251,7 +231,8 @@ const addInterfacePropertyFromField = (config) => {
251
231
  docs: buildFieldDocs({
252
232
  id: config.id,
253
233
  model: config.model,
254
- path: config.path
234
+ path: config.path,
235
+ tabName: config.tabName
255
236
  })
256
237
  });
257
238
  break;
@@ -270,7 +251,8 @@ const addInterfacePropertyFromField = (config) => {
270
251
  docs: buildFieldDocs({
271
252
  id: config.id,
272
253
  model: config.model,
273
- path: config.path
254
+ path: config.path,
255
+ tabName: config.tabName
274
256
  })
275
257
  });
276
258
  break;
@@ -282,7 +264,8 @@ const addInterfacePropertyFromField = (config) => {
282
264
  docs: buildFieldDocs({
283
265
  id: config.id,
284
266
  model: config.model,
285
- path: config.path
267
+ path: config.path,
268
+ tabName: config.tabName
286
269
  })
287
270
  });
288
271
  break;
@@ -296,7 +279,8 @@ const addInterfacePropertyFromField = (config) => {
296
279
  docs: buildFieldDocs({
297
280
  id: config.id,
298
281
  model: config.model,
299
- path: config.path
282
+ path: config.path,
283
+ tabName: config.tabName
300
284
  })
301
285
  });
302
286
  } else {
@@ -306,7 +290,8 @@ const addInterfacePropertyFromField = (config) => {
306
290
  docs: buildFieldDocs({
307
291
  id: config.id,
308
292
  model: config.model,
309
- path: config.path
293
+ path: config.path,
294
+ tabName: config.tabName
310
295
  })
311
296
  });
312
297
  }
@@ -320,7 +305,8 @@ const addInterfacePropertyFromField = (config) => {
320
305
  docs: buildFieldDocs({
321
306
  id: config.id,
322
307
  model: config.model,
323
- path: config.path
308
+ path: config.path,
309
+ tabName: config.tabName
324
310
  })
325
311
  });
326
312
  break;
@@ -334,7 +320,8 @@ const addInterfacePropertyFromField = (config) => {
334
320
  docs: buildFieldDocs({
335
321
  id: config.id,
336
322
  model: config.model,
337
- path: config.path
323
+ path: config.path,
324
+ tabName: config.tabName
338
325
  })
339
326
  });
340
327
  break;
@@ -346,7 +333,8 @@ const addInterfacePropertyFromField = (config) => {
346
333
  docs: buildFieldDocs({
347
334
  id: config.id,
348
335
  model: config.model,
349
- path: config.path
336
+ path: config.path,
337
+ tabName: config.tabName
350
338
  })
351
339
  });
352
340
  break;
@@ -358,7 +346,8 @@ const addInterfacePropertyFromField = (config) => {
358
346
  docs: buildFieldDocs({
359
347
  id: config.id,
360
348
  model: config.model,
361
- path: config.path
349
+ path: config.path,
350
+ tabName: config.tabName
362
351
  })
363
352
  });
364
353
  }
@@ -372,7 +361,8 @@ const addInterfacePropertyFromField = (config) => {
372
361
  docs: buildFieldDocs({
373
362
  id: config.id,
374
363
  model: config.model,
375
- path: config.path
364
+ path: config.path,
365
+ tabName: config.tabName
376
366
  })
377
367
  });
378
368
  break;
@@ -386,7 +376,8 @@ const addInterfacePropertyFromField = (config) => {
386
376
  docs: buildFieldDocs({
387
377
  id: config.id,
388
378
  model: config.model,
389
- path: config.path
379
+ path: config.path,
380
+ tabName: config.tabName
390
381
  })
391
382
  });
392
383
  } else {
@@ -396,7 +387,8 @@ const addInterfacePropertyFromField = (config) => {
396
387
  docs: buildFieldDocs({
397
388
  id: config.id,
398
389
  model: config.model,
399
- path: config.path
390
+ path: config.path,
391
+ tabName: config.tabName
400
392
  })
401
393
  });
402
394
  }
@@ -412,7 +404,8 @@ const addInterfacePropertyFromField = (config) => {
412
404
  docs: buildFieldDocs({
413
405
  id: config.id,
414
406
  model: config.model,
415
- path: config.path
407
+ path: config.path,
408
+ tabName: config.tabName
416
409
  })
417
410
  });
418
411
  } else {
@@ -422,7 +415,8 @@ const addInterfacePropertyFromField = (config) => {
422
415
  docs: buildFieldDocs({
423
416
  id: config.id,
424
417
  model: config.model,
425
- path: config.path
418
+ path: config.path,
419
+ tabName: config.tabName
426
420
  })
427
421
  });
428
422
  }
@@ -435,7 +429,8 @@ const addInterfacePropertyFromField = (config) => {
435
429
  docs: buildFieldDocs({
436
430
  id: config.id,
437
431
  model: config.model,
438
- path: config.path
432
+ path: config.path,
433
+ tabName: config.tabName
439
434
  })
440
435
  });
441
436
  break;
@@ -447,7 +442,8 @@ const addInterfacePropertyFromField = (config) => {
447
442
  docs: buildFieldDocs({
448
443
  id: config.id,
449
444
  model: config.model,
450
- path: config.path
445
+ path: config.path,
446
+ tabName: config.tabName
451
447
  })
452
448
  });
453
449
  break;
@@ -470,7 +466,8 @@ const addInterfacePropertyFromField = (config) => {
470
466
  writer.writeLine(`Item in ${humanReadablePath}`);
471
467
  }
472
468
  }
473
- ]
469
+ ],
470
+ isExported: true
474
471
  });
475
472
  addInterfacePropertiesForFields({
476
473
  interface: itemInterface,
@@ -491,7 +488,8 @@ const addInterfacePropertyFromField = (config) => {
491
488
  docs: buildFieldDocs({
492
489
  id: config.id,
493
490
  model: config.model,
494
- path: config.path
491
+ path: config.path,
492
+ tabName: config.tabName
495
493
  })
496
494
  });
497
495
  break;
@@ -581,7 +579,8 @@ const addInterfacePropertyFromField = (config) => {
581
579
  writer.writeLine(`Item in ${humanReadablePath}`);
582
580
  }
583
581
  }
584
- ]
582
+ ],
583
+ isExported: true
585
584
  });
586
585
  addInterfacePropertiesForFields({
587
586
  interface: itemInterface,
@@ -607,7 +606,8 @@ const addInterfacePropertyFromField = (config) => {
607
606
  }
608
607
  const sliceType = config.sourceFile.addTypeAlias({
609
608
  name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
610
- type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`
609
+ type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
610
+ isExported: true
611
611
  });
612
612
  choiceInterfaceNames.push(sliceType.getName());
613
613
  }
@@ -638,7 +638,8 @@ const addInterfacePropertyFromField = (config) => {
638
638
  docs: buildFieldDocs({
639
639
  id: config.id,
640
640
  model: config.model,
641
- path: config.path
641
+ path: config.path,
642
+ tabName: config.tabName
642
643
  })
643
644
  });
644
645
  break;
@@ -650,7 +651,8 @@ const addInterfacePropertyFromField = (config) => {
650
651
  docs: buildFieldDocs({
651
652
  id: config.id,
652
653
  model: config.model,
653
- path: config.path
654
+ path: config.path,
655
+ tabName: config.tabName
654
656
  })
655
657
  });
656
658
  }
@@ -658,10 +660,11 @@ const addInterfacePropertyFromField = (config) => {
658
660
  };
659
661
  const addInterfacePropertiesForFields = (config) => {
660
662
  for (const name in config.fields) {
661
- addInterfacePropertyFromField({
663
+ addInterfacePropertyForField({
662
664
  ...config,
663
- id: name,
664
- model: config.fields[name]
665
+ id: name.includes("-") ? `"${name}"` : name,
666
+ model: config.fields[name],
667
+ tabName: config.tabName
665
668
  });
666
669
  }
667
670
  };
@@ -688,18 +691,22 @@ const addTypeAliasForCustomType = ({
688
691
  }
689
692
  ]
690
693
  });
691
- addInterfacePropertiesForFields({
692
- fields,
693
- interface: dataInterface,
694
- sourceFile,
695
- path: [
696
- {
697
- id: model.id,
698
- model
699
- }
700
- ],
701
- fieldConfigs
702
- });
694
+ for (const tabName in model.json) {
695
+ const { uid: _uidField, ...tabFields } = model.json[tabName];
696
+ addInterfacePropertiesForFields({
697
+ fields: tabFields,
698
+ interface: dataInterface,
699
+ sourceFile,
700
+ path: [
701
+ {
702
+ id: model.id,
703
+ model
704
+ }
705
+ ],
706
+ fieldConfigs,
707
+ tabName
708
+ });
709
+ }
703
710
  } else {
704
711
  dataInterface = sourceFile.addTypeAlias({
705
712
  name: pascalCase(`${model.id} Document Data`),
@@ -808,7 +815,8 @@ const addTypeAliasForSharedSlice = (config) => {
808
815
  writer.writeLine(`Item in ${humanReadablePath}`);
809
816
  }
810
817
  }
811
- ]
818
+ ],
819
+ isExported: true
812
820
  });
813
821
  addInterfacePropertiesForFields({
814
822
  interface: itemInterface,
@@ -842,7 +850,8 @@ const addTypeAliasForSharedSlice = (config) => {
842
850
  writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
843
851
  }
844
852
  }
845
- ]
853
+ ],
854
+ isExported: true
846
855
  });
847
856
  variationTypeNames.push(variationType.getName());
848
857
  }
@@ -920,6 +929,13 @@ const generateTypes = (config = {}) => {
920
929
  fieldConfigs: config.fieldConfigs || {}
921
930
  });
922
931
  }
932
+ if (config.customTypeModels.length > 0) {
933
+ sourceFile.addTypeAlias({
934
+ name: "AllDocumentTypes",
935
+ type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
936
+ isExported: true
937
+ });
938
+ }
923
939
  }
924
940
  if (config.sharedSliceModels) {
925
941
  for (const model of config.sharedSliceModels) {
@@ -930,6 +946,37 @@ const generateTypes = (config = {}) => {
930
946
  });
931
947
  }
932
948
  }
949
+ if (config.clientIntegration?.includeCreateClientInterface) {
950
+ sourceFile.addImportDeclaration({
951
+ moduleSpecifier: "@prismicio/client",
952
+ namespaceImport: "prismic",
953
+ isTypeOnly: true
954
+ });
955
+ const clientModuleDeclaration = sourceFile.addModule({
956
+ name: '"@prismicio/client"',
957
+ hasDeclareKeyword: true,
958
+ declarationKind: ModuleDeclarationKind.Module
959
+ });
960
+ clientModuleDeclaration.addInterface({
961
+ name: "CreateClient",
962
+ callSignatures: [
963
+ {
964
+ parameters: [
965
+ {
966
+ name: "repositoryNameOrEndpoint",
967
+ type: "string"
968
+ },
969
+ {
970
+ name: "options",
971
+ type: "prismic.ClientConfig",
972
+ hasQuestionToken: true
973
+ }
974
+ ],
975
+ returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
976
+ }
977
+ ]
978
+ });
979
+ }
933
980
  return getSourceFileText(sourceFile);
934
981
  };
935
982