Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

2.8 KB
Raw Blame
import axios from 'axios'
import { SGDBGame, SGDBGrid } from 'common/types'
import { app } from 'electron'

const SGDB_API_URL = 'https://www.steamgriddb.com/api/v2'

interface SGDBResponse<T> {
  success: boolean
  data: T
  errors?: string[]
}

const userAgent = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeroicGamesLauncher/${app.getVersion()}`

/**
 * Search for a game using autocomplete.
 * @param apiKey SteamGridDB API Key
 * @param query Game name query
 * @returns List of games found
 */
export async function searchGame(
  apiKey: string,
  query: string
): Promise<SGDBGame[]> {
  const response = await axios.get<SGDBResponse<SGDBGame[]>>(
    `${SGDB_API_URL}/search/autocomplete/${encodeURIComponent(query)}`,
    {
      headers: {
        Authorization: `Bearer ${apiKey}`,
        'User-Agent': userAgent
      }
    }
  )

  if (!response.data.success) {
    throw new Error(response.data.errors?.join(', ') || 'Search failed')
  }

  return response.data.data
}

/**
 * Get grids for a specific game.
 * @param apiKey SteamGridDB API Key
 * @param args Request arguments
 * @returns List of grids
 */
export async function getGrids(
  apiKey: string,
  args: {
    gameId: number
    dimensions?: string[]
    styles?: string[]
    nsfw?: string
  }
): Promise<SGDBGrid[]> {
  const params: Record<string, string> = {}
  if (args.dimensions && args.dimensions.length > 0) {
    params.dimensions = args.dimensions.join(',')
  }
  if (args.styles && args.styles.length > 0) {
    params.styles = args.styles.join(',')
  }

  const response = await axios.get<SGDBResponse<SGDBGrid[]>>(
    `${SGDB_API_URL}/grids/game/${args.gameId}`,
    {
      params,
      headers: {
        Authorization: `Bearer ${apiKey}`,
        'User-Agent': userAgent
      }
    }
  )

  if (!response.data.success) {
    throw new Error(response.data.errors?.join(', ') || 'Failed to get grids')
  }

  return response.data.data
}

/**
 * Get heroes (wide hero/cover art) for a specific game.
 */
export async function getHeroes(
  apiKey: string,
  args: {
    gameId: number
    dimensions?: string[]
    styles?: string[]
    nsfw?: string
  }
): Promise<SGDBGrid[]> {
  const params: Record<string, string> = {}
  if (args.dimensions && args.dimensions.length > 0) {
    params.dimensions = args.dimensions.join(',')
  }
  if (args.styles && args.styles.length > 0) {
    params.styles = args.styles.join(',')
  }

  const response = await axios.get<SGDBResponse<SGDBGrid[]>>(
    `${SGDB_API_URL}/heroes/game/${args.gameId}`,
    {
      params,
      headers: {
        Authorization: `Bearer ${apiKey}`,
        'User-Agent': userAgent
      }
    }
  )

  if (!response.data.success) {
    throw new Error(response.data.errors?.join(', ') || 'Failed to get heroes')
  }

  return response.data.data
}