Search code examples
f#saturn-framework

Saturn Router - same function for getf and get?


should be simple question but I can't find the API docs that detail how to do this.

I have a controller function like this

let loadScreen (ctx: HttpContext) (number: int) : HttpHandler = htmlString "etc"

And a router that defines two routes that use that function. One passes a default value of 0 to the number parameter. The other allows a user to specify a number

let PageRouter = router {
    (* Works fine: *)
    get "/order" (warbler (fun (_, ctx) -> PageController.loadScreen ctx 0))
    (* Does not compile: *) 
    getf "/order/%i" (fun number func ctx -> PageController.loadScreen ctx number)
}

That gives the error

This expression was expected to have type 'HttpFuncResult' but here has type 'HttpFunc -> 'a -> HttpFuncResult' 

I know it's a simple missing thing but can't figure out what.

Cheers


Solution

  • The get combinator expects a second parameter of type HttpHandler. This is it in your code:

    get "/order" (warbler (fun (_, ctx) -> PageController.loadScreen ctx 0))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                             /
                                    This is an HttpHandler value
    

    getf, on the other hand, expects a different second parameter. It expects a function that takes an int as a parameter and returns a HttpHandler as a result.

    So just stick fun number -> in front of your existing HttpHandler, and voila:

    getf "/order/%i" (fun number -> 
      (warbler (fun (_, ctx) -> PageController.loadScreen ctx number))
    )