@@ -17,7 +17,7 @@ import {
1717import { ZodBuilder } from "../common/schema-builders/zod-schema-builder"
1818import type { ServerOperationBuilder } from "../common/server-operation-builder"
1919import { TypeBuilder } from "../common/type-builder"
20- import { intersect , object } from "../common/type-utils"
20+ import { constStatement , intersect , object } from "../common/type-utils"
2121import { buildExport } from "../common/typescript-common"
2222
2323export type ServerSymbols = {
@@ -32,7 +32,6 @@ export type ServerSymbols = {
3232}
3333
3434export class KoaServerRouterBuilder extends AbstractServerRouterBuilder {
35- private readonly statements : string [ ] = [ ]
3635 private readonly operationTypes : {
3736 operationId : string
3837 statements : string [ ]
@@ -86,30 +85,26 @@ export class KoaServerRouterBuilder extends AbstractServerRouterBuilder {
8685 }
8786 }
8887
89- private addParameterSchemaStatement ( symbolName : string , schema : string ) {
90- this . statements . push ( `const ${ symbolName } = ${ schema } ` )
91- }
88+ protected buildOperation ( builder : ServerOperationBuilder ) : string {
89+ const statements : string [ ] = [ ]
9290
93- protected buildOperation ( builder : ServerOperationBuilder ) : void {
9491 const symbols = this . operationSymbolNames ( builder . operationId )
9592 const params = builder . parameters ( symbols )
9693
9794 if ( params . path . schema ) {
98- this . addParameterSchemaStatement ( symbols . paramSchema , params . path . schema )
95+ statements . push ( constStatement ( symbols . paramSchema , params . path . schema ) )
9996 }
10097 if ( params . query . schema ) {
101- this . addParameterSchemaStatement ( symbols . querySchema , params . query . schema )
98+ statements . push ( constStatement ( symbols . querySchema , params . query . schema ) )
10299 }
103100 if ( params . header . schema ) {
104- this . addParameterSchemaStatement (
105- symbols . requestHeaderSchema ,
106- params . header . schema ,
101+ statements . push (
102+ constStatement ( symbols . requestHeaderSchema , params . header . schema ) ,
107103 )
108104 }
109105 if ( params . body . schema ) {
110- this . addParameterSchemaStatement (
111- symbols . requestBodySchema ,
112- params . body . schema ,
106+ statements . push (
107+ constStatement ( symbols . requestBodySchema , params . body . schema ) ,
113108 )
114109 }
115110
@@ -144,7 +139,7 @@ export class KoaServerRouterBuilder extends AbstractServerRouterBuilder {
144139 ] ,
145140 } )
146141
147- this . statements . push ( `
142+ statements . push ( `
148143const ${ symbols . responseBodyValidator } = ${ builder . responseValidator ( ) }
149144
150145router.${ builder . method . toLowerCase ( ) } ('${ symbols . implPropName } ','${ route ( builder . route ) } ', async (ctx, next) => {
@@ -166,6 +161,8 @@ router.${builder.method.toLowerCase()}('${symbols.implPropName}','${route(builde
166161 ctx.status = status
167162 return next();
168163})` )
164+
165+ return statements . join ( "\n\n" )
169166 }
170167
171168 private operationSymbolNames ( operationId : string ) : ServerSymbols {
@@ -218,7 +215,10 @@ router.${builder.method.toLowerCase()}('${symbols.implPropName}','${route(builde
218215 }
219216 }
220217
221- protected buildRouter ( routerName : string ) : string {
218+ protected buildRouter (
219+ routerName : string ,
220+ routerStatements : string [ ] ,
221+ ) : string {
222222 const moduleName = titleCase ( routerName )
223223 const implementationExportName = `${ moduleName } Implementation`
224224 const createRouterExportName = `create${ moduleName } Router`
@@ -231,7 +231,7 @@ ${this.implementationExport(implementationExportName)}
231231export function ${ createRouterExportName } (implementation: ${ implementationExportName } ): KoaRouter {
232232 const router = new KoaRouter()
233233
234- ${ this . statements . join ( "\n\n" ) }
234+ ${ routerStatements . join ( "\n\n" ) }
235235
236236 return router
237237}
0 commit comments