Skip to content
Snippets Groups Projects
igdb.ts 3.37 KiB
Newer Older
Yusuf Akgül's avatar
Yusuf Akgül committed
import { env } from "@/env.mjs"
import { calculateOffset, getImageURL } from "@/lib/utils"
import { IAuth, IGame } from "@/types/igdb-types"
Yusuf Akgül's avatar
Yusuf Akgül committed
const TWITCH_AUTH_BASE_URL = env.TWITCH_AUTH_BASE_URL ?? ''
const IGDB_BASE_URL = env.IGDB_BASE_URL ?? ''
Yusuf Akgül's avatar
Yusuf Akgül committed
const CLIENT_ID = env.TWITCH_CLIENT_ID ?? ''
const CLIENT_SECRET = env.TWITCH_CLIENT_SECRET ?? ''
let _auth: IAuth
let _lastUpdate = 0

// fetches a new token if the current one is expired
async function getToken(): Promise<IAuth> {
    if (!_auth || Date.now() - _lastUpdate > _auth.expires_in) {
        const url = new URL(`${TWITCH_AUTH_BASE_URL}/token`)
        url.searchParams.set('client_id', CLIENT_ID)
        url.searchParams.set('client_secret', CLIENT_SECRET)
        url.searchParams.set('grant_type', 'client_credentials')

        const response = await fetch(url, { method: 'POST' })
        _auth = await response.json() as IAuth
        _lastUpdate = Date.now()
    }
    return _auth
}

// fetches the top 200 games with a rating of 96 or higher
Yusuf Akgül's avatar
Yusuf Akgül committed
export async function getGames(page = 1, search?: string, category?: number, genre?: number, platform?: number[], sortby = "total_rating_count", order = "desc"): Promise<IGame[]> {
    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.image_id; limit ${limit}; offset ${offset};
Yusuf Akgül's avatar
Yusuf Akgül committed
            sort ${sortby} ${order};
            where total_rating_count > 3
            & cover != null & total_rating != null & rating != null & age_ratings != null
            & name ~ *"${search ? search : ""}"*
            ${category ? `& category = ${category}` : ""}
            ${genre ? `& genres = [${genre}]` : ""}
            ${platform ? `& platforms = (${platform.join(',')})` : ""}
            ;`
    if (!response.ok) {
        throw new Error(`Error fetching games: ${response.statusText}`);
    }
    const games: IGame[] = await response.json() as IGame[];
    games.forEach(game => {
        game.cover.url = getImageURL(game.cover.image_id, 'cover_big');
    });
    return games;
}

// fetches a single game by id
export async function getGame(id: number): Promise<IGame[]> {
    const auth = await getToken()
    const url = new URL(`${IGDB_BASE_URL}/games`)

    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Client-ID': CLIENT_ID,
            'Authorization': `Bearer ${auth.access_token}`
        },
Yusuf Akgül's avatar
Yusuf Akgül committed
            `fields *, cover.image_id, screenshots.image_id, involved_companies.company.name, genres.name, platforms.name;
            limit 1; where total_rating_count > 3
            & cover != null & total_rating != null & rating != null & age_ratings != null
            & id = ${id};`

    if (!response.ok) {
        throw new Error(`Error fetching game: ${response.statusText}`);
    }

    const games = await response.json() as IGame[]

    games.forEach(game => {
        game.cover.url = getImageURL(game.cover.image_id, 'cover_big');

        game.screenshots.forEach(screenshot => {
            screenshot.url = getImageURL(screenshot.image_id, 'screenshot_big');
        });
    });