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-12-14 21:33:48 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LnJlcXVlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QucmVxdWVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQ0Esa0RBQWlEO0FBR2pELElBQUksYU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