fix: Add safe parsing for weapons and chat API endpoints

- Use parseMatchWeaponsSafe instead of parseMatchWeapons
- Use parseMatchChatSafe instead of parseMatchChat
- Throw clear error message when demo not parsed yet
- Prevents Zod validation errors from reaching page loaders
- Matches the pattern already used for rounds endpoint

This fixes Zod errors when navigating to match detail pages where
the demo hasn't been fully parsed yet by the backend.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-12 23:14:19 +01:00
parent 05e6182bcf
commit eb68c5d00b

View File

@@ -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<MatchWeaponsResponse> {
const url = `/match/${matchId}/weapons`;
const data = await apiClient.get<MatchWeaponsResponse>(url);
const data = await apiClient.get<unknown>(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<MatchChatResponse> {
const url = `/match/${matchId}/chat`;
const data = await apiClient.get<MatchChatResponse>(url);
const data = await apiClient.get<unknown>(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;
},
/**