diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c8e519..f8acee9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.0.15 + +* DDB Config fetch + # 0.0.14 * `/ping` endpoint diff --git a/cache.js b/cache.js index 4bde6d2..025daea 100644 --- a/cache.js +++ b/cache.js @@ -17,7 +17,10 @@ class Cache { } add(id, data) { - if (!data || !data.length) return null; + const isArray = Array.isArray(data); + const isString = typeof data === "string" || data instanceof String; + const isObject = typeof data === "object"; + if (!data || ((isArray || isString) && !data.length) || (!isArray && !isString && !isObject)) return null; console.log(`[CACHE ${this.name}] Adding to the Cache (ID: ${id}): ${data.length} items.`); const index = this.items.find(cache => cache.id === id); diff --git a/index.js b/index.js index dbe3686..cff291e 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const CONFIG = require("./config.js"); const authentication = require("./auth.js"); const filterModifiers = require("./filterModifiers.js"); +const lookup = require("./lookup.js"); const spells = require("./spells.js"); const character = require("./character.js"); @@ -33,6 +34,27 @@ app.post(authPath, cors(), express.json(), (req, res) => { }); }); +const configLookupCall= "/proxy/api/config/json"; +app.options(configLookupCall, cors(), (req, res) => res.status(200).send()); +app.get(configLookupCall, cors(), express.json(), (req, res) => { + + lookup + .getConfig() + .then((data) => { + return res + .status(200) + .json({ success: true, message: "Config retrieved.", data: data }); + }) + .catch((error) => { + console.log(error); + if (error === "Forbidden") { + return res.json({ success: false, message: "Forbidden." }); + } + return res.json({ success: false, message: "Unknown error during config loading: " + error }); + }); + +}); + /** * Returns raw json from DDB */ diff --git a/lookup.js b/lookup.js new file mode 100644 index 0000000..0ae9d9a --- /dev/null +++ b/lookup.js @@ -0,0 +1,53 @@ +const fetch = require("node-fetch"); +const CONFIG = require("./config.js"); +const Cache = require("./cache.js"); + +var CACHE_CONFIG = new Cache("CONFIG", 1); + +const getConfig= () => { + return new Promise((resolve, reject) => { + console.log("Retrieving ddb config"); + + const cache = CACHE_CONFIG.exists("DDB_CONFIG"); + console.warn(cache); + if (cache !== undefined) { + console.log("CONFIG API CACHE_CONFIG HIT!"); + return resolve(cache.data); + } + + const url = CONFIG.urls.configUrl; + const options = { + credentials: "include", + headers: { + "User-Agent": "Foundry VTT Character Integrator", + "Accept": "*/*", + }, + method: "GET", + mode: "cors", + redirect: "follow", + }; + + fetch(url, options) + .then(res => res.json()) + .then(json => { + if (json && json.sources) { + CACHE_CONFIG.add("DDB_CONFIG", json); + console.log( + "Adding CACHE_CONFIG to cache..." + ); + resolve(json); + } else { + console.log("Received no valid config data, instead:" + json); + reject(json); + } + }) + .catch(error => { + console.log("Error retrieving DDB Config"); + console.log(error); + reject(error); + }); + + }); +}; + +exports.getConfig = getConfig;