* Got no longer throws errors like mad, this leaves only our own errors to get thrown
* url parameter in Request is replace with domain, port & path * params handled by gotjs * body defaults to null and is only added with truth like values with allowGetBody protection
This commit is contained in:
parent
23c732390d
commit
22998a8119
@ -4,16 +4,39 @@ const { Response } = require('./response.js')
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {String} method GET POST DELETE
|
* @param {String} method GET POST DELETE
|
||||||
* @param {String} url full target url
|
* @param {String} domain
|
||||||
* @param {Object} params Raw object of params to send
|
* @param {Number} port
|
||||||
|
* @param {String} path
|
||||||
|
* @param {Object} Query options to pass into a query string
|
||||||
|
* @param {Buffer} Optional body that defaults to an empty string
|
||||||
*
|
*
|
||||||
* @returns Response
|
* @returns Response
|
||||||
*/
|
*/
|
||||||
exports.Request = async function (method, url, params) {
|
|
||||||
|
exports.Request = async function (method, domain, port, path, params, body=null) {
|
||||||
try {
|
try {
|
||||||
const result = await got(url, {json: params, method: method})
|
// TODO: force https since actual servers do not response on port 80
|
||||||
return new Response(result.statusCode, result.body, null)
|
const url = `http://${domain}:${port}`
|
||||||
|
let options = {
|
||||||
|
method: method,
|
||||||
|
searchParams: params,
|
||||||
|
responseType: 'json',
|
||||||
|
throwHttpErrors: false
|
||||||
|
}
|
||||||
|
if(body) {
|
||||||
|
options.body = body
|
||||||
|
if(method == 'get') {
|
||||||
|
options.allowGetBody = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const resp = await got(url , options)
|
||||||
|
return new Response(
|
||||||
|
resp.statusCode,
|
||||||
|
resp.body,
|
||||||
|
)
|
||||||
|
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
return new Response(null, null, err)
|
return new Response(null, null, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user