2017-01-28 23:51:47 +00:00
|
|
|
"use strict";
|
|
|
|
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());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const plugins = require("./smartrequest.plugins");
|
|
|
|
let buildResponse = (responseArg) => {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
// Continuously update stream with data
|
|
|
|
let body = '';
|
|
|
|
responseArg.on('data', function (chunkArg) {
|
|
|
|
body += chunkArg;
|
|
|
|
});
|
|
|
|
responseArg.on('end', function () {
|
|
|
|
try {
|
|
|
|
responseArg.body = JSON.parse(body);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
responseArg.body = body;
|
|
|
|
}
|
|
|
|
done.resolve(responseArg);
|
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
exports.request = (domainArg, optionsArg = {}, streamArg = false) => __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
let parsedUrl;
|
|
|
|
if (domainArg) {
|
|
|
|
parsedUrl = plugins.url.parse(domainArg);
|
|
|
|
optionsArg.hostname = parsedUrl.hostname;
|
|
|
|
if (parsedUrl.port) {
|
|
|
|
optionsArg.port = parseInt(parsedUrl.port);
|
|
|
|
}
|
|
|
|
optionsArg.path = parsedUrl.path;
|
|
|
|
}
|
|
|
|
if (!parsedUrl || parsedUrl.protocol === 'https:') {
|
|
|
|
let request = plugins.https.request(optionsArg, response => {
|
|
|
|
if (streamArg) {
|
|
|
|
done.resolve(response);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buildResponse(response).then(done.resolve);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (optionsArg.requestBody) {
|
|
|
|
if (typeof optionsArg.requestBody !== 'string') {
|
|
|
|
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
|
|
|
}
|
|
|
|
request.write(optionsArg.requestBody);
|
|
|
|
}
|
|
|
|
request.on('error', (e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
request.end();
|
|
|
|
}
|
|
|
|
else if (parsedUrl.protocol === 'http:') {
|
|
|
|
let request = plugins.http.request(optionsArg, response => {
|
|
|
|
if (streamArg) {
|
|
|
|
done.resolve(response);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buildResponse(response).then(done.resolve);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (optionsArg.requestBody) {
|
|
|
|
if (typeof optionsArg.requestBody !== 'string') {
|
|
|
|
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
|
|
|
}
|
|
|
|
request.write(optionsArg.requestBody);
|
|
|
|
}
|
|
|
|
request.on('error', (e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
request.end();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`);
|
|
|
|
}
|
|
|
|
return done.promise;
|
|
|
|
});
|
2017-01-29 00:12:42 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LnJlcXVlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QucmVxdWVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFDQSxrREFBaUQ7QUFHakQsSUFBSSxhQUFhLEdBQUcsQ0FBQyxXQUFXO0lBQzVCLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQ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