improve types
This commit is contained in:
25
ts/index.ts
25
ts/index.ts
@ -5,30 +5,29 @@ import * as interfaces from './smartrequest.interfaces'
|
||||
|
||||
import { request } from './smartrequest.request'
|
||||
|
||||
|
||||
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'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
optionsArg.method = 'POST'
|
||||
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
|
||||
}
|
||||
|
@ -2,5 +2,5 @@ import * as plugins from './smartrequest.plugins'
|
||||
import * as https from 'https'
|
||||
|
||||
export interface ISmartRequestOptions extends https.RequestOptions {
|
||||
requestBody?: any
|
||||
}
|
||||
requestBody?: any
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ import * as https from 'https'
|
||||
import * as q from 'smartq'
|
||||
|
||||
export {
|
||||
url,
|
||||
http,
|
||||
https,
|
||||
q
|
||||
}
|
||||
url,
|
||||
http,
|
||||
https,
|
||||
q
|
||||
}
|
||||
|
@ -5,23 +5,23 @@ import * as interfaces from './smartrequest.interfaces'
|
||||
let buildResponse = (responseArg): Promise<any> => {
|
||||
let done = plugins.q.defer()
|
||||
// Continuously update stream with data
|
||||
let body = '';
|
||||
let body = ''
|
||||
responseArg.on('data', function (chunkArg) {
|
||||
body += chunkArg;
|
||||
});
|
||||
body += chunkArg
|
||||
})
|
||||
responseArg.on('end', function () {
|
||||
try {
|
||||
responseArg.body = JSON.parse(body);
|
||||
responseArg.body = JSON.parse(body)
|
||||
} catch (err) {
|
||||
responseArg.body = body
|
||||
}
|
||||
done.resolve(responseArg)
|
||||
});
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
export let request = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}, streamArg: boolean = false) => {
|
||||
let done = plugins.q.defer()
|
||||
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)
|
||||
@ -44,7 +44,7 @@ export let request = async (domainArg: string, optionsArg: interfaces.ISmartRequ
|
||||
request.write(optionsArg.requestBody)
|
||||
}
|
||||
request.on('error', (e) => {
|
||||
console.error(e);
|
||||
console.error(e)
|
||||
})
|
||||
request.end()
|
||||
} else if (parsedUrl.protocol === 'http:') {
|
||||
@ -62,11 +62,11 @@ export let request = async (domainArg: string, optionsArg: interfaces.ISmartRequ
|
||||
request.write(optionsArg.requestBody)
|
||||
}
|
||||
request.on('error', (e) => {
|
||||
console.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