Skip to content

Commit 18decb9

Browse files
committed
fix: add missing option when calling use function and next middleware context
1 parent 77f4dda commit 18decb9

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

packages/api/src/client/client.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ export class ApiClient<
150150
return handler;
151151
}
152152

153-
use<NC extends object>(middleware: Middleware<Ctx, NC>) {
154-
return new ApiClient<Req, Ctx & NC>({
153+
use<NC extends SomeObject>(middleware: Middleware<Ctx, NC>) {
154+
return new ApiClient<Req, Prettify<Ctx & NC>>({
155155
action: this.#action,
156-
middlewares: [...this.middlewares, middleware],
156+
handleError: this.handleError,
157+
middlewares: [...this.middlewares, middleware] as Middleware<any>[],
157158
});
158159
}
159160
}

packages/api/src/compose.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import type { AnyAsyncFn, ApiResponse, Options } from './types.js';
1+
import type { AnyAsyncFn, ApiResponse, Options, SomeObject } from './types.js';
22

33
export type MiddlewareOptions<Ctx> = Options & {
44
ctx: Ctx;
55
execute: AnyAsyncFn;
66
res: ApiResponse;
77
};
88

9-
export type Middleware<Ctx, _NextCtx = any> = (
9+
export type Middleware<Ctx, NextCtx extends SomeObject = SomeObject> = (
1010
opts: MiddlewareOptions<Ctx> & {
11-
next: <NC extends object>(opts?: { ctx?: NC }) => Promise<any>;
11+
next: <NC extends SomeObject>(opts?: {
12+
ctx?: NC;
13+
}) => Promise<MiddlewareOptions<Ctx & NC>>;
1214
},
13-
) => Promise<any>;
15+
// biome-ignore lint/suspicious/noConfusingVoidType: <explanation>
16+
) => Promise<void | MiddlewareOptions<Ctx & NextCtx>>;
1417

1518
export function compose(middlewares: Middleware<any>[]) {
1619
return function composed(

packages/api/src/next/action.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
Prettify,
1414
Options,
1515
ApiRequest,
16+
SomeObject,
1617
} from '../types.js';
1718
import { validate } from '../validate.js';
1819
import { noop } from './noop.js';
@@ -323,9 +324,10 @@ export class NextAction<
323324
return handler;
324325
}
325326

326-
use<NC extends object>(middleware: Middleware<Ctx, NC>) {
327-
return new NextAction<Req, Ctx & NC>({
328-
middlewares: [...this.middlewares, middleware],
327+
use<NC extends SomeObject>(middleware: Middleware<Ctx, NC>) {
328+
return new NextAction<Req, Prettify<Ctx & NC>>({
329+
handleError: this.handleError,
330+
middlewares: [...this.middlewares, middleware] as Middleware<any>[],
329331
});
330332
}
331333
}

packages/api/src/next/route.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
InferInput,
99
InferOutput,
1010
Prettify,
11+
SomeObject,
1112
Transform,
1213
} from '../types.js';
1314
import type { Middleware } from '../compose.js';
@@ -206,9 +207,10 @@ export class NextRoute<
206207
return handler;
207208
}
208209

209-
use<NC extends object>(middleware: Middleware<Ctx, NC>) {
210-
return new NextRoute<Req, Ctx & NC>({
211-
middlewares: [...this.middlewares, middleware],
210+
use<NC extends SomeObject>(middleware: Middleware<Ctx, NC>) {
211+
return new NextRoute<Req, Prettify<Ctx & NC>>({
212+
handleError: this.handleError,
213+
middlewares: [...this.middlewares, middleware] as Middleware<any>[],
212214
});
213215
}
214216
}

0 commit comments

Comments
 (0)