* 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:
shockrah 2021-02-11 16:18:36 -08:00
parent 23c732390d
commit 22998a8119

View File

@ -4,15 +4,38 @@ const { Response } = require('./response.js')
/**
*
* @param {String} method GET POST DELETE
* @param {String} url full target url
* @param {Object} params Raw object of params to send
* @param {String} domain
* @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
*/
exports.Request = async function (method, url, params) {
exports.Request = async function (method, domain, port, path, params, body=null) {
try {
const result = await got(url, {json: params, method: method})
return new Response(result.statusCode, result.body, null)
// TODO: force https since actual servers do not response on port 80
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) {
return new Response(null, null, err)
}