This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
167 lines (154 loc) · 3.59 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
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
162
163
164
165
166
167
var xhr = require('xhr');
var xhr2 = require('xhr2');
var querystring = require('querystring');
var Client = module.exports = function Client (opts) {
if (!(this instanceof Client)) {
return new Client(opts);
}
opts = opts || {};
this.baseUrl = opts.baseUrl || '';
this.headers = {
'Content-Type': 'application/json'
};
if (opts.accessToken) {
this.headers['x-access-token'] = opts.accessToken;
}
this.Promise = opts.Promise;
if (!this.Promise && typeof Promise !== 'undefined') {
this.Promise = Promise;
}
};
if (typeof window !== 'undefined') {
window.WineClient = Client;
}
////// utility methods //////
Client.prototype.setAccessToken = function (token) {
this.headers['x-access-token'] = token;
if (!token) {
delete this.headers['x-access-token']
}
};
Client.prototype.send = function (opts, cb) {
var self = this;
var headers = {};
Object.keys(self.headers).forEach(function (key) {
return headers[key] = self.headers[key];
});
if (opts.accessToken) {
headers['x-access-token'] = opts.accessToken;
}
var qs = '';
if (opts.query) {
qs = '?' + querystring.stringify(opts.query);
}
var httpRequest = xhr({
method: opts.method.toUpperCase(),
uri: self.baseUrl + opts.uri + qs,
headers: headers,
json: opts.body,
xhr: new xhr2()
}, function (err, resp, body) {
if (err) {
return cb(err);
}
self.statusCode = resp.statusCode;
var ok = [200, 201, 202, 203, 204, 205, 206];
if (ok.indexOf(resp.statusCode) === -1) {
return cb((body && body.error) || body || resp.statusCode);
}
cb(null, body);
});
if(opts.aborter) {
opts.aborter.then(httpRequest.abort);
}
};
Client.prototype.resolver = function () {
var Promise = this.Promise;
if (!Promise) {
throw new Error('No callback provided and no Promise library specified.');
}
var resolver;
var promise = new Promise(function (resolve, reject) {
resolver = function (err, data) {
if (err) {
return reject(err);
}
return resolve(data);
}
});
resolver.promise = promise;
return resolver;
};
Client.prototype.request = function (opts, cb) {
cb = cb || this.resolver();
this.send({
method: opts.method,
uri: opts.uri,
body: opts.body,
query: opts.query,
aborter: opts.aborter
}, function (err, body) {
if (err) return cb(err);
var result = opts.result ? body[opts.result] : body;
cb(null, result);
});
return cb.promise ? cb.promise : this;
};
Client.prototype.delete = function (uri, query, cb) {
if (typeof query === 'function') {
cb = query;
query = {};
}
return this.request({
method: 'delete',
uri: uri,
query: query
}, cb);
};
Client.prototype.get = function (uri, query, cb) {
if (typeof query === 'function') {
cb = query;
query = {};
}
return this.request({
method: 'get',
uri: uri,
query: query
}, cb);
};
Client.prototype.patch = function (uri, body, query, cb) {
if (typeof query === 'function') {
cb = query;
query = {};
}
return this.request({
method: 'patch',
uri: uri,
body: body,
query: query
}, cb);
};
Client.prototype.post = function (uri, body, query, cb) {
if (typeof query === 'function') {
cb = query;
query = {};
}
return this.request({
method: 'post',
uri: uri,
body: body,
query: query
}, cb);
};
Client.prototype.put = function (uri, body, query, cb) {
if (typeof query === 'function') {
cb = query;
query = {};
}
return this.request({
method: 'put',
uri: uri,
body: body,
query: query
}, cb);
};