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

MultiPart Server Functions #1868

Closed
benwis opened this issue Oct 8, 2023 · 10 comments
Closed

MultiPart Server Functions #1868

benwis opened this issue Oct 8, 2023 · 10 comments
Milestone

Comments

@benwis
Copy link
Contributor

benwis commented Oct 8, 2023

Basically I think it'd be great to have a Sever Function encoding called MultiPartJson and MutltiPartCbor with the goal of making multipart form submissions to server functions easier.

I think we could create a MultipartActionForm component to serialize form data and any uploaded files into a FormData object. That could be sent to the server function route, and arguments to that server fn could be deconstructed from that FormData object with crates like axum_typed_multipart

My primary motivator is to make the processing of file uploads super easy, while keeping the Server Fn infrastructure

@gbj
Copy link
Collaborator

gbj commented Oct 9, 2023

Yes some kind of helper makes sense to me. I haven't worked with multipart forms much before, so I'm very open to API suggestions. Do you want to do some work on this one?

@benwis
Copy link
Contributor Author

benwis commented Oct 9, 2023

Yes some kind of helper makes sense to me. I haven't worked with multipart forms much before, so I'm very open to API suggestions. Do you want to do some work on this one?

Sure, I think I see a way to build it. I'll reach out if I have any questions

@benwis
Copy link
Contributor Author

benwis commented Oct 9, 2023

So I've dug into this a bit, and see a few options

  1. No change to server functions, just create one that takes nothing, and parse the Request or run an extractor on the Request to get the form data. Not sure how this would work with streaming files. You wouldn't be able to rerun this, but perhaps we could create an Action or some other tool to construct Requests to Leptos
  2. Create a serde_multipart crate/implement Serialize/Deserialize for one of the existing MultiPart Form representations. Not sure how well serde handles streaming formats,
  3. Have Axum/Actix handle this, and create a helper/parser/custom handler to have them handle it and parse the data. This seems a bit silly
  4. Create some form of ServerFns that operates on Requests/Responses. Much like Axum does, where they need to impl IntoResponse(or a similar trait) and impl IntoRequest traits

@gbj
Copy link
Collaborator

gbj commented Oct 10, 2023

Related thought: While we're at it, it would be worth making parts of the server fn implementation generic rather than using an enum for Payload and so on. IIRC the encodings both for sending the request and decoding the response are stored in an enum which means they both need to be present at runtime. There could be WASM binary savings if we make it generic over the encoding (which shouldn't change at runtime anyway) instead. Especially if we're adding another encoding.

@benwis
Copy link
Contributor Author

benwis commented Oct 10, 2023 via email

@dvc94ch
Copy link

dvc94ch commented Nov 12, 2023

trying to get file uploads working. it seems like there is an extractor Multipart in axum, but it only implements FromRequest instead of FromRequestParts because it consumes the request body, so the leptos_axum crate can't extract the Multipart. any ideas or examples of how to get file uploads working?

@benwis
Copy link
Contributor Author

benwis commented Nov 12, 2023

Yes, currently your best approach is to use a regular axum handler to handle multipart uploads

@gbj gbj added this to the 0.6 milestone Dec 18, 2023
@gbj
Copy link
Collaborator

gbj commented Dec 18, 2023

For anyone who's curious, our rewrite of server functions (https://github.com/gbj/server_fns) does, in fact, now support multipart uploads (and streaming responses), so this should be included in the next release.

@rakshith-ravi
Copy link
Collaborator

For anyone who's curious, our rewrite of server functions (https://github.com/gbj/server_fns) does, in fact, now support multipart uploads (and streaming responses), so this should be included in the next release.

I was curious how the new implementation is done and was going through the history of the code, and I gotta say - love your commit messages 🤣

@gbj gbj mentioned this issue Jan 4, 2024
20 tasks
@gbj
Copy link
Collaborator

gbj commented Jan 20, 2024

Done in #2158.

@gbj gbj closed this as completed Jan 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants