feat: improve type definitions and refactor API responses for better type safety
This commit is contained in:
@@ -4,17 +4,9 @@
|
||||
* - `getGameById(id)`
|
||||
*/
|
||||
import { fetch } from 'undici';
|
||||
import type { MetadataGame, RawgGameResponse, RawgSearchResponse, PlatformInfo } from '../types';
|
||||
|
||||
export type MetadataGame = {
|
||||
id?: number;
|
||||
name: string;
|
||||
slug?: string;
|
||||
releaseDate?: string;
|
||||
genres?: string[];
|
||||
platforms?: any[];
|
||||
coverUrl?: string;
|
||||
source?: string;
|
||||
};
|
||||
export type { MetadataGame };
|
||||
|
||||
const API_BASE = 'https://api.rawg.io/api';
|
||||
|
||||
@@ -28,18 +20,32 @@ export async function searchGames(query: string): Promise<MetadataGame[]> {
|
||||
)}&page_size=10`;
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) return [];
|
||||
const json = await res.json();
|
||||
const hits = Array.isArray(json.results) ? json.results : [];
|
||||
return hits.map((r: any) => ({
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
slug: r.slug,
|
||||
releaseDate: r.released,
|
||||
genres: Array.isArray(r.genres) ? r.genres.map((g: any) => g.name) : undefined,
|
||||
platforms: r.platforms,
|
||||
coverUrl: r.background_image ?? undefined,
|
||||
source: 'rawg',
|
||||
}));
|
||||
const json: RawgSearchResponse = (await res.json()) as RawgSearchResponse;
|
||||
const hits: RawgGameResponse[] = Array.isArray(json.results) ? json.results : [];
|
||||
return hits.map((r) => {
|
||||
const platforms: PlatformInfo[] | undefined = Array.isArray(r.platforms)
|
||||
? r.platforms.map((p) => ({
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
slug: p.name?.toLowerCase().replace(/\s+/g, '-'),
|
||||
}))
|
||||
: undefined;
|
||||
|
||||
const genres: string[] | undefined = Array.isArray(r.genres)
|
||||
? r.genres.map((g) => g.name)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
slug: r.slug,
|
||||
releaseDate: r.released,
|
||||
genres,
|
||||
platforms,
|
||||
coverUrl: r.background_image ?? undefined,
|
||||
source: 'rawg',
|
||||
};
|
||||
});
|
||||
} catch (err) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.debug('rawgClient.searchGames error', err);
|
||||
@@ -57,14 +63,27 @@ export async function getGameById(id: number): Promise<MetadataGame | null> {
|
||||
)}`;
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) return null;
|
||||
const json = await res.json();
|
||||
const json: RawgGameResponse = (await res.json()) as RawgGameResponse;
|
||||
if (!json) return null;
|
||||
const platforms: PlatformInfo[] | undefined = Array.isArray(json.platforms)
|
||||
? json.platforms.map((p) => ({
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
slug: p.name?.toLowerCase().replace(/\s+/g, '-'),
|
||||
}))
|
||||
: undefined;
|
||||
|
||||
const genres: string[] | undefined = Array.isArray(json.genres)
|
||||
? json.genres.map((g) => g.name)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
id: json.id,
|
||||
name: json.name,
|
||||
slug: json.slug,
|
||||
releaseDate: json.released,
|
||||
genres: Array.isArray(json.genres) ? json.genres.map((g: any) => g.name) : undefined,
|
||||
genres,
|
||||
platforms,
|
||||
coverUrl: json.background_image ?? undefined,
|
||||
source: 'rawg',
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user