This repository was archived by the owner on Dec 26, 2019. It is now read-only.
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
161 lines (143 loc) · 4.66 KB
/
ApiBaseHTTP.js
File metadata and controls
161 lines (143 loc) · 4.66 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
157
158
159
160
161
(function() {
var ApiBase, debug, querystring, slumber,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
extend = 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; },
hasProp = {}.hasOwnProperty;
debug = require('debug')('gitlab:ApiBaseHTTP');
ApiBase = require('./ApiBase').ApiBase;
querystring = require('querystring');
slumber = require('slumber');
module.exports.ApiBaseHTTP = (function(superClass) {
extend(ApiBaseHTTP, superClass);
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 || this.options.oauth_token)) {
throw "`private_token` or `oauth_token` is mandatory";
}
if ((base1 = this.options).slumber == null) {
base1.slumber = {};
}
if ((base2 = this.options.slumber).append_slash == null) {
base2.append_slash = false;
}
this.options.url = this.options.url.replace(/\/api\/v3/, '');
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 = {};
}
if (this.options.token) {
opts.headers = {
'PRIVATE-TOKEN': this.options.token
};
} else {
opts.headers = {
'Authorization': 'Bearer ' + this.options.oauth_token
};
}
return opts;
};
ApiBaseHTTP.prototype.fn_wrapper = function(fn) {
return (function(_this) {
return function(err, response, ret) {
var arity;
arity = fn.length;
switch (arity) {
case 1:
return fn(ret);
case 2:
return fn(err, ret || JSON.parse(response.body).message);
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);