fable-compiler / fable-fetch

Fable bindings for Browsers' Fetch API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fable.Fetch

Fable bindings for Browsers' Fetch API.

If you need helpers for automatic JSON serialization, check Thoth.Fetch.

  • Run tests: npm test
  • Publish: npm run publish (first bump version in RELEASE_NOTES.md and put your Nuget key in NUGET_KEY env var)

Usage

type IUser =
  abstract name: string

let fetchGitHubUser accessToken =
  async {
    let! response =
     fetch "https://api.github.com/user" [
          requestHeaders [
              HttpRequestHeaders.Authorization $"token {accessToken}"
          ] ] |> Async.AwaitPromise
    let! item = response.json<IUser>() |> Async.AwaitPromise
  }

Response Usage

Normally you don't need to use Response manually but, if you are in a service worker and intercepting fetch requests, you can programatically create responses that satisfy your logic, there are a few javascript runtimes that also offer http frameworks that work with responses directly so here is a few ways to create responses

Response.create("Hello World!", [Status 200; ])
Response.create("Teapot!", [Status 418; Headers [| "x-tea", "green" |] ])
Response.create("Bad Request!", [Status 400; Headers [| "x-custom", "fable" |] ])
Response.create("""{ "message": "Bad Request!" }""", [Status 400; Headers [| "content-type", "application/json" |] ])
Response.create(
  Blob.Create([| csvfile |], unbox {| ``type`` = "text/csv" |}),
  [ Status 200; Headers ["content-type", "text/csv"] ]
)

Check the tests for other examples.

About

Fable bindings for Browsers' Fetch API

License:MIT License


Languages

Language:F# 100.0%