koa-json-body
Advanced tools
Comparing version 5.2.0 to 5.3.0
@@ -5,6 +5,16 @@ 'use strict' | ||
/** | ||
* Enable the koa-json-body middleware | ||
* | ||
* @params {Object} [opts] - The options object (optional) | ||
* @returns {Function} - The koa-json-body middleware | ||
*/ | ||
module.exports = function (opts) { | ||
opts = opts || { strict: true } | ||
let _opts = opts || { strict: true, limit: '1mb', fallback: false } | ||
return (ctx, next) => { | ||
if (_opts.fallback) { | ||
ctx.request.body = {} | ||
} | ||
if (ctx.method === 'GET' || ctx.method === 'DELETE') { | ||
@@ -14,6 +24,10 @@ return next() | ||
return body.json(ctx.req, opts).then((body) => { | ||
return body.json(ctx.req, _opts).then((body) => { | ||
ctx.request.body = body | ||
return next() | ||
}, (err) => { | ||
if (err.statusCode) { | ||
ctx.throw(err) | ||
} | ||
return next(err) | ||
@@ -20,0 +34,0 @@ }) |
{ | ||
"name": "koa-json-body", | ||
"version": "5.2.0", | ||
"description": "koa middleware to parse json request bodies", | ||
"version": "5.3.0", | ||
"description": "Single-purpose koa middleware to parse valid JSON request bodies and nothing else.", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha test" | ||
"test": "./node_modules/.bin/standard && NODE_ENV=test ./node_modules/.bin/nyc --reporter=lcov --reporter=text-summary ./node_modules/.bin/mocha 'test/**/*.spec.js'", | ||
"coverage": "./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls" | ||
}, | ||
@@ -30,9 +31,12 @@ "repository": { | ||
"dependencies": { | ||
"co-body": "^4.0.0" | ||
"co-body": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.16", | ||
"koa": "next", | ||
"mocha": "^3.2.0", | ||
"supertest": "^2.0.1" | ||
"nyc": "^10.1.2", | ||
"standard": "^9.0.0", | ||
"supertest": "^3.0.0" | ||
} | ||
} |
koa-json-body | ||
============= | ||
[![Version](https://img.shields.io/npm/v/koa-json-body.svg?style=flat-square)](https://www.npmjs.com/package/koa-json-body) | ||
[![Dependency Status](https://img.shields.io/david/venables/koa-json-body/master.svg?style=flat-square)](https://david-dm.org/venables/koa-json-body) | ||
[![Build Status](https://img.shields.io/travis/venables/koa-json-body/master.svg?style=flat-square)](https://travis-ci.org/venables/koa-json-body) | ||
[![Version](https://img.shields.io/npm/v/koa-json-body.svg)](https://www.npmjs.com/package/koa-json-body) | ||
[![Build Status](https://img.shields.io/travis/venables/koa-json-body/master.svg)](https://travis-ci.org/venables/koa-json-body) | ||
[![Coverage Status](https://img.shields.io/coveralls/venables/koa-json-body.svg)](https://coveralls.io/github/venables/koa-json-body) | ||
[![Dependency Status](https://img.shields.io/david/venables/koa-json-body/master.svg)](https://david-dm.org/venables/koa-json-body) | ||
[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
[![Downloads](https://img.shields.io/npm/dm/koa-json-body.svg?style=flat-square)](https://www.npmjs.com/package/koa-json-body) | ||
[![Downloads](https://img.shields.io/npm/dm/koa-json-body.svg)](https://www.npmjs.com/package/koa-json-body) | ||
Simple [koa](https://github.com/koajs/koa) middleware wrapper around [co-body](https://github.com/visionmedia/co-body) for parsing JSON request bodies. | ||
---------- | ||
This will not parse anythig but valid JSON request bodies. If there is a JSON parsing error, the middleware will set `ctx.request.body` to `{}` and continue. | ||
A single-purpose [koa](https://github.com/koajs/koa) middleware to only parse JSON request bodies and nothing else. | ||
By default, this libarary parses all **valid** JSON bodies on `POST`, `PUT`, and `PATCH` requests, and assigns the value to `ctx.request.body`. | ||
If there is a JSON parsing error, or if the request is not of valid type, `ctx.request.body` is not set, and will be `undefined`. If the JSON request payload is too large (by [default](#options), the limit is `1mb`), a `413 Payload Too Large` error will be thrown. | ||
To ensure `ctx.request.body` contains an empty object `{}` (rather than `undefined`) on missing/invalid payloads, you can set the [`fallback` option](#options) to `true`. | ||
Installation | ||
@@ -18,2 +25,8 @@ ------------ | ||
```bash | ||
yarn add koa-json-body | ||
``` | ||
or via npm: | ||
```bash | ||
npm install koa-json-body --save | ||
@@ -25,6 +38,8 @@ ``` | ||
Available via [co-body](https://github.com/visionmedia/co-body): | ||
* `fallback` - when set to `true`, `ctx.request.body` will always contain `{}` upon missing or invalid payloads. (default: `false`) | ||
* `limit` - number or string representing the request size limit (default: `1mb`) | ||
* `strict` - when set to `true`, koa-json-body will only accept arrays and objects. (default: `true`) | ||
Additional options available via [co-body](https://github.com/cojs/co-body). | ||
Usage | ||
@@ -36,7 +51,7 @@ ----- | ||
```javascript | ||
var jsonBody = require('koa-json-body') | ||
const body = require('koa-json-body') | ||
app.use(jsonBody({ limit: '10kb' })) | ||
app.use(body({ limit: '10kb', fallback: true })) | ||
app.use(function (ctx, next) { | ||
app.use((ctx, next) => { | ||
console.log(ctx.request.body) | ||
@@ -46,8 +61,8 @@ }) | ||
On a per-route basis (using [koa-router](https://github.com/alexmingoia/koa-router)): | ||
On a per-route basis (this example uses [koa-router](https://github.com/alexmingoia/koa-router)): | ||
```javascript | ||
var jsonBody = require('koa-json-body')({ limit: '10kb' }) | ||
const body = require('koa-json-body')({ limit: '10kb' }) | ||
app.post('/users', jsonBody, function (ctx, next) { | ||
app.post('/users', body, (ctx, next) => { | ||
console.log(ctx.request.body) | ||
@@ -57,11 +72,16 @@ }) | ||
Made for koa 2 | ||
-------------- | ||
For koa 0.x and 1.x support, see the [koa-1](https://github.com/venables/koa-json-body/tree/koa-1) branch. | ||
Testing | ||
------- | ||
To test, simply run | ||
Versioning | ||
---------- | ||
``` | ||
yarn test | ||
``` | ||
Major versions will map to co-body major versions (e.g. koa-json-helmet 4.x.x uses co-body 4.y.y) | ||
Made for koa 2 and beyond | ||
------------------------- | ||
For koa 0.x and 1.x support, see the [koa-1](https://github.com/venables/koa-json-body/tree/koa-1) branch. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
95412
186
83
6
1
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedco-body@5.2.0(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinflation@2.1.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
- Removedbytes@2.4.0(transitive)
- Removedco-body@4.2.0(transitive)
- Removediconv-lite@0.4.13(transitive)
- Removedinflation@2.0.0(transitive)
- Removedqs@4.0.0(transitive)
- Removedraw-body@2.1.7(transitive)
Updatedco-body@^5.0.0