|
1 | 1 | import * as classes from '../type/classes'; |
2 | 2 | import {TypeSystem} from './TypeSystem'; |
| 3 | +import {toText} from '../typescript/toText'; |
| 4 | +import type * as ts from '../typescript/types'; |
3 | 5 | import type {TypeBuilder} from '../type/TypeBuilder'; |
4 | 6 |
|
5 | 7 | export interface TypeRouterOptions<R extends RoutesBase> { |
@@ -27,7 +29,7 @@ export class TypeRouter<Routes extends RoutesBase> { |
27 | 29 | this.system = options.system; |
28 | 30 | this.t = this.system.t; |
29 | 31 | this.routes = options.routes; |
30 | | - this.system.importTypes(this.routes); |
| 32 | + // this.system.importTypes(this.routes); |
31 | 33 | } |
32 | 34 |
|
33 | 35 | protected merge<Router extends TypeRouter<any>>(router: Router): TypeRouter<Routes & TypeRouterRoutes<Router>> { |
@@ -60,6 +62,47 @@ export class TypeRouter<Routes extends RoutesBase> { |
60 | 62 | this.routes[name] = <any>type; |
61 | 63 | return <any>this; |
62 | 64 | } |
| 65 | + |
| 66 | + public toTypeScriptAst(): ts.TsTypeLiteral { |
| 67 | + const node: ts.TsTypeLiteral = { |
| 68 | + node: 'TypeLiteral', |
| 69 | + members: [], |
| 70 | + }; |
| 71 | + for (const [name, type] of Object.entries(this.routes)) { |
| 72 | + const schema = type.getSchema(); |
| 73 | + const property: ts.TsPropertySignature = { |
| 74 | + node: 'PropertySignature', |
| 75 | + name, |
| 76 | + type: type.toTypeScriptAst(), |
| 77 | + }; |
| 78 | + if (schema.title) property.comment = schema.title; |
| 79 | + node.members.push(property); |
| 80 | + } |
| 81 | + return node; |
| 82 | + } |
| 83 | + |
| 84 | + public toTypeScriptModuleAst(): ts.TsModuleDeclaration { |
| 85 | + const node: ts.TsModuleDeclaration = { |
| 86 | + node: 'ModuleDeclaration', |
| 87 | + name: 'Router', |
| 88 | + export: true, |
| 89 | + statements: [ |
| 90 | + { |
| 91 | + node: 'TypeAliasDeclaration', |
| 92 | + name: 'Routes', |
| 93 | + type: this.toTypeScriptAst(), |
| 94 | + export: true, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }; |
| 98 | + for (const alias of this.system.aliases.values()) node.statements.push({...alias.toTypeScriptAst(), export: true}); |
| 99 | + return node; |
| 100 | + } |
| 101 | + |
| 102 | + public toTypeScript(): string { |
| 103 | + this.system.exportTypes; |
| 104 | + return toText(this.toTypeScriptModuleAst()); |
| 105 | + } |
63 | 106 | } |
64 | 107 |
|
65 | 108 | export type RoutesBase = Record<string, classes.FunctionType<any, any> | classes.FunctionStreamingType<any, any>>; |
|
0 commit comments