From 02740afd0c9975c05157f814e7e1e600b161137d Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Mon, 30 Mar 2020 23:20:48 +0200 Subject: [PATCH 01/11] Add initial BS-version --- .gitignore | 5 + bsconfig.json | 23 +++++ package-lock.json | 77 +++++++++++++++ package.json | 28 ++++++ src/fetch-js/.npmignore | 5 + src/fetch-js/README.md | 23 +++++ src/fetch-js/examples/Fetch_Js_Example.re | 46 +++++++++ src/fetch-js/src/Fetch_Js.re | 115 ++++++++++++++++++++++ src/fetch-js/src/Fetch_Js.rei | 4 + 9 files changed, 326 insertions(+) create mode 100644 bsconfig.json create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/fetch-js/.npmignore create mode 100644 src/fetch-js/README.md create mode 100644 src/fetch-js/examples/Fetch_Js_Example.re create mode 100644 src/fetch-js/src/Fetch_Js.re create mode 100644 src/fetch-js/src/Fetch_Js.rei diff --git a/.gitignore b/.gitignore index 0e461ce..b75478e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,8 @@ _release *.byte *.native *.install + +# BuckleScript + +lib/ +*.bs.js diff --git a/bsconfig.json b/bsconfig.json new file mode 100644 index 0000000..8804255 --- /dev/null +++ b/bsconfig.json @@ -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"] +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..c586450 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,77 @@ +{ + "name": "fetch-js", + "version": "0.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "bs-fetch": { + "version": "file:../bs-fetch", + "dependencies": { + "bs-platform": { + "version": "7.2.2", + "resolved": "https://registry.npmjs.org/bs-platform/-/bs-platform-7.2.2.tgz", + "integrity": "sha512-PWcFfN+jCTtT/rMaHDhKh+W9RUTpaRunmSF9vbLYcrJbpgCNW6aFKAY33u0P3mLxwuhshN3b4FxqGUBPj6exZQ==" + } + } + }, + "bs-platform": { + "version": "7.2.2", + "resolved": "https://registry.npmjs.org/bs-platform/-/bs-platform-7.2.2.tgz", + "integrity": "sha512-PWcFfN+jCTtT/rMaHDhKh+W9RUTpaRunmSF9vbLYcrJbpgCNW6aFKAY33u0P3mLxwuhshN3b4FxqGUBPj6exZQ==" + }, + "encoding": { + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz", + "integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=", + "requires": { + "iconv-lite": "~0.4.13" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "is-stream": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" + }, + "isomorphic-fetch": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz", + "integrity": "sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk=", + "requires": { + "node-fetch": "^1.0.1", + "whatwg-fetch": ">=0.10.0" + } + }, + "node-fetch": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz", + "integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==", + "requires": { + "encoding": "^0.1.11", + "is-stream": "^1.0.1" + } + }, + "reason-promise": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/reason-promise/-/reason-promise-1.0.2.tgz", + "integrity": "sha512-j8DWV+71wNEKQmyW6zBOowIZq1Qec5CDWyLI37BvgOmAZgRFGFQ1MaJnbhqDe3JsYmaGyqdNMmpCJLl9EDbDAg==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "whatwg-fetch": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz", + "integrity": "sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..9a96206 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "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/Fetch_Js_Example.bs.js" + }, + "dependencies": { + "bs-fetch": "../bs-fetch", + "reason-promise": "^1.0.2", + "bs-platform": "latest", + "isomorphic-fetch": "^2.2.1" + }, + "devDependencies": { + "bs-platform": "latest" + }, + "peerDependencies": { + "bs-platform": ">= 5.0.0" + }, + "files": [ + "src/fetch-js/Fetch.rei", + "src/fetch-js/Fetch.re", + "bsconfig.json" + ] +} diff --git a/src/fetch-js/.npmignore b/src/fetch-js/.npmignore new file mode 100644 index 0000000..a069fd3 --- /dev/null +++ b/src/fetch-js/.npmignore @@ -0,0 +1,5 @@ +.DS_Store +*.install +*.merlin +*.lock +_esy diff --git a/src/fetch-js/README.md b/src/fetch-js/README.md new file mode 100644 index 0000000..b8791cd --- /dev/null +++ b/src/fetch-js/README.md @@ -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) + | Error(errorMsg) => errorMsg->Promise.resolved, + ) + ->Promise.map(Js.log) +); +``` + +See [the examples](./examples/). diff --git a/src/fetch-js/examples/Fetch_Js_Example.re b/src/fetch-js/examples/Fetch_Js_Example.re new file mode 100644 index 0000000..48dd704 --- /dev/null +++ b/src/fetch-js/examples/Fetch_Js_Example.re @@ -0,0 +1,46 @@ +Fetch_Js.( + get("https://httpbin.org/get") + ->Promise.map( + fun + | Ok({Response.status, _}) when Fetch_Js.Status.isSuccessful(status) => "Success!" + | _ => "That's anything but successful. :-(", + ) + ->Promise.get(Js.log) +); + +Fetch_Js.( + post( + "https://httpbin.org/post", + ~headers=[("Authorization", "Bearer xyz")], + ~body="Hello, World!", + ) + ->Promise.flatMap( + fun + | Ok({Response.body, _}) => Body.toString(body) + | Error(errorMsg) => errorMsg->Promise.resolved, + ) + ->Promise.map(Js.log) +); + +let jsonBody = Js.Json.parseExn({| +{ + "foo": "bar" +} +|}); + +Fetch_Js.( + post( + "https://httpbin.org/post", + ~headers=[ + ("Authorization", "Bearer xyz"), + ("content-type", "application/json"), + ], + ~body=Js.Json.stringify(jsonBody), + ) + ->Promise.flatMap( + fun + | Ok({Response.body, _}) => Body.toString(body) + | Error(errorMsg) => errorMsg->Promise.resolved, + ) + ->Promise.map(Js.log) +); \ No newline at end of file diff --git a/src/fetch-js/src/Fetch_Js.re b/src/fetch-js/src/Fetch_Js.re new file mode 100644 index 0000000..a217072 --- /dev/null +++ b/src/fetch-js/src/Fetch_Js.re @@ -0,0 +1,115 @@ +[%bs.raw {| require("isomorphic-fetch") |}]; + +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 = Js.Promise.t(string); + + let make = response => { + let body = BsFetch.Bs_Fetch.Response.text(response); + body; + }; + + let toString = body => { + let (promise, resolve) = Promise.pending(); + + body + |> Js.Promise.then_(body => { + resolve(body); + Js.Promise.resolve(); + }) + |> ignore; + + promise; + }; + + let ofString = body => Js.Promise.resolve(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(); + + 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) + }; + }; + + 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 => { + resolve( + Ok( + Response.make( + ~status= + Status.make(BsFetch.Bs_Fetch.Response.status(response)), + ~body=Body.make(response), + ~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); \ No newline at end of file diff --git a/src/fetch-js/src/Fetch_Js.rei b/src/fetch-js/src/Fetch_Js.rei new file mode 100644 index 0000000..8d5e3ce --- /dev/null +++ b/src/fetch-js/src/Fetch_Js.rei @@ -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); From 0daff8551ebbac9d74d90961fb6722baed21749c Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Mon, 30 Mar 2020 23:42:43 +0200 Subject: [PATCH 02/11] Use jareds backported let-branch --- package-lock.json | 5 +-- package.json | 5 +-- src/fetch-js/examples/Fetch_Js_Example.re | 30 +++++++++++-- src/fetch-js/src/Fetch_Js.re | 53 ++++++++++------------- 4 files changed, 52 insertions(+), 41 deletions(-) diff --git a/package-lock.json b/package-lock.json index c586450..4d8f832 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,9 +15,8 @@ } }, "bs-platform": { - "version": "7.2.2", - "resolved": "https://registry.npmjs.org/bs-platform/-/bs-platform-7.2.2.tgz", - "integrity": "sha512-PWcFfN+jCTtT/rMaHDhKh+W9RUTpaRunmSF9vbLYcrJbpgCNW6aFKAY33u0P3mLxwuhshN3b4FxqGUBPj6exZQ==" + "version": "github:jaredly/bucklescript#32a426d4ef57097fdab11437773f4c8decaaae60", + "from": "github:jaredly/bucklescript#letop" }, "encoding": { "version": "0.1.12", diff --git a/package.json b/package.json index 9a96206..683ff15 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,9 @@ "dependencies": { "bs-fetch": "../bs-fetch", "reason-promise": "^1.0.2", - "bs-platform": "latest", + "bs-platform": "jaredly/bucklescript#letop", "isomorphic-fetch": "^2.2.1" }, - "devDependencies": { - "bs-platform": "latest" - }, "peerDependencies": { "bs-platform": ">= 5.0.0" }, diff --git a/src/fetch-js/examples/Fetch_Js_Example.re b/src/fetch-js/examples/Fetch_Js_Example.re index 48dd704..db3bded 100644 --- a/src/fetch-js/examples/Fetch_Js_Example.re +++ b/src/fetch-js/examples/Fetch_Js_Example.re @@ -1,3 +1,27 @@ +module LetOperators = { + let (let.flatMapOk) = (promise, fn) => + Promise.flatMap( + promise, + fun + | Ok(response) => fn(response) + | Error(e) => Promise.resolved(Error(e)), + ); + + let (let.flatMap) = (promise, fn) => Promise.flatMap(promise, fn); +}; + +open LetOperators; + +Fetch_Js.( + { + let.flatMapOk {Response.body, _} = get("https://httpbin.org/get"); + + Js.log(Body.toString(body)); + + Promise.resolved(Ok()); + } +); + Fetch_Js.( get("https://httpbin.org/get") ->Promise.map( @@ -16,7 +40,7 @@ Fetch_Js.( ) ->Promise.flatMap( fun - | Ok({Response.body, _}) => Body.toString(body) + | Ok({Response.body, _}) => Body.toString(body)->Promise.resolved | Error(errorMsg) => errorMsg->Promise.resolved, ) ->Promise.map(Js.log) @@ -39,8 +63,8 @@ Fetch_Js.( ) ->Promise.flatMap( fun - | Ok({Response.body, _}) => Body.toString(body) + | Ok({Response.body, _}) => Body.toString(body)->Promise.resolved | Error(errorMsg) => errorMsg->Promise.resolved, ) ->Promise.map(Js.log) -); \ No newline at end of file +); diff --git a/src/fetch-js/src/Fetch_Js.re b/src/fetch-js/src/Fetch_Js.re index a217072..c2f06b3 100644 --- a/src/fetch-js/src/Fetch_Js.re +++ b/src/fetch-js/src/Fetch_Js.re @@ -9,27 +9,13 @@ module FetchImplementation = { type result('a, 'error) = Belt.Result.t('a, 'error); module Body = { - type t = Js.Promise.t(string); + type t = string; - let make = response => { - let body = BsFetch.Bs_Fetch.Response.text(response); - body; - }; - - let toString = body => { - let (promise, resolve) = Promise.pending(); + let make = body => body; - body - |> Js.Promise.then_(body => { - resolve(body); - Js.Promise.resolve(); - }) - |> ignore; - - promise; - }; + let toString = body => body; - let ofString = body => Js.Promise.resolve(body); + let ofString = body => make(body); }; module Response = { @@ -90,18 +76,23 @@ module FetchImplementation = { ), ) |> Js.Promise.then_(response => { - resolve( - Ok( - Response.make( - ~status= - Status.make(BsFetch.Bs_Fetch.Response.status(response)), - ~body=Body.make(response), - ~headers, - ~url, - ), - ), - ); - Js.Promise.resolve(); + 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)))) @@ -112,4 +103,4 @@ module FetchImplementation = { }; }; -include Fetch_Core.Fetchify.CreateFetchImplementation(FetchImplementation); \ No newline at end of file +include Fetch_Core.Fetchify.CreateFetchImplementation(FetchImplementation); From f4f84acdfb00a03c6525c6a9aa10e1554b08e368 Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Mon, 30 Mar 2020 23:49:02 +0200 Subject: [PATCH 03/11] Update JSON-example --- src/fetch-js/examples/Fetch_Js_Example.re | 29 ++++++++++++----------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/fetch-js/examples/Fetch_Js_Example.re b/src/fetch-js/examples/Fetch_Js_Example.re index db3bded..9ac0389 100644 --- a/src/fetch-js/examples/Fetch_Js_Example.re +++ b/src/fetch-js/examples/Fetch_Js_Example.re @@ -53,18 +53,19 @@ let jsonBody = Js.Json.parseExn({| |}); Fetch_Js.( - post( - "https://httpbin.org/post", - ~headers=[ - ("Authorization", "Bearer xyz"), - ("content-type", "application/json"), - ], - ~body=Js.Json.stringify(jsonBody), - ) - ->Promise.flatMap( - fun - | Ok({Response.body, _}) => Body.toString(body)->Promise.resolved - | Error(errorMsg) => errorMsg->Promise.resolved, - ) - ->Promise.map(Js.log) + { + 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()); + } ); From 6a7a83fecab1c95239d7b1bcc30be11c9b774c1b Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Tue, 31 Mar 2020 00:02:54 +0200 Subject: [PATCH 04/11] Point to forked bs-fetch --- package-lock.json | 10 ++-------- package.json | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4d8f832..e738398 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,14 +5,8 @@ "requires": true, "dependencies": { "bs-fetch": { - "version": "file:../bs-fetch", - "dependencies": { - "bs-platform": { - "version": "7.2.2", - "resolved": "https://registry.npmjs.org/bs-platform/-/bs-platform-7.2.2.tgz", - "integrity": "sha512-PWcFfN+jCTtT/rMaHDhKh+W9RUTpaRunmSF9vbLYcrJbpgCNW6aFKAY33u0P3mLxwuhshN3b4FxqGUBPj6exZQ==" - } - } + "version": "github:lessp/bs-fetch#758b76406ce20f2885a81de89c55ce22a370ceba", + "from": "github:lessp/bs-fetch#758b764" }, "bs-platform": { "version": "github:jaredly/bucklescript#32a426d4ef57097fdab11437773f4c8decaaae60", diff --git a/package.json b/package.json index 683ff15..4df8c59 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "example": "bsb -make-world && node src/fetch-js/examples/Fetch_Js_Example.bs.js" }, "dependencies": { - "bs-fetch": "../bs-fetch", + "bs-fetch": "lessp/bs-fetch#758b764", "reason-promise": "^1.0.2", "bs-platform": "jaredly/bucklescript#letop", "isomorphic-fetch": "^2.2.1" From 6c030f0b4b41742e2939292f2d3b3ce50f7f92e6 Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Tue, 31 Mar 2020 00:07:39 +0200 Subject: [PATCH 05/11] Update example in JS-README --- src/fetch-js/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fetch-js/README.md b/src/fetch-js/README.md index b8791cd..d9e7628 100644 --- a/src/fetch-js/README.md +++ b/src/fetch-js/README.md @@ -13,7 +13,7 @@ Fetch_Js.( ) ->Promise.flatMap( fun - | Ok({Response.body, _}) => Body.toString(body) + | Ok({Response.body, _}) => Body.toString(body)->Promise.resolved | Error(errorMsg) => errorMsg->Promise.resolved, ) ->Promise.map(Js.log) From 448476be7c7f49a2354ed0f0c05524f060d3c0ee Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Tue, 31 Mar 2020 00:20:55 +0200 Subject: [PATCH 06/11] Use Belt-result --- src/fetch-js/examples/Fetch_Js_Example.re | 6 ++---- test/fetch-core/TestFramework.re | 2 +- test/fetch-core/__snapshots__/placeholder | 0 3 files changed, 3 insertions(+), 5 deletions(-) delete mode 100644 test/fetch-core/__snapshots__/placeholder diff --git a/src/fetch-js/examples/Fetch_Js_Example.re b/src/fetch-js/examples/Fetch_Js_Example.re index 9ac0389..93902df 100644 --- a/src/fetch-js/examples/Fetch_Js_Example.re +++ b/src/fetch-js/examples/Fetch_Js_Example.re @@ -4,7 +4,7 @@ module LetOperators = { promise, fun | Ok(response) => fn(response) - | Error(e) => Promise.resolved(Error(e)), + | Error(e) => Promise.resolved(Belt.Result.Error(e)), ); let (let.flatMap) = (promise, fn) => Promise.flatMap(promise, fn); @@ -15,7 +15,6 @@ open LetOperators; Fetch_Js.( { let.flatMapOk {Response.body, _} = get("https://httpbin.org/get"); - Js.log(Body.toString(body)); Promise.resolved(Ok()); @@ -63,9 +62,8 @@ Fetch_Js.( ], ~body=Js.Json.stringify(jsonBody), ); - Js.log2("Parse JSON: ", Body.toString(body)->Js.Json.parseExn); Promise.resolved(Ok()); } -); +); \ No newline at end of file diff --git a/test/fetch-core/TestFramework.re b/test/fetch-core/TestFramework.re index 935ea94..3ec033b 100644 --- a/test/fetch-core/TestFramework.re +++ b/test/fetch-core/TestFramework.re @@ -1,7 +1,7 @@ include Rely.Make({ let config = Rely.TestFrameworkConfig.initialize({ - snapshotDir: "test/fetch-core/__snapshots__", + snapshotDir: "./test/fetch-core/__snapshots__", projectDir: "", }); }); diff --git a/test/fetch-core/__snapshots__/placeholder b/test/fetch-core/__snapshots__/placeholder deleted file mode 100644 index e69de29..0000000 From 7dfb8762b606941fbb94d3d3c6b8991f7e5b2e65 Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Tue, 31 Mar 2020 00:27:40 +0200 Subject: [PATCH 07/11] Formatting --- src/fetch-js/examples/Fetch_Js_Example.re | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fetch-js/examples/Fetch_Js_Example.re b/src/fetch-js/examples/Fetch_Js_Example.re index 93902df..f54e005 100644 --- a/src/fetch-js/examples/Fetch_Js_Example.re +++ b/src/fetch-js/examples/Fetch_Js_Example.re @@ -66,4 +66,4 @@ Fetch_Js.( Promise.resolved(Ok()); } -); \ No newline at end of file +); From 78c61e9a6fec14ce66a45925bdb27e5a4bd03d28 Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Tue, 31 Mar 2020 16:20:40 +0200 Subject: [PATCH 08/11] Separate examples --- package.json | 2 +- src/fetch-js/examples/Fetch_Js_Example.re | 69 ----------------------- src/fetch-js/examples/GetRequest.re | 9 +++ src/fetch-js/examples/JsonPostRequest.re | 25 ++++++++ src/fetch-js/examples/LetOperators.re | 11 ++++ src/fetch-js/examples/WithLetOperators.re | 11 ++++ 6 files changed, 57 insertions(+), 70 deletions(-) delete mode 100644 src/fetch-js/examples/Fetch_Js_Example.re create mode 100644 src/fetch-js/examples/GetRequest.re create mode 100644 src/fetch-js/examples/JsonPostRequest.re create mode 100644 src/fetch-js/examples/LetOperators.re create mode 100644 src/fetch-js/examples/WithLetOperators.re diff --git a/package.json b/package.json index 4df8c59..508376a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "clean": "bsb -clean-world", "build": "bsb -make-world", "watch": "bsb -make-world -w", - "example": "bsb -make-world && node src/fetch-js/examples/Fetch_Js_Example.bs.js" + "example": "bsb -make-world && node src/fetch-js/examples/WithLetOperators.bs.js" }, "dependencies": { "bs-fetch": "lessp/bs-fetch#758b764", diff --git a/src/fetch-js/examples/Fetch_Js_Example.re b/src/fetch-js/examples/Fetch_Js_Example.re deleted file mode 100644 index f54e005..0000000 --- a/src/fetch-js/examples/Fetch_Js_Example.re +++ /dev/null @@ -1,69 +0,0 @@ -module LetOperators = { - 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); -}; - -open LetOperators; - -Fetch_Js.( - { - let.flatMapOk {Response.body, _} = get("https://httpbin.org/get"); - Js.log(Body.toString(body)); - - Promise.resolved(Ok()); - } -); - -Fetch_Js.( - get("https://httpbin.org/get") - ->Promise.map( - fun - | Ok({Response.status, _}) when Fetch_Js.Status.isSuccessful(status) => "Success!" - | _ => "That's anything but successful. :-(", - ) - ->Promise.get(Js.log) -); - -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) -); - -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()); - } -); diff --git a/src/fetch-js/examples/GetRequest.re b/src/fetch-js/examples/GetRequest.re new file mode 100644 index 0000000..d93bb24 --- /dev/null +++ b/src/fetch-js/examples/GetRequest.re @@ -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) +); diff --git a/src/fetch-js/examples/JsonPostRequest.re b/src/fetch-js/examples/JsonPostRequest.re new file mode 100644 index 0000000..076b583 --- /dev/null +++ b/src/fetch-js/examples/JsonPostRequest.re @@ -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()); + } +); diff --git a/src/fetch-js/examples/LetOperators.re b/src/fetch-js/examples/LetOperators.re new file mode 100644 index 0000000..06d63b2 --- /dev/null +++ b/src/fetch-js/examples/LetOperators.re @@ -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; diff --git a/src/fetch-js/examples/WithLetOperators.re b/src/fetch-js/examples/WithLetOperators.re new file mode 100644 index 0000000..e4a9174 --- /dev/null +++ b/src/fetch-js/examples/WithLetOperators.re @@ -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()); + } +); From c2c128e88f843cbe021d3e71556b787c9e5086e1 Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Sun, 26 Apr 2020 16:51:44 +0200 Subject: [PATCH 09/11] chore: separate helper-function --- src/fetch-js/src/Fetch_Js.re | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/fetch-js/src/Fetch_Js.re b/src/fetch-js/src/Fetch_Js.re index c2f06b3..50be56c 100644 --- a/src/fetch-js/src/Fetch_Js.re +++ b/src/fetch-js/src/Fetch_Js.re @@ -1,5 +1,22 @@ [%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; @@ -43,23 +60,6 @@ module FetchImplementation = { let (promise, resolve) = Promise.pending(); - 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) - }; - }; - BsFetch.Bs_Fetch.fetchWithInit( url, BsFetch.Bs_Fetch.RequestInit.make( From 630b7a50b742a351bcf8a52de4db880c225cf978 Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Sun, 26 Apr 2020 16:57:01 +0200 Subject: [PATCH 10/11] add simple example --- src/fetch-js/examples/Simple.re | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/fetch-js/examples/Simple.re diff --git a/src/fetch-js/examples/Simple.re b/src/fetch-js/examples/Simple.re new file mode 100644 index 0000000..ff27314 --- /dev/null +++ b/src/fetch-js/examples/Simple.re @@ -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), + ); + } +); \ No newline at end of file From c68157e8b8aeb1a916e0d1ad3e3f3d0f77c802a0 Mon Sep 17 00:00:00 2001 From: Tom Ekander Date: Sun, 26 Apr 2020 17:07:33 +0200 Subject: [PATCH 11/11] chore: formatting --- src/fetch-js/examples/Simple.re | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fetch-js/examples/Simple.re b/src/fetch-js/examples/Simple.re index ff27314..08982f5 100644 --- a/src/fetch-js/examples/Simple.re +++ b/src/fetch-js/examples/Simple.re @@ -7,4 +7,4 @@ Fetch_Js.( | Error(err) => Js.log2("Error: ", err), ); } -); \ No newline at end of file +);