-
Notifications
You must be signed in to change notification settings - Fork 462
ktor typesafe routing #3832
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
base: main
Are you sure you want to change the base?
ktor typesafe routing #3832
Conversation
55090a5 to
f6bd738
Compare
f6bd738 to
cb72f82
Compare
tKe
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to consider using a separate module or variant to avoiding forcing a dependency on the resources plugin for consumers that aren't using it.
Can you also ensure the correct imports are used for the resources patch/post/put as currently they'll do the wrong thing and attempt to .recieve<TRoute>(). May be worth adding a test case for each to ensure the right things is being done.
| import io.ktor.server.routing.patch | ||
| import io.ktor.server.routing.post | ||
| import io.ktor.server.routing.put |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
call.receive<TRoute>() instead of using the resources plugin.
| @@ -0,0 +1,79 @@ | |||
| @file:Suppress("API_NOT_AVAILABLE") | |||
|
|
|||
| package arrow.raise.ktor.server.routing.typesafe | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use the .resources package naming to be consistent with the ktor package?
I'm not sure if it's best to fully align with
package arrow.raise.ktor.server.resourcesor keep to a subpackage of
package arrow.raise.ktor.server.routing.resources| import io.ktor.server.routing.put | ||
| import io.ktor.utils.io.KtorDsl | ||
|
|
||
| public typealias TypeSafeRespondingRaiseRoutingHandler<TRoute, TResponse> = suspend context(Raise<Response>) RoutingContext.(TRoute) -> TResponse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we be consistent with this naming? We seem to have Resourced, TypeSafe, and Typed (for the tests). I suggest sticking with Resources to align with the Resources plugin.
| public fun interface ResourcedReceivingRespondingRaiseRoutingHandler<TRoute, TBody, TResponse> { | ||
| context(_: Raise<Response>) | ||
| public suspend fun RoutingContext.handle(route: TRoute, body: TBody): TResponse | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't need to be a fun interface and could just be a typealias for the appropriate lambda (making the usage below simpler).
The reason for the fun interface before was to rely on SAM usage to deprioritise it against the zero-arity equivalents without received parameters (as otherwise the compiler finds it ambiguous if between a no-arg lambda or a one-arg lambda with implicit it parameter).
In this case, both candidates should be distinct by needing either one (explicit or implicit) or two explicit parameters on the lambda.
| commonMain { | ||
| dependencies { | ||
| api(libs.ktor.server.core) | ||
| implementation(libs.ktor.server.resources) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather we avoid bringing in a dependency on the ktor-server-resources plugin for all consumers of this library as it's unnecessary unless explicitly wanting to use this feature.
That said, I'm not sure if gradle feature variants are easily achievable with kotlin multiplatform libraries.
An alternative might be to add another arrow module with the resources support included, that depends on this one. Any thoughts @nomisRev / @serras?
| @@ -0,0 +1,131 @@ | |||
| package arrow.raise.ktor.server.response | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we relocate the tests to the same package?
An attempt at adding support for typesafe routing support to the ktor server raise handlers.
Temporarily named all the functionsNevermind i tried the rename and it seems fine*OrErrorinstead of*OrRaisebecause the conflicting overloads on params and type arguments were funky. Maybe there should be a module for the type safe routing variants and a module for the regular variants? Would be a little restrictive to only use one or the other without weird override conflicts