@@ -21,6 +21,7 @@ import {
2121 isHttpMethod ,
2222 isTruthy ,
2323 titleCase ,
24+ upperFirst ,
2425} from "../../core/utils"
2526import type { OpenapiTypescriptGeneratorConfig } from "../../templates.types"
2627import { CompilationUnit , type ICompilable } from "../common/compilation-units"
@@ -128,6 +129,15 @@ export class ServerRouterBuilder implements ICompilable {
128129 : undefined
129130 let queryParamsType = "void"
130131
132+ const headerParams = operation . parameters
133+ . filter ( ( it ) => it . in === "header" )
134+ . map ( ( it ) => ( { ...it , name : it . name . toLowerCase ( ) } ) )
135+ const headerSchema = headerParams . length
136+ ? schemaBuilder . fromParameters ( headerParams )
137+ : undefined
138+
139+ let headerParamsType = "void"
140+
131141 const { requestBodyParameter} = requestBodyAsParameter ( operation )
132142 const bodyParamIsRequired = Boolean ( requestBodyParameter ?. required )
133143 const bodyParamSchema = requestBodyParameter
@@ -163,6 +173,19 @@ export class ServerRouterBuilder implements ICompilable {
163173 this . statements . push ( `const ${ name } = ${ querySchema . toString ( ) } ` )
164174 }
165175
176+ if ( headerSchema ) {
177+ const name = `${ operation . operationId } HeaderSchema`
178+
179+ headerParamsType = types . schemaObjectToType (
180+ this . input . loader . addVirtualType (
181+ operation . operationId ,
182+ upperFirst ( name ) ,
183+ reduceParamsToOpenApiSchema ( headerParams ) ,
184+ ) ,
185+ )
186+ this . statements . push ( `const ${ name } = ${ headerSchema . toString ( ) } ` )
187+ }
188+
166189 if ( bodyParamSchema && requestBodyParameter ) {
167190 const name = `${ operation . operationId } BodySchema`
168191 bodyParamsType = types . schemaObjectToType (
@@ -244,7 +267,7 @@ export class ServerRouterBuilder implements ICompilable {
244267 ( bodyParamsType === "void" || bodyParamIsRequired
245268 ? ""
246269 : " | undefined" )
247- } , void >,
270+ } , ${ headerParamsType } >,
248271 respond: ${ titleCase ( operation . operationId ) } Responder,
249272 ctx: {request: NextRequest}
250273 ) => Promise<KoaRuntimeResponse<unknown>>` ,
@@ -274,6 +297,11 @@ export class ServerRouterBuilder implements ICompilable {
274297 bodyParamSchema
275298 ? `parseRequestInput(${ operation . operationId } BodySchema, await request.json(), RequestInputType.RequestBody)`
276299 : "undefined"
300+ } ,
301+ headers: ${
302+ headerSchema
303+ ? `parseRequestInput(${ operation . operationId } HeaderSchema, Reflect.get(ctx.request, "headers"), RequestInputType.RequestHeader)`
304+ : "undefined"
277305 }
278306 }
279307
0 commit comments