From 22998a811957034df6360992f4c2ca34547091b8 Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 11 Feb 2021 16:18:36 -0800 Subject: [PATCH] * 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 --- freechat-client/src/request.js | 35 ++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/freechat-client/src/request.js b/freechat-client/src/request.js index 156831e..c2245d1 100644 --- a/freechat-client/src/request.js +++ b/freechat-client/src/request.js @@ -4,16 +4,39 @@ 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) } -} \ No newline at end of file +}