Skip to content

Commit

Permalink
Update formidable version and adjust for compatibility (#2767)
Browse files Browse the repository at this point in the history
The "formidable" version is updated from 2.1.2 to 3.5.1 in package.json and package-lock.json. Corresponding changes have been made in the "web.ts" file to ensure backward compatibility between "formidable" version 3 and 2. Adjustments are made to deal with the removal of the `multiples` option and mechanism in "formidable" v3.

Co-authored-by: Roman Lytvynov <roman.lytvynov@igama.ua>
  • Loading branch information
slrv and Roman Lytvynov committed Apr 26, 2024
1 parent c9fa3f8 commit 2f2b7ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
17 changes: 8 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"commander": "^12.0.0",
"dot-prop": "^6.0.1",
"etag": "^1.8.1",
"formidable": "^2.1.2",
"formidable": "^3.5.1",
"glob": "^8.1.0",
"ioredis": "^5.3.2",
"mime": "^3.0.0",
Expand Down
27 changes: 26 additions & 1 deletion src/servers/web.ts
Expand Up @@ -729,9 +729,34 @@ export class WebServer extends Server {
"There was an error processing this form.",
);
}
resolve({ fields, files });

// this is for backward compatibility formidable v3 and v2,
// because in v3 was deleted `multiples` option and mechanism
const isMultiples = Boolean(this.config?.formOptions?.multiples);
if (isMultiples) {
resolve({ fields, files });
} else {
// reimplementing firstValues values helper
// @see https://github.com/node-formidable/formidable/blob/master/src/helpers/firstValues.js
// but instead of first we are taking last values, mimicking v2 behavior
const lastValues = (val: Record<string, any>) => {
return Object.fromEntries(
Object.entries(val).map(([key, value]) => {
return [key, Array.isArray(value) ? value.at(-1) : value];
}),
);
};

resolve({
// @ts-expect-error wrong result type
fields: lastValues(fields),
// @ts-expect-error wrong result type
files: lastValues(files),
});
}
},
);
// looks like wrong types here
})) as { fields: string[]; files: string[] };

connection.rawConnection.params.body = fields;
Expand Down

0 comments on commit 2f2b7ce

Please sign in to comment.