Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

URL pattern suggestion #19

Open
BennieCopeland opened this issue Feb 23, 2022 · 0 comments
Open

URL pattern suggestion #19

BennieCopeland opened this issue Feb 23, 2022 · 0 comments

Comments

@BennieCopeland
Copy link

BennieCopeland commented Feb 23, 2022

I've based my backend app on some of the code I've found in your solution, and I wanted to share a couple of improvements that Dave Shaw and gsomix on the F# Slack helped me with. I liked what you were doing with PrintfFormat in the UrlPaths module, but I didn't like that I couldn't use it with sprintf to remove the path duplication in the Url module. The following code fixes that.

module UrlPaths =
    // The type aliases are both more readable, but also allows the format to work with both
    // routef and sprintf
    type StringPath = PrintfFormat<string -> string, unit, string, string, string>
    type StringPath2 = PrintfFormat<string -> string -> string, unit, string, string, string * string>
    
    let ``/api/foo`` = "/api/mil-to-mil/cost-categories"
    
    let ``/api/foo/%s`` : StringPath = "/api/mil-to-mil/cost-categories/%s"

module Url =
    let create (baseUrl : string) (route : string) =
        route.TrimStart [| '/' |]
        |> sprintf "%s/%s" baseUrl

    let ``/api/foo/%s`` baseUrl fooId = create baseUrl (sprintf UrlPaths.``/api/foo/%s`` fooId)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant