diff --git a/freechat-client/src/html.js b/freechat-client/src/html.js deleted file mode 100644 index 623aad5..0000000 --- a/freechat-client/src/html.js +++ /dev/null @@ -1,53 +0,0 @@ -const auth = require('./auth.js') - -/** - * @param {String} id HTML ID - * @param {String} type Base tag type => examples include 'button' 'div' - * @param {Object} attrs Config object with the following valid fields and their types - * - * @returns HTMLElement - */ -exports.new_node = function new_node(id, type, attrs) { - let node = document.createElement(type) - if(id) { node.id = id; } - - for(const key in attrs) { - if(key == 'classList') { - attrs['classList'].forEach(cls => node.classList.add(cls)) - continue - } - node[key] = attrs[key] - } - return node -} - -/** - * {Config} config | configuration object passed from the ipc - * {String} dom_id | element container for the server list itself - * */ -exports.build_server_list = function (config, dom_id) { - const channels = require('./channels.js') - if(!config['servers']) { return; } - - - let container = document.getElementById(dom_id) - - for(let server of config['servers']) { - // Request a new token so that further functions don't accidently try to use - // an old token - if(!('jwt' in server)) { - auth.request_new_token(server) - } - - const id = server['domain'] || server['ip'] - - let child = html.new_node(id, 'button', { - 'classList': ['btn', 'btn-outline-secondary', 'btn-nav-settings'], - 'textContent': server['name'], - 'type': 'button', - }) - child.addEventListener('click', () => channels.get_channels(server)) - - container.appendChild(child) - } -} diff --git a/freechat-client/src/request.js b/freechat-client/src/request.js index 4852dfb..ef4a517 100644 --- a/freechat-client/src/request.js +++ b/freechat-client/src/request.js @@ -12,37 +12,38 @@ const fs = require('fs') * @param {Buffer} Optional body that defaults to an empty string * * @returns Response + * @throws HTTPError */ exports.Request = async function (method, domain, port, path, params, body=null) { - try { - let url = `https://${domain}:${port}${path}` - const options = { - method: method, - searchParams: params, - responseType: 'json', - throwHttpErrors: false, - } + let url = `https://${domain}:${port}${path}` + const options = { + method: method, + searchParams: params, + responseType: 'json', + body: body, + } - // NOTE: only in dev environments do we use http - if(process.env['DEV_ENV']) { - url = `http://${domain}:${port}${path}` - } + // NOTE: only in dev environments do we use http + if(process.env['DEV_ENV']) { + url = `http://${domain}:${port}${path}` + } - 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) - } + const resp = await got(url , options) + return new Response( + resp.statusCode, + resp.body, + ) +} + +/* + * @param {String} proto | http/https + * @param {String} hostname | + * @param {Number} port | + * + * @description Helper macro + */ +exports.BuildUrl = function(proto, hostname, port) { + return `${proto}://${hostname}:${port}` }