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)*/@sendexternalfetch: (window, ~input:request, ~init:requestInit=?)=>promise<response>="fetch"/**[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch)*/@sendexternalfetch2: (window, ~input:string, ~init:requestInit=?)=>promise<response>="fetch"While not that bad and usable, it can be improved:
- Rename
fetch2tofetchbecause it is the more common usage of the function. - Rename
fetchtofetchWithRequestfor clarity that this is the “overload” with aRequestobject. - Consider removing the named
~inputand~initarguments 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 */@sendexternalfetch: (window,string, ~init:requestInit=?)=>promise<response>="fetch"/** TODO: add better docs */@sendexternalfetchWithRequest: (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.
Sandboxed Code Generation
Section titled “Sandboxed Code Generation”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):
npmrunbuildAll 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.