Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding useful extension module #5

Open
nojaf opened this issue Nov 9, 2024 · 3 comments
Open

Consider adding useful extension module #5

nojaf opened this issue Nov 9, 2024 · 3 comments

Comments

@nojaf
Copy link
Collaborator

nojaf commented Nov 9, 2024

While the aspiration of this project is to have bindings for anything available in the browser, it might be interesting consider having an additional auxiliary module.

Or to phrase it differently: some API methods are shit. Like https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement. You pass it any string and get an element back. Maybe you want to create something more specific and get a better type back like document.createElement("input") and get HTMLInputElement back.

So having something like:

type document

type htmlInputElement

@scope("document")
external createInputElement: (unit, @as("input") _) => htmlInputElement =
  "createElement"

let i = createInputElement()

The input string can't be misspelled and the return type is better.
Perhaps each current API module can have an APIExt companion module?
Let me know what you thoughts.

@zth
Copy link

zth commented Nov 9, 2024

I think this type of thing should go in the module directly. I'd say that's an idiomatic binding.

@nojaf
Copy link
Collaborator Author

nojaf commented Nov 9, 2024

There will be other examples:

type document

type element = {tag: string}

type htmlInputElement

@scope("document")
external querySelector: string => element = "querySelector"

external unsafeCast: 'i => 'o = "%identity"

let querySelectorInputElement = (selector: string): option<
  htmlInputElement,
> => {
  let i = querySelector(selector)
  i.tag === "input" ? Some(unsafeCast(i)) : None
}

This is not strictly a binding but does provide a safe API.

@zth
Copy link

zth commented Nov 11, 2024

Well, I think we definitely should ship "improved" APIs/helpers. Maybe others have differing opinions on this, but I think so at least. It'll increase the usefulness a lot for the lib.

We need to figure out where to draw the line obviously.
If we go for it, then I think we should find a way to pull completions from the "helper"/extension modules too.

Here's an example of something similar that might fit here - helpers for working efficiently with FormData: https://github.com/zth/res-x/blob/main/src/FormDataHelpers.res

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants