initial
This commit is contained in:
35
ts/index.ts
Normal file
35
ts/index.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import * as https from 'https'
|
||||
|
||||
import * as plugins from './smartrequest.plugins'
|
||||
import * as interfaces from './smartrequest.interfaces'
|
||||
|
||||
import { request } from './smartrequest.request'
|
||||
|
||||
|
||||
export {
|
||||
request
|
||||
}
|
||||
|
||||
export let get = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'GET'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
|
||||
export let post = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'POST'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
|
||||
export let put = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'PUT'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
|
||||
export let del = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'DELETE'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
6
ts/smartrequest.interfaces.ts
Normal file
6
ts/smartrequest.interfaces.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import * as plugins from './smartrequest.plugins'
|
||||
import * as https from 'https'
|
||||
|
||||
export interface SmartRequestOptions extends https.RequestOptions {
|
||||
requestBody?: any
|
||||
}
|
13
ts/smartrequest.plugins.ts
Normal file
13
ts/smartrequest.plugins.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import 'typings-global'
|
||||
import * as url from 'url'
|
||||
import * as http from 'http'
|
||||
import * as https from 'https'
|
||||
|
||||
import * as q from 'smartq'
|
||||
|
||||
export {
|
||||
url,
|
||||
http,
|
||||
https,
|
||||
q
|
||||
}
|
72
ts/smartrequest.request.ts
Normal file
72
ts/smartrequest.request.ts
Normal file
@ -0,0 +1,72 @@
|
||||
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()
|
||||
// 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
|
||||
}
|
||||
|
||||
export let request = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}, streamArg: boolean = false) => {
|
||||
let done = plugins.q.defer()
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
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)
|
||||
} 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
|
||||
}
|
Reference in New Issue
Block a user