prismic-ts-codegen 0.0.4 → 0.0.7

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.
@@ -1,4 +1,4 @@
1
- import { Project } from "ts-morph";
1
+ import { Project, ModuleDeclarationKind } from "ts-morph";
2
2
  import type { CustomTypeModel, SharedSliceModel } from "@prismicio/types";
3
3
 
4
4
  import { BLANK_LINE_IDENTIFIER, NON_EDITABLE_FILE_HEADER } from "./constants";
@@ -6,12 +6,16 @@ import { addTypeAliasForCustomType } from "./lib/addTypeAliasForCustomType";
6
6
  import { addTypeAliasForSharedSlice } from "./lib/addTypeAliasForSharedSlice";
7
7
  import { getSourceFileText } from "./lib/getSourceFileText";
8
8
  import { FieldConfigs } from "./types";
9
+ import { pascalCase } from "./lib/pascalCase";
9
10
 
10
11
  export type GenerateTypesConfig = {
11
12
  customTypeModels?: CustomTypeModel[];
12
13
  sharedSliceModels?: SharedSliceModel[];
13
14
  localeIDs?: string[];
14
15
  fieldConfigs?: FieldConfigs;
16
+ clientIntegration?: {
17
+ includeCreateClientInterface?: boolean;
18
+ };
15
19
  };
16
20
 
17
21
  export const generateTypes = (config: GenerateTypesConfig = {}) => {
@@ -49,6 +53,18 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
49
53
  fieldConfigs: config.fieldConfigs || {},
50
54
  });
51
55
  }
56
+
57
+ if (config.customTypeModels.length > 0) {
58
+ sourceFile.addTypeAlias({
59
+ name: "AllDocumentTypes",
60
+ type: config.customTypeModels
61
+ .map((customTypeModel) =>
62
+ pascalCase(`${customTypeModel.id} Document`),
63
+ )
64
+ .join(" | "),
65
+ isExported: true,
66
+ });
67
+ }
52
68
  }
53
69
 
54
70
  if (config.sharedSliceModels) {
@@ -61,5 +77,42 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
61
77
  }
62
78
  }
63
79
 
80
+ if (config.clientIntegration?.includeCreateClientInterface) {
81
+ sourceFile.addImportDeclaration({
82
+ moduleSpecifier: "@prismicio/client",
83
+ namespaceImport: "prismic",
84
+ isTypeOnly: true,
85
+ });
86
+
87
+ const clientModuleDeclaration = sourceFile.addModule({
88
+ name: '"@prismicio/client"',
89
+ hasDeclareKeyword: true,
90
+ declarationKind: ModuleDeclarationKind.Module,
91
+ });
92
+
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
+ });
115
+ }
116
+
64
117
  return getSourceFileText(sourceFile);
65
118
  };