diff --git a/src/lib/api/matches.ts b/src/lib/api/matches.ts index e42d932..3f99c1f 100644 --- a/src/lib/api/matches.ts +++ b/src/lib/api/matches.ts @@ -1,8 +1,8 @@ import { apiClient } from './client'; import { parseMatchRoundsSafe, - parseMatchWeapons, - parseMatchChat, + parseMatchWeaponsSafe, + parseMatchChatSafe, parseMatchParseResponse } from '$lib/schemas'; import { @@ -56,13 +56,22 @@ export const matchesAPI = { * Get match weapons statistics * @param matchId - Match ID * @returns Weapon statistics for all players + * @throws Error if data is invalid or demo not parsed yet */ async getMatchWeapons(matchId: string | number): Promise { const url = `/match/${matchId}/weapons`; - const data = await apiClient.get(url); + const data = await apiClient.get(url); - // Validate with Zod schema - return parseMatchWeapons(data); + // Validate with Zod schema using safe parse + // This handles cases where the demo hasn't been parsed yet + const result = parseMatchWeaponsSafe(data); + + if (!result.success) { + // If validation fails, it's likely the demo hasn't been parsed yet + throw new Error('Demo not parsed yet or invalid response format'); + } + + return result.data; }, /** @@ -91,13 +100,22 @@ export const matchesAPI = { * Get match chat messages * @param matchId - Match ID * @returns Chat messages from the match + * @throws Error if data is invalid or demo not parsed yet */ async getMatchChat(matchId: string | number): Promise { const url = `/match/${matchId}/chat`; - const data = await apiClient.get(url); + const data = await apiClient.get(url); - // Validate with Zod schema - return parseMatchChat(data); + // Validate with Zod schema using safe parse + // This handles cases where the demo hasn't been parsed yet + const result = parseMatchChatSafe(data); + + if (!result.success) { + // If validation fails, it's likely the demo hasn't been parsed yet + throw new Error('Demo not parsed yet or invalid response format'); + } + + return result.data; }, /**