Skip to content

Commit ea9bd98

Browse files
committed
Initial cut of importing bs-fetch and using automated syntax conversion - no API changes yet
1 parent 1348f74 commit ea9bd98

16 files changed

+1072
-15
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
docs/
55
node_modules/
66
/lib/bs
7-
/lib/js/examples
87
/lib/js/src
98
/lib/js/tests/testHelpers.js
109
npm-debug.log

LICENSE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
MIT License
22

33
Copyright (c) 2021 Tiny Technologies Inc (https://www.tiny.cloud/)
4+
Forked from https://github.com/reasonml-community/bs-webapi-incubator:
5+
Copyright (c) 2017 reasonml-community
6+
Forked from https://github.com/reasonml-community/bs-fetch:
7+
Copyright (c) 2017
48

59
Permission is hereby granted, free of charge, to any person obtaining a copy
610
of this software and associated documentation files (the "Software"), to deal

bsconfig.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@
1919
"warnings": {
2020
"number": "+A-105",
2121
"error": "+A"
22-
},
23-
"bs-dependencies": [
24-
"bs-fetch"
25-
]
22+
}
2623
}

examples/ocaml_examples.res

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
let _ = {
2+
open Js.Promise
3+
Fetch.fetch("/api/hellos/1")
4+
|> then_(Fetch.Response.text)
5+
|> then_(text => print_endline(text) |> resolve)
6+
}
7+
8+
let _ = {
9+
open Js.Promise
10+
Fetch.fetchWithInit("/api/hello", Fetch.RequestInit.make(~method_=Post, ()))
11+
|> then_(Fetch.Response.text)
12+
|> then_(text => print_endline(text) |> resolve)
13+
}
14+
15+
let _ = {
16+
open Js.Promise
17+
Fetch.fetch("/api/fruit")
18+
|> /* assume server returns `["apple", "banana", "pear", ...]` */
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.Array.map(item => item |> Js.Json.decodeString |> Belt.Option.getExn) |> resolve
24+
)
25+
}
26+
27+
let _ = {
28+
let headers = %raw(`
29+
{"Content-type": "application/json"}
30+
`)
31+
let payload = Js.Dict.empty()
32+
Js.Dict.set(payload, "hello", Js.Json.string("world"))
33+
open Js.Promise
34+
Fetch.fetchWithInit(
35+
"/api/hello",
36+
Fetch.RequestInit.make(
37+
~method_=Post,
38+
~body=Fetch.BodyInit.make(Js.Json.stringify(Js.Json.object_(payload))),
39+
~headers=Fetch.HeadersInit.make(headers),
40+
(),
41+
),
42+
) |> then_(Fetch.Response.json)
43+
}

examples/reason_examples.res

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
let _ = {
2+
open Js.Promise
3+
Fetch.fetch("/api/hellos/1")
4+
|> then_(Fetch.Response.text)
5+
|> then_(text => print_endline(text) |> resolve)
6+
}
7+
8+
let _ = {
9+
open Js.Promise
10+
Fetch.fetchWithInit("/api/hello", Fetch.RequestInit.make(~method_=Post, ()))
11+
|> then_(Fetch.Response.text)
12+
|> then_(text => print_endline(text) |> resolve)
13+
}
14+
15+
let _ = {
16+
open Js.Promise
17+
Fetch.fetch("/api/fruit")
18+
/* assume server returns `["apple", "banana", "pear", ...]` */
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.Array.map(item => item |> Js.Json.decodeString |> Belt.Option.getExn) |> resolve
24+
)
25+
}
26+
27+
/* makes a post request with the following json payload { hello: "world" } */
28+
let _ = {
29+
let payload = Js.Dict.empty()
30+
Js.Dict.set(payload, "hello", Js.Json.string("world"))
31+
open Js.Promise
32+
Fetch.fetchWithInit(
33+
"/api/hello",
34+
Fetch.RequestInit.make(
35+
~method_=Post,
36+
~body=Fetch.BodyInit.make(Js.Json.stringify(Js.Json.object_(payload))),
37+
~headers=Fetch.HeadersInit.make({"Content-Type": "application/json"}),
38+
(),
39+
),
40+
) |> then_(Fetch.Response.json)
41+
}
42+
43+
let _ = {
44+
let formData = Fetch.FormData.make()
45+
Fetch.FormData.appendObject(
46+
"image0",
47+
{"type": "image/jpg", "uri": "path/to/it", "name": "image0.jpg"},
48+
formData,
49+
)
50+
51+
open Js.Promise
52+
Fetch.fetchWithInit(
53+
"/api/upload",
54+
Fetch.RequestInit.make(
55+
~method_=Post,
56+
~body=Fetch.BodyInit.makeWithFormData(formData),
57+
~headers=Fetch.HeadersInit.make({"Accept": "*"}),
58+
(),
59+
),
60+
) |> then_(Fetch.Response.json)
61+
}

