connect-traversal
Advanced tools
Comparing version 0.2.1 to 0.3.0
'use strict'; | ||
var util = require('util'); | ||
var util = require('util'), | ||
Chain = require('./chain'); | ||
@@ -69,3 +70,3 @@ | ||
this.resources = {}; | ||
this.paths = {}; | ||
this.chains = {}; | ||
} | ||
@@ -132,3 +133,3 @@ | ||
*/ | ||
Traversal.prototype.registerResourcePath = function(resourceId, ops) { | ||
Traversal.prototype.getResourceChain = function(resourceId) { | ||
if(!this.root) { | ||
@@ -138,26 +139,4 @@ throw Error("root resource hasn't been set yet."); | ||
this.checkResource(resourceId); | ||
if(ops.parent) { | ||
this.checkResource(ops.parent); | ||
} | ||
var callbacks = Array.prototype.slice.call(arguments, 2); | ||
if (!callbacks.length) { | ||
throw Error('registerPath() requires callback functions'); | ||
} | ||
callbacks.forEach(function(fn) { | ||
if ('function' == typeof fn) return; | ||
throw new Error("registerPath() requires callback functions of 'function' type"); | ||
}); | ||
if (!this.paths[resourceId]) { | ||
this.paths[resourceId] = {}; | ||
} | ||
var name = ops.name || 'index'; | ||
if (!this.paths[resourceId][name]) { | ||
this.paths[resourceId][name] = {}; | ||
} | ||
var parent = ops.parent || 'all'; | ||
if (!this.paths[resourceId][name][parent]) { | ||
this.paths[resourceId][name][parent] = {}; | ||
} | ||
var method = ops.method ? ops.method.toLowerCase() : 'all'; | ||
this.paths[resourceId][name][parent][method] = callbacks; | ||
if (!this.chains[resourceId]) this.chains[resourceId] = new Chain(); | ||
return this.chains[resourceId]; | ||
}; | ||
@@ -171,10 +150,4 @@ | ||
Traversal.prototype.getResourcePath = function(resourceId, ops) { | ||
if (this.paths[resourceId]) { | ||
var name; | ||
if (name = this.paths[resourceId][ops.name || 'index']) { | ||
var parent = name[ops.parent] ? name[ops.parent] : name['all']; | ||
if (parent) { | ||
return parent[ops.method.toLowerCase()] || parent['all']; | ||
} | ||
} | ||
if (this.chains[resourceId]) { | ||
return this.chains[resourceId].getCallbacks(ops.name, ops.parent, ops.method); | ||
} | ||
@@ -213,3 +186,3 @@ return null; | ||
this.resources = {}; | ||
this.paths = {}; | ||
this.chains = {}; | ||
this.root = null; | ||
@@ -216,0 +189,0 @@ }; |
{ | ||
"name": "connect-traversal", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Middleware for Connect and Express application which allows to use URL traversal instead of URL dispatching.", | ||
@@ -40,4 +40,4 @@ "keywords": [ | ||
"readmeFilename": "README.md", | ||
"_id": "connect-traversal@0.2.1", | ||
"_id": "connect-traversal@0.3.0", | ||
"_from": "connect-traversal@latest" | ||
} |
@@ -1,2 +0,2 @@ | ||
Traversal for Connect application. v0.2 [![Build Status](https://travis-ci.org/dmnorc/connect-traversal.png)](https://travis-ci.org/dmnorc/connect-traversal) | ||
Traversal for Connect application. v0.3 [![Build Status](https://travis-ci.org/dmnorc/connect-traversal.png)](https://travis-ci.org/dmnorc/connect-traversal) | ||
================= | ||
@@ -91,5 +91,14 @@ [![NPM](https://nodei.co/npm/connect-traversal.png?stars&downloads)](https://nodei.co/npm/connect-traversal/) | ||
``` | ||
Also paths with callbacks should be registered: | ||
Also handlers should be registered: | ||
.method(method) - specify HTTP method for handlers | ||
.parent(resourceName) - specify resource parent for handler. | ||
.name(resourceName) - specify additional name for handler. | ||
.only() - triggers only the most appropriate handler in chain. | ||
.all() - this is additional parent subscribers for all child handlers. | ||
``` | ||
traverse.registerPath('resourceName', options, callback1, callback2, ...) | ||
traversal.getResourceChain('resourceName') | ||
.all(callback1, callback2, ...) // triggers for all child only() with any name, method, parent | ||
.method('get').all(callback1, callback2, ...) // triggers for all child only() with 'get' method | ||
.name('some-name').only(callback1, callback2, ...) // triggers for name 'some-name' and 'get' method. | ||
.parent('SomeResource').only(callback1, callback2, ...) // to additional for above only. Only these handlers trigger for for name 'some-name', 'get' method and parent 'SomeResource' | ||
``` | ||
@@ -100,3 +109,7 @@ options object where can be specified HTTP method, parent resource and name appendix for filtering and special behavior. | ||
// this callbacks will trigger on GET /users/user/123, but for POST 404 will be thrown. | ||
traverse.registerPath('userResource', {method: 'get'}, function(req, res) { | ||
traversal.getResourceChain('userResource').all(function(req, res, next) { | ||
Subscriber for any userResource path. | ||
req.user = req.resource.getUser(); | ||
next(); | ||
}).method('get').only(function(req, res) { | ||
var userResource = req.resource //userResource for user 123 | ||
@@ -113,9 +126,9 @@ | ||
// will trigger for POST /users/create with special appendix path name only | ||
traverse.registerPath('usersResource', {method: 'post', name: 'create'}, function(req, res) { | ||
// req.pathname = 'create' | ||
var resource = req.resource //usersResource | ||
traversal.getResourceChain('usersResource').method('post').name('create').only(function(req, res) { | ||
// req.pathname = 'create' | ||
var resource = req.resource //usersResource | ||
}); | ||
// will trigger for GET /users/random/222/333 with special appendix path name only | ||
traverse.registerPath('usersResource', {method: 'post', name: 'random'}, function(req, res) { | ||
traversal.getResourceChain('usersResource').method('get').name('random').only(function(req, res) { | ||
// req.pathname == 'random' | ||
@@ -131,3 +144,3 @@ // req.resource //UsersResource | ||
// but not for /news because of specified parent resource. | ||
traverse.registerPath('newsResource', {parent: 'userResource'}, function(req, res) { | ||
traversal.getResourceChain('newsResource').method('*').parent('userResource').only(function(req, res) { | ||
var resource = req.resource // newsResource | ||
@@ -134,0 +147,0 @@ resource.parent // userResource |
Sorry, the diff of this file is not supported yet
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
62386
17
356
150