-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (81 loc) · 2.11 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*!
* Restify - Version
* Copyright(c) 2012 Mal Graty <[email protected]>
* MIT Licensed
*
* Based upon:
* Express - Contrib - namespace
* Copyright(c) 2010 TJ Holowaychuk <[email protected]>
* MIT Licensed
* Repo: https://github.com/visionmedia/express-namespace
*/
/**
* Module dependencies.
*/
var restify = require('restify')
, join = require('path').join
, Server = restify.createServer().constructor;
/**
* Coerce `value` to an array, but only if not one already
*
* @param {Object} value
* @return {Array}
*/
function arrayify(value)
{
if ( Array.isArray(value) )
return value;
return [value];
}
/**
* Version using the given `version`, providing a callback `fn()`,
* which will be invoked immediately, resetting the version to the previous.
*
* @param {String} version
* @param {Function} fn
* @return {Server} for chaining
* @api public
*/
exports.ver = function(version, fn) {
var prev = this._ver;
this._ver = arrayify(version);
fn.call(this);
this._ver = prev;
return this;
};
/**
* Return the current version space.
*
* @return {String}
* @api public
*/
exports.__defineGetter__('currentVersion', function() {
return this._ver;
});
/**
* Proxy HTTP methods to provide namespacing support.
* Methods taken from:
* https://github.com/mcavage/node-restify/blob/a3d8dad77e18d342b9e629f221fc113683902170/lib/server.js#L314
*/
['del', 'get', 'head', 'post', 'put', 'patch'].forEach(function(method) {
var orig = Server.prototype[method];
exports[method] = function() {
var args = Array.prototype.slice.call(arguments)
, curr = this.currentVersion
, opts = args.shift();
if ( opts instanceof Object )
if ( opts.version )
opts.version = arrayify(opts.version).concat(curr);
else
opts.version = curr;
else
opts = { path: opts, version: curr };
return orig.call(this, opts, args);
};
return this;
});
// merge
for (var key in exports) {
var desc = Object.getOwnPropertyDescriptor(exports, key);
Object.defineProperty(Server.prototype, key, desc);
}