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

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.
@@ -15,6 +15,7 @@ export type GenerateTypesConfig = {
15
15
  fieldConfigs?: FieldConfigs;
16
16
  clientIntegration?: {
17
17
  includeCreateClientInterface?: boolean;
18
+ includeContentNamespace?: boolean;
18
19
  };
19
20
  };
20
21
 
@@ -34,7 +35,7 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
34
35
 
35
36
  sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
36
37
 
37
- sourceFile.addTypeAlias({
38
+ const simplifyTypeAlias = sourceFile.addTypeAlias({
38
39
  name: "Simplify",
39
40
  typeParameters: [
40
41
  {
@@ -77,7 +78,10 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
77
78
  }
78
79
  }
79
80
 
80
- if (config.clientIntegration?.includeCreateClientInterface) {
81
+ if (
82
+ config.clientIntegration?.includeCreateClientInterface ||
83
+ config.clientIntegration?.includeContentNamespace
84
+ ) {
81
85
  sourceFile.addImportDeclaration({
82
86
  moduleSpecifier: "@prismicio/client",
83
87
  namespaceImport: "prismic",
@@ -90,28 +94,55 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
90
94
  declarationKind: ModuleDeclarationKind.Module,
91
95
  });
92
96
 
93
- clientModuleDeclaration.addInterface({
94
- name: "CreateClient",
95
- callSignatures: [
96
- {
97
- parameters: [
98
- {
99
- name: "repositoryNameOrEndpoint",
100
- type: "string",
101
- },
102
- {
103
- name: "options",
104
- type: "prismic.ClientConfig",
105
- hasQuestionToken: true,
106
- },
107
- ],
108
- returnType:
109
- (config.customTypeModels?.length || 0) > 0
110
- ? "prismic.Client<AllDocumentTypes>"
111
- : "prismic.Client",
112
- },
113
- ],
114
- });
97
+ if (config.clientIntegration.includeCreateClientInterface) {
98
+ clientModuleDeclaration.addInterface({
99
+ name: "CreateClient",
100
+ callSignatures: [
101
+ {
102
+ parameters: [
103
+ {
104
+ name: "repositoryNameOrEndpoint",
105
+ type: "string",
106
+ },
107
+ {
108
+ name: "options",
109
+ type: "prismic.ClientConfig",
110
+ hasQuestionToken: true,
111
+ },
112
+ ],
113
+ returnType:
114
+ (config.customTypeModels?.length || 0) > 0
115
+ ? "prismic.Client<AllDocumentTypes>"
116
+ : "prismic.Client",
117
+ },
118
+ ],
119
+ });
120
+ }
121
+
122
+ if (config.clientIntegration.includeContentNamespace) {
123
+ const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
124
+ name: "Content",
125
+ declarationKind: ModuleDeclarationKind.Namespace,
126
+ });
127
+
128
+ const exportSymbols = sourceFile
129
+ .getExportSymbols()
130
+ .filter((exportSymbol) => {
131
+ // The Simplify utility type should not
132
+ // be exported, but it is included in
133
+ // `getExportSymbols()`'s result.
134
+ return exportSymbol.getName() !== simplifyTypeAlias.getName();
135
+ });
136
+
137
+ contentNamespaceDeclaration.addExportDeclaration({
138
+ isTypeOnly: true,
139
+ namedExports: exportSymbols.map((exportSymbol) => {
140
+ return {
141
+ name: exportSymbol.getName(),
142
+ };
143
+ }),
144
+ });
145
+ }
115
146
  }
116
147
 
117
148
  return getSourceFileText(sourceFile);
@@ -153,7 +153,7 @@ const addInterfacePropertyForField = (
153
153
  } else {
154
154
  config.interface.addProperty({
155
155
  name: config.id,
156
- type: "prismicT.ImageField<null>",
156
+ type: "prismicT.ImageField<never>",
157
157
  docs: buildFieldDocs({
158
158
  id: config.id,
159
159
  model: config.model,
@@ -632,7 +632,7 @@ export const addInterfacePropertiesForFields = (
632
632
  for (const name in config.fields) {
633
633
  addInterfacePropertyForField({
634
634
  ...config,
635
- id: name.includes("-") ? `"${name}"` : name,
635
+ id: name.includes("-") || /^[0-9]/.test(name) ? `"${name}"` : name,
636
636
  model: config.fields[name],
637
637
  tabName: config.tabName,
638
638
  });