fix(now handles binary files): update

This commit is contained in:
2018-06-13 23:12:37 +02:00
parent 52fa217fda
commit df48a06d3e
8 changed files with 48 additions and 14 deletions

View File

@ -3,9 +3,9 @@ import * as https from 'https';
import * as plugins from './smartrequest.plugins';
import * as interfaces from './smartrequest.interfaces';
import { request } from './smartrequest.request';
import { request, extendedIncomingMessage } from './smartrequest.request';
export { request } from './smartrequest.request';
export { request, extendedIncomingMessage } from './smartrequest.request';
export { ISmartRequestOptions } from './smartrequest.interfaces';
export let get = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
@ -14,6 +14,21 @@ export let get = async (domainArg: string, optionsArg: interfaces.ISmartRequestO
return response;
};
export let getBinary = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
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);
response.body = buffer.toString('base64');
});
}
export let post = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
optionsArg.method = 'POST';
if (

View File

@ -31,7 +31,7 @@ export let request = async (
domainArg: string,
optionsArg: interfaces.ISmartRequestOptions = {},
streamArg: boolean = false
): Promise<Response> => {
): Promise<extendedIncomingMessage> => {
let done = plugins.q.defer<any>();
let parsedUrl: plugins.url.Url;
if (domainArg) {