2018-06-13 20:34:49 +00:00
|
|
|
import * as https from 'https';
|
2017-01-28 23:51:47 +00:00
|
|
|
|
2018-06-13 20:34:49 +00:00
|
|
|
import * as plugins from './smartrequest.plugins';
|
|
|
|
import * as interfaces from './smartrequest.interfaces';
|
2017-01-28 23:51:47 +00:00
|
|
|
|
2018-06-13 21:12:37 +00:00
|
|
|
import { request, extendedIncomingMessage } from './smartrequest.request';
|
2017-01-28 23:51:47 +00:00
|
|
|
|
2018-06-13 21:12:37 +00:00
|
|
|
export { request, extendedIncomingMessage } from './smartrequest.request';
|
2018-06-13 20:34:49 +00:00
|
|
|
export { ISmartRequestOptions } from './smartrequest.interfaces';
|
2017-01-28 23:51:47 +00:00
|
|
|
|
2017-01-29 00:12:42 +00:00
|
|
|
export let get = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
2018-06-13 20:34:49 +00:00
|
|
|
optionsArg.method = 'GET';
|
|
|
|
let response = await request(domainArg, optionsArg);
|
|
|
|
return response;
|
|
|
|
};
|
2017-01-28 23:51:47 +00:00
|
|
|
|
2018-06-13 21:12:37 +00:00
|
|
|
export let getBinary = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
2018-06-14 20:48:50 +00:00
|
|
|
const done = plugins.smartq.defer();
|
2018-06-13 21:12:37 +00:00
|
|
|
const response = await request(domainArg, optionsArg, true);
|
|
|
|
var data = [];
|
|
|
|
|
|
|
|
response.on('data', function(chunk) {
|
|
|
|
data.push(chunk);
|
|
|
|
}).on('end', function() {
|
|
|
|
//at this point data is an array of Buffers
|
|
|
|
//so Buffer.concat() can make us a new Buffer
|
|
|
|
//of all of them together
|
|
|
|
const buffer = Buffer.concat(data);
|
2018-06-14 22:00:55 +00:00
|
|
|
response.body = buffer.toString('binary');
|
2018-06-14 20:48:50 +00:00
|
|
|
done.resolve();
|
2018-06-13 21:12:37 +00:00
|
|
|
});
|
2018-06-14 20:48:50 +00:00
|
|
|
await done.promise;
|
2018-06-14 19:11:07 +00:00
|
|
|
return response;
|
2018-06-13 21:12:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 00:12:42 +00:00
|
|
|
export let post = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
2018-06-13 20:34:49 +00:00
|
|
|
optionsArg.method = 'POST';
|
2017-12-14 21:33:48 +00:00
|
|
|
if (
|
2018-06-13 20:34:49 +00:00
|
|
|
typeof optionsArg.requestBody === 'object' &&
|
|
|
|
(!optionsArg.headers || !optionsArg.headers['Content-Type'])
|
|
|
|
) {
|
2017-12-14 21:33:48 +00:00
|
|
|
// make sure headers exist
|
2018-06-13 20:34:49 +00:00
|
|
|
if (!optionsArg.headers) {
|
|
|
|
optionsArg.headers = {};
|
|
|
|
}
|
2017-12-14 21:33:48 +00:00
|
|
|
|
|
|
|
// assign the right Content-Type, leaving all other headers in place
|
|
|
|
Object.assign(optionsArg.headers, {
|
|
|
|
'Content-Type': 'application/json'
|
2018-06-13 20:34:49 +00:00
|
|
|
});
|
2017-12-14 21:33:48 +00:00
|
|
|
}
|
2018-06-13 20:34:49 +00:00
|
|
|
let response = await request(domainArg, optionsArg);
|
|
|
|
return response;
|
|
|
|
};
|
2017-01-28 23:51:47 +00:00
|
|
|
|
2017-01-29 00:12:42 +00:00
|
|
|
export let put = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
2018-06-13 20:34:49 +00:00
|
|
|
optionsArg.method = 'PUT';
|
|
|
|
let response = await request(domainArg, optionsArg);
|
|
|
|
return response;
|
|
|
|
};
|
2017-01-28 23:51:47 +00:00
|
|
|
|
2017-01-29 00:12:42 +00:00
|
|
|
export let del = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
2018-06-13 20:34:49 +00:00
|
|
|
optionsArg.method = 'DELETE';
|
|
|
|
let response = await request(domainArg, optionsArg);
|
|
|
|
return response;
|
|
|
|
};
|