koa-bodyparser
Advanced tools
Comparing version 3.1.0 to 3.2.0
3.2.0 / 2016-07-31 | ||
================== | ||
* test: run ci in node 4, 6 | ||
* feat: support enableTypes and text | ||
3.1.0 / 2016-05-10 | ||
@@ -3,0 +9,0 @@ ================== |
54
index.js
@@ -32,9 +32,10 @@ /**! | ||
const enableTypes = opts.enableTypes || ['json', 'form']; | ||
const enableForm = checkEnable(enableTypes, 'form'); | ||
const enableJson = checkEnable(enableTypes, 'json'); | ||
const enableText = checkEnable(enableTypes, 'text'); | ||
opts.detectJSON = undefined; | ||
opts.onerror = undefined; | ||
const jsonOpts = jsonOptions(opts); | ||
const formOpts = formOptions(opts); | ||
const extendTypes = opts.extendTypes || {}; | ||
// default json types | ||
@@ -53,4 +54,17 @@ const jsonTypes = [ | ||
// default text types | ||
const textTypes = [ | ||
'text/plain', | ||
]; | ||
const jsonOpts = formatOptions(opts, 'json'); | ||
const formOpts = formatOptions(opts, 'form'); | ||
const textOpts = formatOptions(opts, 'text'); | ||
const extendTypes = opts.extendTypes || {}; | ||
extendType(jsonTypes, extendTypes.json); | ||
extendType(formTypes, extendTypes.form); | ||
extendType(textTypes, extendTypes.text); | ||
@@ -67,26 +81,22 @@ return function bodyParser(ctx, next) { | ||
function parseBody(ctx) { | ||
if ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes)) { | ||
if (enableJson && ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes))) { | ||
return parse.json(ctx, jsonOpts); | ||
} else if (ctx.request.is(formTypes)) { | ||
} | ||
if (enableForm && ctx.request.is(formTypes)) { | ||
return parse.form(ctx, formOpts); | ||
} else { | ||
return Promise.resolve({}); | ||
} | ||
if (enableText && ctx.request.is(textTypes)) { | ||
return parse.text(ctx, textOpts) || ''; | ||
} | ||
return Promise.resolve({}); | ||
} | ||
}; | ||
function jsonOptions(opts) { | ||
const jsonOpts = {}; | ||
Object.assign(jsonOpts, opts); | ||
jsonOpts.limit = opts.jsonLimit; | ||
return jsonOpts; | ||
function formatOptions(opts, type) { | ||
var res = {}; | ||
Object.assign(res, opts); | ||
res.limit = opts[type + 'Limit']; | ||
return res; | ||
} | ||
function formOptions(opts) { | ||
const formOpts = {}; | ||
Object.assign(formOpts, opts); | ||
formOpts.limit = opts.formLimit; | ||
return formOpts; | ||
} | ||
function extendType(original, extend) { | ||
@@ -104,1 +114,5 @@ if (extend) { | ||
} | ||
function checkEnable(types, type) { | ||
return types.indexOf(type) >= 0; | ||
} |
{ | ||
"name": "koa-bodyparser", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "a body parser for koa", | ||
@@ -34,3 +34,3 @@ "main": "index.js", | ||
"autod": "1", | ||
"istanbul": "^0.4.3", | ||
"istanbul": "~0.4.3", | ||
"koa": "^2.0.0", | ||
@@ -37,0 +37,0 @@ "mocha": "^2.4.5", |
@@ -51,7 +51,8 @@ koa-bodyparser | ||
* **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`. | ||
* * **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`. | ||
@@ -58,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
9650
93
89