Code Monkey home page Code Monkey logo

Comments (7)

ed-ilyin avatar ed-ilyin commented on August 25, 2024 1

PR is created: #20

from fable-powerpack.

alfonsogarciacaro avatar alfonsogarciacaro commented on August 25, 2024

Here's an example, I hope it helps 😄

  GlobalFetch.fetch(
    RequestInfo.Url url,
    requestProps
      [ RequestProperties.Method HttpMethod.POST
        requestHeaders [ContentType "application/json"]
        RequestProperties.Body !^(toJson myObject) ])

fable-powerpack contains helpers for most common cases like this one. You can also check the tests.

from fable-powerpack.

ed-ilyin avatar ed-ilyin commented on August 25, 2024

I have no problems with predefined headers, but still not clear how to add custom headers.
Tests and code does not have examples.
This does not work:

        let headers =
            [   ContentType "application/x-www-form-urlencoded"
                "API-Key", Json.Encode.string apiKey
                "API-Sign", signature
            ]

from fable-powerpack.

MangelMaxime avatar MangelMaxime commented on August 25, 2024

@ed-ilyin Can you try something like that:

      let headers =  
            [   ContentType "application/x-www-form-urlencoded"
                !!("API-Key", Json.Encode.string apiKey)
                !!("API-Sign", signature)
            ]

!! comes from Fable.Core.JsInterop

I am not sure if this will work, but I saw people using this synthax for custom data attribute in Elmish and it's kind the same situation.
To explain, !! is equivalent to unbox(".....", "......") so it's simply make the compiler happy and Fable should convert the Key/Value into the desired object.

from fable-powerpack.

alfonsogarciacaro avatar alfonsogarciacaro commented on August 25, 2024

Sorry @ed-ilyin, I misread the issue 😅 Thanks for the explanation, @MangelMaxime! That's right, you can use dynamic casting to fake a key-value pair when passing values to keyValueList. Another alternative is to add a union case with Erase attribute to the union type.

| [<Erase>] Custom of key:string * value:obj

Then you can do:

      let headers =  
            [   ContentType "application/x-www-form-urlencoded"
                Custom("API-Key", Json.Encode.string apiKey)
                // Same as !!("API-Key", Json.Encode.string apiKey) but typed
            ]

I just noticed the Erase attribute is not working in these cases in the current release, but I've fixed that for the next one. Also, please not the CaseRules (like LowerFirst letter) don't apply for custom headers.

from fable-powerpack.

ed-ilyin avatar ed-ilyin commented on August 25, 2024

I like the idea with Custom case. Should I create PR?

from fable-powerpack.

alfonsogarciacaro avatar alfonsogarciacaro commented on August 25, 2024

Yes, please :)

from fable-powerpack.

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.