Skip to content
Merged
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
26 changes: 26 additions & 0 deletions docs/content/docs/api-surface.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ WebAPI.URL WebAPI.Fetch WebAPI.UIEvents
WebAPI.Observers WebAPI.Media WebAPI.WebAudio
WebAPI.Storage WebAPI.Messaging WebAPI.Workers
WebAPI.Crypto WebAPI.Performance WebAPI.ViewTransitions
WebAPI.History WebAPI.Location
WebAPI.SafeFetch
```

Expand All @@ -106,6 +107,31 @@ helpers can select only the safe feature:
The `let?` examples below require the experimental `LetUnwrap` setting with the current ReScript 13
alpha compiler. The SafeFetch API itself can also be used with ordinary `switch` expressions.

### DOM types without the Window bundle

`WebAPI.History` independently exposes `WebAPI.History` and its existing
`WebAPI.History.Types.history` type. `WebAPI.Location` exposes `WebAPI.Location`
and depends only on the core `WebAPI.DOM` feature. `WebAPI.Window` includes both
features, so existing Window consumers keep the same API and type identities.

For React bindings that need element, event, window, document, history, and location
types without the complete Window API, select:

```json
{
"dependencies": [
{
"name": "@rescript/webapi",
"features": ["WebAPI.DOMNodes", "WebAPI.History", "WebAPI.Location"]
}
]
}
```

DOMNodes already includes DOM transitively. This selection does not expose
`WebAPI.Window` or `WebAPI.Document`; add `WebAPI.Window` if you need their methods.
An unrestricted WebAPI dependency elsewhere in the dependency graph enables all features.

## Fetch

Use `WebAPI.Fetch.fetch` for string URLs and `WebAPI.Fetch.fetchWithRequest`
Expand Down
18 changes: 16 additions & 2 deletions rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
"DOMNodes"
],
"WebAPI.HTML": ["WebAPI.DOMNodes", "WebAPI.File", "HTML"],
"WebAPI.History": ["History"],
"WebAPI.Location": ["WebAPI.DOM", "Location"],
"WebAPI.Window": [
"WebAPI.History",
"WebAPI.Location",
"WebAPI.HTML",
"WebAPI.Media",
"WebAPI.Storage",
Expand Down Expand Up @@ -294,16 +298,26 @@
"HTMLUnknownElement"
]
},
{
"dir": "src/history",
"subdirs": true,
"feature": "History",
"public": ["History"]
},
{
"dir": "src/location",
"subdirs": true,
"feature": "Location",
"public": ["Location"]
},
{
"dir": "src/window",
"subdirs": true,
"feature": "Window",
"public": [
"CustomElementRegistry",
"Document",
"History",
"IdleDeadline",
"Location",
"VisualViewport",
"Window",
"XPathExpression",
Expand Down
4 changes: 3 additions & 1 deletion scripts/check-features.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const expectedFeatureOwners = new Map([
["WebAPI.DOMNodes", "DOMNodes"],
["WebAPI.File", "File"],
["WebAPI.HTML", "HTML"],
["WebAPI.History", "History"],
["WebAPI.Location", "Location"],
["WebAPI.Window", "Window"],
["WebAPI.CSSOM", "CSSOM"],
["WebAPI.CSSFontLoading", "CSSFontLoading"],
Expand Down Expand Up @@ -237,7 +239,7 @@ const validateCompletionAliases = (sourceEntries) => {
);
};

const validateConfig = (config) => {
export const validateConfig = (config) => {
const featureEntries = Object.entries(config.features ?? {});
const sourceEntries = (config.sources ?? []).filter(
(source) => source !== null && typeof source === "object" && typeof source.feature === "string",
Expand Down
33 changes: 32 additions & 1 deletion scripts/check-features.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";

import { isMissingSafeFetchDiagnostic, parsePublicTypeAlias } from "./check-features.mjs";
import {
isMissingSafeFetchDiagnostic,
parsePublicTypeAlias,
validateConfig,
} from "./check-features.mjs";

test("parses a public type alias without type parameters", () => {
assert.deepEqual(parsePublicTypeAlias("type t = DOMTree.element"), {
Expand All @@ -17,6 +22,32 @@ test("parses a public type alias with type parameters", () => {
});
});

const config = JSON.parse(readFileSync(new URL("../rescript.json", import.meta.url), "utf8"));

test("validates independent History and Location feature ownership", () => {
assert.deepEqual(validateConfig(config), []);
assert.deepEqual(config.features["WebAPI.History"], ["History"]);
assert.deepEqual(config.features["WebAPI.Location"], ["WebAPI.DOM", "Location"]);
assert.ok(config.features["WebAPI.Window"].includes("WebAPI.History"));
assert.ok(config.features["WebAPI.Window"].includes("WebAPI.Location"));
});

test("rejects a missing History feature", () => {
const features = Object.fromEntries(
Object.entries(config.features).filter(([name]) => name !== "WebAPI.History"),
);
assert.ok(
validateConfig({ ...config, features }).some((error) => error.includes("Expected exactly")),
);
});

test("rejects duplicate Location public ownership", () => {
const sources = config.sources.map((source) =>
source.feature === "Window" ? { ...source, public: [...source.public, "Location"] } : source,
);
assert.ok(validateConfig({ ...config, sources }).includes("Duplicate public modules: Location"));
});

test("recognizes the unavailable SafeFetch module diagnostic across line breaks", () => {
assert.equal(
isMissingSafeFetchDiagnostic("The module or file WebAPI.SafeFetch\ncan't be found."),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading