Comparing version 0.0.1 to 0.0.2
@@ -57,3 +57,3 @@ "use strict"; | ||
i++; | ||
return params[key]; | ||
return encodeURIComponent(params[key]); | ||
}); | ||
@@ -68,4 +68,4 @@ }; | ||
// build the verb matching pattern | ||
var m; | ||
for (var i in methods) { | ||
var m, i; | ||
for (i = 0; i < methods.length; i++) { | ||
m = methods[i].toUpperCase(); | ||
@@ -81,2 +81,6 @@ if (m === 'ALL' || m === '*') { | ||
if (!(pattern instanceof RegExp)) { | ||
// ignore trailing slash | ||
if (pattern[pattern.length - 1] === '/') | ||
pattern = pattern.substr(0, pattern.length - 1); | ||
pattern = regEscape(pattern).replace(_paramRegex, function (param) { | ||
@@ -86,7 +90,12 @@ _this.params.push(param.substr(1)); | ||
}); | ||
pattern = new RegExp('^' + pattern + '$', 'i'); | ||
pattern = new RegExp('^' + pattern + '\\/?$', 'i'); | ||
} | ||
this.pattern = pattern; | ||
// set route property on each handler | ||
for (i = 0; i < handlers.length; i++) { | ||
Object.defineProperty(handlers[i], 'trailRoute', { value: this, writable: true }); | ||
} | ||
} | ||
@@ -93,0 +102,0 @@ |
@@ -37,3 +37,4 @@ "use strict"; | ||
// | ||
// a method which will be called after all routes | ||
_this.finally = null; | ||
@@ -43,8 +44,5 @@ /* ------------------------------------------------------------------- | ||
* ---------------------------------------------------------------- */ | ||
_this.handler = function *(next) { | ||
// console.log(this.path); | ||
yield dispatch(0, this); | ||
yield next; | ||
yield dispatch(0, this, next); | ||
}; | ||
@@ -73,7 +71,16 @@ | ||
var r = _routes[name]; | ||
_routesOrder.splice(_routesOrder.indexOf(r), 1); | ||
_routesOrder[_routesOrder.indexOf(r)] = route; | ||
} | ||
else { | ||
_routes[name] = route; | ||
_routesOrder.push(route); | ||
} | ||
}; | ||
_this.url = function (name, params) { | ||
var r = _routes[name]; | ||
if (!r) | ||
throw new Error('Cannot generate path. No route named ' + name); | ||
_routes[name] = route; | ||
_routesOrder.push(route); | ||
return r.buildPath.apply(null, Array.prototype.slice.call(arguments, 1)); | ||
}; | ||
@@ -85,3 +92,3 @@ | ||
function *dispatch (startingIndex, context) { | ||
function *dispatch (startingIndex, context, next) { | ||
var params, r; | ||
@@ -105,3 +112,3 @@ for (var i = startingIndex; i < _routesOrder.length; i++) { | ||
else { | ||
yield dispatch(i + 1, context); | ||
yield dispatch(i + 1, context, next); | ||
} | ||
@@ -115,2 +122,10 @@ }; | ||
} | ||
var wasMatched = startingIndex !== 0; | ||
if (_this.finally) { | ||
yield _this.finally.call(context, next, wasMatched); | ||
} | ||
else if (!wasMatched) { | ||
yield next; | ||
} | ||
} | ||
@@ -137,3 +152,4 @@ | ||
app.register = _this.register; | ||
app.url = _this.url; | ||
} | ||
} |
{ | ||
"name": "koa-trail", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A koa router which supports chained route matching.", | ||
@@ -5,0 +5,0 @@ "author": "Bret Copeland <bret@atlantisflight.org>", |
124
README.md
@@ -5,19 +5,5 @@ # koa-trail | ||
For example, instead of: | ||
For example, assume we have an authenticate middleware function which should run on every `/api/*` route. Instead of: | ||
```javascript | ||
var koa = require('koa'); | ||
var trail = require('koa-trail'); | ||
var app = koa(); | ||
app.use(trail(app)); | ||
function authenticate *(next) { | ||
// ... authenticate the API token | ||
if (!user) | ||
this.throw(403, 'API Token Invalid'); | ||
else | ||
yield next | ||
} | ||
// chain the authenticate on every route | ||
@@ -28,4 +14,2 @@ app.get('/api/users', authenticate, apiController.getUsers); | ||
app.put('/api/:contentId/like', authenticate, apiController.likeContent); | ||
app.listen(3000); | ||
``` | ||
@@ -51,2 +35,106 @@ | ||
_More documentation and features will be coming soon... this is a work in progress._ | ||
--- | ||
* [Usage](#usage) | ||
* [Installation](#installation) | ||
* [Setup](#setup) | ||
* [Setup](#setup) | ||
* [Route Chaining](#route-chaining) | ||
* [Named Routes](#named-routes) | ||
* [Route Parameters](#route-parameters) | ||
* [Wildcard Routes](#wildcard-routes) | ||
* [Router Methods](#router-methods) | ||
* [register](#router-register) | ||
* [VERB](#router-verb) | ||
* [all](#router-all) | ||
* [url](#router-url) | ||
* [finally](#router-finally) | ||
* [License](#license) | ||
<a name="usage"></a> | ||
## Usage | ||
<a name="installation"></a> | ||
### Installation | ||
npm install koa-trail | ||
<a name="setup"></a> | ||
### Setup | ||
```javascript | ||
var koa = require('koa'); | ||
var trail = require('koa-trail'); | ||
var app = koa(); | ||
app.use(trail(app)); | ||
//now we can attach routes | ||
app.get('/users', function *(next) { | ||
// ... | ||
}); | ||
app.post('/users', function *(next) { | ||
// ... | ||
}); | ||
``` | ||
When `app` is passed as a parameter to trail, it creates a method on the `app` for [every http method](#router-verb) (get, post, put, delete, etc), plus [app.all](#router-all), [app.register](#router-register), and [app.url](#router-url). If you would prefer not to "pollute" the `app` object with these methods, you may initialize a router object without passing the app parameter. | ||
```javascript | ||
var koa = require('koa'); | ||
var trail = require('koa-trail'); | ||
var app = koa(); | ||
var router = new trail(); | ||
// pass the router.handler object, not the router object itself | ||
app.use(router.handler); | ||
// now attach routes to the router object instead of app | ||
router.get('/users', function *(next) { | ||
// ... | ||
}); | ||
router.post('/users', function *(next) { | ||
// ... | ||
}); | ||
``` | ||
<a name="route-chaining"></a> | ||
### Route Chaining | ||
<a name="named-routes"></a> | ||
### Named Routes | ||
Routes can be named which makes them | ||
<a name="route-parameters"></a> | ||
### Route Parameters | ||
<a name="wildcard-routes"></a> | ||
### Wildcard Routes | ||
<a name="router-methods"></a> | ||
### Router Methods | ||
<a name="router-register"></a> | ||
#### register | ||
<a name="router-verb"></a> | ||
#### VERB | ||
<a name="router-all"></a> | ||
#### all | ||
<a name="router-url"></a> | ||
#### url | ||
<a name="router-finally"></a> | ||
#### finally | ||
<a name="license"></a> | ||
## License | ||
[MIT](https://github.com/bretcope/koa-trail/raw/master/LICENSE.MIT) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15203
248
137