Comparing version 0.0.4 to 0.1.0
63
index.js
@@ -0,29 +1,56 @@ | ||
/*! | ||
* koa-body | ||
* Copyright (c) 2014. Daryl Lau (@daryllau), Charlike Mike Reagent (@tunnckoCore) | ||
* MIT LICENSE | ||
*/ | ||
'use strict'; | ||
var co_body = require('co-body'); | ||
module.exports = function(opts){ | ||
var patchNode = (opts && 'patchNode' in opts) ? opts.patchNode : false; | ||
var patchKoa = (opts && 'patchKoa' in opts) ? opts.patchKoa : true; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var co_body = require('co-body'); | ||
/** | ||
* Expose `requestbody()`. | ||
*/ | ||
module.exports = requestbody; | ||
/** | ||
* Initialize module middleware with the given `options`: | ||
* - `jsonLimit` limits application/json request body, co-body's option | ||
* - `formLimit` limits application/x-www-form-urlencoded request body, co-body's option | ||
* - `patchNode` Set the body parameter in the **Node** request object `this.req.body` | ||
* - `patchKoa` Set the body parameter in the **Koa** request object `this.request.body` | ||
* - `encoding` request encoding, co-body's option | ||
* | ||
* @param {Object} options | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
function requestbody(opts) { | ||
var jsonLimit = (opts && 'jsonLimit' in opts) ? opts.jsonLimit : '1mb', | ||
formLimit = (opts && 'formLimit' in opts) ? opts.formLimit : '56kb', | ||
patchNode = (opts && 'patchNode' in opts) ? opts.patchNode : false, | ||
patchKoa = (opts && 'patchKoa' in opts) ? opts.patchKoa : true, | ||
encoding = (opts && 'encoding' in opts) ? opts.encoding : 'utf-8'; | ||
return function *(next){ | ||
var body; | ||
if(this.is('application/json')){ | ||
body = yield co_body.json(this); | ||
if(patchNode){ | ||
this.req.body = body; | ||
} | ||
if(patchKoa){ | ||
this.request.body = body; | ||
} | ||
body = yield co_body.json(this,{encoding: encoding, limit: jsonLimit}); | ||
} | ||
else if(this.is('application/x-www-form-urlencoded')){ | ||
body = yield co_body.form(this); | ||
if(patchNode){ | ||
this.req.body = body; | ||
} | ||
if(patchKoa){ | ||
this.request.body = body; | ||
} | ||
body = yield co_body.form(this,{encoding: encoding, limit: formLimit}); | ||
} | ||
if(patchNode){ | ||
this.req.body = body; | ||
} | ||
if(patchKoa){ | ||
this.request.body = body; | ||
} | ||
yield next; | ||
}; | ||
}; |
{ | ||
"name": "koa-body", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "koa middleware that parses request body", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"example.js", | ||
"test.js", | ||
"README.md", | ||
"LICENSE", | ||
"Makefile", | ||
".gitignore", | ||
".npmignore" | ||
], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"start": "make start", | ||
"test": "make test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/dlau/koa-body" | ||
"url": "https://github.com/dlau/koa-body" | ||
}, | ||
"keywords": [ | ||
"koa", | ||
"request", | ||
"middleware", | ||
"body", | ||
"request", | ||
"parse" | ||
"bodyparser", | ||
"parse", | ||
"json", | ||
"bodyjson", | ||
"parsejson", | ||
"parseform", | ||
"form" | ||
], | ||
"author": "Daryl Lau", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/dlau/koa-body/issues" | ||
"dependencies": { | ||
"co-body": "*" | ||
}, | ||
"homepage": "https://github.com/dlau/koa-body", | ||
"dependencies": { | ||
"co-body": "0.0.1" | ||
} | ||
"devDependencies": { | ||
"koa": "*", | ||
"mocha": "*", | ||
"should": "*", | ||
"supertest": "*" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Daryl Lau" | ||
}, | ||
{ | ||
"name": "Charlike Mike Reagent" | ||
} | ||
], | ||
"author": "Daryl Lau (http://weak.io/)", | ||
"license": "MIT" | ||
} |
108
README.md
@@ -0,10 +1,12 @@ | ||
koa-body [![Build Status](https://travis-ci.org/dlau/koa-body.png)](https://travis-ci.org/dlau/koa-body) [![Dependencies Status](https://david-dm.org/dlau/koa-body/status.svg)](https://david-dm.org/dlau/koa-body) | ||
================ | ||
# koa-body | ||
KoaJS middleware that patch body (to Koa#req.body or Node#request.body, or bother) and parse body to json/form with co-body. | ||
[koa](https://github.com/koajs/koa) middleware for parsing a request body. | ||
This is a simple wrapper around [co-body](https://github.com/co/co-body). Provides similar functionality to the express's [request body parser](http://expressjs.com/api.html#req.body) | ||
[koa](https://github.com/koajs/koa) middleware for parsing a request body. This is a simple wrapper around [co-body](https://github.com/co/co-body). Provides similar functionality to the express's [request body parser](http://expressjs.com/api.html#req.body) | ||
Doesn't support multipart. [Discuss](https://github.com/dlau/koa-body/issues/1) | ||
Doesn't support multipart | ||
## Installation | ||
## Install | ||
``` | ||
@@ -14,28 +16,96 @@ $ npm install koa-body | ||
## Options | ||
- patchNode | ||
Set the body parameter in the node request object `this.req.body` | ||
Initialize koa-better-body middleware with the given `options`: | ||
- **patchNode** Set the body parameter in the **Node** request object `this.req.body` | ||
*defauls to false* | ||
- patchKoa | ||
Set the body parameter in the koa request object `this.request.body` | ||
- **patchKoa** Set the body parameter in the **Koa** request object `this.request.body` | ||
*defaults to true* | ||
- **jsonLimit** limits application/json request body, co-body's option | ||
*defauls to '1mb'* | ||
- **formLimit** limits application/x-www-form-urlencoded request body, co-body's option | ||
*defauls to '56kb'* | ||
- **encoding** request encoding, co-body's option | ||
*defauls to 'utf-8'* | ||
## Example | ||
## Usage | ||
```js | ||
var koa = require('koa'); | ||
var koa_body = require('koa-body'); | ||
var app = koa(); | ||
var app = require('koa')() | ||
, betterBody = require('./index'); | ||
/** | ||
* By default body is patching to | ||
* Koa's ctx.request | ||
*/ | ||
app.use(betterBody({patchNode: false, jsonLimit: '1kb', formLimit: '1kb'})); | ||
app.use(koa_body()); | ||
app.use(function *(next){ | ||
var patchKoa = (this.request.body) ? this.request.body : 'patchKoa=true, by default'; | ||
var patchNode = (this.req.body) ? this.req.body : 'patchNode=false, by default'; | ||
app.use(function *(){ | ||
console.log(this.request.body); | ||
if (this.request.method == 'POST') { | ||
this.status = 201; | ||
} else if (this.request.method == 'GET') { | ||
this.status = 200 | ||
} | ||
this.body = JSON.stringify({koa: patchKoa, node: patchNode}); | ||
if (this.request.method == 'PUT') { | ||
this.status = 200; | ||
this.body = 'resource updated successfully'; | ||
} | ||
if (this.request.method == 'DELETE') { | ||
this.status = 204; | ||
this.body = 'resource deleted successfully'; | ||
} | ||
}); | ||
var port = process.env.PORT || 3333; | ||
app.listen(port); | ||
console.log('Koa server start listening to port '+port); | ||
``` | ||
# License | ||
## Usage with [koa-router](https://github.com/alexmingoia/koa-router) | ||
> It's generally better to only parse the body as needed, if using a router that supports middleware composition, we can inject it only for certain routes. | ||
MIT | ||
```js | ||
var app = require('koa')(), | ||
router = require('koa-router'), | ||
koaBody = require('koa-body')(); | ||
app.use(router()); | ||
app.post('/users', koaBody, | ||
function *(next) { | ||
console.log(this.request.body); | ||
// => POST body | ||
} | ||
); | ||
``` | ||
## Features | ||
- 14 passing (174ms) tests | ||
- co-body options: jsonLimit, formLimit, encoding | ||
- freedom to choose - patch body to | ||
* KoaJS Context `this.request.body` | ||
* NodeJS Context `this.req.body` | ||
- only 1 dependency: `co-body` | ||
## Test, Bench, Example | ||
First run `npm install` before run anything. | ||
``` | ||
npm test | ||
npm start | ||
``` | ||
## Credit | ||
|[![Daryl Lau](https://avatars2.githubusercontent.com/u/2764274?s=144)](https://github.com/dlau)| [![Charlike Mike Reagent](https://avatars2.githubusercontent.com/u/5038030?s=144)](https://github.com/tunnckoCore)| | ||
|---|---| | ||
|[Daryl Lau](https://github.com/dlau) (creator) | [George Yanev](https://github.com/tunnckoCore) (contrib)| | ||
## LICENSE | ||
The MIT License, 2014 [Daryl Lau](http://weak.io) ([@daryllau](https://twitter.com/tunnckoCore)), [Charlike Mike Reagent](https://github.com/tunnckoCore) ([@tunnckoCore](https://twitter.com/tunnckoCore)) | ||
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
14644
8
310
111
4
1
1
1
2
+ Added@hapi/bourne@3.0.0(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedco-body@6.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.1.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.1.0(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)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedqs@6.13.1(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)
+ Addedtype-is@1.6.18(transitive)
+ Addedunpipe@1.0.0(transitive)
- Removedbytes@1.0.0(transitive)
- Removedco-body@0.0.1(transitive)
- Removedqs@0.6.6(transitive)
- Removedraw-body@1.1.7(transitive)
- Removedstring_decoder@0.10.31(transitive)
Updatedco-body@*