1
- open Webapi ;
1
+ open Webapi
2
+ open Promise
3
+
2
4
let _ = {
3
- open Js .Promise
4
5
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 )
7
8
}
8
9
9
10
let _ = {
10
- open Js .Promise
11
11
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 )
14
14
}
15
15
16
16
let _ = {
17
- open Js .Promise
18
17
Fetch .fetch ("/api/fruit" )
19
18
/* 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
25
24
)
26
25
}
27
26
28
27
/* makes a post request with the following json payload { hello: "world" } */
29
28
let _ = {
30
29
let payload = Js .Dict .empty ()
31
30
Js .Dict .set (payload , "hello" , Js .Json .string ("world" ))
32
- open Js .Promise
33
31
Fetch .fetchWithInit (
34
32
"/api/hello" ,
35
33
Fetch .RequestInit .make (
@@ -38,7 +36,7 @@ let _ = {
38
36
~headers = Fetch .HeadersInit .make ({"Content-Type" : "application/json" }),
39
37
(),
40
38
),
41
- ) |> then_ (Fetch .Response .json )
39
+ )-> then (Fetch .Response .json )
42
40
}
43
41
44
42
let _ = {
@@ -47,10 +45,9 @@ let _ = {
47
45
formData ,
48
46
"image0" ,
49
47
{"type" : "image/jpg" , "uri" : "path/to/it" , "name" : "image0.jpg" },
50
- ()
48
+ (),
51
49
)
52
50
53
- open Js .Promise
54
51
Fetch .fetchWithInit (
55
52
"/api/upload" ,
56
53
Fetch .RequestInit .make (
@@ -59,16 +56,13 @@ let _ = {
59
56
~headers = Fetch .HeadersInit .make ({"Accept" : "*" }),
60
57
(),
61
58
),
62
- ) |> then_ (Fetch .Response .json )
59
+ )-> then (Fetch .Response .json )
63
60
}
64
61
65
62
let _ = {
66
63
let controller = Fetch .AbortController .make ()
67
64
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
+
73
67
controller -> Fetch .AbortController .abort
74
- }
68
+ }
0 commit comments