ee-soa-transport-rewrite
Advanced tools
Comparing version 0.1.7 to 0.2.0
@@ -5,6 +5,6 @@ "use strict"; | ||
var CachedLoader = { | ||
var CachedLoader = module.exports = new Class({ | ||
_cache: null | ||
, _loader: null | ||
_cache : null | ||
, _loader : null | ||
@@ -32,6 +32,3 @@ , cache: { | ||
if(this.cache.has(key)){ | ||
callback(null, this.cache.get(key)); | ||
return; | ||
} | ||
if(this.cache.has(key)) return callback(null, this.cache.get(key)); | ||
@@ -46,4 +43,2 @@ this.loader.load(key, function(err, rules){ | ||
} | ||
}; | ||
module.exports = new Class(CachedLoader); | ||
}); |
@@ -8,8 +8,8 @@ "use strict"; | ||
var FilterLoader = { | ||
var FilterLoader = module.exports = new Class({ | ||
_loader: null | ||
, _comparator: null | ||
, _loaderKeyFun: null | ||
, _filterKeyFun: null | ||
_loader : null | ||
, _comparator : null | ||
, _loaderKeyFun : null | ||
, _filterKeyFun : null | ||
@@ -55,4 +55,2 @@ , init: function initialize(comparator, loader, options) { | ||
} | ||
}; | ||
module.exports = new Class(FilterLoader); | ||
}); |
@@ -8,7 +8,7 @@ "use strict"; | ||
var RequestRewriteLoader = { | ||
var RequestRewriteLoader = module.exports = new Class({ | ||
inherits: FilterLoader, | ||
inherits : FilterLoader | ||
init: function initialize(loader, options) { | ||
, init: function initialize(loader, options) { | ||
var comp = function(pathname, rule){ | ||
@@ -60,4 +60,2 @@ if(!('path' in rule)){ | ||
} | ||
}; | ||
module.exports = new Class(RequestRewriteLoader); | ||
}); |
@@ -5,6 +5,6 @@ "use strict"; | ||
var TransformingLoader = { | ||
var TransformingLoader = module.exports = new Class({ | ||
_transformer: null | ||
, _loader: null | ||
_transformer : null | ||
, _loader : null | ||
@@ -30,5 +30,3 @@ , transformer: { | ||
this._load(key, function(err, result){ | ||
if(err){ | ||
return callback(err, null); | ||
} | ||
if(err) return callback(err, null); | ||
this._transform(key, result, callback); | ||
@@ -45,4 +43,2 @@ }.bind(this)); | ||
} | ||
}; | ||
module.exports = new Class(TransformingLoader); | ||
}); |
@@ -48,2 +48,3 @@ "use strict"; | ||
, init: function(rule, loader){ | ||
rule = rule || {}; | ||
@@ -50,0 +51,0 @@ |
@@ -7,10 +7,15 @@ "use strict"; | ||
var Template = { | ||
var Template = module.exports = new Class({ | ||
inherits: Rewrite | ||
, _name: 'template' | ||
inherits : Rewrite | ||
, _name : 'template' | ||
, _default : '_default' | ||
, _execute: function(request, callback){ | ||
try { | ||
request.template = this.value; | ||
var templatePath = this.value; | ||
if(!request.template){ | ||
request.template = this.createTemplateContainer(); | ||
} | ||
request.template.set(this.field, templatePath); | ||
} catch(err) { | ||
@@ -21,4 +26,23 @@ return callback(err); | ||
} | ||
}; | ||
module.exports = new Class(Template); | ||
, createTemplateContainer: function(){ | ||
var defaultField = this._default; | ||
return { | ||
_paths : {} | ||
, set : function(status, templatePath){ | ||
if(!status) status = defaultField; | ||
this._paths[status] = templatePath; | ||
} | ||
, get : function(status){ | ||
if(status && Types.string(this._paths[status])) return this._paths[status]; | ||
return this._paths[defaultField]; | ||
} | ||
, resolve : function(status){ | ||
return this.get(status); | ||
} | ||
}; | ||
} | ||
}); |
{ | ||
"name" : "ee-soa-transport-rewrite" | ||
, "description" : "Rewriting middleware for the ee-soa-transports" | ||
, "version" : "0.1.7" | ||
, "version" : "0.2.0" | ||
, "homepage" : "https://github.com/eventEmitter/ee-soa-transport-rewrite" | ||
@@ -6,0 +6,0 @@ , "author" : { |
@@ -18,3 +18,3 @@ #ee-soa-transport-rewrite | ||
- **path** modifies the requested pathname `path` to `value` (use to map to api endpoints). | ||
- **template** sets a template object on the request and binds the template to a key, namely the `field` value (if no `field` is set, it is bound to 'default'). This is useful to map templates to response codes. | ||
- **template** sets a template object on the request and binds the template to a key representing the status code of the response (saved in the `field` property. If no status is set, the template it is bound to a default key). | ||
- **parameter** allows setting arbitrary values to an parameters hashtable called `rewriteParameters` ( `request.rewriteParameters[field] = value`) | ||
@@ -124,16 +124,16 @@ | ||
Different caches are always injected into the loaders which makes them inherently testable. | ||
#Changelog | ||
##v0.1.6 | ||
- added the possibility to bind the templates to a status code | ||
##v0.2.0 | ||
- added the possibility to bind the templates to a status code using the field value (if none set it is bound to a default parameter). The template itself now is an object with a resolve method which takes the status code as a parameter). | ||
##v0.1.5 | ||
- added a method rewrite to switch http methods | ||
##v0.1.2 | ||
- added Option rewrite rule | ||
- values which are of type function are evaluated now | ||
Different caches are always injected into the loaders which makes them inherently testable. | ||
- Todo: Add prepend rules | ||
- Todo: Add templating rules... probably an extends rule... | ||
- Todo: The rule chaining happens "in place" which means the initial rewrite is modified when chained. This should be changed. | ||
- Todo: Add caching | ||
- values which are of type function are evaluated now |
@@ -41,4 +41,3 @@ var assert = require('assert'); | ||
it('should set the template', function(){ | ||
//assert.equal(req.template['default'], 'index.html'); | ||
assert.equal(req.template, 'index.html'); | ||
assert.equal(req.template.resolve(), 'index.html'); | ||
}); | ||
@@ -45,0 +44,0 @@ |
@@ -124,6 +124,5 @@ var assert = require('assert') | ||
it('with the template bound to the "default" key if no field is set', function(){ | ||
/*assert('default' in MockRequest.template); | ||
assert.equal('index.nunjucks.html', MockRequest.template['default']);*/ | ||
assert.equal('index.nunjucks.html', MockRequest.template); | ||
it('with the template bound to a "default" key if no field is set', function(){ | ||
assert.equal('index.nunjucks.html', MockRequest.template.resolve()); | ||
assert.equal('index.nunjucks.html', MockRequest.template.resolve(1000)); | ||
}); | ||
@@ -133,16 +132,11 @@ | ||
templateWithStatusCode.execute(MockRequest, function(err){ | ||
/*assert('404' in MockRequest.template); | ||
assert.equal('error/404.html', MockRequest.template['404']);*/ | ||
assert.equal('error/404.html', MockRequest.template); | ||
assert.equal('error/404.html', MockRequest.template.resolve(404)); | ||
done(err); | ||
}); | ||
}); | ||
// this one will work completely different with the template assignment | ||
it.skip('without modifying the existing templates', function(){ | ||
assert('default' in MockRequest.template); | ||
assert.equal('index.nunjucks.html', MockRequest.template['default']); | ||
it('without modifying the existing templates', function(){ | ||
assert.equal('index.nunjucks.html', MockRequest.template.resolve()); | ||
}); | ||
}); | ||
}); | ||
@@ -149,0 +143,0 @@ |
@@ -29,4 +29,3 @@ var assert = require('assert'); | ||
extender.execute(req, function(err){ | ||
//assert.equal(req.template['default'], 'index.html'); | ||
assert.equal(req.template, 'index.html'); | ||
assert.equal(req.template.resolve(), 'index.html'); | ||
assert.equal(req.pathname, '/somewhere-else/10'); | ||
@@ -33,0 +32,0 @@ // the following test would fail, because the extended rules would be evaluated before it |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
109986
48
1160