prismic-ts-codegen 0.1.5 → 0.1.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.
Files changed (175) hide show
  1. package/README.md +5 -5
  2. package/bin/prismic-ts-codegen.mjs +3 -0
  3. package/dist/cli/configSchema.cjs +38 -0
  4. package/dist/cli/configSchema.cjs.map +1 -0
  5. package/dist/cli/configSchema.d.ts +3 -0
  6. package/dist/cli/configSchema.js +38 -0
  7. package/dist/cli/configSchema.js.map +1 -0
  8. package/dist/cli/constants.cjs +5 -0
  9. package/dist/cli/constants.cjs.map +1 -0
  10. package/dist/cli/constants.d.ts +1 -0
  11. package/dist/cli/constants.js +5 -0
  12. package/dist/cli/constants.js.map +1 -0
  13. package/dist/cli/index.d.ts +1 -0
  14. package/dist/cli/loadConfig.cjs +32 -0
  15. package/dist/cli/loadConfig.cjs.map +1 -0
  16. package/dist/cli/loadConfig.d.ts +6 -0
  17. package/dist/cli/loadConfig.js +32 -0
  18. package/dist/cli/loadConfig.js.map +1 -0
  19. package/dist/cli/loadLocaleIDs.cjs +37 -0
  20. package/dist/cli/loadLocaleIDs.cjs.map +1 -0
  21. package/dist/cli/loadLocaleIDs.d.ts +10 -0
  22. package/dist/cli/loadLocaleIDs.js +20 -0
  23. package/dist/cli/loadLocaleIDs.js.map +1 -0
  24. package/dist/cli/loadModels.cjs +79 -0
  25. package/dist/cli/loadModels.cjs.map +1 -0
  26. package/dist/cli/loadModels.d.ts +15 -0
  27. package/dist/cli/loadModels.js +62 -0
  28. package/dist/cli/loadModels.js.map +1 -0
  29. package/dist/cli/types.d.ts +175 -0
  30. package/dist/cli.cjs +119 -0
  31. package/dist/cli.cjs.map +1 -0
  32. package/dist/cli.js +118 -0
  33. package/dist/cli.js.map +1 -0
  34. package/dist/constants.cjs +29 -0
  35. package/dist/constants.cjs.map +1 -0
  36. package/dist/constants.d.ts +5 -0
  37. package/dist/constants.js +29 -0
  38. package/dist/constants.js.map +1 -0
  39. package/dist/detectTypesProvider.cjs +16 -0
  40. package/dist/detectTypesProvider.cjs.map +1 -0
  41. package/dist/detectTypesProvider.d.ts +13 -0
  42. package/dist/detectTypesProvider.js +16 -0
  43. package/dist/detectTypesProvider.js.map +1 -0
  44. package/dist/generateTypes.cjs +109 -0
  45. package/dist/generateTypes.cjs.map +1 -0
  46. package/dist/generateTypes.d.ts +15 -0
  47. package/dist/generateTypes.js +109 -0
  48. package/dist/generateTypes.js.map +1 -0
  49. package/dist/index.cjs +6 -1086
  50. package/dist/index.cjs.map +1 -1
  51. package/dist/index.d.ts +5 -192
  52. package/dist/index.js +5 -1081
  53. package/dist/index.js.map +1 -1
  54. package/dist/lib/addInterfacePropertiesForFields.cjs +490 -0
  55. package/dist/lib/addInterfacePropertiesForFields.cjs.map +1 -0
  56. package/dist/lib/addInterfacePropertiesForFields.d.ts +21 -0
  57. package/dist/lib/addInterfacePropertiesForFields.js +490 -0
  58. package/dist/lib/addInterfacePropertiesForFields.js.map +1 -0
  59. package/dist/lib/addTypeAliasForCustomType.cjs +81 -0
  60. package/dist/lib/addTypeAliasForCustomType.cjs.map +1 -0
  61. package/dist/lib/addTypeAliasForCustomType.d.ts +11 -0
  62. package/dist/lib/addTypeAliasForCustomType.js +81 -0
  63. package/dist/lib/addTypeAliasForCustomType.js.map +1 -0
  64. package/dist/lib/addTypeAliasForSharedSlice.cjs +149 -0
  65. package/dist/lib/addTypeAliasForSharedSlice.cjs.map +1 -0
  66. package/dist/lib/addTypeAliasForSharedSlice.d.ts +10 -0
  67. package/dist/lib/addTypeAliasForSharedSlice.js +149 -0
  68. package/dist/lib/addTypeAliasForSharedSlice.js.map +1 -0
  69. package/dist/lib/buildFieldDocs.cjs +43 -0
  70. package/dist/lib/buildFieldDocs.cjs.map +1 -0
  71. package/dist/lib/buildFieldDocs.d.ts +14 -0
  72. package/dist/lib/buildFieldDocs.js +43 -0
  73. package/dist/lib/buildFieldDocs.js.map +1 -0
  74. package/dist/lib/buildSharedSliceInterfaceNamePart.cjs +8 -0
  75. package/dist/lib/buildSharedSliceInterfaceNamePart.cjs.map +1 -0
  76. package/dist/lib/buildSharedSliceInterfaceNamePart.d.ts +5 -0
  77. package/dist/lib/buildSharedSliceInterfaceNamePart.js +8 -0
  78. package/dist/lib/buildSharedSliceInterfaceNamePart.js.map +1 -0
  79. package/dist/lib/buildTypeName.cjs +12 -0
  80. package/dist/lib/buildTypeName.cjs.map +1 -0
  81. package/dist/lib/buildTypeName.d.ts +1 -0
  82. package/dist/lib/buildTypeName.js +12 -0
  83. package/dist/lib/buildTypeName.js.map +1 -0
  84. package/dist/lib/getAPIIDPath.cjs +23 -0
  85. package/dist/lib/getAPIIDPath.cjs.map +1 -0
  86. package/dist/lib/getAPIIDPath.d.ts +10 -0
  87. package/dist/lib/getAPIIDPath.js +23 -0
  88. package/dist/lib/getAPIIDPath.js.map +1 -0
  89. package/dist/lib/getFieldDocumentationURL.cjs +8 -0
  90. package/dist/lib/getFieldDocumentationURL.cjs.map +1 -0
  91. package/dist/lib/getFieldDocumentationURL.d.ts +6 -0
  92. package/dist/lib/getFieldDocumentationURL.js +8 -0
  93. package/dist/lib/getFieldDocumentationURL.js.map +1 -0
  94. package/dist/lib/getHumanReadableFieldPath.cjs +19 -0
  95. package/dist/lib/getHumanReadableFieldPath.cjs.map +1 -0
  96. package/dist/lib/getHumanReadableFieldPath.d.ts +10 -0
  97. package/dist/lib/getHumanReadableFieldPath.js +19 -0
  98. package/dist/lib/getHumanReadableFieldPath.js.map +1 -0
  99. package/dist/lib/getHumanReadableFieldType.cjs +37 -0
  100. package/dist/lib/getHumanReadableFieldType.cjs.map +1 -0
  101. package/dist/lib/getHumanReadableFieldType.d.ts +6 -0
  102. package/dist/lib/getHumanReadableFieldType.js +37 -0
  103. package/dist/lib/getHumanReadableFieldType.js.map +1 -0
  104. package/dist/lib/getHumanReadableModelName.cjs +24 -0
  105. package/dist/lib/getHumanReadableModelName.cjs.map +1 -0
  106. package/dist/lib/getHumanReadableModelName.d.ts +7 -0
  107. package/dist/lib/getHumanReadableModelName.js +24 -0
  108. package/dist/lib/getHumanReadableModelName.js.map +1 -0
  109. package/dist/lib/getSourceFileText.cjs +9 -0
  110. package/dist/lib/getSourceFileText.cjs.map +1 -0
  111. package/dist/lib/getSourceFileText.d.ts +2 -0
  112. package/dist/lib/getSourceFileText.js +9 -0
  113. package/dist/lib/getSourceFileText.js.map +1 -0
  114. package/dist/lib/hasOwnProperty.cjs +7 -0
  115. package/dist/lib/hasOwnProperty.cjs.map +1 -0
  116. package/dist/lib/hasOwnProperty.d.ts +10 -0
  117. package/dist/lib/hasOwnProperty.js +7 -0
  118. package/dist/lib/hasOwnProperty.js.map +1 -0
  119. package/dist/lib/isCustomTypeModel.cjs +7 -0
  120. package/dist/lib/isCustomTypeModel.cjs.map +1 -0
  121. package/dist/lib/isCustomTypeModel.d.ts +2 -0
  122. package/dist/lib/isCustomTypeModel.js +7 -0
  123. package/dist/lib/isCustomTypeModel.js.map +1 -0
  124. package/dist/lib/isCustomTypeModelField.cjs +7 -0
  125. package/dist/lib/isCustomTypeModelField.cjs.map +1 -0
  126. package/dist/lib/isCustomTypeModelField.d.ts +2 -0
  127. package/dist/lib/isCustomTypeModelField.js +7 -0
  128. package/dist/lib/isCustomTypeModelField.js.map +1 -0
  129. package/dist/lib/isCustomTypeModelSlice.cjs +9 -0
  130. package/dist/lib/isCustomTypeModelSlice.cjs.map +1 -0
  131. package/dist/lib/isCustomTypeModelSlice.d.ts +2 -0
  132. package/dist/lib/isCustomTypeModelSlice.js +9 -0
  133. package/dist/lib/isCustomTypeModelSlice.js.map +1 -0
  134. package/dist/lib/isSharedSliceModel.cjs +9 -0
  135. package/dist/lib/isSharedSliceModel.cjs.map +1 -0
  136. package/dist/lib/isSharedSliceModel.d.ts +2 -0
  137. package/dist/lib/isSharedSliceModel.js +9 -0
  138. package/dist/lib/isSharedSliceModel.js.map +1 -0
  139. package/dist/lib/pascalCase.cjs +8 -0
  140. package/dist/lib/pascalCase.cjs.map +1 -0
  141. package/dist/lib/pascalCase.d.ts +1 -0
  142. package/dist/lib/pascalCase.js +8 -0
  143. package/dist/lib/pascalCase.js.map +1 -0
  144. package/dist/lib/writeFieldDocsDescription.cjs +29 -0
  145. package/dist/lib/writeFieldDocsDescription.cjs.map +1 -0
  146. package/dist/lib/writeFieldDocsDescription.d.ts +13 -0
  147. package/dist/lib/writeFieldDocsDescription.js +29 -0
  148. package/dist/lib/writeFieldDocsDescription.js.map +1 -0
  149. package/dist/types.d.ts +14 -0
  150. package/package.json +45 -36
  151. package/src/cli/configSchema.ts +2 -0
  152. package/src/cli/index.ts +9 -5
  153. package/src/cli/loadConfig.ts +4 -2
  154. package/src/cli/loadModels.ts +3 -3
  155. package/src/cli/types.ts +10 -0
  156. package/src/constants.ts +6 -3
  157. package/src/detectTypesProvider.ts +32 -0
  158. package/src/generateTypes.ts +11 -6
  159. package/src/index.ts +4 -1
  160. package/src/lib/addInterfacePropertiesForFields.ts +31 -28
  161. package/src/lib/addTypeAliasForCustomType.ts +7 -5
  162. package/src/lib/addTypeAliasForSharedSlice.ts +5 -4
  163. package/src/lib/buildFieldDocs.ts +2 -1
  164. package/src/lib/getAPIIDPath.ts +3 -2
  165. package/src/lib/getFieldDocumentationURL.ts +1 -1
  166. package/src/lib/getHumanReadableFieldPath.ts +2 -1
  167. package/src/lib/getHumanReadableFieldType.ts +2 -2
  168. package/src/lib/getHumanReadableModelName.ts +1 -1
  169. package/src/lib/getSourceFileText.ts +1 -1
  170. package/src/lib/isCustomTypeModel.ts +1 -1
  171. package/src/lib/isCustomTypeModelField.ts +1 -1
  172. package/src/lib/isCustomTypeModelSlice.ts +1 -1
  173. package/src/lib/isSharedSliceModel.ts +1 -1
  174. package/src/types.ts +1 -1
  175. package/bin/cli.mjs +0 -1342
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const tsMorph = require("ts-morph");
4
+ const addTypeAliasForCustomType = require("./lib/addTypeAliasForCustomType.cjs");
5
+ const addTypeAliasForSharedSlice = require("./lib/addTypeAliasForSharedSlice.cjs");
6
+ const buildTypeName = require("./lib/buildTypeName.cjs");
7
+ const getSourceFileText = require("./lib/getSourceFileText.cjs");
8
+ const constants = require("./constants.cjs");
9
+ const generateTypes = (config = {}) => {
10
+ var _a, _b, _c;
11
+ const project = new tsMorph.Project({
12
+ useInMemoryFileSystem: true
13
+ });
14
+ const sourceFile = project.createSourceFile("types.d.ts");
15
+ sourceFile.addImportDeclaration({
16
+ moduleSpecifier: config.typesProvider || "@prismicio/types",
17
+ namespaceImport: "prismic",
18
+ isTypeOnly: true
19
+ });
20
+ sourceFile.addStatements(constants.BLANK_LINE_IDENTIFIER);
21
+ const simplifyTypeAlias = sourceFile.addTypeAlias({
22
+ name: "Simplify",
23
+ typeParameters: [
24
+ {
25
+ name: "T"
26
+ }
27
+ ],
28
+ type: `{ [KeyType in keyof T]: T[KeyType] }`
29
+ });
30
+ if (config.customTypeModels) {
31
+ for (const model of config.customTypeModels) {
32
+ addTypeAliasForCustomType.addTypeAliasForCustomType({
33
+ model,
34
+ sourceFile,
35
+ localeIDs: config.localeIDs || [],
36
+ fieldConfigs: config.fieldConfigs || {}
37
+ });
38
+ }
39
+ if (config.customTypeModels.length > 0) {
40
+ sourceFile.addTypeAlias({
41
+ name: "AllDocumentTypes",
42
+ type: config.customTypeModels.map((customTypeModel) => buildTypeName.buildTypeName(customTypeModel.id, "Document")).join(" | "),
43
+ isExported: true
44
+ });
45
+ }
46
+ }
47
+ if (config.sharedSliceModels) {
48
+ for (const model of config.sharedSliceModels) {
49
+ addTypeAliasForSharedSlice.addTypeAliasForSharedSlice({
50
+ model,
51
+ sourceFile,
52
+ fieldConfigs: config.fieldConfigs || {}
53
+ });
54
+ }
55
+ }
56
+ if (((_a = config.clientIntegration) == null ? void 0 : _a.includeCreateClientInterface) || ((_b = config.clientIntegration) == null ? void 0 : _b.includeContentNamespace)) {
57
+ sourceFile.addImportDeclaration({
58
+ moduleSpecifier: "@prismicio/client",
59
+ namespaceImport: "prismic",
60
+ isTypeOnly: true
61
+ });
62
+ const clientModuleDeclaration = sourceFile.addModule({
63
+ name: '"@prismicio/client"',
64
+ hasDeclareKeyword: true,
65
+ declarationKind: tsMorph.ModuleDeclarationKind.Module
66
+ });
67
+ if (config.clientIntegration.includeCreateClientInterface) {
68
+ clientModuleDeclaration.addInterface({
69
+ name: "CreateClient",
70
+ callSignatures: [
71
+ {
72
+ parameters: [
73
+ {
74
+ name: "repositoryNameOrEndpoint",
75
+ type: "string"
76
+ },
77
+ {
78
+ name: "options",
79
+ type: "prismic.ClientConfig",
80
+ hasQuestionToken: true
81
+ }
82
+ ],
83
+ returnType: (((_c = config.customTypeModels) == null ? void 0 : _c.length) || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
84
+ }
85
+ ]
86
+ });
87
+ }
88
+ if (config.clientIntegration.includeContentNamespace) {
89
+ const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
90
+ name: "Content",
91
+ declarationKind: tsMorph.ModuleDeclarationKind.Namespace
92
+ });
93
+ const exportSymbols = sourceFile.getExportSymbols().filter((exportSymbol) => {
94
+ return exportSymbol.getName() !== simplifyTypeAlias.getName();
95
+ });
96
+ contentNamespaceDeclaration.addExportDeclaration({
97
+ isTypeOnly: true,
98
+ namedExports: exportSymbols.map((exportSymbol) => {
99
+ return {
100
+ name: exportSymbol.getName()
101
+ };
102
+ })
103
+ });
104
+ }
105
+ }
106
+ return getSourceFileText.getSourceFileText(sourceFile);
107
+ };
108
+ exports.generateTypes = generateTypes;
109
+ //# sourceMappingURL=generateTypes.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateTypes.cjs","sources":["../../src/generateTypes.ts"],"sourcesContent":["import type { CustomTypeModel, SharedSliceModel } from \"@prismicio/client\";\nimport { ModuleDeclarationKind, Project } from \"ts-morph\";\n\nimport { addTypeAliasForCustomType } from \"./lib/addTypeAliasForCustomType\";\nimport { addTypeAliasForSharedSlice } from \"./lib/addTypeAliasForSharedSlice\";\nimport { buildTypeName } from \"./lib/buildTypeName\";\nimport { getSourceFileText } from \"./lib/getSourceFileText\";\n\nimport { FieldConfigs } from \"./types\";\n\nimport { BLANK_LINE_IDENTIFIER } from \"./constants\";\n\nexport type TypesProvider = \"@prismicio/client\" | \"@prismicio/types\";\n\nexport type GenerateTypesConfig = {\n\tcustomTypeModels?: CustomTypeModel[];\n\tsharedSliceModels?: SharedSliceModel[];\n\tlocaleIDs?: string[];\n\tfieldConfigs?: FieldConfigs;\n\ttypesProvider?: TypesProvider;\n\tclientIntegration?: {\n\t\tincludeCreateClientInterface?: boolean;\n\t\tincludeContentNamespace?: boolean;\n\t};\n};\n\nexport const generateTypes = (config: GenerateTypesConfig = {}) => {\n\tconst project = new Project({\n\t\tuseInMemoryFileSystem: true,\n\t});\n\n\tconst sourceFile = project.createSourceFile(\"types.d.ts\");\n\n\tsourceFile.addImportDeclaration({\n\t\tmoduleSpecifier: config.typesProvider || \"@prismicio/types\",\n\t\tnamespaceImport: \"prismic\",\n\t\tisTypeOnly: true,\n\t});\n\n\tsourceFile.addStatements(BLANK_LINE_IDENTIFIER);\n\n\tconst simplifyTypeAlias = sourceFile.addTypeAlias({\n\t\tname: \"Simplify\",\n\t\ttypeParameters: [\n\t\t\t{\n\t\t\t\tname: \"T\",\n\t\t\t},\n\t\t],\n\t\ttype: `{ [KeyType in keyof T]: T[KeyType] }`,\n\t});\n\n\tif (config.customTypeModels) {\n\t\tfor (const model of config.customTypeModels) {\n\t\t\taddTypeAliasForCustomType({\n\t\t\t\tmodel,\n\t\t\t\tsourceFile,\n\t\t\t\tlocaleIDs: config.localeIDs || [],\n\t\t\t\tfieldConfigs: config.fieldConfigs || {},\n\t\t\t});\n\t\t}\n\n\t\tif (config.customTypeModels.length > 0) {\n\t\t\tsourceFile.addTypeAlias({\n\t\t\t\tname: \"AllDocumentTypes\",\n\t\t\t\ttype: config.customTypeModels\n\t\t\t\t\t.map((customTypeModel) =>\n\t\t\t\t\t\tbuildTypeName(customTypeModel.id, \"Document\"),\n\t\t\t\t\t)\n\t\t\t\t\t.join(\" | \"),\n\t\t\t\tisExported: true,\n\t\t\t});\n\t\t}\n\t}\n\n\tif (config.sharedSliceModels) {\n\t\tfor (const model of config.sharedSliceModels) {\n\t\t\taddTypeAliasForSharedSlice({\n\t\t\t\tmodel,\n\t\t\t\tsourceFile,\n\t\t\t\tfieldConfigs: config.fieldConfigs || {},\n\t\t\t});\n\t\t}\n\t}\n\n\tif (\n\t\tconfig.clientIntegration?.includeCreateClientInterface ||\n\t\tconfig.clientIntegration?.includeContentNamespace\n\t) {\n\t\tsourceFile.addImportDeclaration({\n\t\t\tmoduleSpecifier: \"@prismicio/client\",\n\t\t\tnamespaceImport: \"prismic\",\n\t\t\tisTypeOnly: true,\n\t\t});\n\n\t\tconst clientModuleDeclaration = sourceFile.addModule({\n\t\t\tname: '\"@prismicio/client\"',\n\t\t\thasDeclareKeyword: true,\n\t\t\tdeclarationKind: ModuleDeclarationKind.Module,\n\t\t});\n\n\t\tif (config.clientIntegration.includeCreateClientInterface) {\n\t\t\tclientModuleDeclaration.addInterface({\n\t\t\t\tname: \"CreateClient\",\n\t\t\t\tcallSignatures: [\n\t\t\t\t\t{\n\t\t\t\t\t\tparameters: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: \"repositoryNameOrEndpoint\",\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: \"options\",\n\t\t\t\t\t\t\t\ttype: \"prismic.ClientConfig\",\n\t\t\t\t\t\t\t\thasQuestionToken: true,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\treturnType:\n\t\t\t\t\t\t\t(config.customTypeModels?.length || 0) > 0\n\t\t\t\t\t\t\t\t? \"prismic.Client<AllDocumentTypes>\"\n\t\t\t\t\t\t\t\t: \"prismic.Client\",\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t});\n\t\t}\n\n\t\tif (config.clientIntegration.includeContentNamespace) {\n\t\t\tconst contentNamespaceDeclaration = clientModuleDeclaration.addModule({\n\t\t\t\tname: \"Content\",\n\t\t\t\tdeclarationKind: ModuleDeclarationKind.Namespace,\n\t\t\t});\n\n\t\t\tconst exportSymbols = sourceFile\n\t\t\t\t.getExportSymbols()\n\t\t\t\t.filter((exportSymbol) => {\n\t\t\t\t\t// The Simplify utility type should not\n\t\t\t\t\t// be exported, but it is included in\n\t\t\t\t\t// `getExportSymbols()`'s result.\n\t\t\t\t\treturn exportSymbol.getName() !== simplifyTypeAlias.getName();\n\t\t\t\t});\n\n\t\t\tcontentNamespaceDeclaration.addExportDeclaration({\n\t\t\t\tisTypeOnly: true,\n\t\t\t\tnamedExports: exportSymbols.map((exportSymbol) => {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tname: exportSymbol.getName(),\n\t\t\t\t\t};\n\t\t\t\t}),\n\t\t\t});\n\t\t}\n\t}\n\n\treturn getSourceFileText(sourceFile);\n};\n"],"names":["Project","BLANK_LINE_IDENTIFIER","addTypeAliasForCustomType","buildTypeName","addTypeAliasForSharedSlice","ModuleDeclarationKind","getSourceFileText"],"mappings":";;;;;;;;AA0BO,MAAM,gBAAgB,CAAC,SAA8B,OAAM;;AAC3D,QAAA,UAAU,IAAIA,gBAAQ;AAAA,IAC3B,uBAAuB;AAAA,EAAA,CACvB;AAEK,QAAA,aAAa,QAAQ,iBAAiB,YAAY;AAExD,aAAW,qBAAqB;AAAA,IAC/B,iBAAiB,OAAO,iBAAiB;AAAA,IACzC,iBAAiB;AAAA,IACjB,YAAY;AAAA,EAAA,CACZ;AAED,aAAW,cAAcC,UAAAA,qBAAqB;AAExC,QAAA,oBAAoB,WAAW,aAAa;AAAA,IACjD,MAAM;AAAA,IACN,gBAAgB;AAAA,MACf;AAAA,QACC,MAAM;AAAA,MACN;AAAA,IACD;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,mBAAyB,WAAU,KAAK,IACtC,qCACA;AAAA,UACJ;AAAA,QACD;AAAA,MAAA,CACD;AAAA,IACD;AAEG,QAAA,OAAO,kBAAkB,yBAAyB;AAC/C,YAAA,8BAA8B,wBAAwB,UAAU;AAAA,QACrE,MAAM;AAAA,QACN,iBAAiBA,QAAsB,sBAAA;AAAA,MAAA,CACvC;AAED,YAAM,gBAAgB,WACpB,iBACA,EAAA,OAAO,CAAC,iBAAgB;AAIxB,eAAO,aAAa,QAAA,MAAc,kBAAkB,QAAO;AAAA,MAAA,CAC3D;AAEF,kCAA4B,qBAAqB;AAAA,QAChD,YAAY;AAAA,QACZ,cAAc,cAAc,IAAI,CAAC,iBAAgB;AACzC,iBAAA;AAAA,YACN,MAAM,aAAa,QAAS;AAAA,UAAA;AAAA,SAE7B;AAAA,MAAA,CACD;AAAA,IACD;AAAA,EACD;AAED,SAAOC,kBAAAA,kBAAkB,UAAU;AACpC;;"}
@@ -0,0 +1,15 @@
1
+ import type { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
2
+ import { FieldConfigs } from "./types";
3
+ export type TypesProvider = "@prismicio/client" | "@prismicio/types";
4
+ export type GenerateTypesConfig = {
5
+ customTypeModels?: CustomTypeModel[];
6
+ sharedSliceModels?: SharedSliceModel[];
7
+ localeIDs?: string[];
8
+ fieldConfigs?: FieldConfigs;
9
+ typesProvider?: TypesProvider;
10
+ clientIntegration?: {
11
+ includeCreateClientInterface?: boolean;
12
+ includeContentNamespace?: boolean;
13
+ };
14
+ };
15
+ export declare const generateTypes: (config?: GenerateTypesConfig) => string;
@@ -0,0 +1,109 @@
1
+ import { Project, ModuleDeclarationKind } from "ts-morph";
2
+ import { addTypeAliasForCustomType } from "./lib/addTypeAliasForCustomType.js";
3
+ import { addTypeAliasForSharedSlice } from "./lib/addTypeAliasForSharedSlice.js";
4
+ import { buildTypeName } from "./lib/buildTypeName.js";
5
+ import { getSourceFileText } from "./lib/getSourceFileText.js";
6
+ import { BLANK_LINE_IDENTIFIER } from "./constants.js";
7
+ const generateTypes = (config = {}) => {
8
+ var _a, _b, _c;
9
+ const project = new Project({
10
+ useInMemoryFileSystem: true
11
+ });
12
+ const sourceFile = project.createSourceFile("types.d.ts");
13
+ sourceFile.addImportDeclaration({
14
+ moduleSpecifier: config.typesProvider || "@prismicio/types",
15
+ namespaceImport: "prismic",
16
+ isTypeOnly: true
17
+ });
18
+ sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
19
+ const simplifyTypeAlias = sourceFile.addTypeAlias({
20
+ name: "Simplify",
21
+ typeParameters: [
22
+ {
23
+ name: "T"
24
+ }
25
+ ],
26
+ type: `{ [KeyType in keyof T]: T[KeyType] }`
27
+ });
28
+ if (config.customTypeModels) {
29
+ for (const model of config.customTypeModels) {
30
+ addTypeAliasForCustomType({
31
+ model,
32
+ sourceFile,
33
+ localeIDs: config.localeIDs || [],
34
+ fieldConfigs: config.fieldConfigs || {}
35
+ });
36
+ }
37
+ if (config.customTypeModels.length > 0) {
38
+ sourceFile.addTypeAlias({
39
+ name: "AllDocumentTypes",
40
+ type: config.customTypeModels.map((customTypeModel) => buildTypeName(customTypeModel.id, "Document")).join(" | "),
41
+ isExported: true
42
+ });
43
+ }
44
+ }
45
+ if (config.sharedSliceModels) {
46
+ for (const model of config.sharedSliceModels) {
47
+ addTypeAliasForSharedSlice({
48
+ model,
49
+ sourceFile,
50
+ fieldConfigs: config.fieldConfigs || {}
51
+ });
52
+ }
53
+ }
54
+ if (((_a = config.clientIntegration) == null ? void 0 : _a.includeCreateClientInterface) || ((_b = config.clientIntegration) == null ? void 0 : _b.includeContentNamespace)) {
55
+ sourceFile.addImportDeclaration({
56
+ moduleSpecifier: "@prismicio/client",
57
+ namespaceImport: "prismic",
58
+ isTypeOnly: true
59
+ });
60
+ const clientModuleDeclaration = sourceFile.addModule({
61
+ name: '"@prismicio/client"',
62
+ hasDeclareKeyword: true,
63
+ declarationKind: ModuleDeclarationKind.Module
64
+ });
65
+ if (config.clientIntegration.includeCreateClientInterface) {
66
+ clientModuleDeclaration.addInterface({
67
+ name: "CreateClient",
68
+ callSignatures: [
69
+ {
70
+ parameters: [
71
+ {
72
+ name: "repositoryNameOrEndpoint",
73
+ type: "string"
74
+ },
75
+ {
76
+ name: "options",
77
+ type: "prismic.ClientConfig",
78
+ hasQuestionToken: true
79
+ }
80
+ ],
81
+ returnType: (((_c = config.customTypeModels) == null ? void 0 : _c.length) || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
82
+ }
83
+ ]
84
+ });
85
+ }
86
+ if (config.clientIntegration.includeContentNamespace) {
87
+ const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
88
+ name: "Content",
89
+ declarationKind: ModuleDeclarationKind.Namespace
90
+ });
91
+ const exportSymbols = sourceFile.getExportSymbols().filter((exportSymbol) => {
92
+ return exportSymbol.getName() !== simplifyTypeAlias.getName();
93
+ });
94
+ contentNamespaceDeclaration.addExportDeclaration({
95
+ isTypeOnly: true,
96
+ namedExports: exportSymbols.map((exportSymbol) => {
97
+ return {
98
+ name: exportSymbol.getName()
99
+ };
100
+ })
101
+ });
102
+ }
103
+ }
104
+ return getSourceFileText(sourceFile);
105
+ };
106
+ export {
107
+ generateTypes
108
+ };
109
+ //# sourceMappingURL=generateTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateTypes.js","sources":["../../src/generateTypes.ts"],"sourcesContent":["import type { CustomTypeModel, SharedSliceModel } from \"@prismicio/client\";\nimport { ModuleDeclarationKind, Project } from \"ts-morph\";\n\nimport { addTypeAliasForCustomType } from \"./lib/addTypeAliasForCustomType\";\nimport { addTypeAliasForSharedSlice } from \"./lib/addTypeAliasForSharedSlice\";\nimport { buildTypeName } from \"./lib/buildTypeName\";\nimport { getSourceFileText } from \"./lib/getSourceFileText\";\n\nimport { FieldConfigs } from \"./types\";\n\nimport { BLANK_LINE_IDENTIFIER } from \"./constants\";\n\nexport type TypesProvider = \"@prismicio/client\" | \"@prismicio/types\";\n\nexport type GenerateTypesConfig = {\n\tcustomTypeModels?: CustomTypeModel[];\n\tsharedSliceModels?: SharedSliceModel[];\n\tlocaleIDs?: string[];\n\tfieldConfigs?: FieldConfigs;\n\ttypesProvider?: TypesProvider;\n\tclientIntegration?: {\n\t\tincludeCreateClientInterface?: boolean;\n\t\tincludeContentNamespace?: boolean;\n\t};\n};\n\nexport const generateTypes = (config: GenerateTypesConfig = {}) => {\n\tconst project = new Project({\n\t\tuseInMemoryFileSystem: true,\n\t});\n\n\tconst sourceFile = project.createSourceFile(\"types.d.ts\");\n\n\tsourceFile.addImportDeclaration({\n\t\tmoduleSpecifier: config.typesProvider || \"@prismicio/types\",\n\t\tnamespaceImport: \"prismic\",\n\t\tisTypeOnly: true,\n\t});\n\n\tsourceFile.addStatements(BLANK_LINE_IDENTIFIER);\n\n\tconst simplifyTypeAlias = sourceFile.addTypeAlias({\n\t\tname: \"Simplify\",\n\t\ttypeParameters: [\n\t\t\t{\n\t\t\t\tname: \"T\",\n\t\t\t},\n\t\t],\n\t\ttype: `{ [KeyType in keyof T]: T[KeyType] }`,\n\t});\n\n\tif (config.customTypeModels) {\n\t\tfor (const model of config.customTypeModels) {\n\t\t\taddTypeAliasForCustomType({\n\t\t\t\tmodel,\n\t\t\t\tsourceFile,\n\t\t\t\tlocaleIDs: config.localeIDs || [],\n\t\t\t\tfieldConfigs: config.fieldConfigs || {},\n\t\t\t});\n\t\t}\n\n\t\tif (config.customTypeModels.length > 0) {\n\t\t\tsourceFile.addTypeAlias({\n\t\t\t\tname: \"AllDocumentTypes\",\n\t\t\t\ttype: config.customTypeModels\n\t\t\t\t\t.map((customTypeModel) =>\n\t\t\t\t\t\tbuildTypeName(customTypeModel.id, \"Document\"),\n\t\t\t\t\t)\n\t\t\t\t\t.join(\" | \"),\n\t\t\t\tisExported: true,\n\t\t\t});\n\t\t}\n\t}\n\n\tif (config.sharedSliceModels) {\n\t\tfor (const model of config.sharedSliceModels) {\n\t\t\taddTypeAliasForSharedSlice({\n\t\t\t\tmodel,\n\t\t\t\tsourceFile,\n\t\t\t\tfieldConfigs: config.fieldConfigs || {},\n\t\t\t});\n\t\t}\n\t}\n\n\tif (\n\t\tconfig.clientIntegration?.includeCreateClientInterface ||\n\t\tconfig.clientIntegration?.includeContentNamespace\n\t) {\n\t\tsourceFile.addImportDeclaration({\n\t\t\tmoduleSpecifier: \"@prismicio/client\",\n\t\t\tnamespaceImport: \"prismic\",\n\t\t\tisTypeOnly: true,\n\t\t});\n\n\t\tconst clientModuleDeclaration = sourceFile.addModule({\n\t\t\tname: '\"@prismicio/client\"',\n\t\t\thasDeclareKeyword: true,\n\t\t\tdeclarationKind: ModuleDeclarationKind.Module,\n\t\t});\n\n\t\tif (config.clientIntegration.includeCreateClientInterface) {\n\t\t\tclientModuleDeclaration.addInterface({\n\t\t\t\tname: \"CreateClient\",\n\t\t\t\tcallSignatures: [\n\t\t\t\t\t{\n\t\t\t\t\t\tparameters: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: \"repositoryNameOrEndpoint\",\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: \"options\",\n\t\t\t\t\t\t\t\ttype: \"prismic.ClientConfig\",\n\t\t\t\t\t\t\t\thasQuestionToken: true,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\treturnType:\n\t\t\t\t\t\t\t(config.customTypeModels?.length || 0) > 0\n\t\t\t\t\t\t\t\t? \"prismic.Client<AllDocumentTypes>\"\n\t\t\t\t\t\t\t\t: \"prismic.Client\",\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t});\n\t\t}\n\n\t\tif (config.clientIntegration.includeContentNamespace) {\n\t\t\tconst contentNamespaceDeclaration = clientModuleDeclaration.addModule({\n\t\t\t\tname: \"Content\",\n\t\t\t\tdeclarationKind: ModuleDeclarationKind.Namespace,\n\t\t\t});\n\n\t\t\tconst exportSymbols = sourceFile\n\t\t\t\t.getExportSymbols()\n\t\t\t\t.filter((exportSymbol) => {\n\t\t\t\t\t// The Simplify utility type should not\n\t\t\t\t\t// be exported, but it is included in\n\t\t\t\t\t// `getExportSymbols()`'s result.\n\t\t\t\t\treturn exportSymbol.getName() !== simplifyTypeAlias.getName();\n\t\t\t\t});\n\n\t\t\tcontentNamespaceDeclaration.addExportDeclaration({\n\t\t\t\tisTypeOnly: true,\n\t\t\t\tnamedExports: exportSymbols.map((exportSymbol) => {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tname: exportSymbol.getName(),\n\t\t\t\t\t};\n\t\t\t\t}),\n\t\t\t});\n\t\t}\n\t}\n\n\treturn getSourceFileText(sourceFile);\n};\n"],"names":[],"mappings":";;;;;;AA0BO,MAAM,gBAAgB,CAAC,SAA8B,OAAM;;AAC3D,QAAA,UAAU,IAAI,QAAQ;AAAA,IAC3B,uBAAuB;AAAA,EAAA,CACvB;AAEK,QAAA,aAAa,QAAQ,iBAAiB,YAAY;AAExD,aAAW,qBAAqB;AAAA,IAC/B,iBAAiB,OAAO,iBAAiB;AAAA,IACzC,iBAAiB;AAAA,IACjB,YAAY;AAAA,EAAA,CACZ;AAED,aAAW,cAAc,qBAAqB;AAExC,QAAA,oBAAoB,WAAW,aAAa;AAAA,IACjD,MAAM;AAAA,IACN,gBAAgB;AAAA,MACf;AAAA,QACC,MAAM;AAAA,MACN;AAAA,IACD;AAAA,IACD,MAAM;AAAA,EAAA,CACN;AAED,MAAI,OAAO,kBAAkB;AACjB,eAAA,SAAS,OAAO,kBAAkB;AAClB,gCAAA;AAAA,QACzB;AAAA,QACA;AAAA,QACA,WAAW,OAAO,aAAa,CAAE;AAAA,QACjC,cAAc,OAAO,gBAAgB,CAAE;AAAA,MAAA,CACvC;AAAA,IACD;AAEG,QAAA,OAAO,iBAAiB,SAAS,GAAG;AACvC,iBAAW,aAAa;AAAA,QACvB,MAAM;AAAA,QACN,MAAM,OAAO,iBACX,IAAI,CAAC,oB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