Skip to content

Commit 1e71529

Browse files
committed
Restoring simpler type inferrence on Middleware::handler.
1 parent 946c044 commit 1e71529

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

express-zod-api/src/middleware.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextFunction, Request, Response } from "express";
22
import { z } from "zod/v4";
3-
import { FlatObject } from "./common-helpers";
3+
import { emptySchema, FlatObject } from "./common-helpers";
44
import { InputValidationError } from "./errors";
55
import { IOSchema } from "./io-schema";
66
import { LogicalContainer } from "./logical-container";
@@ -47,11 +47,7 @@ export class Middleware<
4747
readonly #security?: LogicalContainer<
4848
Security<Extract<keyof z.input<IN>, string>, SCO>
4949
>;
50-
readonly #handler: Handler<
51-
IN extends IOSchema ? z.output<IN> : undefined,
52-
OPT,
53-
OUT
54-
>;
50+
readonly #handler: Handler<z.output<IN>, OPT, OUT>;
5551

5652
constructor({
5753
input,
@@ -69,7 +65,7 @@ export class Middleware<
6965
Security<Extract<keyof z.input<IN>, string>, SCO>
7066
>;
7167
/** @desc The handler returning options available to Endpoints */
72-
handler: Handler<IN extends IOSchema ? z.output<IN> : undefined, OPT, OUT>;
68+
handler: Handler<z.output<IN>, OPT, OUT>;
7369
}) {
7470
super();
7571
this.#schema = input as IN;
@@ -99,9 +95,9 @@ export class Middleware<
9995
logger: ActualLogger;
10096
}) {
10197
try {
102-
const validInput = (
103-
this.#schema ? await this.#schema.parseAsync(input) : undefined
104-
) as IN extends IOSchema ? z.output<IN> : undefined;
98+
const validInput = (await (this.#schema || emptySchema).parseAsync(
99+
input,
100+
)) as z.output<IN>;
105101
return this.#handler({ ...rest, input: validInput });
106102
} catch (e) {
107103
throw e instanceof z.ZodError ? new InputValidationError(e) : e;

0 commit comments

Comments
 (0)