Registry / devops / bs-fetch

bs-fetch

JSON →
library0.6.2jsnpmunverified

Low-level OCaml/ReasonML bindings to the Fetch API for BuckleScript (Belt/bs-platform). Current stable version is 0.6.2, last released in 2019. The package provides thin, type-safe bindings to fetch, Response, Headers, Request, AbortController, and FormData. It targets BuckleScript 5+ and is intended as a foundation for higher-level libraries. Unlike many other fetch bindings, it follows the ML naming conventions (e.g., `Response.type_` instead of `Response._type`) and requires a polyfill in Node.js. The package is no longer actively maintained; users should consider migrating to newer solutions like `promise-fetch` or direct `Js.Promise` + `fetch`.

npm install bs-fetch
INSTALL
IMPORT
SIG · BS-FETCH
B
bs-fetch
devopsjavascriptv0.6.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetch
let fetch = Fetch.fetch;
let fetch = Bs_fetch.fetch;
Bs_fetch is deprecated since 0.2.0, use Fetch module instead.
Response
open Fetch; let response = Response.make();
let response = Bs_fetch.Response.make();
Use the Fetch module; Bs_fetch is deprecated.
Headers
open Fetch; let headers = Headers.make();
let headers = Fetch.Headers.makeWithDict(Js.Dict.fromList([("Content-Type", "application/json")]));
Headers.makeWithDict is available but make() with array pairs is more common in OCaml.
FormData
open Fetch; let formData = FormData.make();
let formData = Fetch.FormData.makeWithDict(...);
FormData was added in 0.6.2 and does not have a makeWithDict variant.

Shows how to use bs-fetch to perform a GET request and parse JSON response, including error handling and Node polyfill setup.

// Install: npm install bs-fetch // bsconfig.json: "bs-dependencies": ["bs-fetch"] // In your ReasonML file: let fetchJson = (url: string) => { open Fetch; Js.Promise.( Fetch.fetch(url) |> then_(Response.json) |> then_(json => { Js.log(json); resolve(json); }) |> catch(err => { Js.log2("Fetch failed", err); resolve(Js.Json.Null); }) ); }; // For Node.js, add at top entry file: [%raw "require('isomorphic-fetch')"];
Debug
Known issues
breakingIn 0.6.0, Response._type was renamed to Response.type_ to follow name mangling conventions.
fix
Replace all uses of `Response._type` with `Response.type_`.
affects: >=0.6.0
deprecatedThe module Bs_fetch is deprecated since 0.2.0. Use Fetch instead.
fix
Change `Bs_fetch.xxx` to `Fetch.xxx`.
affects: >=0.2.0
breakingIn 0.5.0, Headers.append and Headers.delete now return unit instead of being void.
fix
Ensure code that used the return values of these functions is updated.
affects: >=0.5.0
gotchaFetch API is not available in Node.js by default. You must provide a polyfill like isomorphic-fetch.
fix
Install isomorphic-fetch and add `[%raw "require('isomorphic-fetch')"]` at the top of your entry file.
affects: all
gotchaHeaders.makeWithDict is available only for OCaml users; ReasonML users may prefer Headers.make with array pairs.
fix
Use Headers.make([("key", "value")]) or Headers.makeWithDict(Js.Dict.fromList(...)) accordingly.
affects: >=0.3.0
deprecatedThe package is no longer actively maintained. Last release was 2019.
fix
Consider migrating to newer libraries like promise-fetch or directly using Js.Promise with the global fetch binding.
affects: all
Errors
Common errors & fixes
Error: Unbound module Bs_fetch
The deprecated Bs_fetch module is not available in newer versions; use Fetch instead.
fix
Replace `Bs_fetch` with `Fetch` in your code.
ReferenceError: fetch is not defined (Node.js)
fetch API is not available in Node.js without polyfill.
fix
Install isomorphic-fetch: `npm install isomorphic-fetch` and add `[%raw "require('isomorphic-fetch')"]` at the top of your main file.
Error: The variant type Response._type is not accessible (0.6.0+)
Response._type was renamed to Response.type_ in version 0.6.0.
fix
Use `Response.type_` instead of `Response._type`.
Error: Unbound value Headers.makeWithDict (pre-0.3.0)
Headers.makeWithDict was added in 0.3.0.
fix
Upgrade to bs-fetch >=0.3.0 or use Headers.make with array pairs.
Upgrade
Version history
0.6.2latest on npm
Audit
Dependencies
isomorphic-fetchoptionalRequired as a polyfill for Node.js since fetch is a Web API not available natively in Node.
bs-platformrequiredRequired as a peer dependency; the bindings rely on BuckleScript/bs-platform's Js module.
Agent activity
17 hits · last 30 days
node
16
Resources
bs-fetch — npm install bs-fetch · libregistry