Cached file will be used if it is not older than 1 hour

This commit is contained in:
Alex 2023-02-26 21:06:38 -06:00
parent 5d2380c166
commit f1938cb22f

View File

@ -10,6 +10,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
console.error('BMC_TOKEN or BMC_WEBHOOK_SECRET is not defined')
throw new Error('BMC_TOKEN or BMC_WEBHOOK_SECRET is not defined')
}
const cacheFilePath = 'supporters.json'
// Check if the response has been cached and is not older than 1 hour
if (fs.existsSync(cacheFilePath) && cacheFilePath && Date.now() - fs.statSync(cacheFilePath).mtimeMs < 3600000) {
console.log('Returning cached supporters JSON data.')
const supportersJson = fs.readFileSync(cacheFilePath, 'utf8')
res.send(JSON.parse(supportersJson) as any)
return
}
fetch(`https://developers.buymeacoffee.com/api/v1/supporters/?slug=sudoalex`, {
headers: {
Authorization: `Bearer ${TOKEN}`,