wobsoriano/elysia-autoroutes

SUGGESTION: Include option to autogenerate swagger tags

ao-Barbosa opened this issue · 3 comments

It would be interesting to have an option that could automatically generate and add tags to the routes with the necessary specificity.

For example, with the following structure:

[POST]   /auth/sing-in
[POST]   /auth/sign-out

[GET]    /messages/sent/
[GET]    /messages/sent/:id
[POST]   /messages/sent/
[PUT]    /messages/sent/:id
[DELETE] /messages/sent/:id

[GET]    /messages/received/
[GET]    /messages/received/:id
[POST]   /messages/received/
[PUT]    /messages/received/:id
[DELETE] /messages/received/:id

The tags generated would be:

/Auth
/Messages/Sent
/Messages/Received

And those tags would also be automatically added to the route details.

One possible way of implementing this would be:

// /src/utils/autoload.ts

export async function autoload<Decorator extends DecoratorBase>(app: Elysia<string, Decorator>, routesDir: string) {
  ...

  for (const [routeName, routeModule] of Object.entries(routeModules))
    app.group<Elysia<string, Decorator>, string>(routeName, (app) => {
      const ret = routeModule(app);

      ret.routes.forEach(r => {
        r.hooks.detail.tags = [ ...r.hooks.detail.tags || [], routeName ];
      });

      return ret;
    })
}

This ensures that every group mapped has it's routeName attached to it's routes tags.

I will be submitting an PR with the suggested changes.

Submitted PR #13

because author not maintaine library i released elysia-autoload and you can do it there!

example
https://github.com/kravetsone/elysia-autoload/tree/main?tab=readme-ov-file#usage-of-schema-handler

@kravetsone appreciate the update! Ive been away and was only able to work on some of my OSS