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

Hook into multipart form field parsing #147

Open
katywings opened this issue Jun 27, 2024 · 1 comment
Open

Hook into multipart form field parsing #147

katywings opened this issue Jun 27, 2024 · 1 comment
Labels
enhancement New feature or request package: @hattip/multipart Issue related to ./packages/base/multipart

Comments

@katywings
Copy link

Assume a multipart form with the following fields:

  • csrf
  • projectId
  • files

With the current @hattip/multipart parsing you have no way to check the csrf, before processing the files. You also have no way to access the projectId in the handleFile function, which is tedious when you wanna include that projectId in the resulting file path. Currently the only way is to either provide csrf and projectId via headers or query parameters, or to store the files in a temporary directory.

Idea: add options to control the order of the parsing and to hook into fields. Also add the already processed fields as a second parameter of handleFile.

let projectId = '';
const formData = await parseMultipartFormData(request, {
  // not all fields have to be in the order, missing fields will be processed at the end
  order: ["csrf", "projectId"],
  async handleField(name, value) {
    if (name === 'csrf' && value !== PROPER_CSRF) {
       throw new Error("Access denied");
    }

    if (name === 'projectId') {
       projectId = value;
    }

    return value;
  },
  async handleFile(info, fields) {
    // access projectId via "fields", or via the "projectId" variable from above
    ...upload things 
  }
})
katywings added a commit to lufrai/hattip that referenced this issue Jun 27, 2024
katywings added a commit to lufrai/hattip that referenced this issue Jun 27, 2024
@cyco130
Copy link
Member

cyco130 commented Aug 11, 2024

Hi! This is a very good idea and you already seem to have the implementation. Would you like to send a PR?

@aleclarson aleclarson changed the title Suggestion: hook into multipart form field parsing Hook into multipart form field parsing Nov 3, 2024
@aleclarson aleclarson added package: @hattip/multipart Issue related to ./packages/base/multipart enhancement New feature or request labels Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request package: @hattip/multipart Issue related to ./packages/base/multipart
Projects
None yet
Development

No branches or pull requests

3 participants