koa-bodyparser
Advanced tools
Comparing version 3.2.0 to 4.0.0
3.2.0 / 2016-07-31 | ||
4.0.0 / 2017-02-27 | ||
================== | ||
* test: run ci in node 4, 6 | ||
* feat: support enableTypes and text | ||
* refactor: use async function and support koa@2 (#62) | ||
3.1.0 / 2016-05-10 | ||
2.3.0 / 2016-11-14 | ||
================== | ||
* deps: ^ | ||
* no need for parenthesis around single param declaration | ||
* capitalize koa constructor | ||
* feat: support dynamic disable body parser | ||
3.0.0 / 2015-11-18 | ||
2.2.0 / 2016-05-16 | ||
================== | ||
* refactor: more es6 | ||
* Use promises rather than generators | ||
* feat: support enableTypes and text (#44) | ||
2.1.0 / 2016-05-10 | ||
================== | ||
* deps: co-body@4 | ||
2.0.1 / 2015-08-12 | ||
@@ -22,0 +23,0 @@ ================== |
70
index.js
@@ -17,3 +17,4 @@ /**! | ||
const parse = require('co-body'); | ||
var parse = require('co-body'); | ||
var copy = require('copy-to'); | ||
@@ -30,9 +31,9 @@ /** | ||
opts = opts || {}; | ||
const detectJSON = opts.detectJSON; | ||
const onerror = opts.onerror || throwError; | ||
var detectJSON = opts.detectJSON; | ||
var onerror = opts.onerror; | ||
const enableTypes = opts.enableTypes || ['json', 'form']; | ||
const enableForm = checkEnable(enableTypes, 'form'); | ||
const enableJson = checkEnable(enableTypes, 'json'); | ||
const enableText = checkEnable(enableTypes, 'text'); | ||
var enableTypes = opts.enableTypes || ['json', 'form']; | ||
var enableForm = checkEnable(enableTypes, 'form'); | ||
var enableJson = checkEnable(enableTypes, 'json'); | ||
var enableText = checkEnable(enableTypes, 'text'); | ||
@@ -43,3 +44,3 @@ opts.detectJSON = undefined; | ||
// default json types | ||
const jsonTypes = [ | ||
var jsonTypes = [ | ||
'application/json', | ||
@@ -52,3 +53,3 @@ 'application/json-patch+json', | ||
// default form types | ||
const formTypes = [ | ||
var formTypes = [ | ||
'application/x-www-form-urlencoded', | ||
@@ -58,13 +59,12 @@ ]; | ||
// default text types | ||
const textTypes = [ | ||
var textTypes = [ | ||
'text/plain', | ||
]; | ||
var jsonOpts = formatOptions(opts, 'json'); | ||
var formOpts = formatOptions(opts, 'form'); | ||
var textOpts = formatOptions(opts, 'text'); | ||
const jsonOpts = formatOptions(opts, 'json'); | ||
const formOpts = formatOptions(opts, 'form'); | ||
const textOpts = formatOptions(opts, 'text'); | ||
var extendTypes = opts.extendTypes || {}; | ||
const extendTypes = opts.extendTypes || {}; | ||
extendType(jsonTypes, extendTypes.json); | ||
@@ -74,22 +74,28 @@ extendType(formTypes, extendTypes.form); | ||
return function bodyParser(ctx, next) { | ||
if (ctx.request.body !== undefined) return next(); | ||
return parseBody(ctx).then(body => { | ||
ctx.request.body = body; | ||
return next(); | ||
}, err => onerror(err, ctx)); | ||
return async function bodyParser(ctx, next) { | ||
if (ctx.request.body !== undefined) return await next(); | ||
if (ctx.disableBodyParser) return await next(); | ||
try { | ||
ctx.request.body = await parseBody(ctx); | ||
} catch (err) { | ||
if (onerror) { | ||
onerror(err, ctx); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
await next(); | ||
}; | ||
function parseBody(ctx) { | ||
async function parseBody(ctx) { | ||
if (enableJson && ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes))) { | ||
return parse.json(ctx, jsonOpts); | ||
return await parse.json(ctx, jsonOpts); | ||
} | ||
if (enableForm && ctx.request.is(formTypes)) { | ||
return parse.form(ctx, formOpts); | ||
return await parse.form(ctx, formOpts); | ||
} | ||
if (enableText && ctx.request.is(textTypes)) { | ||
return parse.text(ctx, textOpts) || ''; | ||
return await parse.text(ctx, textOpts) || ''; | ||
} | ||
return Promise.resolve({}); | ||
return {}; | ||
} | ||
@@ -100,3 +106,3 @@ }; | ||
var res = {}; | ||
Object.assign(res, opts); | ||
copy(opts).to(res); | ||
res.limit = opts[type + 'Limit']; | ||
@@ -111,12 +117,10 @@ return res; | ||
} | ||
extend.forEach(extend => original.push(extend)); | ||
extend.forEach(function (extend) { | ||
original.push(extend); | ||
}); | ||
} | ||
} | ||
function throwError(err) { | ||
throw err; | ||
} | ||
function checkEnable(types, type) { | ||
return types.indexOf(type) >= 0; | ||
} |
{ | ||
"name": "koa-bodyparser", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"description": "a body parser for koa", | ||
@@ -28,20 +28,18 @@ "main": "index.js", | ||
}, | ||
"publishConfig": { | ||
"tag": "next" | ||
}, | ||
"homepage": "https://github.com/koajs/body-parser", | ||
"devDependencies": { | ||
"autod": "1", | ||
"istanbul": "~0.4.3", | ||
"koa": "^2.0.0", | ||
"mocha": "^2.4.5", | ||
"should": "^8.3.1", | ||
"supertest": "~0.9.0" | ||
"autod": "2.4.2", | ||
"istanbul": "^0.4.5", | ||
"koa": "^2.0.1", | ||
"mocha": "^3.2.0", | ||
"should": "^11.2.0", | ||
"supertest": "^3.0.0" | ||
}, | ||
"dependencies": { | ||
"co-body": "^4.2.0" | ||
"co-body": "^4.2.0", | ||
"copy-to": "^2.0.1" | ||
}, | ||
"engines": { | ||
"node": ">= 4" | ||
"node": ">=7.6" | ||
} | ||
} |
@@ -25,6 +25,4 @@ koa-bodyparser | ||
a body parser for koa, base on [co-body](https://github.com/tj/co-body). | ||
A body parser for koa, base on [co-body](https://github.com/tj/co-body). support `json`, `form` and `text` type body. | ||
___Notice: `koa-bodyparser@3` support `koa@2`, if you want to use this module with `koa@1`, please use `koa-bodyparser@2`.___ | ||
## Install | ||
@@ -37,4 +35,4 @@ | ||
```js | ||
var Koa = require('koa'); | ||
var bodyParser = require('koa-bodyparser'); | ||
var Koa = require('koa'); | ||
@@ -44,4 +42,4 @@ var app = new Koa(); | ||
app.use(ctx => { | ||
// the parsed body will store in this.request.body | ||
app.use(async ctx => { | ||
// the parsed body will store in ctx.request.body | ||
// if nothing was parsed, body will be an empty object {} | ||
@@ -58,3 +56,4 @@ ctx.body = ctx.request.body; | ||
* **jsonLimit**: limit of the `json` body. Default is `1mb`. | ||
* * **strict**: when set to true, JSON parser will only accept arrays and objects. Default is `true`. See [strict mode](https://github.com/cojs/co-body#options) in `co-body`. In strict mode, `this.request.body` will always be an object(or array), this avoid lots of type judging. But text body will always return string type. | ||
* **textLimit**: limit of the `text` body. Default is `1mb`. | ||
* **strict**: when set to true, JSON parser will only accept arrays and objects. Default is `true`. See [strict mode](https://github.com/cojs/co-body#options) in `co-body`. In strict mode, `ctx.request.body` will always be an object(or array), this avoid lots of type judging. But text body will always return string type. | ||
* **detectJSON**: custom json request detect function. Default is `null`. | ||
@@ -90,4 +89,22 @@ | ||
* **disableBodyParser**: you can dynamic disable body parser by set `ctx.disableBodyParser = true`. | ||
```js | ||
app.use(async (ctx, next) => { | ||
if (ctx.path === '/disable') ctx.disableBodyParser = true; | ||
await next(); | ||
}); | ||
app.use(bodyparser()); | ||
``` | ||
## Koa 1 Support | ||
To use `koa-bodyparser` with koa@1, please use [bodyparser 2.x](https://github.com/koajs/bodyparser/tree/2.x). | ||
```bash | ||
npm install koa-bodyparser@2 --save | ||
``` | ||
## 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
10113
100
106
2
+ Addedcopy-to@^2.0.1
+ Addedcopy-to@2.0.1(transitive)