From da0509c91376f56f133872eddd2e4282d76f215e Mon Sep 17 00:00:00 2001 From: "Alex Pooley (@zuedev)" Date: Tue, 21 Jul 2026 23:02:52 +0100 Subject: more org changes --- 174bg/discord-bot/controllers/http.js | 25 +++++++++++++++++++++++++ 174bg/discord-bot/controllers/pocketbase.js | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 174bg/discord-bot/controllers/http.js (limited to '174bg/discord-bot/controllers') diff --git a/174bg/discord-bot/controllers/http.js b/174bg/discord-bot/controllers/http.js new file mode 100644 index 0000000..816b73c --- /dev/null +++ b/174bg/discord-bot/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(); +}; + +return { get }; \ No newline at end of file diff --git a/174bg/discord-bot/controllers/pocketbase.js b/174bg/discord-bot/controllers/pocketbase.js index b808ce4..4046391 100644 --- a/174bg/discord-bot/controllers/pocketbase.js +++ b/174bg/discord-bot/controllers/pocketbase.js @@ -1,17 +1,35 @@ +/** + * Controller module for interacting with the PocketBase API. + */ + import PocketBase from "pocketbase"; const { POCKETBASE_URL, POCKETBASE_SUPERUSER_EMAIL, POCKETBASE_SUPERUSER_PASSWORD } = process.env; +/** + * Logs in to the PocketBase API using the superuser credentials. + * @returns {Promise} A Promise that resolves to the authenticated PocketBase instance. + */ export async function login() { const pb = new PocketBase(POCKETBASE_URL); await pb.collection("_superusers").authWithPassword(POCKETBASE_SUPERUSER_EMAIL, POCKETBASE_SUPERUSER_PASSWORD); return pb; } +/** + * Logs out of the PocketBase API by clearing the authentication store. + * @param {PocketBase} pb - The PocketBase instance to log out from. + * @returns {Promise} A Promise that resolves when the logout is complete. + */ export async function logout(pb) { await pb.authStore.clear(); } +/** + * Retrieves the full list of records from a specified collection in the PocketBase API. + * @param {string} collectionName - The name of the collection to retrieve records from. + * @returns {Promise} A Promise that resolves to an array of records from the specified collection. + */ export async function getFullList(collectionName) { const pb = await login(); const data = await pb.collection(collectionName).getFullList(); -- cgit v1.2.3