@studyportals/sp-hs-misc
Version:
Miscellaneous code used in HouseStark's projects
109 lines (100 loc) • 5.19 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
//# sourceMappingURL=service-layer-requests-sender.class.js.map
var ServiceLayerRequestsSender = function () {
_createClass(ServiceLayerRequestsSender, [{
key: "ongoingRequestsByUrl",
get: function get() {
return this._ongoingRequestsByUrl;
}
}]);
function ServiceLayerRequestsSender() {
var requestSenderConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, ServiceLayerRequestsSender);
this._ongoingRequestsByUrl = new Map();
this.config = requestSenderConfig;
}
_createClass(ServiceLayerRequestsSender, [{
key: "send",
value: function send(request) {
if (this.isIdenticalRequestOngoing(request)) {
return this.getIdenticalOngoingRequestPromise(request);
}
if (this.config.userAgent) {
request.set("User-Agent", this.config.userAgent);
}
return this.sendRequest(request);
}
}, {
key: "isIdenticalRequestOngoing",
value: function isIdenticalRequestOngoing(request) {
return this.ongoingRequestsByUrl.has(request.url);
}
}, {
key: "getIdenticalOngoingRequestPromise",
value: function getIdenticalOngoingRequestPromise(request) {
var ongoingRequestPromise = this.ongoingRequestsByUrl.get(request.url);
if (ongoingRequestPromise) {
return ongoingRequestPromise;
}
throw new Error("Could not find an ongoing request for: " + request.url);
}
}, {
key: "sendRequest",
value: function sendRequest(request) {
var promiseToSendTheRequestAndNormalizeTheOutput = this.sendRequestAndNormalizeOutput(request);
this.registerOngoingRequest(request.url, promiseToSendTheRequestAndNormalizeTheOutput);
return promiseToSendTheRequestAndNormalizeTheOutput;
}
}, {
key: "sendRequestAndNormalizeOutput",
value: function sendRequestAndNormalizeOutput(request) {
var _this = this;
return new Promise(function (resolve, reject) {
request.end(function (err, res) {
_this.removeOngoingRequest(request.url);
if (err) {
reject(err);
} else {
//
// If the request is successfull, the Service Layer
// can return either an empty array (when there is no data),
// or an object that indexes the entities by their IDs.
//
// For consistancy's sake, return an empty array when there
// is no data, or an array of the object's value when entities
// are provided.
if (Array.isArray(res.body)) {
resolve(res.body);
} else {
if ("object" === _typeof(res.body)) {
var values = [];
Object.keys(res.body).forEach(function (key) {
return values.push(res.body[key]);
});
resolve(values);
} else {
reject(new Error("Unexpected body " + res.body));
}
}
}
});
});
}
}, {
key: "registerOngoingRequest",
value: function registerOngoingRequest(url, requestPromise) {
if (this.ongoingRequestsByUrl.has(url)) {
throw new Error("An ongoing request already exists for: " + url);
}
this.ongoingRequestsByUrl.set(url, requestPromise);
}
}, {
key: "removeOngoingRequest",
value: function removeOngoingRequest(url) {
this.ongoingRequestsByUrl.delete(url);
}
}]);
return ServiceLayerRequestsSender;
}();