prismic-ts-codegen 0.1.4 → 0.1.6

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
@@ -15,10 +15,6 @@ import * as prismicCT from '@prismicio/custom-types-client';
15
15
  import _jiti from 'jiti';
16
16
 
17
17
  const BLANK_LINE_IDENTIFIER = "// ___BLANK_LINE_TO_BE_REPLACED___";
18
- const NON_EDITABLE_FILE_HEADER = stripIndent`
19
- // Code generated by prismic-ts-codegen. DO NOT EDIT.
20
- ${BLANK_LINE_IDENTIFIER}
21
- `;
22
18
  const CUSTOM_TYPES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/custom-types";
23
19
  const SHARED_SLICES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/reusing-slices";
24
20
  const FIELD_DOCUMENTATION_URLS = {
@@ -738,7 +734,7 @@ const addInterfacePropertiesForFields = (config) => {
738
734
  for (const name in config.fields) {
739
735
  addInterfacePropertyForField({
740
736
  ...config,
741
- id: name.includes("-") || /^[0-9]/.test(name) ? `"${name}"` : name,
737
+ id: name.includes("-") || name.includes(":") || /^[0-9]/.test(name) ? `"${name}"` : name,
742
738
  model: config.fields[name],
743
739
  tabName: config.tabName
744
740
  });
@@ -1002,11 +998,10 @@ const generateTypes = (config = {}) => {
1002
998
  sourceFile.addImportDeclaration({
1003
999
  moduleSpecifier: "@prismicio/types",
1004
1000
  namespaceImport: "prismicT",
1005
- isTypeOnly: true,
1006
- leadingTrivia: NON_EDITABLE_FILE_HEADER
1001
+ isTypeOnly: true
1007
1002
  });
1008
1003
  sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
1009
- sourceFile.addTypeAlias({
1004
+ const simplifyTypeAlias = sourceFile.addTypeAlias({
1010
1005
  name: "Simplify",
1011
1006
  typeParameters: [
1012
1007
  {
@@ -1043,7 +1038,7 @@ const generateTypes = (config = {}) => {
1043
1038
  });
1044
1039
  }
1045
1040
  }
1046
- if (config.clientIntegration?.includeCreateClientInterface) {
1041
+ if (config.clientIntegration?.includeCreateClientInterface || config.clientIntegration?.includeContentNamespace) {
1047
1042
  sourceFile.addImportDeclaration({
1048
1043
  moduleSpecifier: "@prismicio/client",
1049
1044
  namespaceImport: "prismic",
@@ -1054,25 +1049,44 @@ const generateTypes = (config = {}) => {
1054
1049
  hasDeclareKeyword: true,
1055
1050
  declarationKind: ModuleDeclarationKind.Module
1056
1051
  });
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
- });
1052
+ if (config.clientIntegration.includeCreateClientInterface) {
1053
+ clientModuleDeclaration.addInterface({
1054
+ name: "CreateClient",
1055
+ callSignatures: [
1056
+ {
1057
+ parameters: [
1058
+ {
1059
+ name: "repositoryNameOrEndpoint",
1060
+ type: "string"
1061
+ },
1062
+ {
1063
+ name: "options",
1064
+ type: "prismic.ClientConfig",
1065
+ hasQuestionToken: true
1066
+ }
1067
+ ],
1068
+ returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
1069
+ }
1070
+ ]
1071
+ });
1072
+ }
1073
+ if (config.clientIntegration.includeContentNamespace) {
1074
+ const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
1075
+ name: "Content",
1076
+ declarationKind: ModuleDeclarationKind.Namespace
1077
+ });
1078
+ const exportSymbols = sourceFile.getExportSymbols().filter((exportSymbol) => {
1079
+ return exportSymbol.getName() !== simplifyTypeAlias.getName();
1080
+ });
1081
+ contentNamespaceDeclaration.addExportDeclaration({
1082
+ isTypeOnly: true,
1083
+ namedExports: exportSymbols.map((exportSymbol) => {
1084
+ return {
1085
+ name: exportSymbol.getName()
1086
+ };
1087
+ })
1088
+ });
1089
+ }
1076
1090
  }
1077
1091
  return getSourceFileText(sourceFile);
1078
1092
  };
@@ -1089,7 +1103,8 @@ const configSchema = Joi.object({
1089
1103
  customTypesAPIToken: Joi.string(),
1090
1104
  output: Joi.string(),
1091
1105
  clientIntegration: Joi.object({
1092
- includeCreateClientInterface: Joi.boolean()
1106
+ includeCreateClientInterface: Joi.boolean(),
1107
+ includeContentNamespace: Joi.boolean()
1093
1108
  }),
1094
1109
  locales: Joi.alternatives(
1095
1110
  Joi.array().items(Joi.string().required()),
@@ -1214,6 +1229,8 @@ const loadConfig = (config) => {
1214
1229
  return {};
1215
1230
  };
1216
1231
 
1232
+ const NON_EDITABLE_FILE_HEADER = "// Code generated by prismic-ts-codegen. DO NOT EDIT.";
1233
+
1217
1234
  const cli = meow(
1218
1235
  `
1219
1236
  Usage:
@@ -1288,22 +1305,26 @@ Created prismic-ts-codegen config file: ${configPath}`);
1288
1305
  fetchFromRepository: config.locales && "fetchFromRepository" in config.locales && config.locales.fetchFromRepository
1289
1306
  });
1290
1307
  const hasCustomTypeModels = customTypeModels.length > 0;
1308
+ if (config.clientIntegration?.includeCreateClientInterface && !hasCustomTypeModels) {
1309
+ console.info(
1310
+ "[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."
1311
+ );
1312
+ }
1291
1313
  const types = generateTypes({
1292
1314
  customTypeModels,
1293
1315
  sharedSliceModels,
1294
1316
  localeIDs,
1295
1317
  fieldConfigs: config.fields,
1296
1318
  clientIntegration: {
1297
- includeCreateClientInterface: hasCustomTypeModels ? config.clientIntegration?.includeCreateClientInterface ?? true : false
1319
+ includeCreateClientInterface: hasCustomTypeModels ? config.clientIntegration?.includeCreateClientInterface ?? true : false,
1320
+ includeContentNamespace: config.clientIntegration?.includeContentNamespace ?? true
1298
1321
  }
1299
1322
  });
1300
- if (config.clientIntegration?.includeCreateClientInterface && !hasCustomTypeModels) {
1301
- console.info(
1302
- "[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."
1303
- );
1304
- }
1323
+ const fileContents = `${NON_EDITABLE_FILE_HEADER}
1324
+
1325
+ ${types}`;
1305
1326
  if (config.output) {
1306
- writeFileSync(resolve(config.output), types);
1327
+ writeFileSync(resolve(config.output), fileContents);
1307
1328
  console.info(`
1308
1329
  Generated types in: ${config.output}`);
1309
1330
  } else {
package/dist/index.cjs CHANGED
@@ -3,15 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const tsMorph = require('ts-morph');
6
- const commonTags = require('common-tags');
7
6
  const types = require('@prismicio/types');
8
7
  const pascalCase$1 = require('pascal-case');
9
8
 
10
9
  const BLANK_LINE_IDENTIFIER = "// ___BLANK_LINE_TO_BE_REPLACED___";
11
- const NON_EDITABLE_FILE_HEADER = commonTags.stripIndent`
12
- // Code generated by prismic-ts-codegen. DO NOT EDIT.
13
- ${BLANK_LINE_IDENTIFIER}
14
- `;
15
10
  const CUSTOM_TYPES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/custom-types";
16
11
  const SHARED_SLICES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/reusing-slices";
17
12
  const FIELD_DOCUMENTATION_URLS = {
@@ -731,7 +726,7 @@ const addInterfacePropertiesForFields = (config) => {
731
726
  for (const name in config.fields) {
732
727
  addInterfacePropertyForField({
733
728
  ...config,
734
- id: name.includes("-") || /^[0-9]/.test(name) ? `"${name}"` : name,
729
+ id: name.includes("-") || name.includes(":") || /^[0-9]/.test(name) ? `"${name}"` : name,
735
730
  model: config.fields[name],
736
731
  tabName: config.tabName
737
732
  });
@@ -995,11 +990,10 @@ const generateTypes = (config = {}) => {
995
990
  sourceFile.addImportDeclaration({
996
991
  moduleSpecifier: "@prismicio/types",
997
992
  namespaceImport: "prismicT",
998
- isTypeOnly: true,
999
- leadingTrivia: NON_EDITABLE_FILE_HEADER
993
+ isTypeOnly: true
1000
994
  });
1001
995
  sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
1002
- sourceFile.addTypeAlias({
996
+ const simplifyTypeAlias = sourceFile.addTypeAlias({
1003
997
  name: "Simplify",
1004
998
  typeParameters: [
1005
999
  {
@@ -1036,7 +1030,7 @@ const generateTypes = (config = {}) => {
1036
1030
  });
1037
1031
  }
1038
1032
  }
1039
- if (config.clientIntegration?.includeCreateClientInterface) {
1033
+ if (config.clientIntegration?.includeCreateClientInterface || config.clientIntegration?.includeContentNamespace) {
1040
1034
  sourceFile.addImportDeclaration({
1041
1035
  moduleSpecifier: "@prismicio/client",
1042
1036
  namespaceImport: "prismic",
@@ -1047,25 +1041,44 @@ const generateTypes = (config = {}) => {
1047
1041
  hasDeclareKeyword: true,
1048
1042
  declarationKind: tsMorph.ModuleDeclarationKind.Module
1049
1043
  });
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
- });
1044
+ if (config.clientIntegration.includeCreateClientInterface) {
1045
+ clientModuleDeclaration.addInterface({
1046
+ name: "CreateClient",
1047
+ callSignatures: [
1048
+ {
1049
+ parameters: [
1050
+ {
1051
+ name: "repositoryNameOrEndpoint",
1052
+ type: "string"
1053
+ },
1054
+ {
1055
+ name: "options",
1056
+ type: "prismic.ClientConfig",
1057
+ hasQuestionToken: true
1058
+ }
1059
+ ],
1060
+ returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
1061
+ }
1062
+ ]
1063
+ });
1064
+ }
1065
+ if (config.clientIntegration.includeContentNamespace) {
1066
+ const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
1067
+ name: "Content",
1068
+ declarationKind: tsMorph.ModuleDeclarationKind.Namespace
1069
+ });
1070
+ const exportSymbols = sourceFile.getExportSymbols().filter((exportSymbol) => {
1071
+ return exportSymbol.getName() !== simplifyTypeAlias.getName();
1072
+ });
1073
+ contentNamespaceDeclaration.addExportDeclaration({
1074
+ isTypeOnly: true,
1075
+ namedExports: exportSymbols.map((exportSymbol) => {
1076
+ return {
1077
+ name: exportSymbol.getName()
1078
+ };
1079
+ })
1080
+ });
1081
+ }
1069
1082
  }
1070
1083
  return getSourceFileText(sourceFile);
1071
1084
  };