Skip to content

API Modelling

Bindings follow the browser API where that gives consumers a useful, type-safe ReScript interface. Source files are grouped by API family, but applications use flat public owner modules such as WebAPI.Fetch, WebAPI.Request, and WebAPI.Element. Internal *Types modules are not application-facing namespaces.

JavaScript can expose several signatures for one operation. ReScript cannot overload one value name, so give variants distinct names that describe their input rather than appending numbers. For example, fetch takes a URL and fetchWithRequest takes a Request:

let response = await WebAPI.Fetch.fetch("https://example.com")
let request = WebAPI.Request.fromURL("https://example.com")
let responseFromRequest = await WebAPI.Fetch.fetchWithRequest(request)

When several signatures differ only by optional arguments, keep a single binding with an optional labeled argument when the browser API treats it as one operation.

Constructors use a distinct naming convention:

  • Keep singleton constructors and true default constructors named make.
  • Name typed constructors from* after the source type, such as fromString, fromArrayBuffer, or fromURLWithProtocols.
  • Split a hidden default constructor into make() plus typed from* constructors rather than leaving optional input on make.

For example, a DOMMatrix has a true default constructor alongside string and array forms:

let identity = WebAPI.DOMMatrix.make()
let fromTransform = WebAPI.DOMMatrix.fromString("matrix(1, 0, 0, 1, 0, 0)")
let fromComponents = WebAPI.DOMMatrix.fromArray([1., 0., 0., 1., 0., 0.])

Keep comments on each binding specific to the input shape and include its source type, a consumer example, and links to the relevant MDN or ReScript type documentation. See the documentation guide for the comment format.

Some browser properties are untagged unions in JavaScript. Model these as a small public conversion/decoding API when consumers need to distinguish their variants; do not expose the underlying implementation type just to make it nameable. For example, the public WebAPI.FillStyle module provides conversion helpers and a decoder for Canvas fill styles.

Keep null and empty states explicit when they are part of the browser contract. Use Null.t for nullable results and variants for meaningful empty values; do not replace native results with invented defaults or unchecked casts.

Add compile-only, Node runtime, or browser runtime fixtures according to the API being modeled. The testing guide explains tests/fixtures.json, npm test, npm run test:browser, and packed consumer checks.