Got it! Error coded in in case no numbers are at the end of the URL (so e.g. the error message is for if this URL comes through):
https://www.juicer.io/api/feeds/320738?filter=
Last thing I need to do is somehow sort my sourceData array to match the order of my filters in my function call. Working on that now:
requestPostData(endpoint, feedOptions, callback) {
var filters = Object.entries(this.filters);
var matches = endpoint.match(/filter=([\d,]+)/);
var nums = matches == null ? [] : matches[1].split(",").filter((x) => x);
var output = {};
var sourceData = {};
var sourceNumbers = Object.entries(endpoint.split("?filter=").pop().split(","));
var urls = sourceNumbers.map(filter => endpoint.split("?")[0] + "?filter=" + filter[1]);
if(sourceNumbers[0][1] === "") {
console.error("Error: Please select sources in the element");
return;
}
Promise.all(urls.map(url =>
fetch(url).then(resp => resp.json())
)).then(values => {
values.forEach(function(value, index) {
if(value.posts.items[0].source.source.toLowerCase() in sourceData) {
sourceData[value.posts.items[0].source.source.toLowerCase()] = sourceData[value.posts.items[0].source.source.toLowerCase()] + "," + urls[index].split("?filter=")[1];
} else {
sourceData[value.posts.items[0].source.source.toLowerCase()] = endpoint.split("?filter")[0] + "?filter=" + urls[index].split("?filter=")[1];
}
});
filters.forEach((x) => {
if(sourceData[x[0]] === undefined) {
sourceData[x[0]] = endpoint.split("?filter")[0] + "?filter="+x[0] + "&per=" + x[1] + "&page=" + this.options.feed.page;
} else {
sourceData[x[0]] = sourceData[x[0]] + "&per=" + x[1] + "&page=" + this.options.feed.page;
}
});
console.log(Object.values(sourceData));
Promise.all(Object.values(sourceData).map(url =>
fetch(url).then(resp => resp.json())
)).then(callback);
});
}
Codepen above has been updated to show it working.