prismic-ts-codegen 0.1.2 → 0.1.4

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
@@ -198,10 +198,18 @@ const pascalCase = (input) => pascalCase$1(input, {
198
198
  transform: pascalCaseTransformMerge
199
199
  });
200
200
 
201
- const buildSharedSliceInterfaceName = (config) => {
201
+ const buildSharedSliceInterfaceNamePart = (config) => {
202
202
  return pascalCase(`${config.id} Slice`);
203
203
  };
204
204
 
205
+ const buildTypeName = (...parts) => {
206
+ let name = pascalCase(parts.filter(Boolean).join(" "));
207
+ if (/^[0-9]/.test(name) ? "_" : "") {
208
+ name = `_${name}`;
209
+ }
210
+ return name;
211
+ };
212
+
205
213
  const addInterfacePropertyForField = (config) => {
206
214
  switch (config.model.type) {
207
215
  case "UID": {
@@ -251,12 +259,16 @@ const addInterfacePropertyForField = (config) => {
251
259
  if (config.fieldConfigs.embed?.providerTypes) {
252
260
  for (const providerType in config.fieldConfigs.embed?.providerTypes) {
253
261
  const configuredProviderType = config.fieldConfigs.embed?.providerTypes[providerType];
254
- providerTypes.push(`({ provider_name: "${providerType}" } & ${configuredProviderType})`);
262
+ providerTypes.push(
263
+ `({ provider_name: "${providerType}" } & ${configuredProviderType})`
264
+ );
255
265
  }
256
266
  }
257
267
  config.interface.addProperty({
258
268
  name: config.id,
259
- type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(" | ")})>` : "prismicT.EmbedField",
269
+ type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(
270
+ " | "
271
+ )})>` : "prismicT.EmbedField",
260
272
  docs: buildFieldDocs({
261
273
  id: config.id,
262
274
  model: config.model,
@@ -460,7 +472,13 @@ const addInterfacePropertyForField = (config) => {
460
472
  }
461
473
  case "Group": {
462
474
  const itemInterface = config.sourceFile.addInterface({
463
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Item`),
475
+ name: buildTypeName(
476
+ config.path[0].id,
477
+ "Document",
478
+ "Data",
479
+ config.id,
480
+ "Item"
481
+ ),
464
482
  docs: [
465
483
  {
466
484
  description: (writer) => {
@@ -512,12 +530,22 @@ const addInterfacePropertyForField = (config) => {
512
530
  for (const choiceId in config.model.config.choices) {
513
531
  const choice = config.model.config.choices[choiceId];
514
532
  if (choice.type === CustomTypeModelSliceType.SharedSlice) {
515
- choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
533
+ choiceInterfaceNames.push(
534
+ buildSharedSliceInterfaceNamePart({ id: choiceId })
535
+ );
516
536
  } else if (choice.type === CustomTypeModelSliceType.Slice) {
517
537
  let primaryInterface;
518
538
  if (choice["non-repeat"] && Object.keys(choice["non-repeat"]).length > 0) {
519
539
  primaryInterface = config.sourceFile.addInterface({
520
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Primary`),
540
+ name: buildTypeName(
541
+ config.path[0].id,
542
+ "Document",
543
+ "Data",
544
+ config.id,
545
+ choiceId,
546
+ "Slice",
547
+ "Primary"
548
+ ),
521
549
  docs: [
522
550
  {
523
551
  description: (writer) => {
@@ -538,7 +566,9 @@ const addInterfacePropertyForField = (config) => {
538
566
  }
539
567
  ]
540
568
  });
541
- writer.writeLine(`Primary content in ${humanReadablePath}`);
569
+ writer.writeLine(
570
+ `Primary content in ${humanReadablePath}`
571
+ );
542
572
  }
543
573
  }
544
574
  ]
@@ -568,7 +598,15 @@ const addInterfacePropertyForField = (config) => {
568
598
  let itemInterface;
569
599
  if (choice.repeat && Object.keys(choice.repeat).length > 0) {
570
600
  itemInterface = config.sourceFile.addInterface({
571
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Item`),
601
+ name: buildTypeName(
602
+ config.path[0].id,
603
+ "Document",
604
+ "Data",
605
+ config.id,
606
+ choiceId,
607
+ "Slice",
608
+ "Item"
609
+ ),
572
610
  docs: [
573
611
  {
574
612
  description: (writer) => {
@@ -618,7 +656,14 @@ const addInterfacePropertyForField = (config) => {
618
656
  });
619
657
  }
620
658
  const sliceType = config.sourceFile.addTypeAlias({
621
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
659
+ name: buildTypeName(
660
+ config.path[0].id,
661
+ "Document",
662
+ "Data",
663
+ config.id,
664
+ choiceId,
665
+ "Slice"
666
+ ),
622
667
  type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
623
668
  isExported: true
624
669
  });
@@ -627,7 +672,13 @@ const addInterfacePropertyForField = (config) => {
627
672
  }
628
673
  }
629
674
  const slicesType = config.sourceFile.addTypeAlias({
630
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Slice`),
675
+ name: buildTypeName(
676
+ config.path[0].id,
677
+ "Document",
678
+ "Data",
679
+ config.id,
680
+ "Slice"
681
+ ),
631
682
  type: choiceInterfaceNames.length > 0 ? choiceInterfaceNames.join(" | ") : "never",
632
683
  docs: [
633
684
  {
@@ -698,7 +749,7 @@ const addTypeAliasForCustomType = ({
698
749
  let dataInterface;
699
750
  if (hasDataFields) {
700
751
  dataInterface = sourceFile.addInterface({
701
- name: pascalCase(`${model.id} Document Data`),
752
+ name: buildTypeName(model.id, "Document", "Data"),
702
753
  docs: [
703
754
  {
704
755
  description: `Content for ${model.label} documents`
@@ -723,7 +774,7 @@ const addTypeAliasForCustomType = ({
723
774
  }
724
775
  } else {
725
776
  dataInterface = sourceFile.addTypeAlias({
726
- name: pascalCase(`${model.id} Document Data`),
777
+ name: buildTypeName(model.id, "Document", "Data"),
727
778
  type: `Record<string, never>`,
728
779
  docs: [
729
780
  {
@@ -733,7 +784,7 @@ const addTypeAliasForCustomType = ({
733
784
  });
734
785
  }
735
786
  return sourceFile.addTypeAlias({
736
- name: pascalCase(`${model.id} Document`),
787
+ name: buildTypeName(model.id, "Document"),
737
788
  typeParameters: [
738
789
  {
739
790
  name: "Lang",
@@ -749,7 +800,9 @@ const addTypeAliasForCustomType = ({
749
800
  writer.blankLine();
750
801
  writer.writeLine(`- **API ID**: \`${model.id}\``);
751
802
  writer.writeLine(`- **Repeatable**: \`${model.repeatable}\``);
752
- writer.writeLine(`- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`);
803
+ writer.writeLine(
804
+ `- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`
805
+ );
753
806
  },
754
807
  tags: [
755
808
  {
@@ -769,7 +822,11 @@ const addTypeAliasForSharedSlice = (config) => {
769
822
  let primaryInterface;
770
823
  if (variation.primary && Object.keys(variation.primary).length > 0) {
771
824
  primaryInterface = config.sourceFile.addInterface({
772
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Primary`),
825
+ name: buildTypeName(
826
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
827
+ variation.id,
828
+ "Primary"
829
+ ),
773
830
  docs: [
774
831
  {
775
832
  description: (writer) => {
@@ -810,7 +867,11 @@ const addTypeAliasForSharedSlice = (config) => {
810
867
  let itemInterface;
811
868
  if (variation.items && Object.keys(variation.items).length > 0) {
812
869
  itemInterface = config.sourceFile.addInterface({
813
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Item`),
870
+ name: buildTypeName(
871
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
872
+ variation.id,
873
+ "Item"
874
+ ),
814
875
  docs: [
815
876
  {
816
877
  description: (writer) => {
@@ -850,18 +911,23 @@ const addTypeAliasForSharedSlice = (config) => {
850
911
  });
851
912
  }
852
913
  const variationType = config.sourceFile.addTypeAlias({
853
- name: pascalCase(`${buildSharedSliceInterfaceName({
854
- id: config.model.id
855
- })} ${variation.id}`),
914
+ name: buildTypeName(
915
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
916
+ variation.id
917
+ ),
856
918
  type: `prismicT.SharedSliceVariation<"${variation.id}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
857
919
  docs: [
858
920
  {
859
921
  description: (writer) => {
860
- writer.writeLine(`${variation.name} variation for ${config.model.name} Slice`);
922
+ writer.writeLine(
923
+ `${variation.name} variation for ${config.model.name} Slice`
924
+ );
861
925
  writer.blankLine();
862
926
  writer.writeLine(`- **API ID**: \`${variation.id}\``);
863
927
  writer.writeLine(`- **Description**: \`${variation.description}\``);
864
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
928
+ writer.writeLine(
929
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
930
+ );
865
931
  }
866
932
  }
867
933
  ],
@@ -870,7 +936,7 @@ const addTypeAliasForSharedSlice = (config) => {
870
936
  variationTypeNames.push(variationType.getName());
871
937
  }
872
938
  const variationsType = config.sourceFile.addTypeAlias({
873
- name: pascalCase(`${config.model.id} Slice Variation`),
939
+ name: buildTypeName(config.model.id, "Slice", "Variation"),
874
940
  type: variationTypeNames.length > 0 ? variationTypeNames.join(" | ") : "never",
875
941
  docs: [
876
942
  {
@@ -889,9 +955,9 @@ const addTypeAliasForSharedSlice = (config) => {
889
955
  ]
890
956
  });
891
957
  return config.sourceFile.addTypeAlias({
892
- name: pascalCase(buildSharedSliceInterfaceName({
893
- id: config.model.id
894
- })),
958
+ name: buildTypeName(
959
+ buildSharedSliceInterfaceNamePart({ id: config.model.id })
960
+ ),
895
961
  type: `prismicT.SharedSlice<"${config.model.id}", ${variationsType.getName()}>`,
896
962
  docs: [
897
963
  {
@@ -899,8 +965,12 @@ const addTypeAliasForSharedSlice = (config) => {
899
965
  writer.writeLine(`${config.model.name} Shared Slice`);
900
966
  writer.blankLine();
901
967
  writer.writeLine(`- **API ID**: \`${config.model.id}\``);
902
- writer.writeLine(`- **Description**: \`${config.model.description}\``);
903
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
968
+ writer.writeLine(
969
+ `- **Description**: \`${config.model.description}\``
970
+ );
971
+ writer.writeLine(
972
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
973
+ );
904
974
  }
905
975
  }
906
976
  ],
@@ -946,7 +1016,9 @@ const generateTypes = (config = {}) => {
946
1016
  if (config.customTypeModels.length > 0) {
947
1017
  sourceFile.addTypeAlias({
948
1018
  name: "AllDocumentTypes",
949
- type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
1019
+ type: config.customTypeModels.map(
1020
+ (customTypeModel) => buildTypeName(customTypeModel.id, "Document")
1021
+ ).join(" | "),
950
1022
  isExported: true
951
1023
  });
952
1024
  }