lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.42k stars 489 forks source link

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

Closed Khaaz closed 5 months ago

Khaaz commented 8 months ago

Sorting

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

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

github-actions[bot] commented 8 months ago

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.👀

yalcinozer commented 7 months ago

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

ValentinGurkov commented 6 months ago

It seems that things like

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

also can't get evaluated.

github-actions[bot] commented 5 months ago

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

Khaaz commented 5 months ago

:'(

WoH commented 5 months ago

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.