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/bin/cli.mjs CHANGED
@@ -209,10 +209,18 @@ const pascalCase = (input) => pascalCase$1(input, {
209
209
  transform: pascalCaseTransformMerge
210
210
  });
211
211
 
212
- const buildSharedSliceInterfaceName = (config) => {
212
+ const buildSharedSliceInterfaceNamePart = (config) => {
213
213
  return pascalCase(`${config.id} Slice`);
214
214
  };
215
215
 
216
+ const buildTypeName = (...parts) => {
217
+ let name = pascalCase(parts.filter(Boolean).join(" "));
218
+ if (/^[0-9]/.test(name) ? "_" : "") {
219
+ name = `_${name}`;
220
+ }
221
+ return name;
222
+ };
223
+
216
224
  const addInterfacePropertyForField = (config) => {
217
225
  switch (config.model.type) {
218
226
  case "UID": {
@@ -262,12 +270,16 @@ const addInterfacePropertyForField = (config) => {
262
270
  if (config.fieldConfigs.embed?.providerTypes) {
263
271
  for (const providerType in config.fieldConfigs.embed?.providerTypes) {
264
272
  const configuredProviderType = config.fieldConfigs.embed?.providerTypes[providerType];
265
- providerTypes.push(`({ provider_name: "${providerType}" } & ${configuredProviderType})`);
273
+ providerTypes.push(
274
+ `({ provider_name: "${providerType}" } & ${configuredProviderType})`
275
+ );
266
276
  }
267
277
  }
268
278
  config.interface.addProperty({
269
279
  name: config.id,
270
- type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(" | ")})>` : "prismicT.EmbedField",
280
+ type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(
281
+ " | "
282
+ )})>` : "prismicT.EmbedField",
271
283
  docs: buildFieldDocs({
272
284
  id: config.id,
273
285
  model: config.model,
@@ -471,7 +483,13 @@ const addInterfacePropertyForField = (config) => {
471
483
  }
472
484
  case "Group": {
473
485
  const itemInterface = config.sourceFile.addInterface({
474
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Item`),
486
+ name: buildTypeName(
487
+ config.path[0].id,
488
+ "Document",
489
+ "Data",
490
+ config.id,
491
+ "Item"
492
+ ),
475
493
  docs: [
476
494
  {
477
495
  description: (writer) => {
@@ -523,12 +541,22 @@ const addInterfacePropertyForField = (config) => {
523
541
  for (const choiceId in config.model.config.choices) {
524
542
  const choice = config.model.config.choices[choiceId];
525
543
  if (choice.type === CustomTypeModelSliceType.SharedSlice) {
526
- choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
544
+ choiceInterfaceNames.push(
545
+ buildSharedSliceInterfaceNamePart({ id: choiceId })
546
+ );
527
547
  } else if (choice.type === CustomTypeModelSliceType.Slice) {
528
548
  let primaryInterface;
529
549
  if (choice["non-repeat"] && Object.keys(choice["non-repeat"]).length > 0) {
530
550
  primaryInterface = config.sourceFile.addInterface({
531
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Primary`),
551
+ name: buildTypeName(
552
+ config.path[0].id,
553
+ "Document",
554
+ "Data",
555
+ config.id,
556
+ choiceId,
557
+ "Slice",
558
+ "Primary"
559
+ ),
532
560
  docs: [
533
561
  {
534
562
  description: (writer) => {
@@ -549,7 +577,9 @@ const addInterfacePropertyForField = (config) => {
549
577
  }
550
578
  ]
551
579
  });
552
- writer.writeLine(`Primary content in ${humanReadablePath}`);
580
+ writer.writeLine(
581
+ `Primary content in ${humanReadablePath}`
582
+ );
553
583
  }
554
584
  }
555
585
  ]
@@ -579,7 +609,15 @@ const addInterfacePropertyForField = (config) => {
579
609
  let itemInterface;
580
610
  if (choice.repeat && Object.keys(choice.repeat).length > 0) {
581
611
  itemInterface = config.sourceFile.addInterface({
582
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Item`),
612
+ name: buildTypeName(
613
+ config.path[0].id,
614
+ "Document",
615
+ "Data",
616
+ config.id,
617
+ choiceId,
618
+ "Slice",
619
+ "Item"
620
+ ),
583
621
  docs: [
584
622
  {
585
623
  description: (writer) => {
@@ -629,7 +667,14 @@ const addInterfacePropertyForField = (config) => {
629
667
  });
630
668
  }
631
669
  const sliceType = config.sourceFile.addTypeAlias({
632
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
670
+ name: buildTypeName(
671
+ config.path[0].id,
672
+ "Document",
673
+ "Data",
674
+ config.id,
675
+ choiceId,
676
+ "Slice"
677
+ ),
633
678
  type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
634
679
  isExported: true
635
680
  });
@@ -638,7 +683,13 @@ const addInterfacePropertyForField = (config) => {
638
683
  }
639
684
  }
640
685
  const slicesType = config.sourceFile.addTypeAlias({
641
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Slice`),
686
+ name: buildTypeName(
687
+ config.path[0].id,
688
+ "Document",
689
+ "Data",
690
+ config.id,
691
+ "Slice"
692
+ ),
642
693
  type: choiceInterfaceNames.length > 0 ? choiceInterfaceNames.join(" | ") : "never",
643
694
  docs: [
644
695
  {
@@ -709,7 +760,7 @@ const addTypeAliasForCustomType = ({
709
760
  let dataInterface;
710
761
  if (hasDataFields) {
711
762
  dataInterface = sourceFile.addInterface({
712
- name: pascalCase(`${model.id} Document Data`),
763
+ name: buildTypeName(model.id, "Document", "Data"),
713
764
  docs: [
714
765
  {
715
766
  description: `Content for ${model.label} documents`
@@ -734,7 +785,7 @@ const addTypeAliasForCustomType = ({
734
785
  }
735
786
  } else {
736
787
  dataInterface = sourceFile.addTypeAlias({
737
- name: pascalCase(`${model.id} Document Data`),
788
+ name: buildTypeName(model.id, "Document", "Data"),
738
789
  type: `Record<string, never>`,
739
790
  docs: [
740
791
  {
@@ -744,7 +795,7 @@ const addTypeAliasForCustomType = ({
744
795
  });
745
796
  }
746
797
  return sourceFile.addTypeAlias({
747
- name: pascalCase(`${model.id} Document`),
798
+ name: buildTypeName(model.id, "Document"),
748
799
  typeParameters: [
749
800
  {
750
801
  name: "Lang",
@@ -760,7 +811,9 @@ const addTypeAliasForCustomType = ({
760
811
  writer.blankLine();
761
812
  writer.writeLine(`- **API ID**: \`${model.id}\``);
762
813
  writer.writeLine(`- **Repeatable**: \`${model.repeatable}\``);
763
- writer.writeLine(`- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`);
814
+ writer.writeLine(
815
+ `- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`
816
+ );
764
817
  },
765
818
  tags: [
766
819
  {
@@ -780,7 +833,11 @@ const addTypeAliasForSharedSlice = (config) => {
780
833
  let primaryInterface;
781
834
  if (variation.primary && Object.keys(variation.primary).length > 0) {
782
835
  primaryInterface = config.sourceFile.addInterface({
783
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Primary`),
836
+ name: buildTypeName(
837
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
838
+ variation.id,
839
+ "Primary"
840
+ ),
784
841
  docs: [
785
842
  {
786
843
  description: (writer) => {
@@ -821,7 +878,11 @@ const addTypeAliasForSharedSlice = (config) => {
821
878
  let itemInterface;
822
879
  if (variation.items && Object.keys(variation.items).length > 0) {
823
880
  itemInterface = config.sourceFile.addInterface({
824
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Item`),
881
+ name: buildTypeName(
882
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
883
+ variation.id,
884
+ "Item"
885
+ ),
825
886
  docs: [
826
887
  {
827
888
  description: (writer) => {
@@ -861,18 +922,23 @@ const addTypeAliasForSharedSlice = (config) => {
861
922
  });
862
923
  }
863
924
  const variationType = config.sourceFile.addTypeAlias({
864
- name: pascalCase(`${buildSharedSliceInterfaceName({
865
- id: config.model.id
866
- })} ${variation.id}`),
925
+ name: buildTypeName(
926
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
927
+ variation.id
928
+ ),
867
929
  type: `prismicT.SharedSliceVariation<"${variation.id}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
868
930
  docs: [
869
931
  {
870
932
  description: (writer) => {
871
- writer.writeLine(`${variation.name} variation for ${config.model.name} Slice`);
933
+ writer.writeLine(
934
+ `${variation.name} variation for ${config.model.name} Slice`
935
+ );
872
936
  writer.blankLine();
873
937
  writer.writeLine(`- **API ID**: \`${variation.id}\``);
874
938
  writer.writeLine(`- **Description**: \`${variation.description}\``);
875
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
939
+ writer.writeLine(
940
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
941
+ );
876
942
  }
877
943
  }
878
944
  ],
@@ -881,7 +947,7 @@ const addTypeAliasForSharedSlice = (config) => {
881
947
  variationTypeNames.push(variationType.getName());
882
948
  }
883
949
  const variationsType = config.sourceFile.addTypeAlias({
884
- name: pascalCase(`${config.model.id} Slice Variation`),
950
+ name: buildTypeName(config.model.id, "Slice", "Variation"),
885
951
  type: variationTypeNames.length > 0 ? variationTypeNames.join(" | ") : "never",
886
952
  docs: [
887
953
  {
@@ -900,9 +966,9 @@ const addTypeAliasForSharedSlice = (config) => {
900
966
  ]
901
967
  });
902
968
  return config.sourceFile.addTypeAlias({
903
- name: pascalCase(buildSharedSliceInterfaceName({
904
- id: config.model.id
905
- })),
969
+ name: buildTypeName(
970
+ buildSharedSliceInterfaceNamePart({ id: config.model.id })
971
+ ),
906
972
  type: `prismicT.SharedSlice<"${config.model.id}", ${variationsType.getName()}>`,
907
973
  docs: [
908
974
  {
@@ -910,8 +976,12 @@ const addTypeAliasForSharedSlice = (config) => {
910
976
  writer.writeLine(`${config.model.name} Shared Slice`);
911
977
  writer.blankLine();
912
978
  writer.writeLine(`- **API ID**: \`${config.model.id}\``);
913
- writer.writeLine(`- **Description**: \`${config.model.description}\``);
914
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
979
+ writer.writeLine(
980
+ `- **Description**: \`${config.model.description}\``
981
+ );
982
+ writer.writeLine(
983
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
984
+ );
915
985
  }
916
986
  }
917
987
  ],
@@ -936,7 +1006,7 @@ const generateTypes = (config = {}) => {
936
1006
  leadingTrivia: NON_EDITABLE_FILE_HEADER
937
1007
  });
938
1008
  sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
939
- const simplifyTypeAlias = sourceFile.addTypeAlias({
1009
+ sourceFile.addTypeAlias({
940
1010
  name: "Simplify",
941
1011
  typeParameters: [
942
1012
  {
@@ -957,7 +1027,9 @@ const generateTypes = (config = {}) => {
957
1027
  if (config.customTypeModels.length > 0) {
958
1028
  sourceFile.addTypeAlias({
959
1029
  name: "AllDocumentTypes",
960
- type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
1030
+ type: config.customTypeModels.map(
1031
+ (customTypeModel) => buildTypeName(customTypeModel.id, "Document")
1032
+ ).join(" | "),
961
1033
  isExported: true
962
1034
  });
963
1035
  }
@@ -971,7 +1043,7 @@ const generateTypes = (config = {}) => {
971
1043
  });
972
1044
  }
973
1045
  }
974
- if (config.clientIntegration?.includeCreateClientInterface || config.clientIntegration?.includeContentNamespace) {
1046
+ if (config.clientIntegration?.includeCreateClientInterface) {
975
1047
  sourceFile.addImportDeclaration({
976
1048
  moduleSpecifier: "@prismicio/client",
977
1049
  namespaceImport: "prismic",
@@ -982,44 +1054,25 @@ const generateTypes = (config = {}) => {
982
1054
  hasDeclareKeyword: true,
983
1055
  declarationKind: ModuleDeclarationKind.Module
984
1056
  });
985
- if (config.clientIntegration.includeCreateClientInterface) {
986
- clientModuleDeclaration.addInterface({
987
- name: "CreateClient",
988
- callSignatures: [
989
- {
990
- parameters: [
991
- {
992
- name: "repositoryNameOrEndpoint",
993
- type: "string"
994
- },
995
- {
996
- name: "options",
997
- type: "prismic.ClientConfig",
998
- hasQuestionToken: true
999
- }
1000
- ],
1001
- returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
1002
- }
1003
- ]
1004
- });
1005
- }
1006
- if (config.clientIntegration.includeContentNamespace) {
1007
- const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
1008
- name: "Content",
1009
- declarationKind: ModuleDeclarationKind.Namespace
1010
- });
1011
- const exportSymbols = sourceFile.getExportSymbols().filter((exportSymbol) => {
1012
- return exportSymbol.getName() !== simplifyTypeAlias.getName();
1013
- });
1014
- contentNamespaceDeclaration.addExportDeclaration({
1015
- isTypeOnly: true,
1016
- namedExports: exportSymbols.map((exportSymbol) => {
1017
- return {
1018
- name: exportSymbol.getName()
1019
- };
1020
- })
1021
- });
1022
- }
1057
+ clientModuleDeclaration.addInterface({
1058
+ name: "CreateClient",
1059
+ callSignatures: [
1060
+ {
1061
+ parameters: [
1062
+ {
1063
+ name: "repositoryNameOrEndpoint",
1064
+ type: "string"
1065
+ },
1066
+ {
1067
+ name: "options",
1068
+ type: "prismic.ClientConfig",
1069
+ hasQuestionToken: true
1070
+ }
1071
+ ],
1072
+ returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
1073
+ }
1074
+ ]
1075
+ });
1023
1076
  }
1024
1077
  return getSourceFileText(sourceFile);
1025
1078
  };
@@ -1036,20 +1089,28 @@ const configSchema = Joi.object({
1036
1089
  customTypesAPIToken: Joi.string(),
1037
1090
  output: Joi.string(),
1038
1091
  clientIntegration: Joi.object({
1039
- includeCreateClientInterface: Joi.boolean(),
1040
- includeContentNamespace: Joi.boolean()
1092
+ includeCreateClientInterface: Joi.boolean()
1041
1093
  }),
1042
- locales: Joi.alternatives(Joi.array().items(Joi.string().required()), Joi.object({
1043
- ids: Joi.array().items(Joi.string().required()),
1044
- fetchFromRepository: Joi.boolean()
1045
- })),
1046
- models: Joi.alternatives(Joi.array().items(Joi.string()), Joi.object({
1047
- files: Joi.array().items(Joi.string()),
1048
- fetchFromRepository: Joi.boolean()
1049
- })),
1094
+ locales: Joi.alternatives(
1095
+ Joi.array().items(Joi.string().required()),
1096
+ Joi.object({
1097
+ ids: Joi.array().items(Joi.string().required()),
1098
+ fetchFromRepository: Joi.boolean()
1099
+ })
1100
+ ),
1101
+ models: Joi.alternatives(
1102
+ Joi.array().items(Joi.string()),
1103
+ Joi.object({
1104
+ files: Joi.array().items(Joi.string()),
1105
+ fetchFromRepository: Joi.boolean()
1106
+ })
1107
+ ),
1050
1108
  fields: Joi.object({
1051
1109
  embed: Joi.object({
1052
- providerTypes: Joi.object().pattern(Joi.string(), Joi.string().required())
1110
+ providerTypes: Joi.object().pattern(
1111
+ Joi.string(),
1112
+ Joi.string().required()
1113
+ )
1053
1114
  }),
1054
1115
  integrationFields: Joi.object({
1055
1116
  catalogTypes: Joi.object().pattern(Joi.string(), Joi.string().required())
@@ -1150,7 +1211,8 @@ const loadConfig = (config) => {
1150
1211
  return {};
1151
1212
  };
1152
1213
 
1153
- const cli = meow(`
1214
+ const cli = meow(
1215
+ `
1154
1216
  Usage:
1155
1217
  prismic-ts-codegen [options...]
1156
1218
  prismic-ts-codegen init [options...]
@@ -1160,16 +1222,18 @@ const cli = meow(`
1160
1222
 
1161
1223
  Options:
1162
1224
  -c, --config <path> Path to a prismic-ts-codegen configuration file.
1163
- `, {
1164
- importMeta: import.meta,
1165
- flags: {
1166
- config: {
1167
- type: "string",
1168
- alias: "c",
1169
- isRequired: false
1225
+ `,
1226
+ {
1227
+ importMeta: import.meta,
1228
+ flags: {
1229
+ config: {
1230
+ type: "string",
1231
+ alias: "c",
1232
+ isRequired: false
1233
+ }
1170
1234
  }
1171
1235
  }
1172
- });
1236
+ );
1173
1237
  const main = async () => {
1174
1238
  if (cli.input[0] === "init") {
1175
1239
  const configPath = cli.flags.config || "prismicCodegen.config.ts";
@@ -1227,12 +1291,13 @@ Created prismic-ts-codegen config file: ${configPath}`);
1227
1291
  localeIDs,
1228
1292
  fieldConfigs: config.fields,
1229
1293
  clientIntegration: {
1230
- includeCreateClientInterface: hasCustomTypeModels ? config.clientIntegration?.includeCreateClientInterface ?? true : false,
1231
- includeContentNamespace: config.clientIntegration?.includeContentNamespace ?? true
1294
+ includeCreateClientInterface: hasCustomTypeModels ? config.clientIntegration?.includeCreateClientInterface ?? true : false
1232
1295
  }
1233
1296
  });
1234
1297
  if (config.clientIntegration?.includeCreateClientInterface && !hasCustomTypeModels) {
1235
- console.info("[INFO]: prismic-ts-codegen was configured to automatically integrate with `@prismicio/client`, but the integration was not generated because no Custom Type models were found. Automatic integration requires at least one Custom Type model.");
1298
+ console.info(
1299
+ "[INFO]: prismic-ts-codegen was configured to automatically integrate with `@prismicio/client`, but the integration was generated because no Custom Type models were found. Automatic integration requires at least one Custom Type model."
1300
+ );
1236
1301
  }
1237
1302
  if (config.output) {
1238
1303
  writeFileSync(resolve(config.output), types);