fix(core): update
This commit is contained in:
parent
fe693e6383
commit
74b6bf230f
@ -3,4 +3,5 @@ import * as https from 'https';
|
||||
|
||||
export interface ISmartRequestOptions extends https.RequestOptions {
|
||||
requestBody?: any;
|
||||
autoJsonParse?: boolean;
|
||||
}
|
||||
|
@ -9,7 +9,8 @@ export interface IExtendedIncomingMessage extends IncomingMessage {
|
||||
}
|
||||
|
||||
const buildUtf8Response = (
|
||||
incomingMessageArg: IncomingMessage
|
||||
incomingMessageArg: IncomingMessage,
|
||||
autoJsonParse = true
|
||||
): Promise<IExtendedIncomingMessage> => {
|
||||
let done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
|
||||
// Continuously update stream with data
|
||||
@ -19,11 +20,15 @@ const buildUtf8Response = (
|
||||
});
|
||||
|
||||
incomingMessageArg.on('end', function() {
|
||||
if (autoJsonParse) {
|
||||
try {
|
||||
(incomingMessageArg as IExtendedIncomingMessage).body = JSON.parse(body);
|
||||
} catch (err) {
|
||||
(incomingMessageArg as IExtendedIncomingMessage).body = body;
|
||||
}
|
||||
} else {
|
||||
(incomingMessageArg as IExtendedIncomingMessage).body = body;
|
||||
}
|
||||
done.resolve(incomingMessageArg as IExtendedIncomingMessage);
|
||||
});
|
||||
return done.promise;
|
||||
@ -57,6 +62,16 @@ export let request = async (
|
||||
): Promise<IExtendedIncomingMessage> => {
|
||||
let done = plugins.smartpromise.defer<any>();
|
||||
|
||||
// merge options
|
||||
const defaultOptions: interfaces.ISmartRequestOptions = {
|
||||
autoJsonParse: true
|
||||
}
|
||||
|
||||
optionsArg = {
|
||||
...defaultOptions,
|
||||
...optionsArg
|
||||
}
|
||||
|
||||
// parse url
|
||||
let parsedUrl: plugins.url.Url;
|
||||
parsedUrl = plugins.url.parse(domainArg);
|
||||
@ -114,7 +129,7 @@ export let request = async (
|
||||
if (streamArg) {
|
||||
done.resolve(response);
|
||||
} else {
|
||||
const builtResponse = await buildUtf8Response(response);
|
||||
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
|
||||
done.resolve(builtResponse);
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user