-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add opinionated solution to API routes to reduce boilerplate. #409
Comments
One thing that would help make a good decision here is know the sorts of code that people are writing in their backends. For example, my "todo" example's project has an API route like this, which is pretty simple: @Api
fun addTodo(ctx: ApiContext) {
if (ctx.req.method != HttpMethod.POST) return
val ownerId = ctx.req.params["owner"]
val todo = ctx.req.params["todo"]
if (ownerId == null || todo == null) {
return
}
ctx.data.getValue<TodoStore>().add(ownerId, todo)
ctx.res.status = 200
} Exceptions should automatically get logged and result in an error status code (but I'd have to double check) |
I see two attractive but very different approaches. Current implementations plus syntactic sugarStick close to the current implementation, but add nice helpers to reduce boilerplate. Your example with
Then user's route function could look like this:
Annotations Spring Boot likeUsing function signature plus annotations to parse information about params return types etc in compile time (or on start up with reflections), instead of at run time when handling requests like with current approach. Auto-generate http client and hide it under abstractionRegardless of server side handling, there is cool potential to make developers life a lot easier on the client side.
I'm happy to hear (or see) your thoughts on this |
To expand what I wrote in
Functions: |
From a comment on my Discord:
I have simple GET request returning book for given id, in Spring Boot I would do something like this:
In Kobweb (from what I understand) there is no validation for allowed methods, required params or exception handling (returning 400 or 500). so my code looks like this:
Which seems like tons of boilerplate code (I immediately try to wrap this into custom generic solution, but I wonder why it's not in place already).
So far, Kobweb API routes were inspired by NextJS API routes. They are indeed somewhat low-level, in the sense that it still leaves things up to the user to determine how to respond to an API endpoint in a fairly flexible way.
For example, you could potentially have a single endpoint handle get, post, and delete verbs if you want.
Still, as Kobweb wants to be an opinionated framework in order to reduce boilerplate that most users don't care about, we could probably do better.
A few possible ways forward here that I can think of (code all open for discussion):
Add Some utility methods to the Kobweb backend APIs
e.g. Extend
ApiContext
withhandleGet
,handlePost
, etc. methodsAdd / tweak new annotations into the Kobweb backend APIs
Not too familiar with
@RequestParam
but let's just assume the Kobweb Gradle plugins can figure this out:Leave Kobweb flexible and low-ish level and add an extension library that layers improvements on top
The text was updated successfully, but these errors were encountered: