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:
@@ -1,8 +1,8 @@
|
|||||||
import { apiClient } from './client';
|
import { apiClient } from './client';
|
||||||
import {
|
import {
|
||||||
parseMatchRoundsSafe,
|
parseMatchRoundsSafe,
|
||||||
parseMatchWeapons,
|
parseMatchWeaponsSafe,
|
||||||
parseMatchChat,
|
parseMatchChatSafe,
|
||||||
parseMatchParseResponse
|
parseMatchParseResponse
|
||||||
} from '$lib/schemas';
|
} from '$lib/schemas';
|
||||||
import {
|
import {
|
||||||
@@ -56,13 +56,22 @@ export const matchesAPI = {
|
|||||||
* Get match weapons statistics
|
* Get match weapons statistics
|
||||||
* @param matchId - Match ID
|
* @param matchId - Match ID
|
||||||
* @returns Weapon statistics for all players
|
* @returns Weapon statistics for all players
|
||||||
|
* @throws Error if data is invalid or demo not parsed yet
|
||||||
*/
|
*/
|
||||||
async getMatchWeapons(matchId: string | number): Promise<MatchWeaponsResponse> {
|
async getMatchWeapons(matchId: string | number): Promise<MatchWeaponsResponse> {
|
||||||
const url = `/match/${matchId}/weapons`;
|
const url = `/match/${matchId}/weapons`;
|
||||||
const data = await apiClient.get<MatchWeaponsResponse>(url);
|
const data = await apiClient.get<unknown>(url);
|
||||||
|
|
||||||
// Validate with Zod schema
|
// Validate with Zod schema using safe parse
|
||||||
return parseMatchWeapons(data);
|
// 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
|
* Get match chat messages
|
||||||
* @param matchId - Match ID
|
* @param matchId - Match ID
|
||||||
* @returns Chat messages from the match
|
* @returns Chat messages from the match
|
||||||
|
* @throws Error if data is invalid or demo not parsed yet
|
||||||
*/
|
*/
|
||||||
async getMatchChat(matchId: string | number): Promise<MatchChatResponse> {
|
async getMatchChat(matchId: string | number): Promise<MatchChatResponse> {
|
||||||
const url = `/match/${matchId}/chat`;
|
const url = `/match/${matchId}/chat`;
|
||||||
const data = await apiClient.get<MatchChatResponse>(url);
|
const data = await apiClient.get<unknown>(url);
|
||||||
|
|
||||||
// Validate with Zod schema
|
// Validate with Zod schema using safe parse
|
||||||
return parseMatchChat(data);
|
// 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;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user