Skip to content

Commit

Permalink
feat(getRoutes): add .getRoutes method, part of #5
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Oct 16, 2016
1 parent f3a8757 commit a171a43
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ powerful, flexible and RESTful APIs for enterprise easily!
* [.addRoute](#addroute)
* [.getRoute](#getroute)
* [.addRoutes](#addroutes)
* [.getRoutes](#getroutes)
* [.groupRoutes](#grouproutes)
* [.middleware](#middleware)
* [.legacyMiddleware](#legacymiddleware)
Expand Down Expand Up @@ -256,7 +257,29 @@ router.addRoutes(foo, baz)
console.log(router.routes.length) // 2
```

### [.groupRoutes](index.js#L382)
### [.getRoutes](index.js#L356)
> Simple method that just returns `this.routes`, which is array of route objects.
* `returns` **{Array}**: array of route objects

**Example**

```js
let router = require('koa-better-router')()

router.loadMethods()

console.log(router.routes.length) // 0
console.log(router.getRoutes().length) // 0

router.get('/foo', (ctx, next) => {})
router.get('/bar', (ctx, next) => {})

console.log(router.routes.length) // 2
console.log(router.getRoutes().length) // 2
```

### [.groupRoutes](index.js#L411)
> Groups multiple _"Route Objects"_ into one which middlewares will be these middlewares from the last "source". So let say you have `dest` route with 2 middlewares appended to it and the `src1` route has 3 middlewares, the final (returned) route object will have these 3 middlewares from `src1` not the middlewares from `dest`. Make sense? If not this not make sense for you, please open an issue here, so we can discuss and change it (then will change it in the [koa-rest-router][] too, because there the things with method `.groupResource` are the same).
**Params**
Expand Down Expand Up @@ -298,7 +321,7 @@ app.listen(2222, () => {
})
```

### [.middleware](index.js#L450)
### [.middleware](index.js#L479)
> Active all routes that are defined. You can pass `opts` to pass different `prefix` for your routes. So you can have multiple prefixes with multiple routes using just one single router. You can also use multiple router instances. Pass `legacy: true` to `opts` and you will get generator function that can be used in Koa v1.
**Params**
Expand Down Expand Up @@ -344,7 +367,7 @@ app.listen(4321, () => {
})
```

### [.legacyMiddleware](index.js#L530)
### [.legacyMiddleware](index.js#L559)
> Converts the modern middleware routes to generator functions using [koa-convert][].back under the hood. It is sugar for the `.middleware(true)` or `.middleware({ legacy: true })`
* `returns` **{Function|GeneratorFunction}**
Expand Down
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,35 @@ KoaBetterRouter.prototype.addRoutes = function addRoutes () {
return this
}

/**
* > Simple method that just returns `this.routes`, which
* is array of route objects.
*
* **Example**
*
* ```js
* let router = require('koa-better-router')()
*
* router.loadMethods()
*
* console.log(router.routes.length) // 0
* console.log(router.getRoutes().length) // 0
*
* router.get('/foo', (ctx, next) => {})
* router.get('/bar', (ctx, next) => {})
*
* console.log(router.routes.length) // 2
* console.log(router.getRoutes().length) // 2
* ```
*
* @return {Array} array of route objects
* @api public
*/

KoaBetterRouter.prototype.getRoutes = function getRoutes () {
return this.routes
}

/**
* > Groups multiple _"Route Objects"_ into one which middlewares
* will be these middlewares from the last "source". So let say
Expand Down
8 changes: 7 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test('should have `.addRoute`, `.middleware` and `legacyMiddleware` methods', fu
test.strictEqual(typeof router.addRoute, 'function')
test.strictEqual(typeof router.addRoutes, 'function')
test.strictEqual(typeof router.getRoute, 'function')
test.strictEqual(typeof router.getRoutes, 'function')
test.strictEqual(typeof router.createRoute, 'function')
test.strictEqual(typeof router.groupRoutes, 'function')
test.strictEqual(typeof router.middleware, 'function')
Expand Down Expand Up @@ -120,7 +121,7 @@ test('should `.createRoute` just return route object', function (done) {
function baz (ctx, next) {}
])

test.strictEqual(router.routes.length, 0)
test.strictEqual(router.getRoutes().length, 0)

// route object
test.strictEqual(route.prefix, '/api')
Expand Down Expand Up @@ -288,3 +289,8 @@ test('should add multiple routes into `this.routes`, using `.addRoutes` method',
test.strictEqual(roo.routes.length, 2)
done()
})

test('should `.getRoutes` return `this.routes` array', function (done) {
test.strictEqual(router.getRoutes().length, router.routes.length)
done()
})

0 comments on commit a171a43

Please sign in to comment.