diff options
| author | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2026-07-21 23:02:52 +0100 |
|---|---|---|
| committer | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2026-07-21 23:02:52 +0100 |
| commit | da0509c91376f56f133872eddd2e4282d76f215e (patch) | |
| tree | 7d39fb76ab89855d17fab28ca7e000b3acd9a89d /174bg/discord-bot/controllers/http.js | |
| parent | 764f90577eeadd653f3f6248fbf2b0918c97a3d5 (diff) | |
| download | unnamed-group-da0509c91376f56f133872eddd2e4282d76f215e.tar unnamed-group-da0509c91376f56f133872eddd2e4282d76f215e.tar.gz unnamed-group-da0509c91376f56f133872eddd2e4282d76f215e.tar.bz2 unnamed-group-da0509c91376f56f133872eddd2e4282d76f215e.tar.xz unnamed-group-da0509c91376f56f133872eddd2e4282d76f215e.zip | |
more org changes
Diffstat (limited to '174bg/discord-bot/controllers/http.js')
| -rw-r--r-- | 174bg/discord-bot/controllers/http.js | 25 |
1 files changed, 25 insertions, 0 deletions
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<Object|string|null>} 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 |
