Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use async function and support koa@2 #4

Merged
merged 4 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.... 这个库名不会坑死人么

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无所谓了,koa-bodyparser 是我们维护的,body-parser 估计是最早占了坑了

const override = require('koa-override')

app.use(bodyParser())
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrow function ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrow function 就丢函数名了

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

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();
};
};
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"index.js"
],
"scripts": {
"test": "mocha -r co-mocha -R spec -t 5000 test/*.test.js",
"test": "mocha -R spec -t 5000 test/*.test.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用 egg-bin 不?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用 egg-bin 吧,方便以后统一升级。

"cov": "nyc -r text-summary -r lcov npm test",
"lint": "eslint test *.js",
"ci": "npm run lint && npm run cov",
Expand All @@ -18,12 +18,11 @@
},
"devDependencies": {
"autod": "*",
"co-mocha": "^1.2.0",
"egg-ci": "^1.7.0",
"eslint": "^4.0.0",
"eslint-config-egg": "^4.2.1",
"koa": "1",
"koa-body-parser": "1",
"koa": "2",
"koa-bodyparser": "4",
"mocha": "*",
"nyc": "^11.0.2",
"supertest": "3"
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