Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Experimental WebAPI
GitHub

Code Generation

The original bindings were generated using a modified version ofTypeScript-DOM-lib-generator.These bindings were a great starting point, but they are not perfect.It is more than likely that you will need totweak the generated bindings by hand to make them more idiomatic to ReScript.

For example thewindow.fetch function was generated as:

/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch)
*/
@send
externalfetch: (window, ~input:request, ~init:requestInit=?)
=>promise<response>="fetch"
/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch)
*/
@send
externalfetch2: (window, ~input:string, ~init:requestInit=?)
=>promise<response>="fetch"

While not that bad and usable, it can be improved:

  • Renamefetch2 tofetch because it is the more common usage of the function.
  • Renamefetch tofetchWithRequest for clarity that this is the “overload” with aRequest object.
  • Consider removing the named~input and~init arguments and use positional arguments instead.Motivation: If the function does not have any parameters with the same type, it is more ergonomic to use positional arguments.This heuristic is not set in stone and can be adjusted based on the specific function.
  • The documentation can be improved.
/** TODO: add better docs */
@send
externalfetch: (window,string, ~init:requestInit=?)
=>promise<response>="fetch"
/** TODO: add better docs */
@send
externalfetchWithRequest: (window,request, ~init:requestInit=?)
=>promise<response>="fetch"

Once these changes are made, the bindings can be tested and then committed to the repository.The generation does no longer happen automatically, so manual improvements will not be overwritten.

Not every API was covered by the TypeScript-DOM-lib-generator.
Potentially, you want to add a new API to the bindings and star from code generation.

Inemitter.ts,you can override theinterfaceHierarchy with any new interface or type you want to add.

interfaceHierarchy= [
{
name:"Temp",
entries: [
enums(["WebGLPowerPreference"]),
dictionaries([
"ImageBitmapRenderingContextSettings",
"WebGLContextAttributes",
]),
],
opens: [],
},
];

After running the generator (intools/TypeScript-DOM-lib-generator):

Terminal window
npmrunbuild

All the generated files will be in thetmp folder. You can use this as inspiration for your own bindings.

To figure out if you needenums,dictionaries, orinterfaces, you can look at theJSON dump fileand see how the TypeScript-DOM-lib-generator represents the shape you are after.


[8]ページ先頭

©2009-2025 Movatter.jp