finesse-toolkit
Version:
useful tools for finesse phone system
53 lines • 2.22 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fetch_1 = require("node-fetch");
const util_1 = require("../lib/util");
const index_1 = require("../xml/index");
class FinesseHttpHelper {
static get(url, user) {
return this._httpRequest(url, user, 'GET');
}
static put(url, user, body) {
return this._xmlRequest(url, user, 'PUT', body);
}
static post(url, user, body) {
return this._xmlRequest(url, user, 'POST', body);
}
static _xmlRequest(url, user, method, body) {
let headers = {
'Content-Type': 'application/xml',
'Accept': 'application/xml'
};
let processedBody = index_1.default.objToXml(body);
return this._httpRequest(url, user, method, headers, processedBody);
}
static _basicAuthString(user) {
let userPassString = `${user.username}:${user.password}`;
let base64 = new Buffer(userPassString).toString('base64');
return `Basic ${base64}`;
}
static _httpRequest(url, user, method, headers = {}, body = '') {
return __awaiter(this, void 0, void 0, function* () {
headers['Authorization'] = this._basicAuthString(user);
try {
const response = yield node_fetch_1.default(url, { method, headers, body });
return yield response.text();
}
catch (e) {
util_1.default._promiseErrorHandler(e);
}
return null;
});
}
}
exports.default = FinesseHttpHelper;
;
//# sourceMappingURL=index.js.map