Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

RFC: multipart/form-data #36

Open
HaNdTriX opened this issue Oct 2, 2022 · 0 comments
Open

RFC: multipart/form-data #36

HaNdTriX opened this issue Oct 2, 2022 · 0 comments

Comments

@HaNdTriX
Copy link
Contributor

HaNdTriX commented Oct 2, 2022

Summary

Currently the Form.encType has no effect when using @next-fetch/swr/form.
Enabling encTypes like multipart/form-data would allow all kinds of common features like File Uploads.

Basic example

File: pages/file-upload.tsx

export default function MyImageUpload() {
  const mutation = useImageUpload();
  return (
    <Form encType="multipart/form-data" mutation={mutation}>
      <input type="file" name="someFile" accept="image/*" />
      <button type="submit">Submit</button>
    </Form>
  );
}

File: pages/api/image.swr.ts

import { zfd } from "zod-form-data";

export const useImageUpload = mutation(
  zfd.formData({ someFile: zfd.file() }),
  async (formData) => {
    const file: File = formData.get('someFile')
    // ...
    return { message: "Success" };
  }
);

Motivation

Today next-fetch only supports application/x-www-form-urlencoded forms. These forms are quite limited, since they only allow structured string data.

Adding multipart/form-data support, would allow sending all kind of hybrid data (strings, blobs, etc) to the server while still being able to support native html forms (hookResponse).

Challenges

  • FormData inside Edge Runtime and Node.js still lacks in some features.
  • FormData blobs need to be lazy on the server.
  • zod currently doesn't support ES Maps afaik. Nevertheless zod-form-data can help here.
  • ...to be continued

Adoption strategy

The type definition of @next-fetch/swr/form element should be updated for the time multipart/form-data is not supported, otherwise a breaking change will be needed when the feature has been implemented.

export function Form<Data, Error>({
  mutation,
  mutationConfig,
  ...props
}: React.HTMLProps<HTMLFormElement> &
  React.PropsWithChildren<{
    mutation: HookWithFormSubmission<Data, Error>;
    mutationConfig?: SWRMutationConfiguration<Data, Error>;
+    encType?: 'application/x-www-form-urlencoded'
  }>) {
  const { formProps } = useForm(mutation, mutationConfig);
  return createElement("form", { ...formProps, ...props }, props.children);
}

Later this feature can be allowed by adding multipart/form-data to the encType union.

export function Form<Data, Error>({
  mutation,
  mutationConfig,
  ...props
}: React.HTMLProps<HTMLFormElement> &
  React.PropsWithChildren<{
    mutation: HookWithFormSubmission<Data, Error>;
    mutationConfig?: SWRMutationConfiguration<Data, Error>;
-    encType?: 'application/x-www-form-urlencoded' 
+    encType?: 'application/x-www-form-urlencoded' | 'multipart/form-data'
  }>) {
  const { formProps } = useForm(mutation, mutationConfig);
  return createElement("form", { ...formProps, ...props }, props.children);
}

Links

@HaNdTriX HaNdTriX changed the title RFC: Support multipart/form-data RFC: multipart/form-data Oct 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant