Skip to content

Commit

Permalink
remove limiter && add convert method
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed May 28, 2016
1 parent 151823c commit cd5340d
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 98 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,4 +1,6 @@
node_modules
coverage
.vscode
typings
typings
jsconfig.json
lib
43 changes: 19 additions & 24 deletions README.md
Expand Up @@ -15,7 +15,9 @@ npm install koa-decorators

* [@router](#router)
* [@required](#required)
* [@limiter](#limiter)

## Methods
* [convert](#convert)

## Docs

Expand Down Expand Up @@ -104,36 +106,29 @@ module.exports = class UserController {
> user
```

### @limiter
### convert(fun)

Add rate-limiting for the decorated controller.
Convert an async function to koa middleware decorator.

It accepts two arguments:
- limit: `Number`, milliseconds, the times this controller can be visited in `duration`, by default it is `10000`.
- duration: `Number`, milliseconds, by default it is `60000`.
This method accepts one argument:
- fun: `Function`, the async function to be converted.

#### Example
```js
// apis/user.js
'use strict'
const {limiter} = require('koa-decorators')
const {convert} = require('koa-decorators')

module.exports = class UserController {
@router({method: 'GET', path: '/user'})
@limiter({limit: 3, duration: 60000})
async getUserById (ctx) {
ctx.body = 'user'
}
async function someFun (ctx, next) {
await next()
}
```

```sh
> curl localhost:3003/user
> user
> curl localhost:3003/user
> user
> curl localhost:3003/user
> user
> curl localhost:3003/user
> Too Many Requests
const converted = convert(someFun)

module.exports = class {
@router({method: 'GET', path: '/convert'})
@converted
async convert (ctx) {
ctx.body = 'converted'
}
}
```
8 changes: 6 additions & 2 deletions package.json
@@ -1,13 +1,17 @@
{
"name": "koa-decorators",
"version": "1.3.0",
"version": "1.4.0",
"description": "Decorate koa",
"main": "./lib/index.js",
"scripts": {
"test": "./node_modules/.bin/babel-node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha",
"test": "./node_modules/.bin/standard && ./node_modules/.bin/babel-node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha",
"compile": "rm -rf lib && ./node_modules/.bin/babel src --out-dir lib",
"pub": "npm run compile && npm publish"
},
"standard": {
"ignore": ["lib"],
"parser": "babel-eslint"
},
"repository": {
"type": "git",
"url": "https://github.com/DavidCai1993/koa-decorators.git"
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
@@ -1,11 +1,11 @@
'use strict'
const {router, route} = require('./router')
const required = require('./required')
const limiter = require('./limiter')
const convert = require('./public/convert')

module.exports = {
router,
route,
required,
limiter
convert
}
40 changes: 0 additions & 40 deletions src/limiter.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/public/convert.js
@@ -0,0 +1,17 @@
'use strict'
const assert = require('assert')
const {sureIsArray, decorate} = require('../private/utils')

function handleDescriptor (target, name, descriptor, middleware) {
target[name] = sureIsArray(target[name])
target[name].splice(target[name].length - 1, 0, middleware)

return descriptor
}

function convert (middleware) {
assert(typeof middleware === 'function', 'middleware should be a function')
return decorate(handleDescriptor, [middleware])
}

module.exports = convert
17 changes: 17 additions & 0 deletions test/convert.js
@@ -0,0 +1,17 @@
'use strict'
/* global describe, it */
require('should')
const app = require('./fixtures/app')
const obj = require('./fixtures/apis/test')
const request = require('supertest')(app.listen())

describe('public-convert', function () {
it('should convert async function to koa middleware', async function () {
let {text} = await request
.get('/convert')
.expect(200)

text.should.containEql('test')
obj.flag.should.containEql(true)
})
})
20 changes: 18 additions & 2 deletions test/fixtures/apis/test.js
@@ -1,7 +1,16 @@
'use strict'
const {router} = require('../../../src/router')
const required = require('../../../src/required')
const limiter = require('../../../src/limiter')
const convert = require('../../../src/public/convert')

let obj = {flag: false}

async function mid (ctx, next) {
obj.flag = true
await next()
}

let dec = convert(mid)

module.exports = class testController {
@router({method: 'GET', path: '/test'})
Expand All @@ -22,8 +31,15 @@ module.exports = class testController {
}

@router({method: 'GET', path: '/rate-limit'})
@limiter({limit: 3, duration: 6000 * 10})
async testRateLimit (ctx) {
ctx.body = 'test'
}

@router({method: 'GET', path: '/convert'})
@dec
async testConvert (ctx) {
ctx.body = 'test'
}
}

module.exports = obj
25 changes: 0 additions & 25 deletions test/limter.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/required.js
@@ -1,5 +1,5 @@
'use strict'
/* global describe, before, after */
/* global describe, it */
require('should')
const app = require('./fixtures/app')
const request = require('supertest')(app.listen())
Expand Down
2 changes: 1 addition & 1 deletion test/router.js
@@ -1,5 +1,5 @@
'use strict'
/* global describe, before, after */
/* global describe, it */
require('should')
const app = require('./fixtures/app')
const request = require('supertest')(app.listen())
Expand Down

0 comments on commit cd5340d

Please sign in to comment.