tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

[Question] Invoking CLI function programattically

Open aylusltd opened this issue 1 year ago • 5 comments

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?
      }
  }

aylusltd avatar Jan 12 '24 16:01 aylusltd

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

github-actions[bot] avatar Jan 12 '24 16:01 github-actions[bot]

There is, we export generateRoutes and generateSpec.

WoH avatar Jan 12 '24 16:01 WoH

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?

aylusltd avatar Jan 12 '24 19:01 aylusltd

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.

WoH avatar Jan 15 '24 10:01 WoH

Thank you!

aylusltd avatar Jan 16 '24 17:01 aylusltd

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 Feb 16 '24 00:02 github-actions[bot]