BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

RegisterRoute only accepts 2-5 arguments #79

Closed jtanadi closed 4 years ago

jtanadi commented 4 years ago

I'm working on migrating a service from express. One of the routes currently has 4 middlewares and the TS compiler is giving me an error: Expected 2-5 arguments, but got 6.

app.post("/api/upload", checkType, processFile, uploadFile, cleanup, handler)

After some digging, I think I found the source of the issue in the type declaration file. It seems odd to me that one could only place up to three middlewares before the handler, and after that would have to switch to a different pattern and use an array for the middlewares, placed after the handler. (Also, side note, the second block seems like a duplicate of the first one?)

interface RegisterRoute<P extends Protocol> {
    (
      path: string,
      handler: RequestHandler<P>,
      middlewares?: RequestHandler<P>[]
    ): Service<P>

// this seems like a duplicate?
    (
      path: string,
      handler: RequestHandler<P>,
      middlewares?: RequestHandler<P>[]
    ): Service<P>

    (
      path: string,
      middleware1: RequestHandler<P>,
      handler: RequestHandler<P>,
    ): Service<P>

    (
      path: string,
      middleware1: RequestHandler<P>,
      middleware2: RequestHandler<P>,
      handler: RequestHandler<P>,
    ): Service<P>

    (
      path: string,
      middleware1: RequestHandler<P>,
      middleware2: RequestHandler<P>,
      middleware3: RequestHandler<P>,
      handler: RequestHandler<P>,
    ): Service<P>
  }

Is it not possible to use a rest parameter, since all middlewares and handlers are RequestHandler<P>?

interface RegisterRoute<P extends Protocol> {
    (
      path: string,
      ...middlewaresAndHandler: RequestHandler<P>[]
    ): Service<P>
}
jkyberneees commented 4 years ago

Hi @jtanadi thanks for using restana and contributing to make it better. Totally valid suggestions. I will update and release ASAP.

Regards

jkyberneees commented 4 years ago

Hi @jtanadi as promised, the fix is now available in v.4.3.1

Thanks

jtanadi commented 4 years ago

@jkyberneees Thanks much—that was quick!