tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

Custom middleware does not work with esbuild because emitDecoratorMetadata is not supported

Open jsamilow opened this issue 2 years ago • 3 comments

https://github.com/lukeautry/tsoa/issues/961

According to this ticket, tsoa doesn't rely on the emitDecoratorMetadata TS option. That seemed correct at the time of writing, but sadly appears to no longer be true: https://github.com/lukeautry/tsoa/blob/acdfeb4d398fa7416dd10e251029d9e109c216d3/packages/runtime/src/decorators/middlewares.ts#L41

Because esbuild doesn't support emitDecoratorMetadata, it appears that custom middleware via the @Middlewares annotation cannot be used with esbuild.

Is there any advice for a workaround?

Sorting

  • I'm submitting a ...

    • [ ] bug report
    • [ ] feature request
    • [x] support request
  • I confirm that I

    • [x] used the search to make sure that a similar issue hasn't already been submit

Expected Behavior



Current Behavior



Possible Solution

Steps to Reproduce

Context (Environment)

Version of the library: Version of NodeJS:

  • Confirm you were using yarn not npm: [ ]

Detailed Description

Breaking change?

jsamilow avatar Aug 03 '22 21:08 jsamilow

Hello there jsamilow 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

github-actions[bot] avatar Aug 03 '22 21:08 github-actions[bot]

You need decorators/reflection, but I am not sure why the snippet you referenced would need the emitted metadata

WoH avatar Aug 04 '22 08:08 WoH

Maybe I am misreading, but you are using Reflect.getMetadata: https://github.com/lukeautry/tsoa/blob/acdfeb4d398fa7416dd10e251029d9e109c216d3/packages/runtime/src/decorators/middlewares.ts#L52-L53

According to the documentation for this library, you need emitted metadata. This library doesn't appear to be used anywhere else in the code.

Also see https://stackoverflow.com/questions/68570519/why-cant-reflect-metadata-be-used-in-vite

jsamilow avatar Aug 04 '22 13:08 jsamilow

Any update? Note that the middleware works fine when I run the application with ts-node, so it's plainly the use of esbuild that's tripping things up. If you're open to a pull request to remove the dependency on reflect-metadata I would look into how much work is involved.

jsamilow avatar Aug 08 '22 21:08 jsamilow

I don't know what the status is for esbuild on supporting decorators. My understanding is that emitDecoratorMetadata only adds additional information about the shape of the target which we don't rely on afaik.

Current workaround: Use TS-Node, TSC or SWC

E: But we can test this: Does TSC with emitDecoratorMetadata= false work? If the answer is no I'd be happy to take a closer look

WoH avatar Aug 12 '22 13:08 WoH

We fixed it. It turns out the issue is import {Middlewares} from 'tsoa'. It works if we do import {Middlewares} from '@tsoa/runtime'. This is surprising but it works. I guess I was barking up the totally wrong tree. Sorry about that and thanks for looking!

jsamilow avatar Aug 12 '22 15:08 jsamilow

@jsamilow thanks for the suggestion! Build fails if I'm importing from tsoa but it works just fine with @tsoa/runtime

bkiac avatar Nov 09 '22 14:11 bkiac