From 72487019b3f255cece40a8073bbad8bc9968a3b7 Mon Sep 17 00:00:00 2001 From: "Alex Pooley (@zuedev)" Date: Wed, 22 Jul 2026 15:32:57 +0100 Subject: major code refactor, add uex lookup --- 174bg/discord-bot/source/controllers/http.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 174bg/discord-bot/source/controllers/http.js (limited to '174bg/discord-bot/source/controllers/http.js') diff --git a/174bg/discord-bot/source/controllers/http.js b/174bg/discord-bot/source/controllers/http.js new file mode 100644 index 0000000..1472b18 --- /dev/null +++ b/174bg/discord-bot/source/controllers/http.js @@ -0,0 +1,25 @@ +/** + * Controller module for interacting with HTTP resources, mainly for fetching JSON data from external APIs. + */ + +/** + * Fetches data from a specified URL and returns the response as JSON or text. + * @param {string} url - The URL to fetch data from. + * @returns {Promise} A Promise that resolves to the parsed JSON object, text, or null if the request fails. + */ +export async function get(url) { + const response = await fetch(url); + + if (!response.ok) return null; + + // is it json? + const contentType = response.headers.get("content-type"); + if (contentType && contentType.includes("application/json")) { + return await response.json(); + } + + // fallback to text + return await response.text(); +}; + +export default { get }; \ No newline at end of file -- cgit v1.2.3