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());
|
|
|
|
});
|
|
|
|
};
|
2017-06-09 19:48:16 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2017-01-28 23:51:47 +00:00
|
|
|
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-06-09 19:48:16 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LnJlcXVlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QucmVxdWVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQ0Esa0RBQWlEO0FBR2pELElBQUksYU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