Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
update dependencies, improve code.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Oct 2, 2016
1 parent 82f4b83 commit 0217d68
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = Router
function RouterState (root) {
this.root = typeof root === 'string' ? root.replace(/(\/)+$/, '') : ''
this.trie = new Trie()
this.preHooks = []
this.beforeHooks = []
this.otherwise = null
}

Expand All @@ -39,7 +39,7 @@ Router.prototype.otherwise = function (handler) {
}

Router.prototype.use = function (fn) {
this._routerState.preHooks.push(toThunkableFn(fn))
this._routerState.beforeHooks.push(toThunkableFn(fn))
return this
}

Expand All @@ -53,10 +53,10 @@ Router.prototype.toThunk = function () {
Router.prototype.route = function (context) {
var state = this._routerState
var otherwise = state.otherwise
var preHooks = this._routerState.preHooks.slice()
var beforeHooks = this._routerState.beforeHooks.slice()

function worker (ctx, handler) {
return thunk.seq.call(ctx, preHooks)(function (err) {
return thunk.seq.call(ctx, beforeHooks)(function (err) {
if (err != null) throw err
return handler
})
Expand Down Expand Up @@ -84,7 +84,7 @@ Router.prototype.route = function (context) {
// OPTIONS support
if (method === 'OPTIONS') {
this.status = 204
this.set('Allow', matched.node.allowMethods)
this.set('allow', matched.node.allowMethods)
return done()
}

Expand All @@ -94,7 +94,7 @@ Router.prototype.route = function (context) {
// it's a 405 error
if (!handler) {
if (otherwise) return worker(this, otherwise)(done)
this.set('Allow', matched.node.allowMethods)
this.set('allow', matched.node.allowMethods)
this.throw(405, this.method + ' is not allowed in "' + this.path + '".')
}

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"authors": [
"Yan Qing <[email protected]>"
],
"version": "1.5.1",
"version": "1.5.2",
"main": "index.js",
"repository": {
"type": "git",
Expand All @@ -24,14 +24,14 @@
],
"dependencies": {
"methods": "^1.1.2",
"route-trie": "^1.2.6",
"thunks": "^4.5.0"
"route-trie": "^1.2.7",
"thunks": "^4.7.5"
},
"devDependencies": {
"standard": "^8.0.0",
"standard": "^8.3.0",
"supertest": "^2.0.0",
"tman": "^1.1.0",
"toa": "^1.8.7"
"tman": "^1.6.0",
"toa": "^1.8.13"
},
"scripts": {
"test": "standard && tman"
Expand Down
50 changes: 32 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var assert = require('assert')
var request = require('supertest')
var thunk = require('thunks')()
var toa = require('toa')
var Toa = require('toa')
var tman = require('tman')
var Router = require('..')

Expand All @@ -15,7 +15,8 @@ tman.suite('toa-router', function () {
this.body = 'OK'
})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -31,7 +32,8 @@ tman.suite('toa-router', function () {
this.body = '/' + this.params.type + '/' + this.params.id
})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router
})

Expand All @@ -51,7 +53,7 @@ tman.suite('toa-router', function () {
this.body = 'POST /' + this.params.type + '/' + this.params.id
})

var app = toa()
var app = new Toa()
app.use(router.toThunk())

return request(app.listen())
Expand All @@ -66,7 +68,8 @@ tman.suite('toa-router', function () {
this.status = 200
})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -81,7 +84,8 @@ tman.suite('toa-router', function () {
this.status = 200
})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -98,7 +102,8 @@ tman.suite('toa-router', function () {
.put('/:type/:id', function () {})
.del('/:type/:id', function () {})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -114,7 +119,9 @@ tman.suite('toa-router', function () {
var router = new Router()
router.get('/', function () {})

var app = toa(function () {
var app = new Toa()
app.onerror = function () {}
app.use(function () {
return router.route(this)
})

Expand All @@ -129,7 +136,8 @@ tman.suite('toa-router', function () {
.get('/:type/:id', function () {})
.post('/:type/:id', function () {})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -146,7 +154,8 @@ tman.suite('toa-router', function () {
this.body = 'otherwise'
})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -164,7 +173,8 @@ tman.suite('toa-router', function () {
this.body = 'otherwise'
})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -182,7 +192,8 @@ tman.suite('toa-router', function () {
.put(function () {})
.del(function () {})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -202,7 +213,8 @@ tman.suite('toa-router', function () {
.put(function () {})
.del(function () {})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return router.route(this)
})

Expand All @@ -226,7 +238,8 @@ tman.suite('toa-router', function () {
this.body = 'api'
})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return this.thunk.all(router2.route(this), router1.route(this))
})

Expand All @@ -251,7 +264,8 @@ tman.suite('toa-router', function () {
}
})

var app = toa(function () {
var app = new Toa()
app.use(function () {
return this.thunk.all(router2.route(this), router1.route(this))
})

Expand Down Expand Up @@ -281,7 +295,7 @@ tman.suite('toa-router', function () {
this.body = String(count)
})

var app = toa()
var app = new Toa()
app.use(router.toThunk())

return request(app.listen())
Expand Down Expand Up @@ -310,7 +324,7 @@ tman.suite('toa-router', function () {
this.body = String(count)
})

var app = toa()
var app = new Toa()
app.use(router.toThunk())

return request(app.listen())
Expand Down Expand Up @@ -343,7 +357,7 @@ tman.suite('toa-router', function () {
this.body = 'Hello ' + this.state.name
})

var app = toa()
var app = new Toa()
app.use(router1.toThunk())
app.use(router2.toThunk())

Expand Down

0 comments on commit 0217d68

Please sign in to comment.