anuragsoni / routes

typed bidirectional router for OCaml/ReasonML web applications
https://anuragsoni.github.io/routes/
BSD 3-Clause "New" or "Revised" License
145 stars 11 forks source link

Add ksprintf. #123

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hi again @anuragsoni.

I'm PRing this really just to get your take on this function. For context, I'm wanting to use sprintf to create internal urls to routes we serve, but it seems to be difficult to use these at the moment. For example, I would like to have a function which takes a target, and turns it into a complete url with the protocol and domain name, but I'm struggling to write this function. And going further, I'd like to have a function which takes a target, and automatically performs an internal get request. So I think I want a function like

internal_get : ('a, response) target -> 'a

where, at the use site, 'a will be something like int -> string -> response.

With ksprintf, I could write

let target = Routes.(s "foo" / int / s "bar" / str /? nil) 
in Routes.ksprintf (fun str -> do_get (Printf.sprintf "https://example.com%s" str)) foo

- : int -> string -> string = <fun>

Is there another way to achieve this, or arguments against it?

anuragsoni commented 3 years ago

Hi @Chattered

ksprintf looks like a decent addition. Thanks again for the patch :) I think a ksprintf style function makes sense for the usecase you talk about.

I would like to have a function which takes a target, and turns it into a complete url with the protocol and domain name,

I think making this workflow nicer is a good step. ksprintf helps here, and i wonder if it'll be worth it to add a routes.uri package that makes it easier to get a fully formatted Uri.t for printing, and it could accept a Uri.t as input for parsing. I'm making an assumption here but I think a lot of OCaml users might be using the ocaml-uri library if they make client calls.