tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

Allow providing function as well as string in @Route and @Get @Post etc

Open Khaaz opened this issue 8 months ago • 2 comments

Sorting

  • I'm submitting a ...

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

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

Expected Behavior

I would like to be able to call @Route, @Post, @Get etc with functions. In my specific case, I would like the return value from the function call.

In my use case, I would want to use it like this:

@Route(OPEN_API.ACCOUNTS.router())
export class AccountsController extends Controller {

That doesn't work as it yield undefined, since it's executed at compile time (OPEN_API.ACCOUNTS.router() returns a string).

Ideally, and to make this more flexible for everyone, the ideal would be to be able to pass function like that:

@Route(() => "accounts")
@Route(() => OPEN_API.ACCOUNTS.router())
export class AccountsController extends Controller {

The decorator itself would execute the function and it would behave as expected.

Current Behavior

Right now, since this is done at build / compile time, there is no way to get the result of the function and to pass that to tsoa.

You HAVE to provide a string:

@Route("accounts")
export class AccountsController extends Controller {

Possible Solution

Allowing to pass function as well as string that would get resolved by tsoa directly

@Route(() => "accounts")
export class AccountsController extends Controller {

Context (Environment)

Version of the library: 5.1.1 Version of NodeJS: 20.9

  • Confirm you were using yarn not npm: [X]

Detailed Description

Modify the current parser so it parses function as well as string. And if it finds a function it just executed it to get the results.

Breaking change?

Not a breaking change as we would still be supporting string

Khaaz avatar Dec 17 '23 15:12 Khaaz

Hello there Khaaz 👋

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 Dec 17 '23 15:12 github-actions[bot]

Hey, I have a question. I have a controller which I use for doing some experiments while developing. I want to ignore that route/controller in production. Is there a way to do it? I am checking prod<=>dev status via process.env.NODE_ENV Thanks

yalcinozer avatar Jan 03 '24 11:01 yalcinozer

It seems that things like

@Route(`v1/products/${ProductTypeKeysEnum.TRAINING}`)

also can't get evaluated.

ValentinGurkov avatar Feb 01 '24 20:02 ValentinGurkov

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Mar 03 '24 00:03 github-actions[bot]

:'(

Khaaz avatar Mar 05 '24 00:03 Khaaz

I don't think we want to run arbitrary, functions at build time.

It seems that things like

@Route(`v1/products/${ProductTypeKeysEnum.TRAINING}`)

also can't get evaluated.

This one, assuming it actually is an enum is a different issue, but I'm happy to review a PR for it.

WoH avatar Mar 05 '24 08:03 WoH