koa-bodyparser
Advanced tools
Comparing version 2.1.0 to 2.2.0
2.2.0 / 2016-05-16 | ||
================== | ||
* feat: support enableTypes and text (#44) | ||
2.1.0 / 2016-05-10 | ||
@@ -3,0 +8,0 @@ ================== |
64
index.js
@@ -32,9 +32,11 @@ /**! | ||
var onerror = opts.onerror; | ||
var enableTypes = opts.enableTypes || ['json', 'form']; | ||
var enableForm = checkEnable(enableTypes, 'form'); | ||
var enableJson = checkEnable(enableTypes, 'json'); | ||
var enableText = checkEnable(enableTypes, 'text'); | ||
opts.detectJSON = undefined; | ||
opts.onerror = undefined; | ||
var jsonOpts = jsonOptions(opts); | ||
var formOpts = formOptions(opts); | ||
var extendTypes = opts.extendTypes || {}; | ||
// default json types | ||
@@ -53,10 +55,21 @@ var jsonTypes = [ | ||
// default text types | ||
var textTypes = [ | ||
'text/plain', | ||
]; | ||
var jsonOpts = formatOptions(opts, 'json'); | ||
var formOpts = formatOptions(opts, 'form'); | ||
var textOpts = formatOptions(opts, 'text'); | ||
var extendTypes = opts.extendTypes || {}; | ||
extendType(jsonTypes, extendTypes.json); | ||
extendType(formTypes, extendTypes.form); | ||
extendType(textTypes, extendTypes.text); | ||
return function *bodyParser(next) { | ||
if (this.request.body !== undefined) return yield* next; | ||
try { | ||
yield* parseBody(this); | ||
this.request.body = yield parseBody(this); | ||
} catch (err) { | ||
@@ -69,31 +82,26 @@ if (onerror) { | ||
} | ||
yield* next; | ||
yield next; | ||
}; | ||
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 = {}; | ||
if (enableJson && ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes))) { | ||
return yield parse.json(ctx, jsonOpts); | ||
} | ||
if (enableForm && ctx.request.is(formTypes)) { | ||
return yield parse.form(ctx, formOpts); | ||
} | ||
if (enableText && ctx.request.is(textTypes)) { | ||
return yield parse.text(ctx, textOpts) || ''; | ||
} | ||
return {}; | ||
} | ||
}; | ||
function jsonOptions(opts) { | ||
var jsonOpts = {}; | ||
copy(opts).to(jsonOpts); | ||
jsonOpts.limit = opts.jsonLimit; | ||
return jsonOpts; | ||
function formatOptions(opts, type) { | ||
var res = {}; | ||
copy(opts).to(res); | ||
res.limit = opts[type + 'Limit']; | ||
return res; | ||
} | ||
function formOptions(opts) { | ||
var formOpts = {}; | ||
copy(opts).to(formOpts); | ||
formOpts.limit = opts.formLimit; | ||
return formOpts; | ||
} | ||
function extendType(original, extend) { | ||
@@ -109,1 +117,5 @@ if (extend) { | ||
} | ||
function checkEnable(types, type) { | ||
return types.indexOf(type) >= 0; | ||
} |
{ | ||
"name": "koa-bodyparser", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "a body parser for koa", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,3 +25,3 @@ 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. | ||
@@ -50,7 +50,9 @@ ## Install | ||
* **encode**: requested encoding. Default is `utf-8` by `co-body` | ||
* **formLimit**: limit of the `urlencoded` body. If the body ends up being larger than this limit, a 413 error code is returned. Default is `56kb` | ||
* **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` | ||
* **detectJSON**: custom json request detect function. Default is `null` | ||
* **enableTypes**: parser will only parse when request type hits enableTypes, default is `['json', 'form']`. | ||
* **encode**: requested encoding. Default is `utf-8` by `co-body`. | ||
* **formLimit**: limit of the `urlencoded` body. If the body ends up being larger than this limit, a 413 error code is returned. Default is `56kb`. | ||
* **jsonLimit**: limit of the `json` body. Default is `1mb`. | ||
* **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, `this.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`. | ||
@@ -57,0 +59,0 @@ ```js |
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
9515
99
96