coryd.dev/src/data/feeds.js

56 lines
1.3 KiB
JavaScript

import EleventyFetch from "@11ty/eleventy-fetch";
const { POSTGREST_URL, POSTGREST_API_KEY } = process.env;
const fetchFeeds = async () => {
try {
return await EleventyFetch(`${POSTGREST_URL}/optimized_feeds?select=*`, {
duration: "1h",
type: "json",
fetchOptions: {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${POSTGREST_API_KEY}`,
},
},
});
} catch (error) {
console.error("Error fetching feed metadata:", error);
return [];
}
};
const fetchFeedData = async (feedKey) => {
try {
return await EleventyFetch(`${POSTGREST_URL}/rpc/get_feed_data`, {
duration: "1h",
type: "json",
fetchOptions: {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${POSTGREST_API_KEY}`,
},
body: JSON.stringify({ feed_key: feedKey }),
},
});
} catch (error) {
console.error(`Error fetching feed data for ${feedKey}:`, error);
return [];
}
};
export default async function () {
const feeds = await fetchFeeds();
const feedsWithData = [];
for (const feed of feeds) {
feedsWithData.push({
...feed,
entries: await fetchFeedData(feed.data),
});
}
return feedsWithData;
};