umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
144 lines (143 loc) • 4.15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Request = void 0;
const utils_1 = require("../../utils");
class Request {
static HEADER_FORM_DATA = {
'Content-Type': 'multipart/form-data',
};
static HEADER_RSS_XML = {
'Content-Type': 'application/rss+xml',
};
static HEADER_AP_JSON = {
'Content-Type': 'application/json',
};
static HEADER_AP_XML = {
'Content-Type': 'application/xml',
};
static HEADER_GZIP = { 'Content-Encoding': 'gzip' };
url;
get;
post;
header;
attach;
isAttachContent;
attachName;
customRequest;
maxTimeQuery;
isConvertJson;
_error;
_setTimeOut;
constructor() {
this.url = null;
this.get = null;
this.post = null;
this.header = null;
this.attach = null;
this.isAttachContent = false;
this.attachName = 'file';
this.customRequest = null;
this.maxTimeQuery = null;
this.isConvertJson = true;
this._error = null;
this._setTimeOut = null;
}
async send(url = null) {
if (url) {
this.url = url;
}
this._error = null;
const data = (await this._run());
if (this._error) {
return { status: false, data: null, err: this._error };
}
return { status: true, data };
}
_getUrl() {
let url = this.url || '';
if (this.get) {
url += '?' + (0, utils_1.httpBuildQuery)(this.get);
}
return url;
}
_clearTimeout() {
if (this._setTimeOut) {
clearTimeout(this._setTimeOut);
this._setTimeOut = null;
}
}
async _run() {
if (this.url) {
try {
this._clearTimeout();
const response = await fetch(this._getUrl(), this._getOptions());
this._clearTimeout();
if (response.ok) {
if (this.isConvertJson) {
return await response.json();
}
return await response.text();
}
this._error = 'Не удалось получить данные с ' + this.url;
}
catch (e) {
this._error = e.message;
}
}
else {
this._error = 'Не указан url!';
}
return null;
}
_getOptions() {
const options = {};
if (this.maxTimeQuery) {
const controller = new AbortController();
const signal = controller.signal;
this._setTimeOut = setTimeout(() => controller.abort(), this.maxTimeQuery);
options.signal = signal;
}
let post = null;
if (this.attach) {
if ((0, utils_1.isFile)(this.attach)) {
post = Request.getAttachFile(this.attach, this.attachName);
}
else {
this._error = `Не удалось найти файл: ${this.attach}`;
return;
}
}
if (this.post) {
post = { ...post, ...this.post };
}
if (post) {
options.body = JSON.stringify(post);
}
if (this.header) {
options.headers = this.header;
}
if (this.customRequest) {
options.method = this.customRequest;
}
return options;
}
static getAttachFile(filePath, fileName) {
try {
const formData = new FormData();
const fileResult = (0, utils_1.fread)(filePath);
if (fileResult.data) {
const blob = new Blob([fileResult.data], { type: 'application/octet-stream' });
formData.append(fileName || 'file', blob);
return formData;
}
}
catch (e) {
console.error('Ошибка при чтении файла:', e);
}
return null;
}
getError() {
return this._error;
}
}
exports.Request = Request;