smartrequest/ts/smartrequest.request.ts

86 lines
2.5 KiB
TypeScript
Raw Normal View History

2018-06-13 20:34:49 +00:00
import * as https from 'https';
import * as plugins from './smartrequest.plugins';
import * as interfaces from './smartrequest.interfaces';
2017-01-28 23:51:47 +00:00
2018-06-13 20:34:49 +00:00
import { IncomingMessage } from 'http';
export interface extendedIncomingMessage extends IncomingMessage {
body: any;
}
2018-06-13 20:34:49 +00:00
let buildResponse = (incomingMessageArg: IncomingMessage): Promise<extendedIncomingMessage> => {
let done = plugins.smartpromise.defer<extendedIncomingMessage>();
2017-06-05 17:09:40 +00:00
// Continuously update stream with data
2018-06-13 20:34:49 +00:00
let body = '';
incomingMessageArg.on('data', function(chunkArg) {
body += chunkArg;
});
incomingMessageArg.on('end', function() {
2017-06-05 17:09:40 +00:00
try {
2018-06-13 20:34:49 +00:00
(incomingMessageArg as extendedIncomingMessage).body = JSON.parse(body);
2017-06-05 17:09:40 +00:00
} catch (err) {
2018-06-13 20:34:49 +00:00
(incomingMessageArg as extendedIncomingMessage).body = body;
2017-06-05 17:09:40 +00:00
}
2018-06-13 20:34:49 +00:00
done.resolve(incomingMessageArg as extendedIncomingMessage);
});
return done.promise;
};
2017-01-28 23:51:47 +00:00
2018-06-13 20:34:49 +00:00
export let request = async (
domainArg: string,
optionsArg: interfaces.ISmartRequestOptions = {},
streamArg: boolean = false
2018-06-13 21:12:37 +00:00
): Promise<extendedIncomingMessage> => {
let done = plugins.smartpromise.defer<any>();
2018-06-13 20:34:49 +00:00
let parsedUrl: plugins.url.Url;
2017-06-05 17:09:40 +00:00
if (domainArg) {
2018-06-13 20:34:49 +00:00
parsedUrl = plugins.url.parse(domainArg);
optionsArg.hostname = parsedUrl.hostname;
if (parsedUrl.port) {
optionsArg.port = parseInt(parsedUrl.port);
}
optionsArg.path = parsedUrl.path;
2017-06-05 17:09:40 +00:00
}
if (!parsedUrl || parsedUrl.protocol === 'https:') {
let request = plugins.https.request(optionsArg, response => {
if (streamArg) {
2018-06-13 20:34:49 +00:00
done.resolve(response);
2017-06-05 17:09:40 +00:00
} else {
2018-06-13 20:34:49 +00:00
buildResponse(response).then(done.resolve);
2017-06-05 17:09:40 +00:00
}
2018-06-13 20:34:49 +00:00
});
2017-06-05 17:09:40 +00:00
if (optionsArg.requestBody) {
if (typeof optionsArg.requestBody !== 'string') {
2018-06-13 20:34:49 +00:00
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
2017-06-05 17:09:40 +00:00
}
2018-06-13 20:34:49 +00:00
request.write(optionsArg.requestBody);
2017-01-28 23:51:47 +00:00
}
2018-06-13 20:34:49 +00:00
request.on('error', e => {
console.error(e);
});
request.end();
2017-06-05 17:09:40 +00:00
} else if (parsedUrl.protocol === 'http:') {
let request = plugins.http.request(optionsArg, response => {
if (streamArg) {
2018-06-13 20:34:49 +00:00
done.resolve(response);
2017-06-05 17:09:40 +00:00
} else {
2018-06-13 20:34:49 +00:00
buildResponse(response).then(done.resolve);
2017-06-05 17:09:40 +00:00
}
2018-06-13 20:34:49 +00:00
});
2017-06-05 17:09:40 +00:00
if (optionsArg.requestBody) {
if (typeof optionsArg.requestBody !== 'string') {
2018-06-13 20:34:49 +00:00
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
2017-06-05 17:09:40 +00:00
}
2018-06-13 20:34:49 +00:00
request.write(optionsArg.requestBody);
2017-01-28 23:51:47 +00:00
}
2018-06-13 20:34:49 +00:00
request.on('error', e => {
console.error(e);
});
request.end();
2017-06-05 17:09:40 +00:00
} else {
2018-06-13 20:34:49 +00:00
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`);
2017-06-05 17:09:40 +00:00
}
2018-06-13 20:34:49 +00:00
return done.promise;
};