prismic-ts-codegen 0.1.3-alpha.0 → 0.1.3

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
@@ -202,10 +202,18 @@ const pascalCase = (input) => pascalCase$1.pascalCase(input, {
202
202
  transform: pascalCase$1.pascalCaseTransformMerge
203
203
  });
204
204
 
205
- const buildSharedSliceInterfaceName = (config) => {
205
+ const buildSharedSliceInterfaceNamePart = (config) => {
206
206
  return pascalCase(`${config.id} Slice`);
207
207
  };
208
208
 
209
+ const buildTypeName = (...parts) => {
210
+ let name = pascalCase(parts.filter(Boolean).join(" "));
211
+ if (/^[0-9]/.test(name) ? "_" : "") {
212
+ name = `_${name}`;
213
+ }
214
+ return name;
215
+ };
216
+
209
217
  const addInterfacePropertyForField = (config) => {
210
218
  switch (config.model.type) {
211
219
  case "UID": {
@@ -255,12 +263,16 @@ const addInterfacePropertyForField = (config) => {
255
263
  if (config.fieldConfigs.embed?.providerTypes) {
256
264
  for (const providerType in config.fieldConfigs.embed?.providerTypes) {
257
265
  const configuredProviderType = config.fieldConfigs.embed?.providerTypes[providerType];
258
- providerTypes.push(`({ provider_name: "${providerType}" } & ${configuredProviderType})`);
266
+ providerTypes.push(
267
+ `({ provider_name: "${providerType}" } & ${configuredProviderType})`
268
+ );
259
269
  }
260
270
  }
261
271
  config.interface.addProperty({
262
272
  name: config.id,
263
- type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(" | ")})>` : "prismicT.EmbedField",
273
+ type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(
274
+ " | "
275
+ )})>` : "prismicT.EmbedField",
264
276
  docs: buildFieldDocs({
265
277
  id: config.id,
266
278
  model: config.model,
@@ -464,7 +476,13 @@ const addInterfacePropertyForField = (config) => {
464
476
  }
465
477
  case "Group": {
466
478
  const itemInterface = config.sourceFile.addInterface({
467
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Item`),
479
+ name: buildTypeName(
480
+ config.path[0].id,
481
+ "Document",
482
+ "Data",
483
+ config.id,
484
+ "Item"
485
+ ),
468
486
  docs: [
469
487
  {
470
488
  description: (writer) => {
@@ -516,12 +534,22 @@ const addInterfacePropertyForField = (config) => {
516
534
  for (const choiceId in config.model.config.choices) {
517
535
  const choice = config.model.config.choices[choiceId];
518
536
  if (choice.type === types.CustomTypeModelSliceType.SharedSlice) {
519
- choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
537
+ choiceInterfaceNames.push(
538
+ buildSharedSliceInterfaceNamePart({ id: choiceId })
539
+ );
520
540
  } else if (choice.type === types.CustomTypeModelSliceType.Slice) {
521
541
  let primaryInterface;
522
542
  if (choice["non-repeat"] && Object.keys(choice["non-repeat"]).length > 0) {
523
543
  primaryInterface = config.sourceFile.addInterface({
524
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Primary`),
544
+ name: buildTypeName(
545
+ config.path[0].id,
546
+ "Document",
547
+ "Data",
548
+ config.id,
549
+ choiceId,
550
+ "Slice",
551
+ "Primary"
552
+ ),
525
553
  docs: [
526
554
  {
527
555
  description: (writer) => {
@@ -542,7 +570,9 @@ const addInterfacePropertyForField = (config) => {
542
570
  }
543
571
  ]
544
572
  });
545
- writer.writeLine(`Primary content in ${humanReadablePath}`);
573
+ writer.writeLine(
574
+ `Primary content in ${humanReadablePath}`
575
+ );
546
576
  }
547
577
  }
548
578
  ]
@@ -572,7 +602,15 @@ const addInterfacePropertyForField = (config) => {
572
602
  let itemInterface;
573
603
  if (choice.repeat && Object.keys(choice.repeat).length > 0) {
574
604
  itemInterface = config.sourceFile.addInterface({
575
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Item`),
605
+ name: buildTypeName(
606
+ config.path[0].id,
607
+ "Document",
608
+ "Data",
609
+ config.id,
610
+ choiceId,
611
+ "Slice",
612
+ "Item"
613
+ ),
576
614
  docs: [
577
615
  {
578
616
  description: (writer) => {
@@ -622,7 +660,14 @@ const addInterfacePropertyForField = (config) => {
622
660
  });
623
661
  }
624
662
  const sliceType = config.sourceFile.addTypeAlias({
625
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
663
+ name: buildTypeName(
664
+ config.path[0].id,
665
+ "Document",
666
+ "Data",
667
+ config.id,
668
+ choiceId,
669
+ "Slice"
670
+ ),
626
671
  type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
627
672
  isExported: true
628
673
  });
@@ -631,7 +676,13 @@ const addInterfacePropertyForField = (config) => {
631
676
  }
632
677
  }
633
678
  const slicesType = config.sourceFile.addTypeAlias({
634
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Slice`),
679
+ name: buildTypeName(
680
+ config.path[0].id,
681
+ "Document",
682
+ "Data",
683
+ config.id,
684
+ "Slice"
685
+ ),
635
686
  type: choiceInterfaceNames.length > 0 ? choiceInterfaceNames.join(" | ") : "never",
636
687
  docs: [
637
688
  {
@@ -702,7 +753,7 @@ const addTypeAliasForCustomType = ({
702
753
  let dataInterface;
703
754
  if (hasDataFields) {
704
755
  dataInterface = sourceFile.addInterface({
705
- name: pascalCase(`${model.id} Document Data`),
756
+ name: buildTypeName(model.id, "Document", "Data"),
706
757
  docs: [
707
758
  {
708
759
  description: `Content for ${model.label} documents`
@@ -727,7 +778,7 @@ const addTypeAliasForCustomType = ({
727
778
  }
728
779
  } else {
729
780
  dataInterface = sourceFile.addTypeAlias({
730
- name: pascalCase(`${model.id} Document Data`),
781
+ name: buildTypeName(model.id, "Document", "Data"),
731
782
  type: `Record<string, never>`,
732
783
  docs: [
733
784
  {
@@ -737,7 +788,7 @@ const addTypeAliasForCustomType = ({
737
788
  });
738
789
  }
739
790
  return sourceFile.addTypeAlias({
740
- name: pascalCase(`${model.id} Document`),
791
+ name: buildTypeName(model.id, "Document"),
741
792
  typeParameters: [
742
793
  {
743
794
  name: "Lang",
@@ -753,7 +804,9 @@ const addTypeAliasForCustomType = ({
753
804
  writer.blankLine();
754
805
  writer.writeLine(`- **API ID**: \`${model.id}\``);
755
806
  writer.writeLine(`- **Repeatable**: \`${model.repeatable}\``);
756
- writer.writeLine(`- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`);
807
+ writer.writeLine(
808
+ `- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`
809
+ );
757
810
  },
758
811
  tags: [
759
812
  {
@@ -773,7 +826,11 @@ const addTypeAliasForSharedSlice = (config) => {
773
826
  let primaryInterface;
774
827
  if (variation.primary && Object.keys(variation.primary).length > 0) {
775
828
  primaryInterface = config.sourceFile.addInterface({
776
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Primary`),
829
+ name: buildTypeName(
830
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
831
+ variation.id,
832
+ "Primary"
833
+ ),
777
834
  docs: [
778
835
  {
779
836
  description: (writer) => {
@@ -814,7 +871,11 @@ const addTypeAliasForSharedSlice = (config) => {
814
871
  let itemInterface;
815
872
  if (variation.items && Object.keys(variation.items).length > 0) {
816
873
  itemInterface = config.sourceFile.addInterface({
817
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Item`),
874
+ name: buildTypeName(
875
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
876
+ variation.id,
877
+ "Item"
878
+ ),
818
879
  docs: [
819
880
  {
820
881
  description: (writer) => {
@@ -854,18 +915,23 @@ const addTypeAliasForSharedSlice = (config) => {
854
915
  });
855
916
  }
856
917
  const variationType = config.sourceFile.addTypeAlias({
857
- name: pascalCase(`${buildSharedSliceInterfaceName({
858
- id: config.model.id
859
- })} ${variation.id}`),
918
+ name: buildTypeName(
919
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
920
+ variation.id
921
+ ),
860
922
  type: `prismicT.SharedSliceVariation<"${variation.id}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
861
923
  docs: [
862
924
  {
863
925
  description: (writer) => {
864
- writer.writeLine(`${variation.name} variation for ${config.model.name} Slice`);
926
+ writer.writeLine(
927
+ `${variation.name} variation for ${config.model.name} Slice`
928
+ );
865
929
  writer.blankLine();
866
930
  writer.writeLine(`- **API ID**: \`${variation.id}\``);
867
931
  writer.writeLine(`- **Description**: \`${variation.description}\``);
868
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
932
+ writer.writeLine(
933
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
934
+ );
869
935
  }
870
936
  }
871
937
  ],
@@ -874,7 +940,7 @@ const addTypeAliasForSharedSlice = (config) => {
874
940
  variationTypeNames.push(variationType.getName());
875
941
  }
876
942
  const variationsType = config.sourceFile.addTypeAlias({
877
- name: pascalCase(`${config.model.id} Slice Variation`),
943
+ name: buildTypeName(config.model.id, "Slice", "Variation"),
878
944
  type: variationTypeNames.length > 0 ? variationTypeNames.join(" | ") : "never",
879
945
  docs: [
880
946
  {
@@ -893,9 +959,9 @@ const addTypeAliasForSharedSlice = (config) => {
893
959
  ]
894
960
  });
895
961
  return config.sourceFile.addTypeAlias({
896
- name: pascalCase(buildSharedSliceInterfaceName({
897
- id: config.model.id
898
- })),
962
+ name: buildTypeName(
963
+ buildSharedSliceInterfaceNamePart({ id: config.model.id })
964
+ ),
899
965
  type: `prismicT.SharedSlice<"${config.model.id}", ${variationsType.getName()}>`,
900
966
  docs: [
901
967
  {
@@ -903,8 +969,12 @@ const addTypeAliasForSharedSlice = (config) => {
903
969
  writer.writeLine(`${config.model.name} Shared Slice`);
904
970
  writer.blankLine();
905
971
  writer.writeLine(`- **API ID**: \`${config.model.id}\``);
906
- writer.writeLine(`- **Description**: \`${config.model.description}\``);
907
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
972
+ writer.writeLine(
973
+ `- **Description**: \`${config.model.description}\``
974
+ );
975
+ writer.writeLine(
976
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
977
+ );
908
978
  }
909
979
  }
910
980
  ],
@@ -929,7 +999,7 @@ const generateTypes = (config = {}) => {
929
999
  leadingTrivia: NON_EDITABLE_FILE_HEADER
930
1000
  });
931
1001
  sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
932
- const simplifyTypeAlias = sourceFile.addTypeAlias({
1002
+ sourceFile.addTypeAlias({
933
1003
  name: "Simplify",
934
1004
  typeParameters: [
935
1005
  {
@@ -950,7 +1020,9 @@ const generateTypes = (config = {}) => {
950
1020
  if (config.customTypeModels.length > 0) {
951
1021
  sourceFile.addTypeAlias({
952
1022
  name: "AllDocumentTypes",
953
- type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
1023
+ type: config.customTypeModels.map(
1024
+ (customTypeModel) => buildTypeName(customTypeModel.id, "Document")
1025
+ ).join(" | "),
954
1026
  isExported: true
955
1027
  });
956
1028
  }
@@ -964,7 +1036,7 @@ const generateTypes = (config = {}) => {
964
1036
  });
965
1037
  }
966
1038
  }
967
- if (config.clientIntegration?.includeCreateClientInterface || config.clientIntegration?.includeContentNamespace) {
1039
+ if (config.clientIntegration?.includeCreateClientInterface) {
968
1040
  sourceFile.addImportDeclaration({
969
1041
  moduleSpecifier: "@prismicio/client",
970
1042
  namespaceImport: "prismic",
@@ -975,44 +1047,25 @@ const generateTypes = (config = {}) => {
975
1047
  hasDeclareKeyword: true,
976
1048
  declarationKind: tsMorph.ModuleDeclarationKind.Module
977
1049
  });
978
- if (config.clientIntegration.includeCreateClientInterface) {
979
- clientModuleDeclaration.addInterface({
980
- name: "CreateClient",
981
- callSignatures: [
982
- {
983
- parameters: [
984
- {
985
- name: "repositoryNameOrEndpoint",
986
- type: "string"
987
- },
988
- {
989
- name: "options",
990
- type: "prismic.ClientConfig",
991
- hasQuestionToken: true
992
- }
993
- ],
994
- returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
995
- }
996
- ]
997
- });
998
- }
999
- if (config.clientIntegration.includeContentNamespace) {
1000
- const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
1001
- name: "Content",
1002
- declarationKind: tsMorph.ModuleDeclarationKind.Namespace
1003
- });
1004
- const exportSymbols = sourceFile.getExportSymbols().filter((exportSymbol) => {
1005
- return exportSymbol.getName() !== simplifyTypeAlias.getName();
1006
- });
1007
- contentNamespaceDeclaration.addExportDeclaration({
1008
- isTypeOnly: true,
1009
- namedExports: exportSymbols.map((exportSymbol) => {
1010
- return {
1011
- name: exportSymbol.getName()
1012
- };
1013
- })
1014
- });
1015
- }
1050
+ clientModuleDeclaration.addInterface({
1051
+ name: "CreateClient",
1052
+ callSignatures: [
1053
+ {
1054
+ parameters: [
1055
+ {
1056
+ name: "repositoryNameOrEndpoint",
1057
+ type: "string"
1058
+ },
1059
+ {
1060
+ name: "options",
1061
+ type: "prismic.ClientConfig",
1062
+ hasQuestionToken: true
1063
+ }
1064
+ ],
1065
+ returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
1066
+ }
1067
+ ]
1068
+ });
1016
1069
  }
1017
1070
  return getSourceFileText(sourceFile);
1018
1071
  };