Andre Stefanov
08/24/2021, 1:50 PMcorneil
08/24/2021, 2:10 PMnschulzke
08/24/2021, 8:11 PM// In the shared module:
data class Resource(val something: String)
val getResourceById = Location.GetId<Resource>("/resource/{id}")
// In the client module:
val resource = getResourceById.get("idHere")
// In the server module:
fun Routing.resourceController() {
get(getResourceById) { id ->
return resources.get(id)
}
}
get
on Location.GetId
which stuffs the argument into the `Location`'s string (replacing {id}
) and issues the GET request using your client of choice (Ktor?).
The server module has an extension function get
on Route
which takes a Location.GetId
instead of a raw string. For convenience, I have the body
lambda accept the ID as a string and return the type specified when the location was defined. The extension function does the translation to create the regular Ktor route with call.respond
.inline
with reified types, so that the type parameters used when creating the Location
can be used for serialization and deserialization.Kamil Kalisz
08/26/2021, 6:31 AMnschulzke
08/26/2021, 3:15 PM