-
Notifications
You must be signed in to change notification settings - Fork 53
/
reason_examples.re
73 lines (68 loc) · 1.79 KB
/
reason_examples.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
let _ =
Js.Promise.(
Fetch.fetch("/api/hellos/1")
|> then_(Fetch.Response.text)
|> then_(text => print_endline(text) |> resolve)
);
let _ =
Js.Promise.(
Fetch.fetchWithInit(
"/api/hello",
Fetch.RequestInit.make(~method_=Post, ()),
)
|> then_(Fetch.Response.text)
|> then_(text => print_endline(text) |> resolve)
);
let _ =
Js.Promise.(
Fetch.fetch("/api/fruit")
/* assume server returns `["apple", "banana", "pear", ...]` */
|> then_(Fetch.Response.json)
|> then_(json => Js.Json.decodeArray(json) |> resolve)
|> then_(opt => Belt.Option.getExn(opt) |> resolve)
|> then_(items =>
items
|> Js.Array.map(item =>
item |> Js.Json.decodeString |> Belt.Option.getExn
)
|> resolve
)
);
/* makes a post request with the following json payload { hello: "world" } */
let _ = {
let payload = Js.Dict.empty();
Js.Dict.set(payload, "hello", Js.Json.string("world"));
Js.Promise.(
Fetch.fetchWithInit(
"/api/hello",
Fetch.RequestInit.make(
~method_=Post,
~body=
Fetch.BodyInit.make(Js.Json.stringify(Js.Json.object_(payload))),
~headers=Fetch.HeadersInit.make({"Content-Type": "application/json"}),
(),
),
)
|> then_(Fetch.Response.json)
);
};
let _ = {
let formData = Fetch.FormData.make();
Fetch.FormData.appendObject(
"image0",
{"type": "image/jpg", "uri": "path/to/it", "name": "image0.jpg"},
formData,
);
Js.Promise.(
Fetch.fetchWithInit(
"/api/upload",
Fetch.RequestInit.make(
~method_=Post,
~body=Fetch.BodyInit.makeWithFormData(formData),
~headers=Fetch.HeadersInit.make({"Accept": "*"}),
(),
),
)
|> then_(Fetch.Response.json)
);
};