I find it difficult to type this code every time I have to use a route file. (app: ElysiaApp) => app.get('/') => ... is repeated in many route files. The information on each route specification / is meaningless (same when using params too).
I come from RemixJS background and I love their API. They provide us a way of getting into the code without any biolerplate. Also, the blurred line between client and server is highly preferred by me.
Hence, for ElysiaJS, I use this helper function.
import { ElysiaApp } from '..'
type LoaderHandler = Parameters<ElysiaApp['get']>['1']
type ActionHandler = Parameters<ElysiaApp['post']>['1']
type DeleteHandler = Parameters<ElysiaApp['delete']>['1']
export function page(
loader?: LoaderHandler,
action?: ActionHandler,
del?: DeleteHandler,
) {
return (
app: ElysiaApp,
) =>
app.get('/', loader || errorResponse).post(
'/',
action || errorResponse,
).delete('/', del || errorResponse)
}
function errorResponse() {
return ({ error: 'Page not found.' })
}
The code has become much cleaner and nice. Everything works as expected as well. I wish this was builtin and we could also remove more boilerplate. Having routes in filesystem is better for organization otherwise we have to think of file name every time we create a new feature. If someone can execute above plan then it is highly appreciated.
I find it difficult to type this code every time I have to use a route file.
(app: ElysiaApp) => app.get('/') => ...
is repeated in many route files. The information on each route specification/
is meaningless (same when using params too).This is current way of using a route file
I come from RemixJS background and I love their API. They provide us a way of getting into the code without any biolerplate. Also, the blurred line between client and server is highly preferred by me.
Hence, for ElysiaJS, I use this helper function.
And then write each route like this
The code has become much cleaner and nice. Everything works as expected as well. I wish this was builtin and we could also remove more boilerplate. Having routes in filesystem is better for organization otherwise we have to think of file name every time we create a new feature. If someone can execute above plan then it is highly appreciated.