mirror of
https://github.com/rowtype-yoga/purescript-fetch.git
synced 2024-11-26 07:41:31 +03:00
Add fetch-argonaut and simplify examples
This commit is contained in:
parent
726b8d86da
commit
9ac24ba1b6
42
README.md
42
README.md
@ -31,10 +31,8 @@ Perform a `POST` request:
|
||||
do
|
||||
{ status, text } <- fetch "https://httpbin.org/post"
|
||||
{ method: POST
|
||||
, mode: Cors
|
||||
, body: """{"hello":"world"}"""
|
||||
, headers: { "Content-Type": "application/json" }
|
||||
, referrer: (ReferrerUrl "https://httpbin.org")
|
||||
}
|
||||
responseText <- text
|
||||
```
|
||||
@ -44,18 +42,40 @@ do
|
||||
`fetch` works well with `yoga-json` and `argonaut`, use our little helper libraries.
|
||||
|
||||
### yoga-json
|
||||
You can use `fetch-yoga-json` helper library to simplify json handling
|
||||
|
||||
```bash
|
||||
spago install fetch-yoga-json
|
||||
```
|
||||
|
||||
```purescript
|
||||
do
|
||||
{ json } <- fetch "https://httpbin.org/post"
|
||||
{ method: POST
|
||||
, body: writeJSON { hello: "world" }
|
||||
, headers: { "Content-Type": "application/json" }
|
||||
}
|
||||
{ "data": d, url, origin } <- fromJSON json
|
||||
type HttpBinResponse = { json :: { hello :: String } }
|
||||
|
||||
main :: Effect Unit
|
||||
main = launchAff_ do
|
||||
{ json } <- fetch "https://httpbin.org/post"
|
||||
{ method: POST
|
||||
, body: """{"hello":"world"}"""
|
||||
, headers: { "Content-Type": "application/json" }
|
||||
}
|
||||
{ json: { hello: world } } :: HttpBinResponse <- fromJSON json
|
||||
log world
|
||||
```
|
||||
|
||||
### argonaut
|
||||
|
||||
Coming soon!
|
||||
```bash
|
||||
spago install fetch-argonaut
|
||||
```
|
||||
|
||||
```purescript
|
||||
type HttpBinResponse = { json :: { hello :: String } }
|
||||
|
||||
do
|
||||
{ json } <- fetch "https://httpbin.org/post"
|
||||
{ method: POST
|
||||
, body: """{"hello":"world"}"""
|
||||
, headers: { "Content-Type": "application/json" }
|
||||
}
|
||||
{ json: { hello: world } } :: HttpBinResponse <- fromJson json
|
||||
log world
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user