Hi, the docs pretty much say this isn’t possible, ...
# http4k
m
Hi, the docs pretty much say this isn’t possible, but just in case I’ve missed something, is there a way I can continue using typesafe routing (for dynamic path params) when using the
"/path/{param}"
pattern, or is this only possible for contract routes in the
"/path" / Path.uuid().of("param")
pattern? Here’s an example of the contract routing style, where http4k handles type-safety for the path parameter (n.b. using Result4k for error handling in these examples)…
Copy code
fun getThingRoute(retriever: Retriever) =
   "/path" / Path.uuid().of("id") bindContract GET to { id ->
       {
           retriever(id)
               .map { Response(OK).body(it) }
               .mapFailure { it.toErrorResponse() }
               .get()
       }
   }
And the non-contract routing pattern I’d like to switch to, but which I think needs our own typesafe error handling (if we want to handle non-UUID path parameters, for example)...
Copy code
fun getThingRoute(retriever: Retriever) =
   "/path/{id}-something-after-param" bind GET to { request ->
       resultFrom { UUID.fromString(request.path("id")) }
           .mapFailure { NotFound("ID must be a valid UUID") }
           .flatMap { id ->
               retriever(id)
                   .map { Response(OK).body(it) }
           }
           .mapFailure { it.toErrorResponse() }
           .get()
   }
Thanks!
👋 1
👋🏻 1
d
Sorry for the late reply - missed this when it popped up. Anyway - you correct that you can't mix and match the templating styles
If you want to get that working you can just write a custom Path lens which strips off the suffix and then converts to a uuid
(using the map() function)
m
ah nice, yes - thanks very much David