1
- import type { Middleware , Response } from 'koa' ;
1
+ import type {
2
+ Middleware ,
3
+ ParameterizedContext ,
4
+ DefaultState ,
5
+ DefaultContext ,
6
+ } from 'koa' ;
2
7
import type { IncomingMessage } from 'http' ;
3
8
import {
4
9
createHandler as createRawHandler ,
5
10
HandlerOptions as RawHandlerOptions ,
6
11
OperationContext ,
7
12
} from '../handler' ;
8
13
9
- /**
10
- * @category Server/koa
11
- */
12
- export interface RequestContext {
13
- res : Response ;
14
- }
15
-
16
14
/**
17
15
* Handler options when using the koa adapter.
18
16
*
19
17
* @category Server/koa
20
18
*/
21
- export type HandlerOptions < Context extends OperationContext = undefined > =
22
- RawHandlerOptions < IncomingMessage , RequestContext , Context > ;
19
+ export type HandlerOptions <
20
+ Context extends OperationContext = undefined ,
21
+ KoaState = DefaultState ,
22
+ KoaContext = DefaultContext ,
23
+ > = RawHandlerOptions <
24
+ IncomingMessage ,
25
+ ParameterizedContext < KoaState , KoaContext > ,
26
+ Context
27
+ > ;
23
28
24
29
/**
25
30
* The ready-to-use handler for [Koa](https://expressjs.com).
@@ -58,9 +63,13 @@ export type HandlerOptions<Context extends OperationContext = undefined> =
58
63
*
59
64
* @category Server/koa
60
65
*/
61
- export function createHandler < Context extends OperationContext = undefined > (
62
- options : HandlerOptions < Context > ,
63
- ) : Middleware {
66
+ export function createHandler <
67
+ Context extends OperationContext = undefined ,
68
+ KoaState = DefaultState ,
69
+ KoaContext = DefaultContext ,
70
+ > (
71
+ options : HandlerOptions < Context , KoaState , KoaContext > ,
72
+ ) : Middleware < KoaState , KoaContext > {
64
73
const handler = createRawHandler ( options ) ;
65
74
return async function requestListener ( ctx ) {
66
75
const [ body , init ] = await handler ( {
@@ -84,7 +93,7 @@ export function createHandler<Context extends OperationContext = undefined>(
84
93
} ) ;
85
94
} ,
86
95
raw : ctx . req ,
87
- context : { res : ctx . response } ,
96
+ context : ctx ,
88
97
} ) ;
89
98
ctx . response . status = init . status ;
90
99
ctx . response . message = init . statusText ;
0 commit comments