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 +}