Skip to content

Commit 01fa9d1

Browse files
authored
Parameterize KoaContextFunctionArgument and KoaMiddlewareOptions (#132)
1 parent 13f7a3f commit 01fa9d1

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

.changeset/empty-walls-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@as-integrations/koa': patch
3+
---
4+
5+
Parameterize `KoaContextFunctionArgument` and `KoaMiddlewareOptions` so that the `ctx` parameter in the `context` function is correctly typed when this library is used along with a context-parameterized Koa app.

src/index.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,60 @@ import type Koa from 'koa';
1414
// the `koa-bodyparser` types "polyfill" the `koa` types
1515
import type * as _ from 'koa-bodyparser';
1616

17-
export interface KoaContextFunctionArgument {
18-
ctx: Koa.Context;
17+
export interface KoaContextFunctionArgument<
18+
StateT = Koa.DefaultState,
19+
ContextT = Koa.DefaultContext,
20+
> {
21+
ctx: Koa.ParameterizedContext<StateT, ContextT>;
1922
}
2023

21-
interface KoaMiddlewareOptions<TContext extends BaseContext> {
22-
context?: ContextFunction<[KoaContextFunctionArgument], TContext>;
24+
interface KoaMiddlewareOptions<TContext extends BaseContext, StateT, ContextT> {
25+
context?: ContextFunction<
26+
[KoaContextFunctionArgument<StateT, ContextT>],
27+
TContext
28+
>;
2329
}
2430

2531
export function koaMiddleware<
2632
StateT = Koa.DefaultState,
2733
ContextT = Koa.DefaultContext,
2834
>(
2935
server: ApolloServer<BaseContext>,
30-
options?: KoaMiddlewareOptions<BaseContext>,
36+
options?: KoaMiddlewareOptions<BaseContext, StateT, ContextT>,
3137
): Koa.Middleware<StateT, ContextT>;
3238
export function koaMiddleware<
3339
TContext extends BaseContext,
3440
StateT = Koa.DefaultState,
3541
ContextT = Koa.DefaultContext,
3642
>(
3743
server: ApolloServer<TContext>,
38-
options: WithRequired<KoaMiddlewareOptions<TContext>, 'context'>,
44+
options: WithRequired<
45+
KoaMiddlewareOptions<TContext, StateT, ContextT>,
46+
'context'
47+
>,
3948
): Koa.Middleware<StateT, ContextT>;
4049
export function koaMiddleware<
4150
TContext extends BaseContext,
4251
StateT = Koa.DefaultState,
4352
ContextT = Koa.DefaultContext,
4453
>(
4554
server: ApolloServer<TContext>,
46-
options?: KoaMiddlewareOptions<TContext>,
55+
options?: KoaMiddlewareOptions<TContext, StateT, ContextT>,
4756
): Koa.Middleware<StateT, ContextT> {
4857
server.assertStarted('koaMiddleware()');
4958

5059
// This `any` is safe because the overload above shows that context can
5160
// only be left out if you're using BaseContext as your context, and {} is a
5261
// valid BaseContext.
5362
const defaultContext: ContextFunction<
54-
[KoaContextFunctionArgument],
63+
[KoaContextFunctionArgument<StateT, ContextT>],
5564
any
5665
> = async () => ({});
5766

58-
const context: ContextFunction<[KoaContextFunctionArgument], TContext> =
59-
options?.context ?? defaultContext;
67+
const context: ContextFunction<
68+
[KoaContextFunctionArgument<StateT, ContextT>],
69+
TContext
70+
> = options?.context ?? defaultContext;
6071

6172
return async (ctx) => {
6273
if (!ctx.request.body) {

0 commit comments

Comments
 (0)