Skip to content

Commit 23a3af7

Browse files
Merge pull request #269 from elementar/koa-multipart-body
Koa multipart body
2 parents f859d7a + 4718f1f commit 23a3af7

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/driver/koa/KoaDriver.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ export class KoaDriver extends BaseDriver implements Driver {
111111
.forEach(param => {
112112
defaultMiddlewares.push(multer(param.extraOptions).array(param.name));
113113
});
114+
115+
defaultMiddlewares.push(this.fixMulterRequestAssignment);
114116
}
115117

116118
// user used middlewares
@@ -411,4 +413,21 @@ export class KoaDriver extends BaseDriver implements Driver {
411413
}
412414
}
413415

416+
/**
417+
* This middleware fixes a bug on koa-multer implementation.
418+
*
419+
* This bug should be fixed by koa-multer PR #15: https://github.com/koa-modules/multer/pull/15
420+
*/
421+
private async fixMulterRequestAssignment(ctx: any, next: Function) {
422+
if ("request" in ctx) {
423+
if (ctx.req.body) ctx.request.body = ctx.req.body;
424+
if (ctx.req.file) ctx.request.file = ctx.req.file;
425+
if (ctx.req.files) {
426+
ctx.request.files = ctx.req.files;
427+
ctx.files = ctx.req.files;
428+
}
429+
}
430+
431+
return await next();
432+
}
414433
}

test/functional/action-params.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ describe("action parameters", () => {
240240
return `<html><body>${uploadedFileName}</body></html>`;
241241
}
242242

243+
@Post("/files-with-body")
244+
postFileWithBody(@UploadedFile("myfile") file: any, @Body() body: any): any {
245+
uploadedFileName = file.originalname;
246+
return `<html><body>${uploadedFileName} - ${JSON.stringify(body)}</body></html>`;
247+
}
248+
249+
@Post("/files-with-body-param")
250+
postFileWithBodyParam(@UploadedFile("myfile") file: any, @BodyParam("p1") p1: string): any {
251+
uploadedFileName = file.originalname;
252+
return `<html><body>${uploadedFileName} - ${p1}</body></html>`;
253+
}
254+
243255
@Post("/files-with-limit")
244256
postFileWithLimit(@UploadedFile("myfile", { options: { limits: { fileSize: 2 } } }) file: any): any {
245257
return `<html><body>${file.originalname}</body></html>`;
@@ -682,6 +694,49 @@ describe("action parameters", () => {
682694
});
683695
});
684696

697+
describe("@UploadedFile with @Body should return both the file and the body", () => {
698+
const requestOptions = {
699+
formData: {
700+
myfile: {
701+
value: "hello world",
702+
options: {
703+
filename: "hello-world.txt",
704+
contentType: "image/text"
705+
}
706+
},
707+
anotherField: "hi",
708+
andOther: "hello",
709+
}
710+
};
711+
712+
assertRequest([3001, 3002], "post", "files-with-body", undefined, requestOptions, response => {
713+
expect(response).to.be.status(200);
714+
expect(response).to.have.header("content-type", "text/html; charset=utf-8");
715+
expect(response.body).to.be.equal(`<html><body>hello-world.txt - {"anotherField":"hi","andOther":"hello"}</body></html>`);
716+
});
717+
});
718+
719+
describe("@UploadedFile with @BodyParam should return both the file and the body param", () => {
720+
const requestOptions = {
721+
formData: {
722+
myfile: {
723+
value: "hello world",
724+
options: {
725+
filename: "hello-world.txt",
726+
contentType: "image/text"
727+
}
728+
},
729+
p1: "hi, i'm a param",
730+
}
731+
};
732+
733+
assertRequest([3001, 3002], "post", "files-with-body-param", undefined, requestOptions, response => {
734+
expect(response).to.be.status(200);
735+
expect(response).to.have.header("content-type", "text/html; charset=utf-8");
736+
expect(response.body).to.be.equal("<html><body>hello-world.txt - hi, i'm a param</body></html>");
737+
});
738+
});
739+
685740
describe("@UploadedFile with passed uploading options (limit) should throw an error", () => {
686741
const validRequestOptions = {
687742
formData: {

0 commit comments

Comments
 (0)