-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Automatically convert FormData to JSON #1309
Comments
The reason we use |
oh wait, I think I misunderstood — you want a mechanism for converting the |
Yes, passing P.S. is
|
It's not. Those annotations would have no meaning if the data came via a form submission rather than a |
I think there might be some misunderstanding. Sorry if I was unclear, let me try again:
This feature request is meant to address exactly that. What I think svelte kit should do, given an annotated form, is to generate a manifest containing the mapping between field names and annotated types, and it also knows which manifest corresponds to which endpoint, something like: endpoint: /cart
method: post
types:
items[0].price: number And when that endpoint receives
it should use the manifest to convert it to { items: [{ price: 99 }] } (Maybe return 400 for any request body (and/or content-type) that doesn’t match the manifest, if the endpoint is specified to auto-convert.) Ideally it can also auto convert multipart, with binary data being converted to base64 strings. |
In newer versions, we expose a |
Are you looking for something like this? https://dev.to/danawoodman/getting-form-body-data-in-your-sveltekit-endpoints-4a85 |
Is your feature request related to a problem? Please describe.
Since svelte advocates progressive enhancement, I think an utility to automatically convert FormData to JSON can be really helpful to write forms that works w/o JavaScript.
The demo currently does it manually, but once the form becomes complex, the conversion can be cumbersome. e.g.,:
Describe the solution you'd like
Svelte kit provides an utility that automatically converts FormData to JSON. However, since FormData erases type like number, a mapping between FormData and types is needed. One solution I can think of is that svelte kit allows specifying an attribute like
svelte:type="number"
on form controls, and svelte kit would parse the types into a manifest keyed by form action, if it points to an endpoint, and the utility is only usable in the endpoint that uses the manifest to generate the corresponding JSON.Describe alternatives you've considered
How important is this feature to you?
Not very, since it's manually convertible.
Additional context
The text was updated successfully, but these errors were encountered: