|
| 1 | +--- |
| 2 | +title: Code Generation |
| 3 | +description: Learn more about the code generation process for @rescript/webapi. |
| 4 | +slug: "03-code-generation" |
| 5 | +--- |
| 6 | + |
| 7 | +The original bindings were generated using a modified version of [TypeScript-DOM-lib-generator](https://github.com/microsoft/TypeScript-DOM-lib-generator). |
| 8 | +These bindings were a great starting point, but they are not perfect. |
| 9 | +It is more than likely that you will need to **tweak the generated bindings by hand** to make them more idiomatic to ReScript. |
| 10 | + |
| 11 | +For example the `window.fetch` function was generated as: |
| 12 | + |
| 13 | +```ReScript |
| 14 | +/** |
| 15 | +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch) |
| 16 | +*/ |
| 17 | +@send |
| 18 | +external fetch: (window, ~input: request, ~init: requestInit=?) |
| 19 | + => Promise.t<response> = "fetch" |
| 20 | +
|
| 21 | +/** |
| 22 | +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch) |
| 23 | +*/ |
| 24 | +@send |
| 25 | +external fetch2: (window, ~input: string, ~init: requestInit=?) |
| 26 | + => Promise.t<response> = "fetch" |
| 27 | +``` |
| 28 | + |
| 29 | +While not that bad and usable, it can be improved: |
| 30 | + |
| 31 | +- Rename `fetch2` to `fetch` because it is the more common usage of the function. |
| 32 | +- Rename `fetch` to `fetch_with_request` for clarity that this is the "overload" with a `Request` object. |
| 33 | +- Consider removing the named `~input` and `~init` arguments and use positional arguments instead. |
| 34 | + Motivation: If the function does not have any parameters with the same type, it is more ergonomic to use positional arguments. |
| 35 | + This heuristic is not set in stone and can be adjusted based on the specific function. |
| 36 | +- The documentation can be improved. |
| 37 | + |
| 38 | +```ReScript |
| 39 | +/** TODO: add better docs */ |
| 40 | +@send |
| 41 | +external fetch: (window, string, ~init: requestInit=?) |
| 42 | + => Promise.t<response> = "fetch" |
| 43 | +
|
| 44 | +/** TODO: add better docs */ |
| 45 | +@send |
| 46 | +external fetch_with_request: (window, request, ~init: requestInit=?) |
| 47 | + => Promise.t<response> = "fetch" |
| 48 | +``` |
| 49 | + |
| 50 | +Once these changes are made, the bindings can be tested and then committed to the repository. |
| 51 | +The generation does no longer happen automatically, so manual improvements will not be overwritten. |
| 52 | + |
| 53 | +## Sandboxed Code Generation |
| 54 | + |
| 55 | +Not every API was covered by the TypeScript-DOM-lib-generator. |
| 56 | +Potentially, you want to add a new API to the bindings and star from code generation. |
| 57 | + |
| 58 | +In [emitter.ts](https://github.com/rescript-lang/experimental-rescript-webapi/blob/main/tools/TypeScript-DOM-lib-generator/src/build/emitter.ts), |
| 59 | +you can override the `interfaceHierarchy` with any new interface or type you want to add. |
| 60 | + |
| 61 | +```typescript |
| 62 | +interfaceHierarchy = [ |
| 63 | + { |
| 64 | + name: "Temp", |
| 65 | + entries: [ |
| 66 | + enums(["WebGLPowerPreference"]), |
| 67 | + dictionaries([ |
| 68 | + "ImageBitmapRenderingContextSettings", |
| 69 | + "WebGLContextAttributes", |
| 70 | + ]), |
| 71 | + ], |
| 72 | + opens: [], |
| 73 | + }, |
| 74 | +]; |
| 75 | +``` |
| 76 | + |
| 77 | +After running the generator (in `tools/TypeScript-DOM-lib-generator`): |
| 78 | + |
| 79 | +```shell |
| 80 | +npm run build |
| 81 | +``` |
| 82 | + |
| 83 | +All the generated files will be in the `tmp` folder. You can use this as inspiration for your own bindings. |
| 84 | + |
| 85 | +To figure out if you need `enums`, `dictionaries`, or `interfaces`, you can look at the [JSON dump](https://github.com/rescript-lang/experimental-rescript-webapi/blob/main/tools/TypeScript-DOM-lib-generator/dom-dump.json) file |
| 86 | +and see how the TypeScript-DOM-lib-generator represents the shape you are after. |
0 commit comments