fix(core): update to latest standards
This commit is contained in:
56
ts/index.ts
56
ts/index.ts
@ -1,45 +1,47 @@
|
||||
import * as https from 'https'
|
||||
import * as https from 'https';
|
||||
|
||||
import * as plugins from './smartrequest.plugins'
|
||||
import * as interfaces from './smartrequest.interfaces'
|
||||
import * as plugins from './smartrequest.plugins';
|
||||
import * as interfaces from './smartrequest.interfaces';
|
||||
|
||||
import { request } from './smartrequest.request'
|
||||
import { request } from './smartrequest.request';
|
||||
|
||||
export { request } from './smartrequest.request'
|
||||
export { ISmartRequestOptions } from './smartrequest.interfaces'
|
||||
export { request } from './smartrequest.request';
|
||||
export { ISmartRequestOptions } from './smartrequest.interfaces';
|
||||
|
||||
export let get = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'GET'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
optionsArg.method = 'GET';
|
||||
let response = await request(domainArg, optionsArg);
|
||||
return response;
|
||||
};
|
||||
|
||||
export let post = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'POST'
|
||||
optionsArg.method = 'POST';
|
||||
if (
|
||||
typeof optionsArg.requestBody === 'object'
|
||||
&& (!optionsArg.headers || (!optionsArg.headers['Content-Type']))
|
||||
){
|
||||
typeof optionsArg.requestBody === 'object' &&
|
||||
(!optionsArg.headers || !optionsArg.headers['Content-Type'])
|
||||
) {
|
||||
// make sure headers exist
|
||||
if(!optionsArg.headers) { optionsArg.headers = {} }
|
||||
if (!optionsArg.headers) {
|
||||
optionsArg.headers = {};
|
||||
}
|
||||
|
||||
// assign the right Content-Type, leaving all other headers in place
|
||||
Object.assign(optionsArg.headers, {
|
||||
'Content-Type': 'application/json'
|
||||
})
|
||||
});
|
||||
}
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
let response = await request(domainArg, optionsArg);
|
||||
return response;
|
||||
};
|
||||
|
||||
export let put = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'PUT'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
optionsArg.method = 'PUT';
|
||||
let response = await request(domainArg, optionsArg);
|
||||
return response;
|
||||
};
|
||||
|
||||
export let del = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'DELETE'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
optionsArg.method = 'DELETE';
|
||||
let response = await request(domainArg, optionsArg);
|
||||
return response;
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as plugins from './smartrequest.plugins'
|
||||
import * as https from 'https'
|
||||
import * as plugins from './smartrequest.plugins';
|
||||
import * as https from 'https';
|
||||
|
||||
export interface ISmartRequestOptions extends https.RequestOptions {
|
||||
requestBody?: any
|
||||
requestBody?: any;
|
||||
}
|
||||
|
@ -1,12 +1,7 @@
|
||||
import * as url from 'url'
|
||||
import * as http from 'http'
|
||||
import * as https from 'https'
|
||||
import * as url from 'url';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
|
||||
import * as q from 'smartq'
|
||||
import * as q from 'smartq';
|
||||
|
||||
export {
|
||||
url,
|
||||
http,
|
||||
https,
|
||||
q
|
||||
}
|
||||
export { url, http, https, q };
|
||||
|
@ -1,72 +1,85 @@
|
||||
import * as https from 'https'
|
||||
import * as plugins from './smartrequest.plugins'
|
||||
import * as interfaces from './smartrequest.interfaces'
|
||||
import * as https from 'https';
|
||||
import * as plugins from './smartrequest.plugins';
|
||||
import * as interfaces from './smartrequest.interfaces';
|
||||
|
||||
let buildResponse = (responseArg): Promise<any> => {
|
||||
let done = plugins.q.defer()
|
||||
import { IncomingMessage } from 'http';
|
||||
|
||||
export interface extendedIncomingMessage extends IncomingMessage {
|
||||
body: any
|
||||
};
|
||||
|
||||
let buildResponse = (incomingMessageArg: IncomingMessage): Promise<extendedIncomingMessage> => {
|
||||
let done = plugins.q.defer<extendedIncomingMessage>();
|
||||
// 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
|
||||
}
|
||||
let body = '';
|
||||
incomingMessageArg.on('data', function(chunkArg) {
|
||||
body += chunkArg;
|
||||
});
|
||||
|
||||
export let request = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}, streamArg: boolean = false): Promise<Response> => {
|
||||
let done = plugins.q.defer<any>()
|
||||
let parsedUrl: plugins.url.Url
|
||||
incomingMessageArg.on('end', function() {
|
||||
try {
|
||||
(incomingMessageArg as extendedIncomingMessage).body = JSON.parse(body);
|
||||
} catch (err) {
|
||||
(incomingMessageArg as extendedIncomingMessage).body = body;
|
||||
}
|
||||
done.resolve(incomingMessageArg as extendedIncomingMessage);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
export let request = async (
|
||||
domainArg: string,
|
||||
optionsArg: interfaces.ISmartRequestOptions = {},
|
||||
streamArg: boolean = false
|
||||
): Promise<Response> => {
|
||||
let done = plugins.q.defer<any>();
|
||||
let parsedUrl: plugins.url.Url;
|
||||
if (domainArg) {
|
||||
parsedUrl = plugins.url.parse(domainArg)
|
||||
optionsArg.hostname = parsedUrl.hostname
|
||||
if (parsedUrl.port) { optionsArg.port = parseInt(parsedUrl.port) }
|
||||
optionsArg.path = parsedUrl.path
|
||||
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)
|
||||
done.resolve(response);
|
||||
} else {
|
||||
buildResponse(response).then(done.resolve)
|
||||
buildResponse(response).then(done.resolve);
|
||||
}
|
||||
})
|
||||
});
|
||||
if (optionsArg.requestBody) {
|
||||
if (typeof optionsArg.requestBody !== 'string') {
|
||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody)
|
||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
||||
}
|
||||
request.write(optionsArg.requestBody)
|
||||
request.write(optionsArg.requestBody);
|
||||
}
|
||||
request.on('error', (e) => {
|
||||
console.error(e)
|
||||
})
|
||||
request.end()
|
||||
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)
|
||||
done.resolve(response);
|
||||
} else {
|
||||
buildResponse(response).then(done.resolve)
|
||||
buildResponse(response).then(done.resolve);
|
||||
}
|
||||
})
|
||||
});
|
||||
if (optionsArg.requestBody) {
|
||||
if (typeof optionsArg.requestBody !== 'string') {
|
||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody)
|
||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
||||
}
|
||||
request.write(optionsArg.requestBody)
|
||||
request.write(optionsArg.requestBody);
|
||||
}
|
||||
request.on('error', (e) => {
|
||||
console.error(e)
|
||||
})
|
||||
request.end()
|
||||
request.on('error', e => {
|
||||
console.error(e);
|
||||
});
|
||||
request.end();
|
||||
} else {
|
||||
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`)
|
||||
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`);
|
||||
}
|
||||
return done.promise
|
||||
}
|
||||
return done.promise;
|
||||
};
|
||||
|
Reference in New Issue
Block a user