-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow files and data to be send in the same multipart request
- Loading branch information
1 parent
7998250
commit eb4ee00
Showing
7 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const Koa = require('koa'); | ||
const Router = require('@koa/router'); | ||
|
||
const bodyMiddleware = require('../body'); | ||
const { request } = require('../../testing'); | ||
|
||
const file = __dirname + '/../__fixtures__/test.png'; | ||
|
||
describe('bodyMiddleware', () => { | ||
it('should be able to send files and data in the same request', async () => { | ||
// Leaning on supertest and koa here as multipart form | ||
// data is difficult to send through a node stream. | ||
const router = new Router(); | ||
router.post('/', (ctx) => { | ||
const { originalFilename } = ctx.request.files.file; | ||
ctx.body = { | ||
...ctx.request.body, | ||
originalFilename, | ||
}; | ||
}); | ||
|
||
const app = new Koa(); | ||
app.use(bodyMiddleware()); | ||
app.use(router.routes()); | ||
|
||
const response = await request( | ||
'POST', | ||
'/', | ||
{ | ||
foo: 'bar', | ||
}, | ||
{ app, file } | ||
); | ||
expect(response.body).toEqual({ | ||
foo: 'bar', | ||
originalFilename: 'test.png', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const compose = require('koa-compose'); | ||
const { koaBody } = require('koa-body'); | ||
|
||
module.exports = function () { | ||
return compose([ | ||
koaBody({ | ||
multipart: true, | ||
}), | ||
parseMultipartBody(), | ||
]); | ||
}; | ||
|
||
// Multipart bodies are assumed to be simple key/value pairs. | ||
// This middleware parses each field as JSON to allow the client | ||
// to send files and JSON data in the same request. | ||
function parseMultipartBody() { | ||
return async (ctx, next) => { | ||
if (ctx.request.files) { | ||
for (let [key, value] of Object.entries(ctx.request.body)) { | ||
ctx.request.body[key] = JSON.parse(value); | ||
} | ||
} | ||
return next(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters