Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fable-compiler/fable-fetch
Fable bindings for Browsers' Fetch API
https://github.com/fable-compiler/fable-fetch
Last synced: 2 months ago
JSON representation
Fable bindings for Browsers' Fetch API
- Host: GitHub
- URL: https://github.com/fable-compiler/fable-fetch
- Owner: fable-compiler
- License: mit
- Created: 2018-12-10T09:57:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-21T08:24:56.000Z (3 months ago)
- Last Synced: 2024-04-14T12:57:24.182Z (3 months ago)
- Language: F#
- Size: 304 KB
- Stars: 16
- Watchers: 6
- Forks: 8
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Lists
- awesome-fable - Fable.Fetch - Fable bindings for Browsers' Fetch API. (Libraries)
README
# Fable.Fetch
Fable bindings for Browsers' [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
If you need helpers for automatic JSON serialization, check [Thoth.Fetch](https://github.com/thoth-org/Thoth.Fetch#thothfetch-).
- 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
```fsharp
type IUser =
abstract name: stringlet fetchGitHubUser accessToken =
async {
let! response =
fetch "https://api.github.com/user" [
requestHeaders [
HttpRequestHeaders.Authorization $"token {accessToken}"
] ] |> Async.AwaitPromise
let! item = response.json() |> 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
```fsharp
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](https://github.com/fable-compiler/fable-fetch/blob/master/tests/FetchTests.fs) for other examples.