-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (34 loc) · 973 Bytes
/
index.js
File metadata and controls
43 lines (34 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var methods = require('methods');
var cache = [];
methods.push('del');
function API(app, base, version) {
this.app = app;
this.prefix = base + (version ? '/' + version : '');
}
methods.forEach(function(method){
API.prototype[method] = function(_route) {
var args = Array.prototype.slice.call(arguments);
args[0] = this.prefix + _route;
return this.app[method].apply(this.app, args);
};
});
function get(argsObject) {
var cached = cache.filter(function(args){
return args.a === argsObject.a && args.b === argsObject.b && args.v === argsObject.v;
})[0];
return cached ? cached.api : false;
}
function store(argsObject) {
cache.push(argsObject);
}
function version(app, base, version) {
var args = {a:app, b:base, v:version};
var api = get(args);
if (!api) {
api = new API(app, base, version);
args.api = api;
store(args);
}
return api;
}
exports.version = version;