Skip to content

Commit

Permalink
refactor: use async function and support koa@2 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Nov 8, 2017
1 parent d58fe55 commit e92571f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 57 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
- '8'
- '9'
install:
- npm i npminstall && npminstall
script:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $ npm install koa-override --save
## Usage

```js
const bodyParser = require('koa-body-parser')
const bodyParser = require('koa-bodyparser')
const override = require('koa-override')

app.use(bodyParser())
Expand Down
5 changes: 2 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
Expand All @@ -11,6 +10,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ module.exports = options => {
options = options || {};
options.allowedMethods = options.allowedMethods || [ 'POST' ];

return function* overrideMethod(next) {
const orginalMethod = this.request.method;
if (options.allowedMethods.indexOf(orginalMethod) === -1) return yield next;
return function overrideMethod(ctx, next) {
const orginalMethod = ctx.request.method;
if (options.allowedMethods.indexOf(orginalMethod) === -1) return next();

let method;
// body support
const body = this.request.body;
const body = ctx.request.body;
if (body && body._method) {
method = body._method.toUpperCase();
} else {
// header support
const header = this.get('x-http-method-override');
const header = ctx.get('x-http-method-override');
if (header) {
method = header.toUpperCase();
}
Expand All @@ -30,11 +30,11 @@ module.exports = options => {
// if you want to support other methods,
// just create your own utility!
if (methods.indexOf(method) === -1) {
this.throw(400, `invalid overriden method: "${method}"`);
ctx.throw(400, `invalid overriden method: "${method}"`);
}
this.request.method = method;
ctx.request.method = method;
}

yield next;
return next();
};
};
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"index.js"
],
"scripts": {
"test": "mocha -r co-mocha -R spec -t 5000 test/*.test.js",
"cov": "nyc -r text-summary -r lcov npm test",
"test": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint test *.js",
"ci": "npm run lint && npm run cov",
"autod": "autod -w --prefix '^'"
Expand All @@ -18,14 +18,12 @@
},
"devDependencies": {
"autod": "*",
"co-mocha": "^1.2.0",
"egg-bin": "^4.3.5",
"egg-ci": "^1.7.0",
"eslint": "^4.0.0",
"eslint-config-egg": "^4.2.1",
"koa": "1",
"koa-body-parser": "1",
"mocha": "*",
"nyc": "^11.0.2",
"koa": "2",
"koa-bodyparser": "4",
"supertest": "3"
},
"homepage": "https://github.com/node-modules/koa-override",
Expand All @@ -44,10 +42,10 @@
"rewrite"
],
"engines": {
"node": ">= 4.0.0"
"node": ">= 8.0.0"
},
"ci": {
"version": "4, 6, 8"
"version": "8, 9"
},
"author": "fengmk2 <[email protected]> (http://fengmk2.com)",
"license": "MIT"
Expand Down
68 changes: 34 additions & 34 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
const assert = require('assert');
const request = require('supertest');
const koa = require('koa');
const bodyParser = require('koa-body-parser');
const bodyParser = require('koa-bodyparser');
const override = require('../');

describe('override method middleware', () => {
it('should override with x-http-method-override header', () => {
const app = koa();
const app = new koa();
app.use(override());
app.use(function* () {
this.body = {
method: this.method,
url: this.url,
app.use(ctx => {
ctx.body = {
method: ctx.method,
url: ctx.url,
};
});

Expand All @@ -28,14 +28,14 @@ describe('override method middleware', () => {
});

it('should override with body._method', () => {
const app = koa();
const app = new koa();
app.use(bodyParser());
app.use(override());
app.use(function* () {
this.body = {
method: this.method,
url: this.url,
body: this.request.body,
app.use(ctx => {
ctx.body = {
method: ctx.method,
url: ctx.url,
body: ctx.request.body,
};
});

Expand All @@ -57,7 +57,7 @@ describe('override method middleware', () => {
});

it('should throw invalid overriden method error', () => {
const app = koa();
const app = new koa();
app.on('error', function(err) {
assert.equal(err.message, 'invalid overriden method: "SAVE"');
});
Expand All @@ -71,14 +71,14 @@ describe('override method middleware', () => {
});

it('should dont override method on body._method and header both not match', () => {
const app = koa();
const app = new koa();
app.use(bodyParser());
app.use(override());
app.use(function* () {
this.body = {
method: this.method,
url: this.url,
body: this.request.body,
app.use(ctx => {
ctx.body = {
method: ctx.method,
url: ctx.url,
body: ctx.request.body,
};
});

Expand All @@ -95,12 +95,12 @@ describe('override method middleware', () => {
});

it('should not allow override with x-http-method-override header on GET request', () => {
const app = koa();
const app = new koa();
app.use(override());
app.use(function* () {
this.body = {
method: this.method,
url: this.url,
app.use(ctx => {
ctx.body = {
method: ctx.method,
url: ctx.url,
};
});

Expand All @@ -115,12 +115,12 @@ describe('override method middleware', () => {
});

it('should not allow override with x-http-method-override header on PUT request', () => {
const app = koa();
const app = new koa();
app.use(override());
app.use(function* () {
this.body = {
method: this.method,
url: this.url,
app.use(ctx => {
ctx.body = {
method: ctx.method,
url: ctx.url,
};
});

Expand All @@ -135,14 +135,14 @@ describe('override method middleware', () => {
});

it('should custom options.allowedMethods work', () => {
const app = koa();
const app = new koa();
app.use(override({
allowedMethods: [ 'POST', 'PUT' ],
}));
app.use(function* () {
this.body = {
method: this.method,
url: this.url,
app.use(ctx => {
ctx.body = {
method: ctx.method,
url: ctx.url,
};
});

Expand Down

0 comments on commit e92571f

Please sign in to comment.