Git Product home page Git Product logo

Comments (3)

pimbrouwers avatar pimbrouwers commented on July 4, 2024 5

I just rolled out a fix for this. Huge shoutout to @mlaily for the discovery.

from falco.

seangwright avatar seangwright commented on July 4, 2024 1

@sheridanchris I noticed this same behavior when doing async/Task work with Request.mapForm in v4.0.

You can see in this discussion how I had to adopt a different approach (lower-level) to compose my async work with the APIs Falco provides.

Here's two approaches I ended up trying that both work.

Approach 1 - Request.mapFormAsync

  let post: HttpHandler =
    Services.inject<ShopContext> (fun db ->

      let mapAsync = fun (form: FormCollectionReader) ->
        form.TryGetGuid "id"
        |> BasketDomain.updateBasket db 1
        |> Async.StartAsTask

      Request.mapFormAsync mapAsync (fun quantity ->
        match quantity with
        | None -> Response.redirectPermanently "/basket?error=notfound"
        | Some q -> Response.redirectPermanently $"/basket?added={q}"))

where Request.mapFormAsync was something I added:

module Request =
  open System.Threading.Tasks
  open Microsoft.AspNetCore.Http
  open Falco

  let mapFormAsync
    (mapAsync : FormCollectionReader -> Task<'T>)
    (next : 'T -> HttpHandler) =
    fun (ctx: HttpContext) -> (task {
      let! form = Request.getForm ctx
      let! res = mapAsync form
      return! next res ctx
    }:> Task)

Approach 2 - Request.getForm

  let postAlternate: HttpHandler =
    Services.inject<ShopContext> (fun db -> fun ctx ->
      task {
        let! form = Request.getForm ctx

        let productId = form.TryGetGuid "id"

        let! quantity = BasketDomain.updateBasket db 1 productId |> Async.StartAsTask

        return!
          match quantity with
          | Some q -> Response.redirectPermanently $"/basket?added={q}" ctx
          | None -> Response.redirectPermanently "/basket?error=notfound" ctx
      })

from falco.

sheridanchris avatar sheridanchris commented on July 4, 2024

@seangwright Thank you for the suggestions. I'll make sure to try this out ASAP.

from falco.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.