-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: body parser support disable, ignore and match (eggjs#150)
- Loading branch information
1 parent
c736ac9
commit 1082d6d
Showing
13 changed files
with
150 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
'use strict'; | ||
|
||
module.exports = require('koa-bodyparser'); | ||
const bodyParser = require('koa-bodyparser'); | ||
const pathMatching = require('egg-path-matching'); | ||
|
||
module.exports = function(options) { | ||
const bodyParserMiddleware = bodyParser(options); | ||
const match = pathMatching(options); | ||
return function* (next) { | ||
if (!options.enable) return yield next; | ||
if (!match(this)) return yield next; | ||
|
||
return yield bodyParserMiddleware.call(this, 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
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
19 changes: 19 additions & 0 deletions
19
test/fixtures/apps/body_parser_testapp_disable/app/router.js
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,19 @@ | ||
module.exports = app => { | ||
app.get('/test/body_parser/user', function* () { | ||
this.body = { | ||
url: this.url, | ||
csrf: this.csrf | ||
}; | ||
}); | ||
|
||
app.post('/test/body_parser/user', function* () { | ||
this.body = this.request.body; | ||
}); | ||
|
||
app.post('/test/body_parser/foo.json', function* () { | ||
this.body = this.request.body; | ||
}); | ||
app.post('/test/body_parser/form.json', function* () { | ||
this.body = this.request.body; | ||
}); | ||
}; |
9 changes: 9 additions & 0 deletions
9
test/fixtures/apps/body_parser_testapp_disable/config/config.default.js
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,9 @@ | ||
exports.bodyParser = { | ||
enable: false, | ||
}; | ||
|
||
exports.security = { | ||
csrf: false, | ||
}; | ||
|
||
exports.keys = 'foo'; |
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,3 @@ | ||
{ | ||
"name": "body_parser_testapp_disable" | ||
} |
19 changes: 19 additions & 0 deletions
19
test/fixtures/apps/body_parser_testapp_ignore/app/router.js
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,19 @@ | ||
module.exports = app => { | ||
app.get('/test/body_parser/user', function* () { | ||
this.body = { | ||
url: this.url, | ||
csrf: this.csrf | ||
}; | ||
}); | ||
|
||
app.post('/test/body_parser/user', function* () { | ||
this.body = this.request.body; | ||
}); | ||
|
||
app.post('/test/body_parser/foo.json', function* () { | ||
this.body = this.request.body; | ||
}); | ||
app.post('/test/body_parser/form.json', function* () { | ||
this.body = this.request.body; | ||
}); | ||
}; |
9 changes: 9 additions & 0 deletions
9
test/fixtures/apps/body_parser_testapp_ignore/config/config.default.js
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,9 @@ | ||
exports.bodyParser = { | ||
ignore: '/test/body_parser/foo.json', | ||
}; | ||
|
||
exports.security = { | ||
csrf: false, | ||
}; | ||
|
||
exports.keys = 'foo'; |
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,3 @@ | ||
{ | ||
"name": "body_parser_testapp_ignore" | ||
} |
19 changes: 19 additions & 0 deletions
19
test/fixtures/apps/body_parser_testapp_match/app/router.js
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,19 @@ | ||
module.exports = app => { | ||
app.get('/test/body_parser/user', function* () { | ||
this.body = { | ||
url: this.url, | ||
csrf: this.csrf | ||
}; | ||
}); | ||
|
||
app.post('/test/body_parser/user', function* () { | ||
this.body = this.request.body; | ||
}); | ||
|
||
app.post('/test/body_parser/foo.json', function* () { | ||
this.body = this.request.body; | ||
}); | ||
app.post('/test/body_parser/form.json', function* () { | ||
this.body = this.request.body; | ||
}); | ||
}; |
9 changes: 9 additions & 0 deletions
9
test/fixtures/apps/body_parser_testapp_match/config/config.default.js
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,9 @@ | ||
exports.bodyParser = { | ||
match: '/test/body_parser/foo.json', | ||
}; | ||
|
||
exports.security = { | ||
csrf: false, | ||
}; | ||
|
||
exports.keys = 'foo'; |
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,3 @@ | ||
{ | ||
"name": "body_parser_testapp_match" | ||
} |