Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ _release
*.byte
*.native
*.install

# BuckleScript

lib/
*.bs.js
23 changes: 23 additions & 0 deletions bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Fetch",
"namespace": true,
"sources": [
{
"dir": "src/fetch-js",
"subdirs": true
},
{
"dir": "src/fetch-core",
"subdirs": true
}
],
"package-specs": {
"module": "commonjs",
"in-source": true
},
"refmt": 3,
"suffix": ".bs.js",
"generate-merlin": true,
"bs-dev-dependencies": ["bs-fetch", "reason-promise"],
"bs-dependencies": ["bs-fetch", "reason-promise"]
}
70 changes: 70 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "fetch-js",
"version": "0.1.0",
"license": "MIT",
"scripts": {
"clean": "bsb -clean-world",
"build": "bsb -make-world",
"watch": "bsb -make-world -w",
"example": "bsb -make-world && node src/fetch-js/examples/WithLetOperators.bs.js"
},
"dependencies": {
"bs-fetch": "lessp/bs-fetch#758b764",
"reason-promise": "^1.0.2",
"bs-platform": "jaredly/bucklescript#letop",
"isomorphic-fetch": "^2.2.1"
},
"peerDependencies": {
"bs-platform": ">= 5.0.0"
},
"files": [
"src/fetch-js/Fetch.rei",
"src/fetch-js/Fetch.re",
"bsconfig.json"
]
}
5 changes: 5 additions & 0 deletions src/fetch-js/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
*.install
*.merlin
*.lock
_esy
23 changes: 23 additions & 0 deletions src/fetch-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Fetch JS

Fetch client for BuckleScript.

## Examples

```reason
Fetch_Js.(
post(
"https://httpbin.org/post",
~headers=[("Authorization", "Bearer xyz")],
~body="Hello, World!",
)
->Promise.flatMap(
fun
| Ok({Response.body, _}) => Body.toString(body)->Promise.resolved
| Error(errorMsg) => errorMsg->Promise.resolved,
)
->Promise.map(Js.log)
);
```

See [the examples](./examples/).
9 changes: 9 additions & 0 deletions src/fetch-js/examples/GetRequest.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Fetch_Js.(
get("https://httpbin.org/get")
->Promise.map(
fun
| Ok({Response.status, _}) when Status.isSuccessful(status) => "Success!"
| _ => "That's anything but successful. :-(",
)
->Promise.get(Js.log)
);
25 changes: 25 additions & 0 deletions src/fetch-js/examples/JsonPostRequest.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
open LetOperators;

let jsonBody = Js.Json.parseExn({|
{
"foo": "bar"
}
|});

Fetch_Js.(
{
let.flatMapOk {Response.body, _} =
post(
"https://httpbin.org/post",
~headers=[
("Authorization", "Bearer xyz"),
("content-type", "application/json"),
],
~body=Js.Json.stringify(jsonBody),
);

Js.log2("Parse JSON: ", Body.toString(body)->Js.Json.parseExn);

Promise.resolved(Ok());
}
);
11 changes: 11 additions & 0 deletions src/fetch-js/examples/LetOperators.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let (let.flatMapOk) = (promise, fn) =>
Promise.flatMap(
promise,
fun
| Ok(response) => fn(response)
| Error(e) => Promise.resolved(Belt.Result.Error(e)),
);

let (let.flatMap) = (promise, fn) => Promise.flatMap(promise, fn);
let (let.map) = Promise.map;
let (let.mapOk) = Promise.mapOk;
10 changes: 10 additions & 0 deletions src/fetch-js/examples/Simple.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Fetch_Js.(
{
get("https://httpbin.org/get")
->Promise.map(
fun
| Ok({Response.body, _}) => Js.log2("Body:", Body.toString(body))
| Error(err) => Js.log2("Error: ", err),
);
}
);
11 changes: 11 additions & 0 deletions src/fetch-js/examples/WithLetOperators.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
open LetOperators;

Fetch_Js.(
{
let.flatMapOk {Response.body, _} = get("https://httpbin.org/get");

Js.log(Body.toString(body));

Promise.resolved(Ok());
}
);
106 changes: 106 additions & 0 deletions src/fetch-js/src/Fetch_Js.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
[%bs.raw {| require("isomorphic-fetch") |}];

let decodeRequestMethod = meth => {
let methStringified = Method.toString(meth);

switch (methStringified) {
| "GET" => BsFetch.Bs_Fetch.Get
| "HEAD" => Head
| "POST" => Post
| "PUT" => Put
| "DELETE" => Delete
| "CONNECT" => Connect
| "OPTIONS" => Options
| "TRACE" => Trace
| "PATCH" => Patch
| otherMethod => Other(otherMethod)
};
};

module FetchImplementation = {
module Headers = Fetch_Core.Headers;
module Method = Fetch_Core.Method;
module Status = Fetch_Core.Status;

type promise('a) = Promise.t('a);
type result('a, 'error) = Belt.Result.t('a, 'error);

module Body = {
type t = string;

let make = body => body;

let toString = body => body;

let ofString = body => make(body);
};

module Response = {
module Body = Body;
module Status = Status;

type t = {
body: Body.t,
headers: list(Headers.t),
status: Status.t,
url: string,
};

let make = (~body, ~headers, ~status, ~url) => {
body,
headers,
status,
url,
};
};

let fetch = (~body=?, ~headers=[], ~meth=`GET, url) => {
let {Fetch_Core.Request.headers, body, meth, url} =
Fetch_Core.Request.create(~body, ~headers, ~meth, ~url);

let (promise, resolve) = Promise.pending();

BsFetch.Bs_Fetch.fetchWithInit(
url,
BsFetch.Bs_Fetch.RequestInit.make(
~method_=decodeRequestMethod(meth),
~body=
BsFetch.Bs_Fetch.BodyInit.make(
Belt.Option.getWithDefault(body, ""),
),
~headers=
BsFetch.Bs_Fetch.HeadersInit.makeWithDict(
Js.Dict.fromList(headers),
),
(),
),
)
|> Js.Promise.then_(response => {
BsFetch.Bs_Fetch.Response.text(response)
|> Js.Promise.then_(body => {
resolve(
Ok(
Response.make(
~status=
Status.make(
BsFetch.Bs_Fetch.Response.status(response),
),
~body=Body.make(body),
~headers,
~url,
),
),
);
Js.Promise.resolve();
})
})
|> Js.Promise.catch(error =>
Js.Promise.resolve(resolve(Error(Js.String.make(error))))
)
|> ignore;

promise;
};
};

include Fetch_Core.Fetchify.CreateFetchImplementation(FetchImplementation);
4 changes: 4 additions & 0 deletions src/fetch-js/src/Fetch_Js.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include
Fetch_Core.Fetchify.FETCHIFIED with
type promise('a) := Promise.t('a) and
type result('a, 'error) := Belt.Result.t('a, 'error);
Empty file.