@@ -21,6 +21,7 @@ import {
21
21
isHttpMethod ,
22
22
isTruthy ,
23
23
titleCase ,
24
+ upperFirst ,
24
25
} from "../../core/utils"
25
26
import type { OpenapiTypescriptGeneratorConfig } from "../../templates.types"
26
27
import { CompilationUnit , type ICompilable } from "../common/compilation-units"
@@ -128,6 +129,15 @@ export class ServerRouterBuilder implements ICompilable {
128
129
: undefined
129
130
let queryParamsType = "void"
130
131
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
+
131
141
const { requestBodyParameter} = requestBodyAsParameter ( operation )
132
142
const bodyParamIsRequired = Boolean ( requestBodyParameter ?. required )
133
143
const bodyParamSchema = requestBodyParameter
@@ -163,6 +173,19 @@ export class ServerRouterBuilder implements ICompilable {
163
173
this . statements . push ( `const ${ name } = ${ querySchema . toString ( ) } ` )
164
174
}
165
175
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
+
166
189
if ( bodyParamSchema && requestBodyParameter ) {
167
190
const name = `${ operation . operationId } BodySchema`
168
191
bodyParamsType = types . schemaObjectToType (
@@ -244,7 +267,7 @@ export class ServerRouterBuilder implements ICompilable {
244
267
( bodyParamsType === "void" || bodyParamIsRequired
245
268
? ""
246
269
: " | undefined" )
247
- } , void >,
270
+ } , ${ headerParamsType } >,
248
271
respond: ${ titleCase ( operation . operationId ) } Responder,
249
272
ctx: {request: NextRequest}
250
273
) => Promise<KoaRuntimeResponse<unknown>>` ,
@@ -274,6 +297,11 @@ export class ServerRouterBuilder implements ICompilable {
274
297
bodyParamSchema
275
298
? `parseRequestInput(${ operation . operationId } BodySchema, await request.json(), RequestInputType.RequestBody)`
276
299
: "undefined"
300
+ } ,
301
+ headers: ${
302
+ headerSchema
303
+ ? `parseRequestInput(${ operation . operationId } HeaderSchema, Reflect.get(ctx.request, "headers"), RequestInputType.RequestHeader)`
304
+ : "undefined"
277
305
}
278
306
}
279
307
0 commit comments