Skip to content

Commit 04ef171

Browse files
committed
refactor: move things around
1 parent 3619ee3 commit 04ef171

3 files changed

Lines changed: 30 additions & 21 deletions

File tree

packages/openapi-code-generator/src/typescript/common/abstract-server-router-builder.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {ServerOperationBuilder} from "./server-operation-builder"
77
import type {TypeBuilder} from "./type-builder"
88

99
export abstract class AbstractServerRouterBuilder implements ICompilable {
10+
private readonly statements: string[] = []
11+
1012
protected constructor(
1113
public readonly filename: string,
1214
public readonly name: string,
@@ -25,17 +27,21 @@ export abstract class AbstractServerRouterBuilder implements ICompilable {
2527
this.types,
2628
this.schemaBuilder,
2729
)
28-
this.buildOperation(builder)
30+
const result = this.buildOperation(builder)
31+
this.statements.push(result)
2932
}
3033

3134
protected abstract buildImports(): void
3235

33-
protected abstract buildOperation(builder: ServerOperationBuilder): void
36+
protected abstract buildOperation(builder: ServerOperationBuilder): string
3437

35-
protected abstract buildRouter(routerName: string): string
38+
protected abstract buildRouter(
39+
routerName: string,
40+
statements: string[],
41+
): string
3642

3743
toString(): string {
38-
return this.buildRouter(this.name)
44+
return this.buildRouter(this.name, this.statements)
3945
}
4046

4147
toCompilationUnit(): CompilationUnit {

packages/openapi-code-generator/src/typescript/common/type-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export const object = wrap((properties: MaybeString[]): string => {
7979
return `{\n${definedProperties.join(",\n")}\n}`
8080
})
8181

82+
export const constStatement = (name: string, value: string): string =>
83+
`const ${name} = ${value}`
84+
8285
export const array = (type: string): string => `(${type})[]`
8386

8487
export const coerceToString = (it: string | number): string => it.toString()

packages/openapi-code-generator/src/typescript/typescript-koa/typescript-koa.generator.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import {ZodBuilder} from "../common/schema-builders/zod-schema-builder"
1818
import type {ServerOperationBuilder} from "../common/server-operation-builder"
1919
import {TypeBuilder} from "../common/type-builder"
20-
import {intersect, object} from "../common/type-utils"
20+
import {constStatement, intersect, object} from "../common/type-utils"
2121
import {buildExport} from "../common/typescript-common"
2222

2323
export type ServerSymbols = {
@@ -32,7 +32,6 @@ export type ServerSymbols = {
3232
}
3333

3434
export 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(`
148143
const ${symbols.responseBodyValidator} = ${builder.responseValidator()}
149144
150145
router.${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)}
231231
export 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

Comments
 (0)