Published: 28 Sep 2023
Low-level bindings to fetch for Melange.
melange-fetch is intended as a thin layer atop the JS API, upon which more high-level and idiomatic libraries can be made. Once such a library has been established, these bindings will likely be refactored to be even thinner.
Js.Promise.( Fetch.fetch("/api/hellos/1") |> then_(Fetch.Response.text) |> then_(text => print_endline(text) |> resolve) );
opam pin add melange-fetch.dev git+https://github.com/melange-community/melange-fetch.git#master
melange-fetch to the
libraries field in your dune file:
(melange.emit ;; or (library ...) ... (libraries melange-fetch))
fetch is a Web API that isn't available out-of-the-box in Node.js, and will therefore need to be polyfilled. isomorphic-fetch one such polyfill that seems to work well. Just install it via npm and add
[%raw "require('isomorphic-fetch')"] to the top of your main file, but be aware that there are some subtle differences and even parts that are missing entirely.
Response.type_to follow the name mangling convention trends
Headers.delete. They were missing the return type.
bs-platformto 5.0.4 to stop compiler from complaining.
HeadersInit.makeWithDictso as to be able to make headers in OCaml too
bs-platform>= 3.0.0 due to internal dependency on Js.boolean that has now been removed
Bs_fetchis kept around as an alias for backwards compatibility, but should be considered deprecated.