node-telegram-api/lib/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

42 lines
834 B
JavaScript

import unirest from 'unirest';
export default function fetch(path, data = {}) {
return new Promise((resolve, reject) => {
const files = {};
for (let key of Object.keys(data)) {
if (data[key].file) {
files[key] = data[key].file;
delete data[key];
}
}
unirest.post('https://api.telegram.org/bot' + path)
.field(data)
.attach(files)
.end(response => {
if (response.statusType === 4 || response.statusType === 5) {
reject(response);
} else {
resolve(response.body);
}
});
});
}
export function getBody(stream) {
let data = '';
return new Promise((resolve, reject) => {
stream.on('data', chunk => {
data += chunk;
});
stream.on('end', () => {
resolve(data);
});
stream.on('error', reject);
});
}