Skip to content

Commit 5d9a674

Browse files
committed
fix: port headers changes
1 parent 54baedd commit 5d9a674

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isDefined,
2424
isHttpMethod,
2525
titleCase,
26+
upperFirst,
2627
} from "../../core/utils"
2728
import type {OpenapiTypescriptGeneratorConfig} from "../../templates.types"
2829
import {CompilationUnit, type ICompilable} from "../common/compilation-units"
@@ -130,6 +131,15 @@ export class ServerRouterBuilder implements ICompilable {
130131
: undefined
131132
let queryParamsType = "void"
132133

134+
const headerParams = operation.parameters
135+
.filter((it) => it.in === "header")
136+
.map((it) => ({...it, name: it.name.toLowerCase()}))
137+
const headerSchema = headerParams.length
138+
? schemaBuilder.fromParameters(headerParams)
139+
: undefined
140+
141+
let headerParamsType = "void"
142+
133143
const {requestBodyParameter} = requestBodyAsParameter(operation)
134144
const bodyParamIsRequired = Boolean(requestBodyParameter?.required)
135145
const bodyParamSchema = requestBodyParameter
@@ -165,6 +175,19 @@ export class ServerRouterBuilder implements ICompilable {
165175
this.statements.push(`const ${name} = ${querySchema.toString()}`)
166176
}
167177

178+
if (headerSchema) {
179+
const name = `${operation.operationId}HeaderSchema`
180+
181+
headerParamsType = types.schemaObjectToType(
182+
this.input.loader.addVirtualType(
183+
operation.operationId,
184+
upperFirst(name),
185+
reduceParamsToOpenApiSchema(headerParams),
186+
),
187+
)
188+
this.statements.push(`const ${name} = ${headerSchema.toString()}`)
189+
}
190+
168191
if (bodyParamSchema && requestBodyParameter) {
169192
const name = `${operation.operationId}BodySchema`
170193
bodyParamsType = types.schemaObjectToType(
@@ -246,7 +269,7 @@ export class ServerRouterBuilder implements ICompilable {
246269
(bodyParamsType === "void" || bodyParamIsRequired
247270
? ""
248271
: " | undefined")
249-
}, void>,
272+
}, ${headerParamsType}>,
250273
respond: ${titleCase(operation.operationId)}Responder,
251274
ctx: {request: NextRequest}
252275
) => Promise<KoaRuntimeResponse<unknown>>`,
@@ -276,6 +299,11 @@ export class ServerRouterBuilder implements ICompilable {
276299
bodyParamSchema
277300
? `parseRequestInput(${operation.operationId}BodySchema, await request.json(), RequestInputType.RequestBody)`
278301
: "undefined"
302+
},
303+
headers: ${
304+
headerSchema
305+
? `parseRequestInput(${operation.operationId}HeaderSchema, Reflect.get(ctx.request, "headers"), RequestInputType.RequestHeader)`
306+
: "undefined"
279307
}
280308
}
281309

0 commit comments

Comments
 (0)