wobsoriano / elysia-autoroutes

File system routes for Elysia.js.
MIT License
112 stars 8 forks source link

SUGGESTION: Include option to autogenerate swagger tags #12

Closed ao-Barbosa closed 9 months ago

ao-Barbosa commented 11 months ago

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.

ao-Barbosa commented 11 months ago

Submitted PR #13

kravetsone commented 9 months ago

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

wobsoriano commented 9 months ago

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