Merge branch 'route-wildcards'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents eee6926, 18faa91 · Browse files · 4 files·+88 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+88 added−2 removed
- tests:+65 added−0 removed
- other:+23 added−2 removed
Provenance
- Commit
fefa06b- Tree
ea38a92- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
eee6926- Parent 2
18faa91
4 files changed+88 added−2 removed
modified:lib/express/spec/mocks.js+1 added
142 unmodified linesExpand all14314414514614714814915015124 unmodified lines142 unmodified linesExpand allreset = function() { Express.routes = [] Express.plugins = [] Express.settings = {} Express.params = {} configure('test')}
/**24 unmodified linesmodified:lib/express/core.js+6 added-1 removed
164 unmodified lines165 */166 167 mapParams: function(route) {168 route.keys.each(function(key, i){169- this.request.params.path[key] = this.request.captures[++i]169+ var val = this.request.captures[++i]170+ if (key in Express.params)171+ if ((val = Express.params[key].call(this.request, val)) === false)172+ this.request.passed = true173+ this.request.params.path[key] = this.request.captures[i] = val174 }, this)175 }176 })177 145 unmodified lines323 config: [],324 routes: [],325 plugins: [],326 settings: {},327+ params: {},328 server: new Server329 }330 331 // --- Defaults22 unmodified linesmodified:lib/express/dsl.js+17 added-1 removed
105 unmodified lines106 fn = env, env = 'all'107 if (fn instanceof Function)108 return Express.config.push([env, fn])109 if (typeof env !== 'string')110- throw new Error('environment required')110+ throw new TypeError('environment required')111 Express.config.each(function(conf){112 if (conf[0] === env ||113 conf[0] === 'all')114 conf[1].call(Express)115 })116 }117 118+/**119+ * Pre-process param _key_ with _fn_.120+ *121+ * @param {string} key122+ * @param {function} fn123+ * @api public124+ */125+126+exports.param = function(key, fn) {127+ if (typeof key !== 'string')128+ throw new TypeError('param key must be a string')129+ if (typeof fn !== 'function')130+ throw new TypeError('param must pass a function to process "' + key + '"')131+ Express.params[key] = fn132+}133+134 // --- Routing API135 136 exports.get = exports.view = route('get')137 exports.post = exports.create = route('post')2 unmodified linesmodified:spec/spec.routing.js+64 added
2 unmodified lines3 before_each4 reset()5 end6 7+ describe 'param()'8+ describe 'given no args'9+ it 'should throw an error'10+ -{ param() }.should.throw_error TypeError11+ end12+ end13+ 14+ describe 'given a non-string as the first argument'15+ it 'should throw an error'16+ -{ param(12) }.should.throw_error TypeError17+ end18+ end19+ 20+ describe 'given no callback function'21+ it 'should throw an error'22+ -{ param('foo') }.should.throw_error TypeError23+ end24+ end25+ 26+ describe 'given a non-function as the second argument'27+ it 'should throw an error'28+ -{ param('foo', 2) }.should.throw_error TypeError29+ end30+ end31+ 32+ describe 'with key / callback function'33+ it 'should pre-process a value before it is passed to a route'34+ param('user_id', function(val){35+ val = parseInt(val)36+ return isNaN(val) ? false : val37+ })38+ get('/user/:user_id', function(id){39+ return String(typeof id === 'number')40+ })41+ get('/user/99').body.should.eql 'true'42+ end43+ 44+ it 'should pass when false is explictly returned'45+ param('user_id', function(val){46+ val = parseInt(val)47+ return isNaN(val) ? false : val48+ })49+ get('/user/:user_id', function(id){50+ return String(typeof id === 'number')51+ })52+ get('/user/:name', function(name){53+ return name54+ })55+ get('/user/99').body.should.eql 'true'56+ get('/user/tj').body.should.eql 'tj'57+ end58+ 59+ it 'should evaluate in context to the request'60+ param('user_id', function(val){61+ this.pass()62+ })63+ get('/user/:user_id', function(id){64+ return String(typeof id === 'number')65+ })66+ get('/user/99').status.should.eql 40467+ end68+ end69+ end70+ 71 describe 'route'72 describe 'with callback function'73 it 'should respond with a body string'74 get('/user', function(){307 unmodified lines