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

fix: allow multiple body parser coexist #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 1 addition & 4 deletions lib/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ module.exports = function(req, opts){
opts.qs = opts.qs || qs;

// raw-body returns a Promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
return raw(inflate(req), opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think why we write this code is becaulse inflate will throw in some situation, so we need to wrap it in a promise chain.

.then(function(str){
try {
var parsed = opts.qs.parse(str, queryString);
Expand Down
5 changes: 1 addition & 4 deletions lib/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ module.exports = function(req, opts){
var strict = opts.strict !== false;

// raw-body returns a promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
return raw(inflate(req), opts)
.then(function(str) {
try {
var parsed = parse(str);
Expand Down
5 changes: 1 addition & 4 deletions lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ module.exports = function(req, opts){
opts.limit = opts.limit || '1mb';

// raw-body returns a Promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
return raw(inflate(req), opts)
.then(str => {
// ensure return the same format with json / form
return opts.returnRawBody ? { parsed: str, raw: str } : str;
Expand Down
29 changes: 29 additions & 0 deletions test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,33 @@ describe('parse(req, opts)', function(){
})
})

describe('multiple parser coexist', function(){
it('should parse', function(done){
var app = koa();

app.use(function *(next){
this.request.textParser = parse.text(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we just check the content-type before call parse[method]?

yield next;
});
app.use(function *(next){
this.request.formParser = parse.form(this);
yield next;
});
app.use(function *(){
this.body = {
json: yield parse(this),
text: yield this.request.textParser,
form: yield this.request.formParser
};
});

request(app.listen())
.post('/')
.set('content-type', 'application/json')
.send('{"hello":"world"}')
.expect(200)
.expect({"json":{"hello":"world"},"text":"{\"hello\":\"world\"}","form":{"{\"hello\":\"world\"}":""}}, done);
})
})

})