koa-bodyparser
Advanced tools
Comparing version 1.5.0 to 1.6.0
1.5.0 / 2015-04-04 | ||
1.6.0 / 2015-05-01 | ||
================== | ||
* feat: support custom error handler | ||
1.5.0 / 2015-04-04 | ||
================== | ||
* Use an empty object instead of null, if no body is parsed | ||
1.4.1 / 2015-03-10 | ||
1.4.1 / 2015-03-10 | ||
================== | ||
@@ -12,3 +17,3 @@ | ||
1.4.0 / 2015-02-26 | ||
1.4.0 / 2015-02-26 | ||
================== | ||
@@ -18,3 +23,3 @@ | ||
1.3.1 / 2015-01-27 | ||
1.3.1 / 2015-01-27 | ||
================== | ||
@@ -24,3 +29,3 @@ | ||
1.3.0 / 2014-11-27 | ||
1.3.0 / 2014-11-27 | ||
================== | ||
@@ -27,0 +32,0 @@ |
31
index.js
@@ -31,3 +31,6 @@ /**! | ||
var detectJSON = opts.detectJSON; | ||
var onerror = opts.onerror; | ||
opts.detectJSON = undefined; | ||
opts.onerror = undefined; | ||
var jsonOpts = jsonOptions(opts); | ||
@@ -54,12 +57,12 @@ var formOpts = formOptions(opts); | ||
return function *bodyParser(next) { | ||
if (this.request.body !== undefined) { | ||
return yield* next; | ||
} | ||
if (this.request.body !== undefined) return yield* next; | ||
if ((detectJSON && detectJSON(this)) || this.request.is(jsonTypes)) { | ||
this.request.body = yield parse.json(this, jsonOpts); | ||
} else if (this.request.is(formTypes)) { | ||
this.request.body = yield parse.form(this, formOpts); | ||
} else { | ||
this.request.body = {}; | ||
try { | ||
yield* parseBody(this); | ||
} catch (err) { | ||
if (onerror) { | ||
onerror(err, this); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
@@ -69,2 +72,12 @@ | ||
}; | ||
function* parseBody(ctx) { | ||
if ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes)) { | ||
ctx.request.body = yield parse.json(ctx, jsonOpts); | ||
} else if (ctx.request.is(formTypes)) { | ||
ctx.request.body = yield parse.form(ctx, formOpts); | ||
} else { | ||
ctx.request.body = {}; | ||
} | ||
} | ||
}; | ||
@@ -71,0 +84,0 @@ |
{ | ||
"name": "koa-bodyparser", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "a body parser for koa", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -68,5 +68,12 @@ koa-bodyparser | ||
``` | ||
* **onerror**: support custom error handle, if `koa-bodyparser` throw an error, you can customize the response like: | ||
```js | ||
app.use(bodyparser({ | ||
onerror: function (err, ctx) { | ||
ctx.throw('body parse error', 422); | ||
} | ||
})); | ||
``` | ||
## Licences | ||
[MIT](LICENSE) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8117
89
79