Skip to content

Commit 55e32d6

Browse files
committed
Add Module Type Structure
1 parent 58b5390 commit 55e32d6

File tree

6 files changed

+93
-7
lines changed

6 files changed

+93
-7
lines changed

docs/content/docs/contributing/api-modelling.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: API Modelling
33
description: Learn more about the API modelling process of @rescript/webapi.
4-
slug: "04-api-modelling"
4+
slug: "05-api-modelling"
55
---
66

77
import { Aside, Code, Icon } from "@astrojs/starlight/components";

docs/content/docs/contributing/module-structure.mdx renamed to docs/content/docs/contributing/api-module-structure.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Module Structure
3-
description: Learn more about the module structure of @rescript/webapi.
4-
slug: "02-module-structure"
2+
title: API Module Structure
3+
description: Learn more about the API module structure of @rescript/webapi.
4+
slug: "02-api-module-structure"
55
---
66

77
import { Aside } from "@astrojs/starlight/components";

docs/content/docs/contributing/code-generation.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Code Generation
33
description: Learn more about the code generation process for @rescript/webapi.
4-
slug: "03-code-generation"
4+
slug: "04-code-generation"
55
---
66

77
The original bindings were generated using a modified version of [TypeScript-DOM-lib-generator](https://github.com/microsoft/TypeScript-DOM-lib-generator).

docs/content/docs/contributing/documentation.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Documentation"
33
description: Learn more about the relevance of adding documentation to @rescript/webapi.
4-
slug: "06-documentation"
4+
slug: "07-documentation"
55
---
66

77
After the bindings are generated, all you got was a link to the MDN documentation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: Module Type Structure
3+
description: Learn more about the module structure of @rescript/webapi.
4+
slug: "03-module-type-structure"
5+
---
6+
7+
import { Aside, FileTree, Code } from "@astrojs/starlight/components";
8+
9+
Every interface in a Web API module can potentially contain methods. These methods are modeled in a separate module named after the interface.
10+
11+
The primary reason for this separation is to handle method overloads.
12+
As explained in the [Design Philosophy](../design-philosophy) section, ReScript does not permit records to define the same properties more than once.
13+
Therefore, methods with overloads cannot be modeled within the same record type.
14+
15+
## Bindings
16+
17+
Another advantage of having a separate file is that these bindings can utilize all types defined in the API module.
18+
Under normal circumstances, the type module only contains `@send` bindings where the type is the first parameter.
19+
20+
<FileTree>
21+
22+
- DOMAPI
23+
- HTMLButtonElement.res
24+
25+
</FileTree>
26+
27+
```ReScript
28+
/**
29+
Returns whether a form will validate when it is submitted, without having to submit it.
30+
[Read more on MDN](
31+
https://developer.mozilla.org/docs/Web/API/HTMLButtonElement/checkValidity)
32+
*/
33+
@send
34+
external checkValidity: htmlButtonElement => bool = "checkValidity"
35+
```
36+
37+
## Inheritance
38+
39+
When an interface inherits from another interface, the base interface methods can be [included](https://rescript-lang.org/syntax-lookup#include) into the inheriting interface.
40+
All methods from [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement#instance_methods) should also be available on [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement#instance_methods).
41+
42+
export const htmlElementModule = `
43+
open DOMAPI
44+
45+
// A concrete type for \`T.t\` is passed later using the \`include\` keyword.
46+
module Impl = (T: { type t }) => {
47+
48+
/**
49+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus)
50+
*/
51+
@send
52+
external focus: (T.t, ~options: focusOptions=?) => unit = "focus"
53+
54+
}
55+
56+
include Impl({ type t = htmlElement })
57+
`;
58+
59+
<Code
60+
code={htmlElementModule}
61+
title="DOMAPI/HTMLElement.res"
62+
lang="ReScript"
63+
></Code>
64+
65+
export const buttonModule = `
66+
open DOMAPI
67+
68+
// Include all the methods from HTMLElement
69+
include HTMLElement.Impl({ type t = htmlButtonElement })
70+
71+
// Add additional methods specific to HTMLButtonElement:
72+
73+
/**
74+
Returns whether a form will validate when it is submitted, without having to submit it.
75+
[Read more on MDN](
76+
https://developer.mozilla.org/docs/Web/API/HTMLButtonElement/checkValidity)
77+
*/
78+
@send
79+
external checkValidity: htmlButtonElement => bool = "checkValidity"
80+
`;
81+
82+
<Code
83+
code={buttonModule}
84+
title="DOMAPI/HTMLButtonElement.res"
85+
lang="ReScript"
86+
></Code>

docs/content/docs/contributing/testing.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Testing
33
description: Learn more about testing the bindings for @rescript/webapi.
4-
slug: "05-testing"
4+
slug: "06-testing"
55
---
66

77
import { Aside, FileTree } from "@astrojs/starlight/components";

0 commit comments

Comments
 (0)