Skip to content

Commit 9bbccad

Browse files
committed
feat: add TS type generation as aliases
1 parent a336644 commit 9bbccad

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Generate versatile [typebox](https://github.com/sinclairzx81/typebox) schemes fr
33

44
> Currently does not support [mongoDB composite types](https://www.prisma.io/docs/orm/prisma-schema/data-model/models#defining-composite-types)
55
6-
> Development is currently on hold, please see [here](https://github.com/m1212e/prismabox/issues/59)
6+
> Development is currently on hold, please see [here](https://github.com/m1212e/prismabox/issues/59)
77
88
Install it in your project,
99
```bash
@@ -22,6 +22,10 @@ generator prismabox {
2222
typeboxImportVariableName = "t"
2323
// you also can specify the dependency from which the above import should happen. This is useful if a package re-exports the typebox package and you would like to use that
2424
typeboxImportDependencyName = "elysia"
25+
// optionally generate TS aliases for each schema: export type MySchema = UnwrapSchema<typeof MySchema>
26+
generateTsTypes = true
27+
// optionally customize the unwrap utility type symbol (imported from the same package as typeboxImportDependencyName)
28+
unwrapSchemaImportName = "UnwrapSchema"
2529
// by default the generated schemes do not allow additional properties. You can allow them by setting this to true
2630
additionalProperties = true
2731
// optionally enable the data model generation. See the data model section below for more info

src/config.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ const configSchema = Type.Object(
1515
* The name of the dependency to import the Type from typebox
1616
*/
1717
typeboxImportDependencyName: Type.String({ default: "@sinclair/typebox" }),
18+
/**
19+
* Whether to generate TypeScript type aliases for each generated schema.
20+
*
21+
* When enabled, every `export const Foo = ...` also emits
22+
* `export type Foo = UnwrapSchema<typeof Foo>`.
23+
*/
24+
generateTsTypes: Type.Boolean({ default: true }),
25+
/**
26+
* The symbol name used to unwrap a schema into a TypeScript type.
27+
* Imported as a type from `typeboxImportDependencyName`.
28+
*/
29+
unwrapSchemaImportName: Type.String({ default: "Type.Static" }),
1830
/**
1931
* Whether to allow additional properties in the generated schemes
2032
*/
@@ -49,7 +61,7 @@ const configSchema = Type.Object(
4961
allowRecursion: Type.Boolean({ default: true }),
5062
/**
5163
* Additional fields to add to the generated schemes (must be valid strings in the context of usage)
52-
* @example
64+
* @example
5365
* ```prisma
5466
* generator prismabox {
5567
provider = "node ./dist/cli.js"

src/model.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,25 @@ export type ProcessedModel = {
2727
function convertModelToStandalone(
2828
input: Pick<ProcessedModel, "name" | "stringRepresentation">,
2929
) {
30-
return `export const ${getConfig().exportedTypePrefix}${input.name} = ${input.stringRepresentation}\n`;
30+
const generatedName = `${getConfig().exportedTypePrefix}${input.name}`;
31+
32+
let exportStr = `export const ${generatedName} = ${input.stringRepresentation}\n`;
33+
34+
if (getConfig().generateTsTypes) {
35+
exportStr += `export type ${generatedName} = ${getConfig().unwrapSchemaImportName}<typeof ${generatedName}>\n`
36+
}
37+
38+
return exportStr;
3139
}
3240

3341
function typepoxImportStatement() {
34-
return `import { ${getConfig().typeboxImportVariableName} } from "${
42+
let imports = getConfig().typeboxImportVariableName
43+
44+
if (getConfig().generateTsTypes) {
45+
imports += `, type ${getConfig().unwrapSchemaImportName}`;
46+
}
47+
48+
return `import { ${imports} } from "${
3549
getConfig().typeboxImportDependencyName
3650
}"\n`;
3751
}

0 commit comments

Comments
 (0)