From d476b2715a935cea2d70c250af47cf16c08d0ce2 Mon Sep 17 00:00:00 2001 From: Simon Ratner Date: Fri, 28 Jul 2017 00:51:35 -0700 Subject: [PATCH] Update koa middleware to match v2.x api --- lib/middlewares/koa.js | 10 +++++----- package.json | 17 +++++++++-------- spec/middlewares/koa.spec.js | 26 +++++++++++++------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/middlewares/koa.js b/lib/middlewares/koa.js index f13fda9..3347b92 100644 --- a/lib/middlewares/koa.js +++ b/lib/middlewares/koa.js @@ -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. @@ -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. @@ -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() } } diff --git a/package.json b/package.json index d7e55e6..8c193b7 100644 --- a/package.json +++ b/package.json @@ -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", @@ -40,6 +40,7 @@ ], "author": "ssowonny", "contributors": [ + "simonratner", "vellotis" ], "license": "MIT", @@ -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", @@ -61,6 +62,6 @@ }, "dependencies": { "es6-object-assign": "^1.0.2", - "lodash": "^3.0.0" + "lodash": "^4.17.4" } } diff --git a/spec/middlewares/koa.spec.js b/spec/middlewares/koa.spec.js index b8a7859..3f5466a 100644 --- a/spec/middlewares/koa.spec.js +++ b/spec/middlewares/koa.spec.js @@ -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()) @@ -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) @@ -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) @@ -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) @@ -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)