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

chore: upgrade formidable #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
],
"dependencies": {
"@types/co-body": "^6.1.0",
"@types/formidable": "^2.0.5",
"@types/formidable": "^3.4.4",
"@types/koa": "^2.13.5",
"co-body": "^6.1.0",
"formidable": "^2.0.1",
"formidable": "^3.5.1",
"zod": "^3.19.1"
},
"devDependencies": {
Expand Down
13 changes: 3 additions & 10 deletions src/utils/parse-with-formidable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ParseWithFormidableResult = {
files: Files;
};

export default function parseWithFormidable(
export default async function parseWithFormidable(
ctx: Context,
options: ExtendedFormidableOptions,
): Promise<ParseWithFormidableResult> {
Expand All @@ -17,13 +17,6 @@ export default function parseWithFormidable(
if (onFileBegin) {
form.on('fileBegin', onFileBegin);
}
return new Promise((resolve, reject) => {
form.parse(ctx.req, (error, fields, files) => {
if (error) {
reject(error);
return;
}
resolve({ fields, files });
});
});
const [fields, files] = await form.parse(ctx.req);
return { fields, files };
}
20 changes: 10 additions & 10 deletions test/unit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ describe('koa-body', () => {
res.body.user.should.have.property('name', mostRecentUser.name);
res.body.user.should.have.property('followers', mostRecentUser.followers);

res.body.user.should.have.property('name', 'daryl');
res.body.user.should.have.property('followers', '30');
res.body.user.should.have.property('name', ['daryl']);
res.body.user.should.have.property('followers', ['30']);

done();
});
Expand Down Expand Up @@ -163,10 +163,10 @@ describe('koa-body', () => {
res.body.user.names.should.be.an.Array().and.have.lengthOf(2);
res.body.user.names[0].should.equal('John');
res.body.user.names[1].should.equal('Paul');
res.body._files.firstField.should.be.an.Object();
res.body._files.firstField.originalFilename.should.equal('package.json');
should(fs.statSync(res.body._files.firstField.filepath)).be.ok();
fs.unlinkSync(res.body._files.firstField.filepath);
res.body._files.firstField.should.be.an.Array().and.have.lengthOf(1);
res.body._files.firstField[0].originalFilename.should.equal('package.json');
should(fs.statSync(res.body._files.firstField[0].filepath)).be.ok();
fs.unlinkSync(res.body._files.firstField[0].filepath);

res.body._files.secondField.should.be.an.Array().and.have.lengthOf(2);
res.body._files.secondField.should.containDeep([
Expand Down Expand Up @@ -236,10 +236,10 @@ describe('koa-body', () => {
.end((err, res) => {
if (err) return done(err);

res.body._files.firstField.should.be.an.Object();
res.body._files.firstField.newFilename.should.equal('backage.json');
should(fs.statSync(res.body._files.firstField.filepath)).be.ok();
fs.unlinkSync(res.body._files.firstField.filepath);
res.body._files.firstField.should.be.an.Array().and.have.lengthOf(1);
res.body._files.firstField[0].newFilename.should.equal('backage.json');
should(fs.statSync(res.body._files.firstField[0].filepath)).be.ok();
fs.unlinkSync(res.body._files.firstField[0].filepath);

done();
});
Expand Down