Skip to content

Commit b37a6f9

Browse files
authored
feat(use/koa): expose full Koa context to options (#86)
1 parent d0457c2 commit b37a6f9

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/use/koa.ts

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import type { Middleware, Response } from 'koa';
1+
import type {
2+
Middleware,
3+
ParameterizedContext,
4+
DefaultState,
5+
DefaultContext,
6+
} from 'koa';
27
import type { IncomingMessage } from 'http';
38
import {
49
createHandler as createRawHandler,
510
HandlerOptions as RawHandlerOptions,
611
OperationContext,
712
} from '../handler';
813

9-
/**
10-
* @category Server/koa
11-
*/
12-
export interface RequestContext {
13-
res: Response;
14-
}
15-
1614
/**
1715
* Handler options when using the koa adapter.
1816
*
1917
* @category Server/koa
2018
*/
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+
>;
2328

2429
/**
2530
* The ready-to-use handler for [Koa](https://expressjs.com).
@@ -58,9 +63,13 @@ export type HandlerOptions<Context extends OperationContext = undefined> =
5863
*
5964
* @category Server/koa
6065
*/
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> {
6473
const handler = createRawHandler(options);
6574
return async function requestListener(ctx) {
6675
const [body, init] = await handler({
@@ -84,7 +93,7 @@ export function createHandler<Context extends OperationContext = undefined>(
8493
});
8594
},
8695
raw: ctx.req,
87-
context: { res: ctx.response },
96+
context: ctx,
8897
});
8998
ctx.response.status = init.status;
9099
ctx.response.message = init.statusText;

0 commit comments

Comments
 (0)