elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.25k stars 219 forks source link

Filesystem based routing? #65

Closed m1212e closed 11 months ago

m1212e commented 1 year ago

Hi,

I recently got into bun and was looking for a nice webframework which supports filesystem based routing. I like it because it - by design - enforces a clear structure for the project and you know where to find a certain route. Elysia does a few other things really really well in my opinion so I thought about using it but as far as I see it does not support fs based routes. Is this a thing you considered? Maybe also in a hybrid approach like I did here: https://github.com/m1212e/bun-fs-router-plugin

itsyoboieltr commented 1 year ago

+1, I would be really interested in this, I was just about to raise a proposal for this exactly.

fecony commented 1 year ago

But Elysia doesn't really have frontend part so file based routing for api doesn't make sense..?

itsyoboieltr commented 1 year ago

But Elysia doesn't really have frontend part so file based routing for api doesn't make sense..?

This has nothing to do with frontend or not.

For reference, check Next.js route handler implementation for a file-system based api.

fecony commented 1 year ago

But Elysia doesn't really have frontend part so file based routing for api doesn't make sense..?

This has nothing to do with frontend or not.

For reference, check Next.js route handler implementation for a file-system based api.

I didn't play with file based api, from one side looks like overhead of splitting everything on the other looks like a good solution to extract those and make smaller

ethanniser commented 1 year ago

I don't think it would be too hard to make your own simple implementation See the "However, it isn't too difficult to make a file-based router. Here's a file-based example code with builder:" part of the Waku docs

wobsoriano commented 1 year ago

Here's one - https://github.com/wobsoriano/elysia-autoroutes

itsyoboieltr commented 1 year ago

I also made an implementation of a file system routing plugin, based on groups, called the group router.

m1212e commented 1 year ago

There are a few options, you could calculate the routes based on the current file path and just pass a helper function to spit out the correct relative path based on the file location, you could also utilize the bun.js macros/build plugins to set the correct path at build time. But what I could not get to work properly was the eden type safety. Since it works without code generation and TS does not support build time macros, I don't really seee a way to pull this off. Maybe one of the elysia devs knows how to implement something like this.

ethanniser commented 1 year ago

was just randomly reading the bun docs today and saw it has a native FileSystemRouter API that may be of use here

https://bun.sh/docs/api/file-system-router

wobsoriano commented 1 year ago

was just randomly reading the bun docs today and saw it has a native FileSystemRouter API that may be of use here

https://bun.sh/docs/api/file-system-router

elysia-autoroutes uses it!

m1212e commented 1 year ago

@wobsoriano Did you manage to get eden types to work with it?

wobsoriano commented 1 year ago

No. That's the biggest challenge @m1212e - how do we combine types from the main ElysiaApp instance with the route files?

m1212e commented 1 year ago

I don't see a way to do this without running some kind of process at development time.

wobsoriano commented 1 year ago

@m1212e what other meta framework do, like Nuxt, is to generate types for each route at dev time

itsyoboieltr commented 1 year ago

@m1212e what other meta framework do, like Nuxt, is to generate types for each route at dev time

@wobsoriano Actually, I tried to do something similar here (with not much success): https://github.com/elysiajs/elysia/issues/79

SaltyAom commented 11 months ago

Closing as we have several plugins like Elysia Autoroute, Group Router and several other.

Elysia is not going to focus on File Based Routing at the moment. All the discussion will be moving to #79 or feels free to open a new issue for dedicated topic.