Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-json-body

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-json-body - npm Package Compare versions

Comparing version 5.2.0 to 5.3.0

test/integration.spec.js

18

lib/index.js

@@ -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 @@ })

14

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc