node-telegram-api/build/functions/fetch.js
Mahdi Dibaiee b53817ced7 Switch from restler to unirest
restler is buggy and doesn't support utf8 multipart requests
2015-07-06 04:35:25 +04:30

74 lines
1.8 KiB
JavaScript

'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports['default'] = fetch;
exports.getBody = getBody;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _unirest = require('unirest');
var _unirest2 = _interopRequireDefault(_unirest);
function fetch(path) {
var data = arguments[1] === undefined ? {} : arguments[1];
return new Promise(function (resolve, reject) {
var files = {};
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = Object.keys(data)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
if (data[key].file) {
files[key] = data[key].file;
delete data[key];
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
_unirest2['default'].post('https://api.telegram.org/bot' + path).field(data).attach(files).end(function (response) {
if (response.statusType === 4 || response.statusType === 5) {
reject(response);
} else {
resolve(response.body);
}
});
});
}
function getBody(stream) {
var data = '';
return new Promise(function (resolve, reject) {
stream.on('data', function (chunk) {
data += chunk;
});
stream.on('end', function () {
resolve(data);
});
stream.on('error', reject);
});
}