osmos-aurelia-api-client
Version:
Osmos api client for aurelia project
183 lines (148 loc) • 6.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ApiClient = undefined;
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; }; }();
var _aureliaFetchClient = require('aurelia-fetch-client');
var _guid = require('./guid');
var _environment = require('environment');
var _environment2 = _interopRequireDefault(_environment);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _apiUrl = _environment2.default.baseUrl;
var _client = new _aureliaFetchClient.HttpClient();
var ApiClient = exports.ApiClient = function () {
function ApiClient() {
_classCallCheck(this, ApiClient);
this.currentRequests = [];
}
_createClass(ApiClient, [{
key: 'get',
value: function get(options) {
var o = options || {};
o.method = 'get';
return this.execute(o);
}
}, {
key: 'post',
value: function post(options) {
var o = options || {};
o.method = 'post';
return this.execute(o);
}
}, {
key: 'put',
value: function put(options) {
var o = options || {};
o.method = 'put';
return this.execute(o);
}
}, {
key: 'delete',
value: function _delete(options) {
var o = options || {};
o.method = 'delete';
return this.execute(o);
}
}, {
key: 'execute',
value: function execute(options) {
var _this = this;
console.info('options', options);
var o = {
method: options && options.method ? options.method : 'get',
headers: options && options.headers ? options.headers : {},
body: options && options.data ? (0, _aureliaFetchClient.json)(options.data) : null
};
if (options && options.formData) {
o.body = options.formData;
}
console.info('options', o);
var url = options && options.url ? options.url : '';
url = _apiUrl + url;
var apiRequest = this._addRequest(url, o.method);
var promise = new Promise(function (resolve, reject) {
_client.fetch(url, o).then(function (response) {
_this._removeRequest(apiRequest.id);
if (response && !response.ok) {
return response.text().then(function (text) {
reject(new Error(JSON.stringify({
status: response.status,
body: text
})));
});
}
resolve(response);
}).catch(function (error) {
_this._removeRequest(apiRequest.id);
reject(error);
});
});
return promise;
}
}, {
key: '_addRequest',
value: function _addRequest(url, method) {
var apiRequest = new ApiRequest(url, method);
this.currentRequests.push(apiRequest);
return apiRequest;
}
}, {
key: '_removeRequest',
value: function _removeRequest(id) {
this.currentRequests = this.currentRequests.filter(function (item) {
return item.id !== id;
});
}
}], [{
key: 'downloadToBlob',
value: function downloadToBlob(options, onprogress) {
var promise = new Promise(function (resolve, reject) {
if (!options || !options.url || !options.url.length) reject('bad xhr arguments');
var xhr = new XMLHttpRequest();
xhr.open('GET', options.url, true);
xhr.responseType = 'blob';
if (options.headers) {
Object.keys(options.headers).forEach(function (key) {
xhr.setRequestHeader(key, options.headers[key]);
});
}
xhr.onload = function (event) {
options.xhr = null;
if (xhr.status >= 200 && xhr.status < 300) {
console.info('downloadToBlob : ' + options.url);
var result = URL.createObjectURL(xhr.response);
console.info('result', result);
resolve(result);
} else {
console.info('1st reject:', xhr);
reject(new Error(xhr.statusText));
}
};
xhr.onerror = function (error, x) {
options.xhr = null;
console.info('2nd reject:', error, x, xhr);
reject(new Error(xhr.statusText));
};
xhr.onprogress = onprogress;
// xhr.onprogress = (event) => {
// if (event.lengthComputable) {
// ref.loadedBytes = event.loaded;
// ref.size = event.total;
// }
// };
options.xhr = xhr;
xhr.send();
});
return promise;
}
}]);
return ApiClient;
}();
var ApiRequest = function ApiRequest(url, method) {
_classCallCheck(this, ApiRequest);
this.id = _guid.Guid.New();
this.url = url;
this.method = method;
};