1- open Webapi ;
1+ open Webapi
2+ open Promise
3+
24let _ = {
3- open Js .Promise
45 Fetch .fetch ("/api/hellos/1" )
5- |> then_ (Fetch .Response .text )
6- |> then_ (text => print_endline (text ) |> resolve )
6+ -> then (Fetch .Response .text )
7+ -> then (text => print_endline (text )-> resolve )
78}
89
910let _ = {
10- open Js .Promise
1111 Fetch .fetchWithInit ("/api/hello" , Fetch .RequestInit .make (~method_ = Post , ()))
12- |> then_ (Fetch .Response .text )
13- |> then_ (text => print_endline (text ) |> resolve )
12+ -> then (Fetch .Response .text )
13+ -> then (text => print_endline (text )-> resolve )
1414}
1515
1616let _ = {
17- open Js .Promise
1817 Fetch .fetch ("/api/fruit" )
1918 /* assume server returns `["apple", "banana", "pear", ...]` */
20- |> then_ (Fetch .Response .json )
21- |> then_ (json => Js .Json .decodeArray (json ) |> resolve )
22- |> then_ (opt => Belt .Option .getExn (opt ) |> resolve )
23- |> then_ (items =>
24- items |> Js .Array .map (item => item |> Js .Json .decodeString |> Belt .Option .getExn ) |> resolve
19+ -> then (Fetch .Response .json )
20+ -> then (json => Js .Json .decodeArray (json )-> resolve )
21+ -> then (opt => Belt .Option .getExn (opt )-> resolve )
22+ -> then (items =>
23+ items -> Js .Array2 .map (item => item -> Js .Json .decodeString -> Belt .Option .getExn )-> resolve
2524 )
2625}
2726
2827/* makes a post request with the following json payload { hello: "world" } */
2928let _ = {
3029 let payload = Js .Dict .empty ()
3130 Js .Dict .set (payload , "hello" , Js .Json .string ("world" ))
32- open Js .Promise
3331 Fetch .fetchWithInit (
3432 "/api/hello" ,
3533 Fetch .RequestInit .make (
@@ -38,7 +36,7 @@ let _ = {
3836 ~headers = Fetch .HeadersInit .make ({"Content-Type" : "application/json" }),
3937 (),
4038 ),
41- ) |> then_ (Fetch .Response .json )
39+ )-> then (Fetch .Response .json )
4240}
4341
4442let _ = {
@@ -47,10 +45,9 @@ let _ = {
4745 formData ,
4846 "image0" ,
4947 {"type" : "image/jpg" , "uri" : "path/to/it" , "name" : "image0.jpg" },
50- ()
48+ (),
5149 )
5250
53- open Js .Promise
5451 Fetch .fetchWithInit (
5552 "/api/upload" ,
5653 Fetch .RequestInit .make (
@@ -59,16 +56,13 @@ let _ = {
5956 ~headers = Fetch .HeadersInit .make ({"Accept" : "*" }),
6057 (),
6158 ),
62- ) |> then_ (Fetch .Response .json )
59+ )-> then (Fetch .Response .json )
6360}
6461
6562let _ = {
6663 let controller = Fetch .AbortController .make ()
6764
68- let _ = Fetch .fetchWithInit (
69- "/api/fruit" ,
70- Fetch .RequestInit .make (~signal = controller .signal , ())
71- )
72-
65+ let _ = Fetch .fetchWithInit ("/api/fruit" , Fetch .RequestInit .make (~signal = controller .signal , ()))
66+
7367 controller -> Fetch .AbortController .abort
74- }
68+ }
0 commit comments