prismic-ts-codegen
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -17,2 +17,5 @@ import { CustomTypeModel, SharedSliceModel } from '@prismicio/types'; | ||
fieldConfigs?: FieldConfigs; | ||
clientIntegration?: { | ||
includeCreateClientInterface?: boolean; | ||
}; | ||
}; | ||
@@ -45,2 +48,22 @@ declare const generateTypes: (config?: GenerateTypesConfig) => string; | ||
/** | ||
* Configuration for automatic `@prismicio/client` integration. | ||
*/ | ||
clientIntegration?: { | ||
/** | ||
* Determines if a `@prismicio/client` integration with automatic typing | ||
* should be included in the output. | ||
* | ||
* If set to `true`, Prismic clients will automatically be typed with the | ||
* generated Custom Types and Slices. | ||
* | ||
* **Note**: If your project queries content from multiple Prismic | ||
* repositories, set `includeCreateClientInterface` to `false` and manually | ||
* provide the generated `AllDocumentTypes` type to `@prismicio/client`'s | ||
* `createClient()` function instead. | ||
* | ||
* @defaultValue `true` | ||
*/ | ||
includeCreateClientInterface?: boolean; | ||
}; | ||
/** | ||
* Configuration for languages for the Prismic repository. | ||
@@ -47,0 +70,0 @@ * |
@@ -1,2 +0,2 @@ | ||
import { Project } from 'ts-morph'; | ||
import { Project, ModuleDeclarationKind } from 'ts-morph'; | ||
import { stripIndent } from 'common-tags'; | ||
@@ -932,2 +932,9 @@ import { CustomTypeModelFieldType, CustomTypeModelLinkSelectType, CustomTypeModelSliceType } from '@prismicio/types'; | ||
} | ||
if (config.customTypeModels.length > 0) { | ||
sourceFile.addTypeAlias({ | ||
name: "AllDocumentTypes", | ||
type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "), | ||
isExported: true | ||
}); | ||
} | ||
} | ||
@@ -943,2 +950,33 @@ if (config.sharedSliceModels) { | ||
} | ||
if (config.clientIntegration?.includeCreateClientInterface) { | ||
sourceFile.addImportDeclaration({ | ||
moduleSpecifier: "@prismicio/client", | ||
namespaceImport: "prismic", | ||
isTypeOnly: true | ||
}); | ||
const clientModuleDeclaration = sourceFile.addModule({ | ||
name: '"@prismicio/client"', | ||
hasDeclareKeyword: true, | ||
declarationKind: ModuleDeclarationKind.Module | ||
}); | ||
clientModuleDeclaration.addInterface({ | ||
name: "CreateClient", | ||
callSignatures: [ | ||
{ | ||
parameters: [ | ||
{ | ||
name: "repositoryNameOrEndpoint", | ||
type: "string" | ||
}, | ||
{ | ||
name: "options", | ||
type: "prismic.ClientConfig", | ||
hasQuestionToken: true | ||
} | ||
], | ||
returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client" | ||
} | ||
] | ||
}); | ||
} | ||
return getSourceFileText(sourceFile); | ||
@@ -945,0 +983,0 @@ }; |
{ | ||
"name": "prismic-ts-codegen", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "An experimental Prismic model-to-TypeScript-type generator", | ||
@@ -84,2 +84,5 @@ "keywords": [ | ||
}, | ||
"optionalDependencies": { | ||
"@prismicio/client": "^6.6" | ||
}, | ||
"engines": { | ||
@@ -86,0 +89,0 @@ "node": ">=12.7.0" |
@@ -87,2 +87,6 @@ # prismic-ts-codegen | ||
clientIntegration: { | ||
includeCreateClientInterface: true, | ||
}, | ||
locales: { | ||
@@ -89,0 +93,0 @@ ids: ["en-us", "fr-fr", "en-gb"], |
@@ -6,3 +6,11 @@ import Joi from "joi"; | ||
export const configSchema = Joi.object<Config>({ | ||
repositoryName: Joi.string(), | ||
repositoryName: Joi.string() | ||
.when("locales.fetchFromRepository", { | ||
is: true, | ||
then: Joi.required(), | ||
}) | ||
.when("models.fetchFromRepository", { | ||
is: true, | ||
then: Joi.required(), | ||
}), | ||
accessToken: Joi.string(), | ||
@@ -13,2 +21,6 @@ customTypesAPIToken: Joi.string(), | ||
clientIntegration: { | ||
includeCreateClientInterface: Joi.boolean, | ||
}, | ||
locales: Joi.alternatives( | ||
@@ -23,5 +35,5 @@ Joi.array().items(Joi.string().required()), | ||
models: Joi.alternatives( | ||
Joi.array().items(Joi.string().required()), | ||
Joi.array().items(Joi.string()), | ||
Joi.object({ | ||
files: Joi.array().items(Joi.string().required()), | ||
files: Joi.array().items(Joi.string()), | ||
fetchFromRepository: Joi.boolean(), | ||
@@ -42,27 +54,2 @@ }), | ||
}), | ||
}) | ||
.when( | ||
Joi.object({ | ||
locales: Joi.object({ | ||
fetchFromRepository: Joi.valid(true), | ||
}), | ||
}), | ||
{ | ||
then: Joi.object({ | ||
repositoryName: Joi.required(), | ||
}), | ||
}, | ||
) | ||
.when( | ||
Joi.object({ | ||
models: Joi.object({ | ||
fetchFromRepository: Joi.valid(true), | ||
}), | ||
}), | ||
{ | ||
then: Joi.object({ | ||
repositoryName: Joi.required(), | ||
customTypesAPIToken: Joi.required(), | ||
}), | ||
}, | ||
); | ||
}); |
@@ -104,2 +104,4 @@ import { existsSync, writeFileSync } from "fs"; | ||
const hasCustomTypeModels = customTypeModels.length > 0; | ||
const types = generateTypes({ | ||
@@ -109,6 +111,22 @@ customTypeModels, | ||
localeIDs, | ||
clientIntegration: { | ||
includeCreateClientInterface: hasCustomTypeModels | ||
? config.clientIntegration?.includeCreateClientInterface ?? true | ||
: false, | ||
}, | ||
}); | ||
if ( | ||
config.clientIntegration?.includeCreateClientInterface && | ||
!hasCustomTypeModels | ||
) { | ||
console.info( | ||
"[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.", | ||
); | ||
} | ||
if (config.output) { | ||
writeFileSync(resolvePath(config.output), types); | ||
console.info(`\nGenerated types in: ${config.output}`); | ||
} else { | ||
@@ -115,0 +133,0 @@ process.stdout.write(types + "\n"); |
@@ -29,2 +29,23 @@ /** | ||
/** | ||
* Configuration for automatic `@prismicio/client` integration. | ||
*/ | ||
clientIntegration?: { | ||
/** | ||
* Determines if a `@prismicio/client` integration with automatic typing | ||
* should be included in the output. | ||
* | ||
* If set to `true`, Prismic clients will automatically be typed with the | ||
* generated Custom Types and Slices. | ||
* | ||
* **Note**: If your project queries content from multiple Prismic | ||
* repositories, set `includeCreateClientInterface` to `false` and manually | ||
* provide the generated `AllDocumentTypes` type to `@prismicio/client`'s | ||
* `createClient()` function instead. | ||
* | ||
* @defaultValue `true` | ||
*/ | ||
includeCreateClientInterface?: boolean; | ||
}; | ||
/** | ||
* Configuration for languages for the Prismic repository. | ||
@@ -31,0 +52,0 @@ * |
@@ -1,2 +0,2 @@ | ||
import { Project } from "ts-morph"; | ||
import { Project, ModuleDeclarationKind } from "ts-morph"; | ||
import type { CustomTypeModel, SharedSliceModel } from "@prismicio/types"; | ||
@@ -9,2 +9,3 @@ | ||
import { FieldConfigs } from "./types"; | ||
import { pascalCase } from "./lib/pascalCase"; | ||
@@ -16,2 +17,5 @@ export type GenerateTypesConfig = { | ||
fieldConfigs?: FieldConfigs; | ||
clientIntegration?: { | ||
includeCreateClientInterface?: boolean; | ||
}; | ||
}; | ||
@@ -54,2 +58,14 @@ | ||
} | ||
if (config.customTypeModels.length > 0) { | ||
sourceFile.addTypeAlias({ | ||
name: "AllDocumentTypes", | ||
type: config.customTypeModels | ||
.map((customTypeModel) => | ||
pascalCase(`${customTypeModel.id} Document`), | ||
) | ||
.join(" | "), | ||
isExported: true, | ||
}); | ||
} | ||
} | ||
@@ -67,3 +83,40 @@ | ||
if (config.clientIntegration?.includeCreateClientInterface) { | ||
sourceFile.addImportDeclaration({ | ||
moduleSpecifier: "@prismicio/client", | ||
namespaceImport: "prismic", | ||
isTypeOnly: true, | ||
}); | ||
const clientModuleDeclaration = sourceFile.addModule({ | ||
name: '"@prismicio/client"', | ||
hasDeclareKeyword: true, | ||
declarationKind: ModuleDeclarationKind.Module, | ||
}); | ||
clientModuleDeclaration.addInterface({ | ||
name: "CreateClient", | ||
callSignatures: [ | ||
{ | ||
parameters: [ | ||
{ | ||
name: "repositoryNameOrEndpoint", | ||
type: "string", | ||
}, | ||
{ | ||
name: "options", | ||
type: "prismic.ClientConfig", | ||
hasQuestionToken: true, | ||
}, | ||
], | ||
returnType: | ||
(config.customTypeModels?.length || 0) > 0 | ||
? "prismic.Client<AllDocumentTypes>" | ||
: "prismic.Client", | ||
}, | ||
], | ||
}); | ||
} | ||
return getSourceFileText(sourceFile); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
325053
5087
184
13