lukeautry / tsoa

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

[Question] Invoking CLI function programattically #1538

Closed aylusltd closed 6 months ago

aylusltd commented 7 months ago

Is there a function in the code that can be passed a concrete controller class as an argument and returns the swagger doc for it?

My use case is I'm building an admin panel for typeorm, and I have a function that excepts an array of Entities. I want to dynamically build the routes from the entities. As a nice to have I'd like to also use tsoa to document those routes, but I can't figure out how to do that. My code looks something like this:

  export function init(entities:any[]){
      for(const entity of entities){
          const C = class
          { 
            // CRUD Routes here
          }
          Object.defineProperty (C, 'name', {value: entity.name+"Controller"});
          // Create swagger & register route here?
      }
  }
github-actions[bot] commented 7 months ago

Hello there aylusltd 👋

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

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

WoH commented 7 months ago

There is, we export generateRoutes and generateSpec.

aylusltd commented 7 months ago

Those are in CLI, right: https://github.com/lukeautry/tsoa/blob/e381199bf4456efe645196548a727a1987a8d0c2/packages/cli/src/module/generate-spec.ts#L17 and https://github.com/lukeautry/tsoa/blob/e381199bf4456efe645196548a727a1987a8d0c2/packages/cli/src/module/generate-routes.ts#L10

I couldn't make heads or tails of those arguments. I want to extend Controller for each Entity and pass that extended Class in. It looks like generateRoutes for example takes a whole bunch of project level arguments.

The function I'm looking for would take just the class (or ideally an array of classes) as an argument.

Does that make sense, or is there a way I could wrap my array of Classes to pass it to generateRoutes?

WoH commented 7 months ago

https://github.com/lukeautry/tsoa/blob/master/packages/runtime/src/config.ts

Here is where that Config is defined. You wanna pass each controller as an entry into the controller glob.

I would still lean towards collecting all controllers and then running it once, so you don't have to register the n routes files.

E: You basically need to pass only: a dir where we place the routes.ts, a spec version, the entry point (the file you run) and the glob and you should be good to go.

aylusltd commented 7 months ago

Thank you!

github-actions[bot] commented 6 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