Files
magikarp-collection/src/lib/api.ts

31 lines
857 B
TypeScript

import axios from 'axios';
import type { CardsResponse } from '@/types/pokemon';
const BASE = 'https://api.pokemontcg.io/v2';
export type FetchCardsParams = {
q?: string;
page?: number;
pageSize?: number;
};
export async function fetchMagikarpCards(params: FetchCardsParams = {}): Promise<CardsResponse> {
const { q, page = 1, pageSize = 50 } = params;
// Keep query minimal to avoid parser errors on API side
const parts = [
'name:magikarp',
];
if (q) parts.push(q);
const url = `${BASE}/cards`;
const headers: Record<string, string> = {};
const apiKey = process.env.POKEMON_TCG_API_KEY || process.env.NEXT_PUBLIC_POKEMON_TCG_API_KEY;
if (apiKey) headers['X-Api-Key'] = apiKey;
const { data } = await axios.get<CardsResponse>(url, {
params: { q: parts.join(' '), page, pageSize },
headers,
});
return data;
}