This repository has been archived by the owner on Mar 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqlient.js
91 lines (77 loc) · 2.47 KB
/
qlient.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
module.exports = Qlient;
var url = require('url'),
util = require('util'),
events = require('events'),
EventEmitter = events.EventEmitter;;
var Resource = require('./resource');
function isObject (obj) {
return (typeof obj === 'object' && obj !== null);
}
function toJSONRef (res) {
if (!isObject(res)) { return null; }
return JSON.stringify(Object.keys(res.value).map(function (key) {
var value = res.value[key];
if (value instanceof Resource) {
return { key: key, value: { $ref: res.abspath } };
} else {
return { key: key, value: value };
}
}).toObject());
}
function Qlient (baseurl, services) {
this.baseurl = baseurl;
this.services = services.map(function (service) {
return { key: service.name, value: service.new(this) };
}.bind(this)).toObject();
this.defaultHeaders = {};
this._connections = 0;
EventEmitter.call(this);
this.emit('stats', 0);
}
util.inherits(Qlient, EventEmitter);
Qlient.prototype.request = function (method, pathname, query, headers, objBody) {
headers = headers || {};
Object.keys(this.defaultHeaders).forEach(function (key) {
if (!headers.hasOwnProperty(key)) {
headers[key] = this.defaultHeaders[key];
}
}.bind(this));
var urlObj = url.parse(this.baseurl + pathname);
urlObj.query = query;
var endPoint = url.format(urlObj).slice(0, -1); // remove last slash
var body = toJSONRef(objBody);
return (new Qlient.Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
this._connections -= 1;
this.emit('stats', this._connections);
//console.log('response', xhr.response, xhr.status, endPoint);
if (xhr.status >= 400) {
reject(xhr);
} else {
resolve(JSON.parse(xhr.response));
}
}.bind(this);
xhr.onerror = function (err) {
this._connections -= 1;
this.emit('stats', this._connections);
reject(err);
}.bind(this);
this._connections += 1;
this.emit('stats', this._connections);
xhr.open(method, endPoint);
Object.keys(headers).forEach(function (key) {
xhr.setRequestHeader(key, headers[key]);
});
xhr.send(body);
}.bind(this))).catch(function (err) {
console.warn(err);
});
};
Qlient.prototype.setBasicAuth = function (user, password) {
if (user === null) {
delete this.defaultHeaders['Authorization'];
} else {
this.defaultHeaders['Authorization'] = 'Basic ' + global.btoa(user + ':' + password);
}
};