Implement GiantBomb API with caching

This commit is contained in:
Justin Edmund 2024-08-05 23:38:52 -07:00
parent ac837fb750
commit 13cce1a6c4

View file

@ -0,0 +1,49 @@
import { promisify } from 'util'
import redis from '../redis-client'
import GiantBombAPI from 'giantbombing-api'
import type { RequestHandler } from './$types'
const CACHE_TTL = 60 * 60 * 24 * 7
const giantBombAPI = new GiantBombAPI({
apiKey: process.env.GIANTBOMB_API_KEY,
userAgent: 'jedmund.com/1.0'
})
const getSearchAsync = promisify(giantBombAPI.getSearch.bind(giantBombAPI))
export const POST: RequestHandler = async ({ request }) => {
const { games } = await request.json()
const results = await Promise.all(games.map(getCachedOrFetchGame))
return new Response(JSON.stringify(results))
}
async function getCachedOrFetchGame(game: SerializableGameInfo): Promise<SerializableGameInfo> {
const cachedData = await redis.get(`giantbomb:${game.name}`)
if (cachedData) {
console.log(`Using cached data for Giant Bomb data about ${game.name}`)
return JSON.parse(cachedData)
}
const options = {
query: game.name,
resources: 'game',
resource_type: 'game',
limit: 1
}
try {
const response = await getSearchAsync(options)
const revisedGame = {
...game,
coverURL: response.results[0].image.medium_url
}
await redis.setex(`giantbomb:${game.name}`, CACHE_TTL, JSON.stringify(revisedGame))
return revisedGame
} catch (error) {
console.error(`Error fetching data for ${game.name}:`, error)
return game // Return original game if fetch fails
}
}