forked from moul/node-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiBaseHTTP.js
More file actions
156 lines (138 loc) · 4.48 KB
/
ApiBaseHTTP.js
File metadata and controls
156 lines (138 loc) · 4.48 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Generated by CoffeeScript 1.7.1
(function() {
var ApiBase, debug, querystring, slumber,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
debug = require('debug')('gitlab:ApiBaseHTTP');
ApiBase = require('./ApiBase').ApiBase;
querystring = require('querystring');
slumber = require('slumber');
module.exports.ApiBaseHTTP = (function(_super) {
__extends(ApiBaseHTTP, _super);
function ApiBaseHTTP() {
this.patch = __bind(this.patch, this);
this.put = __bind(this.put, this);
this.post = __bind(this.post, this);
this["delete"] = __bind(this["delete"], this);
this.get = __bind(this.get, this);
this.fn_wrapper = __bind(this.fn_wrapper, this);
this.prepare_opts = __bind(this.prepare_opts, this);
this.init = __bind(this.init, this);
this.handleOptions = __bind(this.handleOptions, this);
return ApiBaseHTTP.__super__.constructor.apply(this, arguments);
}
ApiBaseHTTP.prototype.handleOptions = function() {
var _base, _base1, _base2;
ApiBaseHTTP.__super__.handleOptions.apply(this, arguments);
if ((_base = this.options).base_url == null) {
_base.base_url = '';
}
if (!this.options.url) {
throw "`url` is mandatory";
}
if (!this.options.token) {
throw "`private_token` is mandatory";
}
if ((_base1 = this.options).slumber == null) {
_base1.slumber = {};
}
if ((_base2 = this.options.slumber).append_slash == null) {
_base2.append_slash = false;
}
if (this.options.auth != null) {
this.options.slumber.auth = this.options.auth;
}
return debug("handleOptions()");
};
ApiBaseHTTP.prototype.init = function() {
var api;
ApiBaseHTTP.__super__.init.apply(this, arguments);
api = slumber.API(this.options.url, this.options.slumber);
return this.slumber = api(this.options.base_url);
};
ApiBaseHTTP.prototype.prepare_opts = function(opts) {
if (opts.__query == null) {
opts.__query = {};
}
opts.__query.private_token = this.options.token;
return opts;
};
ApiBaseHTTP.prototype.fn_wrapper = function(fn) {
return (function(_this) {
return function(err, response, ret) {
var arity;
if (err) {
debug('an error has occured', err);
}
arity = fn.length;
switch (arity) {
case 1:
return fn(ret);
case 2:
return fn(err, ret);
case 3:
return fn(err, response, ret);
}
};
})(this);
};
ApiBaseHTTP.prototype.get = function(path, query, fn) {
var opts;
if (query == null) {
query = {};
}
if (fn == null) {
fn = null;
}
if ('function' === typeof query) {
fn = query;
query = {};
}
opts = this.prepare_opts(query);
return this.slumber(path).get(opts, this.fn_wrapper(fn));
};
ApiBaseHTTP.prototype["delete"] = function(path, fn) {
var opts;
if (fn == null) {
fn = null;
}
opts = this.prepare_opts({});
return this.slumber(path)["delete"](opts, this.fn_wrapper(fn));
};
ApiBaseHTTP.prototype.post = function(path, data, fn) {
var opts;
if (data == null) {
data = {};
}
if (fn == null) {
fn = null;
}
opts = this.prepare_opts(data);
return this.slumber(path).post(opts, this.fn_wrapper(fn));
};
ApiBaseHTTP.prototype.put = function(path, data, fn) {
var opts;
if (data == null) {
data = {};
}
if (fn == null) {
fn = null;
}
opts = this.prepare_opts(data);
return this.slumber(path).put(opts, this.fn_wrapper(fn));
};
ApiBaseHTTP.prototype.patch = function(path, data, fn) {
var opts;
if (data == null) {
data = {};
}
if (fn == null) {
fn = null;
}
opts = this.prepare_opts(data);
return this.slumber(path).patch(opts, this.fn_wrapper(fn));
};
return ApiBaseHTTP;
})(ApiBase);
}).call(this);