Skip to content

Commit

Permalink
Update koa middleware to match v2.x api
Browse files Browse the repository at this point in the history
  • Loading branch information
simonratner committed Jul 28, 2017
1 parent 09f18c0 commit d476b27
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
10 changes: 5 additions & 5 deletions lib/middlewares/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ var Parameters = require('../parameters')
/**
* Koa middleware for strong params.
*
* @return {generator}
* @return {Function}
* @api public
*/

module.exports = function () {
return function *(next) {
return function (ctx, next) {

/**
* Params data.
Expand All @@ -21,7 +21,7 @@ module.exports = function () {
* Params `getter` and `setter`.
*/

Object.defineProperty(this, 'params', {
Object.defineProperty(ctx, 'params', {

/**
* Returns an extended data object of merged context params.
Expand Down Expand Up @@ -54,12 +54,12 @@ module.exports = function () {
* method, use `koa-bodyparser` middleware.
*/

this.params = _.merge({}, this.request.body, this.query)
ctx.params = _.merge({}, ctx.request.body, ctx.query)

/*
* Next middleware.
*/

yield next
return next()
}
}
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "Rails-style strong parameters for javascript projects. (e.g. Express, Koa)",
"main": "index.js",
"scripts": {
"test": "standard && mocha --harmony --recursive ./spec"
"test": "standard && mocha --recursive ./spec"
},
"engines": {
"node": ">= 0.11.9"
"node": "8"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,6 +40,7 @@
],
"author": "ssowonny",
"contributors": [
"simonratner",
"vellotis"
],
"license": "MIT",
Expand All @@ -48,11 +49,11 @@
},
"homepage": "https://github.com/ssowonny/strong-params",
"devDependencies": {
"body-parser": "~1.10.2",
"express": "~4.11.1",
"koa": "^0.10.0",
"koa-bodyparser": "^1.0.1",
"koa-qs": "^1.0.0",
"body-parser": "^1.17.2",
"express": "^4.15.3",
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"koa-qs": "^2.0.0",
"mocha": "^3.0.2",
"request": "^2.40.0",
"should": "^11.1.0",
Expand All @@ -61,6 +62,6 @@
},
"dependencies": {
"es6-object-assign": "^1.0.2",
"lodash": "^3.0.0"
"lodash": "^4.17.4"
}
}
26 changes: 13 additions & 13 deletions spec/middlewares/koa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('koaMiddleware', function () {
ctx = {}
ctx.port = 3001
ctx.url = 'http://localhost:' + ctx.port
ctx.app = koa()
ctx.app = new koa() // eslint-disable-line new-cap
qs(ctx.app)
ctx.app.use(bodyparser())
ctx.app.use(params.koaMiddleware())
Expand All @@ -28,8 +28,8 @@ describe('koaMiddleware', function () {
describe('req.params.all()', function () {

it('should return `all` params', function (done) {
ctx.app.use(function *() {
this.body = this.params.all()
ctx.app.use(function (ctx, next) {
ctx.body = ctx.params.all()
})
ctx.server = ctx.app.listen(ctx.port)

Expand All @@ -44,8 +44,8 @@ describe('koaMiddleware', function () {
describe('req.params.permit()', function () {

it('should return `permit` selected params', function (done) {
ctx.app.use(function *() {
this.body = this.params.permit('p1', 'a2').value()
ctx.app.use(function (ctx, next) {
ctx.body = ctx.params.permit('p1', 'a2').value()
})
ctx.server = ctx.app.listen(ctx.port)

Expand All @@ -60,8 +60,8 @@ describe('koaMiddleware', function () {
describe('req.params.require()', function () {

it('should return a `params` object of the required key', function (done) {
ctx.app.use(function *() {
this.body = this.params.require('p1').all()
ctx.app.use(function (ctx, next) {
ctx.body = ctx.params.require('p1').all()
})
ctx.server = ctx.app.listen(ctx.port)

Expand All @@ -72,16 +72,16 @@ describe('koaMiddleware', function () {
})

it('should throw an exception if the required key does not exist', function (done) {
ctx.app.use(function * (next) {
ctx.app.use(async function (ctx, next) {
try {
yield* next
await next()
} catch (err) {
this.response.status = 500
this.response.body = err.message
ctx.response.status = 500
ctx.response.body = err.message
}
})
ctx.app.use(function *() {
this.body = this.params.require('xx').all()
ctx.app.use(function (ctx, next) {
ctx.body = ctx.params.require('xx').all()
})
ctx.server = ctx.app.listen(ctx.port)

Expand Down

0 comments on commit d476b27

Please sign in to comment.