Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseHandler handler method return type should take into account async handlers #1689

Open
2 of 4 tasks
islazh opened this issue Dec 30, 2024 · 2 comments
Open
2 of 4 tasks
Assignees

Comments

@islazh
Copy link

islazh commented Dec 30, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When creating a middleware with a handler, the handler's type is

abstract handler(req: express.Request, res: express.Response, next: express.NextFunction): void;

Express middleware handlers may be async, so the return type should be void | Promise<void>

Steps to reproduce

Create an async middleware handler with no-misused-promises turned on in ESLint config

Expected behavior

No ESLint errors

Possible solution

ESLint warning:

Promise-returning method provided where a void return was expected by extended/implemented type 'BaseMiddleware'

Package version

6.0.1

Node.js version

No response

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Stack trace

No response

Other

No response

@notaphplover
Copy link
Member

notaphplover commented Dec 30, 2024

Hey @islazh, after having a look at the docs I don't think Express middleware handlers may be async. Having a look at express types:

export interface RequestHandler<
    P = ParamsDictionary,
    ResBody = any,
    ReqBody = any,
    ReqQuery = ParsedQs,
    LocalsObj extends Record<string, any> = Record<string, any>,
> {
    // tslint:disable-next-line callable-types (This is extended from and can't extend from a type alias in ts<2.2)
    (
        req: Request<P, ResBody, ReqBody, ReqQuery, LocalsObj>,
        res: Response<ResBody, LocalsObj>,
        next: NextFunction,
    ): void;
}

Closing the issue now. Feel free to provide a code example of an asyncronous middleware working on express and I'll reopen the issue.

EDIT: My bad, After having a look at the right types:

export interface RequestHandler<
    P = ParamsDictionary,
    ResBody = any,
    ReqBody = any,
    ReqQuery = ParsedQs,
    LocalsObj extends Record<string, any> = Record<string, any>,
> {
    // tslint:disable-next-line callable-types (This is extended from and can't extend from a type alias in ts<2.2)
    (
        req: Request<P, ResBody, ReqBody, ReqQuery, LocalsObj>,
        res: Response<ResBody, LocalsObj>,
        next: NextFunction,
    ): void | Promise<void>;
}

It seems it's perfectly fine to provide an async handler

@notaphplover
Copy link
Member

Ok, I had a look at some articles and did some tests and it seems it's possible. Reopening the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants