From 0122e9b456423e4bfc50dcefd1f39e6be5a4afa9 Mon Sep 17 00:00:00 2001
From: Cory Dransfeldt <hi@coryd.dev>
Date: Mon, 13 Mar 2023 19:17:50 -0700
Subject: [PATCH] fetch refactoring

---
 src/_data/now.js         | 12 ++++++------
 src/_data/nowplaying.js  | 18 +++++++++---------
 src/_data/status.js      | 12 ++++++------
 src/_data/webmentions.js | 12 ++++++------
 4 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/src/_data/now.js b/src/_data/now.js
index f176dec9..447c087e 100644
--- a/src/_data/now.js
+++ b/src/_data/now.js
@@ -1,12 +1,12 @@
-const EleventyFetch = require('@11ty/eleventy-fetch')
+const fetch = require('node-fetch')
+const { AssetCache } = require('@11ty/eleventy-fetch')
 
 module.exports = async function () {
     const url = 'https://utils.coryd.dev/api/now?endpoints=artists,albums,books,movies,tv'
-    const res = EleventyFetch(url, {
-        duration: '1h',
-        type: 'json',
-    })
-    const now = await res
+    const asset = new AssetCache('now_data')
+    if (asset.isCacheValid('1h')) return await asset.getCachedValue()
+    const now = await fetch(url).then((res) => res.json())
+    await asset.save(now, 'json')
     return {
         artists: now.artists,
         albums: now.albums,
diff --git a/src/_data/nowplaying.js b/src/_data/nowplaying.js
index dffe4ac8..a2df52a4 100644
--- a/src/_data/nowplaying.js
+++ b/src/_data/nowplaying.js
@@ -1,15 +1,15 @@
-const EleventyFetch = require('@11ty/eleventy-fetch')
+const fetch = require('node-fetch')
+const { AssetCache } = require('@11ty/eleventy-fetch')
 
 module.exports = async function () {
     const url = 'https://utils.coryd.dev/api/music?limit=1&period=7day'
-    const res = EleventyFetch(url, {
-        duration: '0s',
-        type: 'json',
-    })
-    const music = await res
+    const asset = new AssetCache('now_playing_data')
+    if (asset.isCacheValid('3m')) return await asset.getCachedValue()
+    const nowPlaying = await fetch(url).then((res) => res.json())
+    await asset.save(nowPlaying, 'json')
     return {
-        artist: music.recenttracks.track[0].artist['#text'],
-        title: music.recenttracks.track[0].name,
-        url: music.recenttracks.track[0].url,
+        artist: nowPlaying.recenttracks.track[0].artist['#text'],
+        title: nowPlaying.recenttracks.track[0].name,
+        url: nowPlaying.recenttracks.track[0].url,
     }
 }
diff --git a/src/_data/status.js b/src/_data/status.js
index 5a520b78..31bf958f 100644
--- a/src/_data/status.js
+++ b/src/_data/status.js
@@ -1,11 +1,11 @@
-const EleventyFetch = require('@11ty/eleventy-fetch')
+const fetch = require('node-fetch')
+const { AssetCache } = require('@11ty/eleventy-fetch')
 
 module.exports = async function () {
     const url = 'https://api.omg.lol/address/cory/statuses/'
-    const res = EleventyFetch(url, {
-        duration: '1d',
-        type: 'json',
-    })
-    const status = await res
+    const asset = new AssetCache('status_data')
+    if (asset.isCacheValid('1h')) return await asset.getCachedValue()
+    const status = await fetch(url).then((res) => res.json())
+    await asset.save(status, 'json')
     return status.response.statuses[0]
 }
diff --git a/src/_data/webmentions.js b/src/_data/webmentions.js
index a5d459d6..aeeb2620 100644
--- a/src/_data/webmentions.js
+++ b/src/_data/webmentions.js
@@ -1,12 +1,12 @@
-const EleventyFetch = require('@11ty/eleventy-fetch')
+const fetch = require('node-fetch')
+const { AssetCache } = require('@11ty/eleventy-fetch')
 
 module.exports = async function () {
     const url = 'https://utils.coryd.dev/api/webmentions'
-    const res = EleventyFetch(url, {
-        duration: '1d',
-        type: 'json',
-    })
-    const webmentions = await res
+    const asset = new AssetCache('webmentions_data')
+    if (asset.isCacheValid('1h')) return await asset.getCachedValue()
+    const webmentions = await fetch(url).then((res) => res.json())
+    await asset.save(webmentions, 'json')
     return {
         mentions: webmentions.children,
     }