Skip to content

Commit eaf41af

Browse files
authored
fix: parametrize responder generation (#325)
extract `koa` specific stuff from `server-operation-builder`
1 parent add7434 commit eaf41af

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

packages/openapi-code-generator/src/typescript/server/server-operation-builder.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,37 @@ export class ServerOperationBuilder {
122122
return `responseValidationFactory([${pairs}], ${defaultResponse?.schema})`
123123
}
124124

125-
responder(): {implementation: string; type: string} {
125+
responder(
126+
responderType: string,
127+
runtimeResponseType: string,
128+
): {
129+
implementation: string
130+
type: string
131+
} {
126132
const {specific, defaultResponse} = this.responseSchemas()
127-
// TODO: figure out what to do about the KoaRuntimeResponse class
133+
128134
const type = intersect(
129135
object([
130136
...specific.map((it) =>
131137
it.isWildCard
132-
? `with${it.statusType}(status: ${it.statusType}): KoaRuntimeResponse<${it.type}>`
133-
: `with${it.statusType}(): KoaRuntimeResponse<${it.type}>`,
138+
? `with${it.statusType}(status: ${it.statusType}): ${runtimeResponseType}<${it.type}>`
139+
: `with${it.statusType}(): ${runtimeResponseType}<${it.type}>`,
134140
),
135141
defaultResponse &&
136-
`withDefault(status: StatusCode): KoaRuntimeResponse<${defaultResponse.type}>`,
142+
`withDefault(status: StatusCode): ${runtimeResponseType}<${defaultResponse.type}>`,
137143
]),
138-
"KoaRuntimeResponder",
144+
responderType,
139145
)
140146

141147
const implementation = object([
142148
...specific.map((it) =>
143149
it.isWildCard
144-
? `with${it.statusType}(status: ${it.statusType}) {return new KoaRuntimeResponse<${it.type}>(status) }`
145-
: `with${it.statusType}() {return new KoaRuntimeResponse<${it.type}>(${it.statusType}) }`,
150+
? `with${it.statusType}(status: ${it.statusType}) {return new ${runtimeResponseType}<${it.type}>(status) }`
151+
: `with${it.statusType}() {return new ${runtimeResponseType}<${it.type}>(${it.statusType}) }`,
146152
),
147153
defaultResponse &&
148-
`withDefault(status: StatusCode) { return new KoaRuntimeResponse<${defaultResponse.type}>(status) }`,
149-
"withStatus(status: StatusCode) { return new KoaRuntimeResponse(status)}",
154+
`withDefault(status: StatusCode) { return new ${runtimeResponseType}<${defaultResponse.type}>(status) }`,
155+
`withStatus(status: StatusCode) { return new ${runtimeResponseType}(status)}`,
150156
])
151157

152158
return {implementation, type}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ export class KoaRouterBuilder extends AbstractRouterBuilder {
9292
}
9393

9494
const responseSchemas = builder.responseSchemas()
95-
const responder = builder.responder()
95+
const responder = builder.responder(
96+
"KoaRuntimeResponder",
97+
"KoaRuntimeResponse",
98+
)
9699

97100
this.operationTypes.push({
98101
operationId: builder.operationId,

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,7 @@ export class KoaServerBuilder implements ICompilable {
1111
) {
1212
this.imports
1313
.from("@nahkies/typescript-koa-runtime/server")
14-
.add(
15-
"startServer",
16-
"ServerConfig",
17-
"Response",
18-
"KoaRuntimeResponse",
19-
"KoaRuntimeResponder",
20-
"StatusCode2xx",
21-
"StatusCode3xx",
22-
"StatusCode4xx",
23-
"StatusCode5xx",
24-
"StatusCode",
25-
)
14+
.add("startServer", "ServerConfig")
2615
}
2716

2817
toString(): string {

0 commit comments

Comments
 (0)