prismic-ts-codegen 0.1.4 → 0.1.5
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 +56 -35
- package/dist/index.cjs +41 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +23 -3
- package/dist/index.js +41 -28
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/configSchema.ts +1 -0
- package/src/cli/constants.ts +2 -0
- package/src/cli/index.ts +14 -9
- package/src/cli/types.ts +23 -3
- package/src/constants.ts +0 -6
- package/src/generateTypes.ts +56 -26
package/src/generateTypes.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Project, ModuleDeclarationKind } from "ts-morph";
|
|
2
2
|
import type { CustomTypeModel, SharedSliceModel } from "@prismicio/types";
|
|
3
3
|
|
|
4
|
-
import { BLANK_LINE_IDENTIFIER
|
|
4
|
+
import { BLANK_LINE_IDENTIFIER } from "./constants";
|
|
5
5
|
import { addTypeAliasForCustomType } from "./lib/addTypeAliasForCustomType";
|
|
6
6
|
import { addTypeAliasForSharedSlice } from "./lib/addTypeAliasForSharedSlice";
|
|
7
7
|
import { getSourceFileText } from "./lib/getSourceFileText";
|
|
@@ -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
|
|
|
@@ -29,12 +30,11 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
|
|
|
29
30
|
moduleSpecifier: "@prismicio/types",
|
|
30
31
|
namespaceImport: "prismicT",
|
|
31
32
|
isTypeOnly: true,
|
|
32
|
-
leadingTrivia: NON_EDITABLE_FILE_HEADER,
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
|
|
36
36
|
|
|
37
|
-
sourceFile.addTypeAlias({
|
|
37
|
+
const simplifyTypeAlias = sourceFile.addTypeAlias({
|
|
38
38
|
name: "Simplify",
|
|
39
39
|
typeParameters: [
|
|
40
40
|
{
|
|
@@ -77,7 +77,10 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
if (
|
|
80
|
+
if (
|
|
81
|
+
config.clientIntegration?.includeCreateClientInterface ||
|
|
82
|
+
config.clientIntegration?.includeContentNamespace
|
|
83
|
+
) {
|
|
81
84
|
sourceFile.addImportDeclaration({
|
|
82
85
|
moduleSpecifier: "@prismicio/client",
|
|
83
86
|
namespaceImport: "prismic",
|
|
@@ -90,28 +93,55 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
|
|
|
90
93
|
declarationKind: ModuleDeclarationKind.Module,
|
|
91
94
|
});
|
|
92
95
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
96
|
+
if (config.clientIntegration.includeCreateClientInterface) {
|
|
97
|
+
clientModuleDeclaration.addInterface({
|
|
98
|
+
name: "CreateClient",
|
|
99
|
+
callSignatures: [
|
|
100
|
+
{
|
|
101
|
+
parameters: [
|
|
102
|
+
{
|
|
103
|
+
name: "repositoryNameOrEndpoint",
|
|
104
|
+
type: "string",
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: "options",
|
|
108
|
+
type: "prismic.ClientConfig",
|
|
109
|
+
hasQuestionToken: true,
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
returnType:
|
|
113
|
+
(config.customTypeModels?.length || 0) > 0
|
|
114
|
+
? "prismic.Client<AllDocumentTypes>"
|
|
115
|
+
: "prismic.Client",
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (config.clientIntegration.includeContentNamespace) {
|
|
122
|
+
const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
|
|
123
|
+
name: "Content",
|
|
124
|
+
declarationKind: ModuleDeclarationKind.Namespace,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const exportSymbols = sourceFile
|
|
128
|
+
.getExportSymbols()
|
|
129
|
+
.filter((exportSymbol) => {
|
|
130
|
+
// The Simplify utility type should not
|
|
131
|
+
// be exported, but it is included in
|
|
132
|
+
// `getExportSymbols()`'s result.
|
|
133
|
+
return exportSymbol.getName() !== simplifyTypeAlias.getName();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
contentNamespaceDeclaration.addExportDeclaration({
|
|
137
|
+
isTypeOnly: true,
|
|
138
|
+
namedExports: exportSymbols.map((exportSymbol) => {
|
|
139
|
+
return {
|
|
140
|
+
name: exportSymbol.getName(),
|
|
141
|
+
};
|
|
142
|
+
}),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
115
145
|
}
|
|
116
146
|
|
|
117
147
|
return getSourceFileText(sourceFile);
|