Skip to content

Commit

Permalink
DDB Config lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPrimate committed Apr 19, 2022
1 parent 7a0b5b4 commit 784d339
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.0.15

* DDB Config fetch

# 0.0.14

* `/ping` endpoint
Expand Down
5 changes: 4 additions & 1 deletion cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
*/
Expand Down
53 changes: 53 additions & 0 deletions lookup.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 784d339

Please sign in to comment.