Skip to content

Commit

Permalink
Update middleware to be koa v2 Promises
Browse files Browse the repository at this point in the history
  • Loading branch information
venables committed Nov 10, 2015
1 parent acffb82 commit a885b2c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ 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.svg?style=flat-square)](https://david-dm.org/venables/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)
[![Downloads](https://img.shields.io/npm/dm/koa-json-body.svg?style=flat-square)](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.

NOTE
----

This branch is for koa v2, which uses Promises instead of Generator functions. For koa 0.x and 1.x support, see the [koa-1](https://github.com/venables/koa-json-body/tree/koa-1) branch.

Installation
------------

Expand Down
17 changes: 0 additions & 17 deletions index.js

This file was deleted.

20 changes: 20 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const body = require('co-body');

module.exports = function(opts) {
opts = opts || { strict: true };

return (ctx, next) => {
if (ctx.method === 'GET' || ctx.method === 'DELETE') {
return next();
}

return body.json(ctx.req, opts).then((body) => {
ctx.request.body = body;
return next();
}, (err) => {
return next(err);
});
};
};
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "koa-json-body",
"version": "4.0.0",
"version": "5.0.0-alpha.1",
"description": "koa middleware to parse json request bodies",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha test"
},
"repository": {
"type": "git",
Expand All @@ -23,7 +23,18 @@
"url": "https://github.com/venables/koa-json-body/issues"
},
"homepage": "https://github.com/venables/koa-json-body",
"engines": {
"node": ">= 4.0.0"
},
"dependencies": {
"co-body": "^4.0.0"
},
"devDependencies": {
"koa": ">= 2.0.0-alpha.3",
"mocha": "^2.3.3",
"supertest": "^1.1.0"
},
"peerDependencies": {
"koa": ">= 2.0.0-alpha.3"
}
}
47 changes: 47 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const body = require('../');
const Koa = require('koa');
const request = require('supertest');

describe('integration', function() {
let app;

before(function() {
app = new Koa();
app.use(body());
app.use( ctx => {
ctx.body = ctx.request.body;
});
});

it('does nothing on a GET request', function(done) {
request(app.listen())
.get('/')
.expect(204, done);
});

it('does nothing on a DELETE request', function(done) {
request(app.listen())
.delete('/')
.expect(204, done);
});

it('parses JSON and assigns it to ctx.request.body', function(done) {
request(app.listen())
.post('/')
.send({
something: 'awesome'
})
.expect(200, { something: 'awesome' }, done);
});

it('silently handles invalid JSON', function(done) {
request(app.listen())
.post('/')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send('{ "not": "json"')
.expect(204, done);
});
});

0 comments on commit a885b2c

Please sign in to comment.