diff --git a/app/api/games/route.ts b/app/api/games/route.ts index 842198fe639044332f6c6fd4c8b96b76c8f5648e..23a5fa56f6f76a8a9309fe3546e756f4afa94dfe 100644 --- a/app/api/games/route.ts +++ b/app/api/games/route.ts @@ -3,6 +3,10 @@ import { NextRequest, NextResponse } from "next/server"; export async function GET(req: NextRequest) { const p = req.nextUrl.searchParams; - const games = await getGames(p.get('page') ? parseInt(p.get('page') as string) : undefined); - return NextResponse.json(games); + try { + const games = await getGames(p.get('page') ? parseInt(p.get('page') as string) : undefined); + return NextResponse.json(games); + } catch (error) { + return NextResponse.json(error, { status: 500 }); + } } \ No newline at end of file diff --git a/lib/igdb.ts b/lib/igdb.ts index 0542905e3034ef01e29c0c547522f1ff5c69e518..b924e9b84847056d12209b540361a517df1f60eb 100644 --- a/lib/igdb.ts +++ b/lib/igdb.ts @@ -28,29 +28,33 @@ async function getToken(): Promise<IAuth> { } // fetches the top 200 games with a rating of 96 or higher -export async function getGames(page = 1): Promise<IGame[]> { +export async function getGames(page = 1): Promise<IGame[] | unknown> { const auth = await getToken() const url = new URL(`${IGDB_BASE_URL}/games`) let offset = calculateOffset(page, limit) - const response = await fetch(url, { - method: 'POST', - headers: { - 'Client-ID': CLIENT_ID, - 'Authorization': `Bearer ${auth.access_token}` - }, - body: `fields name, cover.*; limit ${limit}; offset ${offset}; + try { + const response = await fetch(url, { + method: 'POST', + headers: { + 'Client-ID': CLIENT_ID, + 'Authorization': `Bearer ${auth.access_token}` + }, + body: `fields name, cover.*; limit ${limit}; offset ${offset}; sort total_rating desc; where total_rating_count > 2 & cover != null & total_rating != null & rating != null;` - }) - const games = await response.json() as IGame[] + }) + const games = await response.json() as IGame[] - games.forEach(game => { - game.cover.url = getImageURL(game.cover.image_id, 'cover_big') - }) + games.forEach(game => { + game.cover.url = getImageURL(game.cover.image_id, 'cover_big') + }) - return games + return games + } catch (error) { + console.log(error) + } } // fetches a single game by id