lib/js/examples/dom_example.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
var Belt_Option = require("rescript/lib/js/belt_Option.js");
4+
var Caml_option = require("rescript/lib/js/caml_option.js");
5+
var Webapi__Dom__Element = require("../src/Webapi/Dom/Webapi__Dom__Element.js");
6+
var Webapi__Dom__Document = require("../src/Webapi/Dom/Webapi__Dom__Document.js");
7+
8+
function unwrapUnsafely(x) {
9+
if (x !== undefined) {
10+
return Caml_option.valFromOption(x);
11+
}
12+
throw {
13+
RE_EXN_ID: "Invalid_argument",
14+
_1: "Passed `None` to unwrapUnsafely",
15+
Error: new Error()
16+
};
17+
}
18+
19+
document.createElement("div").className;
20+
21+
Belt_Option.map(Caml_option.nullable_to_opt(document.createElement("div").nextElementSibling), (function (prim) {
22+
return prim.innerText;
23+
}));
24+
25+
Belt_Option.map(Caml_option.nullable_to_opt(document.createElement("div").parentElement), (function (prim) {
26+
return prim.innerText;
27+
}));
28+
29+
var el = unwrapUnsafely(Webapi__Dom__Element.asHtmlElement(document.createElement("div")));
30+
31+
Belt_Option.map(Belt_Option.flatMap(Webapi__Dom__Document.asHtmlDocument(document), (function (prim) {
32+
return Caml_option.nullable_to_opt(prim.body);
33+
})), (function (body) {
34+
body.appendChild(el);
35+
36+
}));
37+
38+
document.createElement("div").addEventListener("mousemove", (function (e) {
39+
console.log([
40+
e.screenX,
41+
e.screenY
42+
]);
43+
44+
}));
45+
46+
exports.unwrapUnsafely = unwrapUnsafely;
47+
exports.el = el;
48+
/* Not a pure module */

lib/js/examples/ocaml_examples.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
var Fetch = require("../src/Fetch.js");
4+
var Js_json = require("bs-platform/lib/js/js_json.js");
5+
var Belt_Option = require("bs-platform/lib/js/belt_Option.js");
6+
var Caml_option = require("bs-platform/lib/js/caml_option.js");
7+
8+
fetch("/api/hellos/1").then((function (prim) {
9+
return prim.text();
10+
})).then((function (text) {
11+
return Promise.resolve((console.log(text), undefined));
12+
}));
13+
14+
fetch("/api/hello", Fetch.RequestInit.make(/* Post */2, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then((function (prim) {
15+
return prim.text();
16+
})).then((function (text) {
17+
return Promise.resolve((console.log(text), undefined));
18+
}));
19+
20+
fetch("/api/fruit").then((function (prim) {
21+
return prim.json();
22+
})).then((function (json) {
23+
return Promise.resolve(Js_json.decodeArray(json));
24+
})).then((function (opt) {
25+
return Promise.resolve(Belt_Option.getExn(opt));
26+
})).then((function (items) {
27+
return Promise.resolve(items.map((function (item) {
28+
return Belt_Option.getExn(Js_json.decodeString(item));
29+
})));
30+
}));
31+
32+
var headers = ({"Content-type": "application/json"});
33+
34+
var payload = { };
35+
36+
payload["hello"] = "world";
37+
38+
fetch("/api/hello", Fetch.RequestInit.make(/* Post */2, Caml_option.some(headers), Caml_option.some(JSON.stringify(payload)), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then((function (prim) {
39+
return prim.json();
40+
}));
41+
42+
/* Not a pure module */

lib/js/examples/reason_examples.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
var Fetch = require("../src/Fetch.js");
4+
var Js_json = require("bs-platform/lib/js/js_json.js");
5+
var Belt_Option = require("bs-platform/lib/js/belt_Option.js");
6+
var Caml_option = require("bs-platform/lib/js/caml_option.js");
7+
8+
fetch("/api/hellos/1").then((function (prim) {
9+
return prim.text();
10+
})).then((function (text) {
11+
return Promise.resolve((console.log(text), undefined));
12+
}));
13+
14+
fetch("/api/hello", Fetch.RequestInit.make(/* Post */2, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then((function (prim) {
15+
return prim.text();
16+
})).then((function (text) {
17+
return Promise.resolve((console.log(text), undefined));
18+
}));
19+
20+
fetch("/api/fruit").then((function (prim) {
21+
return prim.json();
22+
})).then((function (json) {
23+
return Promise.resolve(Js_json.decodeArray(json));
24+
})).then((function (opt) {
25+
return Promise.resolve(Belt_Option.getExn(opt));
26+
})).then((function (items) {
27+
return Promise.resolve(items.map((function (item) {
28+
return Belt_Option.getExn(Js_json.decodeString(item));
29+
})));
30+
}));
31+
32+
var payload = { };
33+
34+
payload["hello"] = "world";
35+
36+
fetch("/api/hello", Fetch.RequestInit.make(/* Post */2, {
37+
"Content-Type": "application/json"
38+
}, Caml_option.some(JSON.stringify(payload)), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then((function (prim) {
39+
return prim.json();
40+
}));
41+
42+
var formData = new FormData();
43+
44+
formData.append("image0", {
45+
type: "image/jpg",
46+
uri: "path/to/it",
47+
name: "image0.jpg"
48+
}, undefined);
49+
50+
fetch("/api/upload", Fetch.RequestInit.make(/* Post */2, {
51+
Accept: "*"
52+
}, Caml_option.some(formData), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then((function (prim) {
53+
return prim.json();
54+
}));
55+
56+
/* Not a pure module */

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,5 @@
3030
"license": "MIT",
3131
"devDependencies": {
3232
"rescript": "^9.1.4"
33-
},
34-
"dependencies": {
35-
"bs-fetch": "^0.6.2"
3633
}
3734
}

0 commit comments

Comments
 (0)