koa-bodyparser
Advanced tools
Comparing version 4.3.0 to 4.4.0
4.4.0 / 2023-03-15 | ||
================== | ||
**features** | ||
* [[`a9a6476`](http://github.com/koajs/bodyparser/commit/a9a647641bb883746c9691e86b8f87739df4e374)] - feat: Support scim json format (#151) (ask me anything :) <<caryyu.tg@gmail.com>>) | ||
**fixes** | ||
* [[`4d931c6`](http://github.com/koajs/bodyparser/commit/4d931c634e9b59a843152f56d68b3ef2e1719675)] - fix: revert html parser, use text directly (dead-horse <<dead_horse@qq.com>>) | ||
**others** | ||
* [[`c02ec0c`](http://github.com/koajs/bodyparser/commit/c02ec0c062f92e1114b4196534669367eae14ccc)] - Update README.md (#149) (sgywzy <<44345776+sgywzy@users.noreply.github.com>>) | ||
* [[`85b426f`](http://github.com/koajs/bodyparser/commit/85b426fea3d98481fd4acbafce0857189199426e)] - Recommend @koa/multer for multipart/form-data (#145) (Jim Fisher <<jameshfisher@gmail.com>>) | ||
* [[`afecb1a`](http://github.com/koajs/bodyparser/commit/afecb1ab7303ebd36d1a50d6bfe5fc3125759e43)] - Update Repo + Add Html Parser (#134) (imed jaberi <<imed_jebari@hotmail.fr>>) | ||
* [[`ecc6ebf`](http://github.com/koajs/bodyparser/commit/ecc6ebfad7179e0009501723e7b2227d25c9603d)] - docs: fix broken npmjs link (#132) (Joel Colucci <<joelcolucci@users.noreply.github.com>>) | ||
* [[`336b287`](http://github.com/koajs/bodyparser/commit/336b2879dc7c0e048d79e28bf23d4b8fe2589376)] - Update README.md (haoxin <<haoxinst@gmail.com>>) | ||
* [[`e02cb7d`](http://github.com/koajs/bodyparser/commit/e02cb7dd2c798a116ef12c776da30c710697dea5)] - Update README.md (#125) (thaiworldgame <<36978149+thaiworldgame@users.noreply.github.com>>) | ||
4.3.0 / 2020-03-24 | ||
@@ -3,0 +20,0 @@ ================== |
78
index.js
@@ -1,2 +0,2 @@ | ||
/**! | ||
/** ! | ||
* koa-body-parser - index.js | ||
@@ -17,4 +17,4 @@ * Copyright(c) 2014 | ||
var parse = require('co-body'); | ||
var copy = require('copy-to'); | ||
const parse = require('co-body'); | ||
const copy = require('copy-to'); | ||
@@ -29,15 +29,15 @@ /** | ||
module.exports = function (opts) { | ||
module.exports = function(opts) { | ||
opts = opts || {}; | ||
var detectJSON = opts.detectJSON; | ||
var onerror = opts.onerror; | ||
const {detectJSON} = opts; | ||
const {onerror} = opts; | ||
var enableTypes = opts.enableTypes || ['json', 'form']; | ||
var enableForm = checkEnable(enableTypes, 'form'); | ||
var enableJson = checkEnable(enableTypes, 'json'); | ||
var enableText = checkEnable(enableTypes, 'text'); | ||
var enableXml = checkEnable(enableTypes, 'xml'); | ||
const enableTypes = opts.enableTypes || ['json', 'form']; | ||
const enableForm = checkEnable(enableTypes, 'form'); | ||
const enableJson = checkEnable(enableTypes, 'json'); | ||
const enableText = checkEnable(enableTypes, 'text'); | ||
const enableXml = checkEnable(enableTypes, 'xml'); | ||
opts.detectJSON = undefined; | ||
opts.onerror = undefined; | ||
opts.onerror = undefined; // eslint-disable-line unicorn/prefer-add-event-listener | ||
@@ -48,3 +48,3 @@ // force co-body return raw body | ||
// default json types | ||
var jsonTypes = [ | ||
const jsonTypes = [ | ||
'application/json', | ||
@@ -54,26 +54,20 @@ 'application/json-patch+json', | ||
'application/csp-report', | ||
'application/scim+json' | ||
]; | ||
// default form types | ||
var formTypes = [ | ||
'application/x-www-form-urlencoded', | ||
]; | ||
const formTypes = ['application/x-www-form-urlencoded']; | ||
// default text types | ||
var textTypes = [ | ||
'text/plain', | ||
]; | ||
const textTypes = ['text/plain']; | ||
// default xml types | ||
var xmlTypes = [ | ||
'text/xml', | ||
'application/xml', | ||
]; | ||
const xmlTypes = ['text/xml', 'application/xml']; | ||
var jsonOpts = formatOptions(opts, 'json'); | ||
var formOpts = formatOptions(opts, 'form'); | ||
var textOpts = formatOptions(opts, 'text'); | ||
var xmlOpts = formatOptions(opts, 'xml'); | ||
const jsonOpts = formatOptions(opts, 'json'); | ||
const formOpts = formatOptions(opts, 'form'); | ||
const textOpts = formatOptions(opts, 'text'); | ||
const xmlOpts = formatOptions(opts, 'xml'); | ||
var extendTypes = opts.extendTypes || {}; | ||
const extendTypes = opts.extendTypes || {}; | ||
@@ -85,5 +79,6 @@ extendType(jsonTypes, extendTypes.json); | ||
// eslint-disable-next-line func-names | ||
return async function bodyParser(ctx, next) { | ||
if (ctx.request.body !== undefined) return await next(); | ||
if (ctx.disableBodyParser) return await next(); | ||
if (ctx.request.body !== undefined || ctx.disableBodyParser) | ||
return await next(); // eslint-disable-line no-return-await | ||
try { | ||
@@ -100,2 +95,3 @@ const res = await parseBody(ctx); | ||
} | ||
await next(); | ||
@@ -105,14 +101,21 @@ }; | ||
async function parseBody(ctx) { | ||
if (enableJson && ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes))) { | ||
return await parse.json(ctx, jsonOpts); | ||
if ( | ||
enableJson && | ||
((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes)) | ||
) { | ||
return await parse.json(ctx, jsonOpts); // eslint-disable-line no-return-await | ||
} | ||
if (enableForm && ctx.request.is(formTypes)) { | ||
return await parse.form(ctx, formOpts); | ||
return await parse.form(ctx, formOpts); // eslint-disable-line no-return-await | ||
} | ||
if (enableText && ctx.request.is(textTypes)) { | ||
return await parse.text(ctx, textOpts) || ''; | ||
return (await parse.text(ctx, textOpts)) || ''; | ||
} | ||
if (enableXml && ctx.request.is(xmlTypes)) { | ||
return await parse.text(ctx, xmlOpts) || ''; | ||
return (await parse.text(ctx, xmlOpts)) || ''; | ||
} | ||
return {}; | ||
@@ -123,3 +126,3 @@ } | ||
function formatOptions(opts, type) { | ||
var res = {}; | ||
const res = {}; | ||
copy(opts).to(res); | ||
@@ -135,3 +138,4 @@ res.limit = opts[type + 'Limit']; | ||
} | ||
extend.forEach(function (extend) { | ||
extend.forEach(function(extend) { | ||
original.push(extend); | ||
@@ -138,0 +142,0 @@ }); |
{ | ||
"name": "koa-bodyparser", | ||
"version": "4.3.0", | ||
"description": "a body parser for koa", | ||
"version": "4.4.0", | ||
"description": "a body parser for Koa", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
"lint": "xo", | ||
"lint:fix": "xo --fix", | ||
"test": "mocha --require should test/*.spec.js --exit", | ||
"coverage": "nyc npm run test --reporter=lcov", | ||
"test-ci": "npm run lint && npm run coverage" | ||
}, | ||
@@ -23,15 +27,17 @@ "repository": { | ||
], | ||
"author": "dead_horse <dead_horse@qq.com> (http://deadhorse.me)", | ||
"author": { | ||
"name": "dead_horse", | ||
"email": "dead_horse@qq.com", | ||
"url": " http://deadhorse.me" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/koajs/body-parser/issues" | ||
}, | ||
"homepage": "https://github.com/koajs/body-parser", | ||
"devDependencies": { | ||
"autod": "2.4.2", | ||
"istanbul": "^0.4.5", | ||
"koa": "^2.0.1", | ||
"mocha": "^3.2.0", | ||
"should": "^11.2.0", | ||
"supertest": "^3.0.0" | ||
"eslint-config-xo-lass": "^1.0.3", | ||
"husky": "^4.2.5", | ||
"koa": "^2", | ||
"mocha": "^7.1.1", | ||
"nyc": "^15.0.1", | ||
"should": "^13.2.3", | ||
"supertest": "^4.0.2", | ||
"xo": "0.25.4" | ||
}, | ||
@@ -42,5 +48,25 @@ "dependencies": { | ||
}, | ||
"xo": { | ||
"prettier": true, | ||
"space": true, | ||
"extends": [ | ||
"xo-lass" | ||
], | ||
"rules": { | ||
"node/no-deprecated-api": "off", | ||
"no-unused-vars": "off", | ||
"no-prototype-builtins": "off", | ||
"prefer-rest-params": "off" | ||
}, | ||
"ignores": [ | ||
"test/**" | ||
] | ||
}, | ||
"engines": { | ||
"node": ">=8.0.0" | ||
} | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/koajs/body-parser/issues" | ||
}, | ||
"homepage": "https://github.com/koajs/body-parser" | ||
} |
@@ -1,4 +0,4 @@ | ||
koa-bodyparser | ||
=============== | ||
# [**koa-bodyparser**](https://github.com/koajs/bodyparser) | ||
[![NPM version][npm-image]][npm-url] | ||
@@ -9,6 +9,5 @@ [![build status][travis-image]][travis-url] | ||
[![node version][node-image]][node-url] | ||
[![Gittip][gittip-image]][gittip-url] | ||
[npm-image]: https://img.shields.io/npm/v/koa-bodyparser.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/koa-bodyparser | ||
[npm-url]: https://npmjs.com/package/koa-bodyparser | ||
[travis-image]: https://img.shields.io/travis/koajs/bodyparser.svg?style=flat-square | ||
@@ -20,11 +19,8 @@ [travis-url]: https://travis-ci.org/koajs/bodyparser | ||
[david-url]: https://david-dm.org/koajs/bodyparser | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_7.6-green.svg?style=flat-square | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_8-green.svg?style=flat-square | ||
[node-url]: http://nodejs.org/download/ | ||
[gittip-image]: https://img.shields.io/gittip/dead-horse.svg?style=flat-square | ||
[gittip-url]: https://www.gittip.com/dead-horse/ | ||
A body parser for koa, based on [co-body](https://github.com/tj/co-body). support `json`, `form` and `text` type body. | ||
> Notice: this module don't support parsing multipart format data, please use [co-busboy](https://github.com/cojs/busboy) to parse multipart format data. | ||
> Notice: this module doesn't support parsing multipart format data, please use [`@koa/multer`](https://github.com/koajs/multer) to parse multipart format data. | ||
@@ -38,6 +34,6 @@ ## Install | ||
```js | ||
var Koa = require('koa'); | ||
var bodyParser = require('koa-bodyparser'); | ||
const Koa = require('koa'); | ||
const bodyParser = require('koa-bodyparser'); | ||
var app = new Koa(); | ||
const app = new Koa(); | ||
app.use(bodyParser()); | ||
@@ -64,3 +60,3 @@ | ||
```js | ||
app.use(bodyparser({ | ||
app.use(bodyParser({ | ||
detectJSON: function (ctx) { | ||
@@ -75,3 +71,3 @@ return /\.json$/i.test(ctx.path); | ||
```js | ||
app.use(bodyparser({ | ||
app.use(bodyParser({ | ||
extendTypes: { | ||
@@ -86,5 +82,5 @@ json: ['application/x-javascript'] // will parse application/x-javascript type body as a JSON string | ||
```js | ||
app.use(bodyparser({ | ||
app.use(bodyParser({ | ||
onerror: function (err, ctx) { | ||
ctx.throw('body parse error', 422); | ||
ctx.throw(422, 'body parse error'); | ||
} | ||
@@ -96,9 +92,9 @@ })); | ||
```js | ||
app.use(async (ctx, next) => { | ||
if (ctx.path === '/disable') ctx.disableBodyParser = true; | ||
await next(); | ||
}); | ||
app.use(bodyparser()); | ||
``` | ||
```js | ||
app.use(async (ctx, next) => { | ||
if (ctx.path === '/disable') ctx.disableBodyParser = true; | ||
await next(); | ||
}); | ||
app.use(bodyParser()); | ||
``` | ||
@@ -120,4 +116,4 @@ ## Raw Body | ||
## Licences | ||
#### 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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
14712
8
113
112
1