Skip to content

Commit 1ed576d

Browse files
committedNov 12, 2024
Add philosophy and project status
1 parent 8d54170 commit 1ed576d

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
 

‎docs/content/docs/philosophy.mdx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: Design Philosophy
3+
description: Learn more about the design philosophy of @rescript/webapi.
4+
---
5+
6+
The core idea of these ReScript bindings is that each interface is modeled as a record type. Where possible, inheritance is represented using record spreading, and methods are modeled as functions in a separate module. This design allows for greater familiarity with the underlying JavaScript APIs, making it more friendly for newcomers.
7+
8+
## ReScript v12
9+
10+
These bindings utilize new features introduced in ReScript v12; thus, they are not compatible with older versions of ReScript.
11+
12+
## Web APIs
13+
14+
The bindings are generated from the [MDN Web API documentation](https://developer.mozilla.org/en-US/docs/Web/API). This project aims to reflect a 1:1 mapping of the Web API to the ReScript bindings whenever possible.
15+
16+
In other words, if you are searching for a specific JavaScript binding, begin your journey at the [MDN Web API documentation](https://developer.mozilla.org/en-US/docs/Web/API) and determine which module contains your sample. Ensure that the module is available in the bindings by checking the specific API. Please [open an issue](https://github.com/rescript-lang/experimental-rescript-webapi/issues/new/choose) if you require an API that is not yet present.
17+
18+
Each API will have its interface and auxiliary types in a module named after the API, suffixed with `API` to prevent collisions with the type module.
19+
20+
```ReScript
21+
open Global
22+
open DOMAPI
23+
24+
let myElement: element = document->Document.createElement( ~localName = "div")
25+
```
26+
27+
## Interfaces
28+
29+
Since ReScript does not have the concept of classes or interfaces, the bindings represent the interfaces as record types. These record types can inherit properties from a "base" type through spreading. This can only be done if there are no circular references in the inheritance chain. If circular references exist, spreading is avoided, and the base properties are duplicated instead.
30+
31+
## Methods
32+
33+
Methods are modeled as functions in a separate module. The idea is that these will be accessible via the `->` operator. For better discoverability, [tooling enhancements](https://github.com/rescript-lang/rescript-vscode/pull/1054) are planned to simplify finding the correct method.
34+
35+
Inherited methods are duplicated in the inheriting module to eliminate the need to cast the type to the base type.
36+
37+
### Overloads
38+
39+
JavaScript supports function overloads, where a function can have multiple signatures. In ReScript, this is not possible, and a method can have multiple bindings with slightly different names. By entering the correct name, tooling should detect all variations of the method.
40+
41+
## Type Conversion
42+
43+
In some cases, type conversion will be required. Subtypes can safely be cast to their base type using conversion helpers within their module.
44+
45+
```ReScript
46+
let element: element = document->Document.createElement( ~localName = "div")
47+
let node: node = element->Element.asNode
48+
```
49+
50+
Any other conversions can be performed using the `Prelude.unsafeConversation` helper. This should be done with caution, as it can lead to runtime errors.
51+
52+
```ReScript
53+
let element: element = document->Document.createElement( ~localName = "div")
54+
// This is potentially unsafe, as the type system cannot guarantee the conversion
55+
let divElement: htmlDivElement = element->Prelude.unsafeConversation
56+
```

‎docs/content/docs/project-status.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Project Status
3+
description: Learn more about the current status of @rescript/webapi.
4+
---
5+
6+
This project is highly experimental and subject to change.
7+
It is a work in progress and might not cover every API.
8+
The core idea is to prioritize ground coverage based on community needs.
9+
We aim to focus on the most used APIs first and then expand from there.
10+
11+
## Contributions
12+
13+
Contributions are welcome! Please [open an issue](https://github.com/rescript-lang/experimental-rescript-webapi/issues/new/choose) if you have a specific API you would like to see added.
14+
This really helps prioritize our work and ensures that the most important APIs are addressed first.
15+
16+
## Current Status
17+
18+
Currently, all bindings are generated using a modified version of [TypeScript-DOM-lib-generator](https://github.com/microsoft/TypeScript-DOM-lib-generator).
19+
Eventually, the generation may reach a level where manual tweaking can enhance the developer experience.
20+
In the short term, generation could still be improved.
21+
22+
Coding contributions are encouraged, but please be aware that the project is still in a very experimental stage.
23+
It might be a little daunting and rough to jump right in.
24+
25+
## Future
26+
27+
Our goal is to make this a true community project. Consumers of the bindings should be able to contribute back to the project with minimal hassle.
28+
This means that the project should be easy to understand and contribute to. Once a contribution is accepted, a new release will follow shortly.

0 commit comments

Comments
 (0)
Please sign in to comment.