indent code
This commit is contained in:
parent
2ef0f378d7
commit
bf2cfe0735
@ -3,70 +3,70 @@ import * as plugins from './smartrequest.plugins'
|
|||||||
import * as interfaces from './smartrequest.interfaces'
|
import * as interfaces from './smartrequest.interfaces'
|
||||||
|
|
||||||
let buildResponse = (responseArg): Promise<any> => {
|
let buildResponse = (responseArg): Promise<any> => {
|
||||||
let done = plugins.q.defer()
|
let done = plugins.q.defer()
|
||||||
// Continuously update stream with data
|
// Continuously update stream with data
|
||||||
let body = '';
|
let body = '';
|
||||||
responseArg.on('data', function (chunkArg) {
|
responseArg.on('data', function (chunkArg) {
|
||||||
body += chunkArg;
|
body += chunkArg;
|
||||||
});
|
});
|
||||||
responseArg.on('end', function () {
|
responseArg.on('end', function () {
|
||||||
try {
|
try {
|
||||||
responseArg.body = JSON.parse(body);
|
responseArg.body = JSON.parse(body);
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
responseArg.body = body
|
responseArg.body = body
|
||||||
}
|
}
|
||||||
done.resolve(responseArg)
|
done.resolve(responseArg)
|
||||||
});
|
});
|
||||||
return done.promise
|
return done.promise
|
||||||
}
|
}
|
||||||
|
|
||||||
export let request = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}, streamArg: boolean = false) => {
|
export let request = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}, streamArg: boolean = false) => {
|
||||||
let done = plugins.q.defer()
|
let done = plugins.q.defer()
|
||||||
let parsedUrl: plugins.url.Url
|
let parsedUrl: plugins.url.Url
|
||||||
if (domainArg) {
|
if (domainArg) {
|
||||||
parsedUrl = plugins.url.parse(domainArg)
|
parsedUrl = plugins.url.parse(domainArg)
|
||||||
optionsArg.hostname = parsedUrl.hostname
|
optionsArg.hostname = parsedUrl.hostname
|
||||||
if(parsedUrl.port) { optionsArg.port = parseInt(parsedUrl.port) }
|
if (parsedUrl.port) { optionsArg.port = parseInt(parsedUrl.port) }
|
||||||
optionsArg.path = parsedUrl.path
|
optionsArg.path = parsedUrl.path
|
||||||
|
}
|
||||||
|
if (!parsedUrl || parsedUrl.protocol === 'https:') {
|
||||||
|
let request = plugins.https.request(optionsArg, response => {
|
||||||
|
if (streamArg) {
|
||||||
|
done.resolve(response)
|
||||||
|
} else {
|
||||||
|
buildResponse(response).then(done.resolve)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (optionsArg.requestBody) {
|
||||||
|
if (typeof optionsArg.requestBody !== 'string') {
|
||||||
|
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody)
|
||||||
|
}
|
||||||
|
request.write(optionsArg.requestBody)
|
||||||
}
|
}
|
||||||
if (!parsedUrl || parsedUrl.protocol === 'https:') {
|
request.on('error', (e) => {
|
||||||
let request = plugins.https.request(optionsArg, response => {
|
console.error(e);
|
||||||
if (streamArg) {
|
})
|
||||||
done.resolve(response)
|
request.end()
|
||||||
} else {
|
} else if (parsedUrl.protocol === 'http:') {
|
||||||
buildResponse(response).then(done.resolve)
|
let request = plugins.http.request(optionsArg, response => {
|
||||||
}
|
if (streamArg) {
|
||||||
})
|
done.resolve(response)
|
||||||
if (optionsArg.requestBody) {
|
} else {
|
||||||
if(typeof optionsArg.requestBody !== 'string') {
|
buildResponse(response).then(done.resolve)
|
||||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody)
|
}
|
||||||
}
|
})
|
||||||
request.write(optionsArg.requestBody)
|
if (optionsArg.requestBody) {
|
||||||
}
|
if (typeof optionsArg.requestBody !== 'string') {
|
||||||
request.on('error', (e) => {
|
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody)
|
||||||
console.error(e);
|
}
|
||||||
})
|
request.write(optionsArg.requestBody)
|
||||||
request.end()
|
|
||||||
} else if (parsedUrl.protocol === 'http:') {
|
|
||||||
let request = plugins.http.request(optionsArg, response => {
|
|
||||||
if (streamArg) {
|
|
||||||
done.resolve(response)
|
|
||||||
} else {
|
|
||||||
buildResponse(response).then(done.resolve)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if (optionsArg.requestBody) {
|
|
||||||
if(typeof optionsArg.requestBody !== 'string') {
|
|
||||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody)
|
|
||||||
}
|
|
||||||
request.write(optionsArg.requestBody)
|
|
||||||
}
|
|
||||||
request.on('error', (e) => {
|
|
||||||
console.error(e);
|
|
||||||
})
|
|
||||||
request.end()
|
|
||||||
} else {
|
|
||||||
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`)
|
|
||||||
}
|
}
|
||||||
return done.promise
|
request.on('error', (e) => {
|
||||||
|
console.error(e);
|
||||||
|
})
|
||||||
|
request.end()
|
||||||
|
} else {
|
||||||
|
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`)
|
||||||
|
}
|
||||||
|
return done.promise
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user