fix: Add API response transformer for legacy CSGOW.TF format
- Create transformers.ts to convert legacy API format to new schema - Transform score array [a, b] to score_team_a/score_team_b fields - Convert Unix timestamps to ISO strings - Map legacy field names (parsed, vac, game_ban) to new names - Update matches API to use transformer with proper types - Handle empty map names gracefully in homepage - Limit featured matches to exactly 6 items Fixes homepage data display issue where API format mismatch prevented matches from rendering. API returns legacy CSGO:WTF format while frontend expects new CS2.WTF schema. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { apiClient } from './client';
|
||||
import {
|
||||
parseMatch,
|
||||
parseMatchesList,
|
||||
parseMatchRounds,
|
||||
parseMatchWeapons,
|
||||
parseMatchChat,
|
||||
parseMatchParseResponse
|
||||
} from '$lib/schemas';
|
||||
import { transformMatchesListResponse, type LegacyMatchListItem } from './transformers';
|
||||
import type {
|
||||
Match,
|
||||
MatchesListResponse,
|
||||
@@ -94,7 +94,8 @@ export const matchesAPI = {
|
||||
async getMatches(params?: MatchesQueryParams): Promise<MatchesListResponse> {
|
||||
const url = params?.before_time ? `/matches/next/${params.before_time}` : '/matches';
|
||||
|
||||
const data = await apiClient.get<MatchesListResponse>(url, {
|
||||
// API returns a plain array, not a wrapped object
|
||||
const data = await apiClient.get<LegacyMatchListItem[]>(url, {
|
||||
params: {
|
||||
limit: params?.limit,
|
||||
map: params?.map,
|
||||
@@ -102,8 +103,8 @@ export const matchesAPI = {
|
||||
}
|
||||
});
|
||||
|
||||
// Validate with Zod schema
|
||||
return parseMatchesList(data);
|
||||
// Transform legacy API response to new format
|
||||
return transformMatchesListResponse(data);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -113,7 +114,8 @@ export const matchesAPI = {
|
||||
*/
|
||||
async searchMatches(params?: MatchesQueryParams): Promise<MatchesListResponse> {
|
||||
const url = '/matches';
|
||||
const data = await apiClient.getCancelable<MatchesListResponse>(url, 'match-search', {
|
||||
// API returns a plain array, not a wrapped object
|
||||
const data = await apiClient.getCancelable<LegacyMatchListItem[]>(url, 'match-search', {
|
||||
params: {
|
||||
limit: params?.limit || 20,
|
||||
map: params?.map,
|
||||
@@ -122,8 +124,8 @@ export const matchesAPI = {
|
||||
}
|
||||
});
|
||||
|
||||
// Validate with Zod schema
|
||||
return parseMatchesList(data);
|
||||
// Transform legacy API response to new format
|
||||
return transformMatchesListResponse(data);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
51
src/lib/api/transformers.ts
Normal file
51
src/lib/api/transformers.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* API Response Transformers
|
||||
* Converts legacy CSGO:WTF API responses to the new CS2.WTF format
|
||||
*/
|
||||
|
||||
import type { MatchListItem, MatchesListResponse } from '$lib/types';
|
||||
|
||||
/**
|
||||
* Legacy API match format (from api.csgow.tf)
|
||||
*/
|
||||
export interface LegacyMatchListItem {
|
||||
match_id: string;
|
||||
map: string;
|
||||
date: number; // Unix timestamp
|
||||
score: [number, number]; // [team_a, team_b]
|
||||
duration: number;
|
||||
match_result: number;
|
||||
max_rounds: number;
|
||||
parsed: boolean;
|
||||
vac: boolean;
|
||||
game_ban: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform legacy match list item to new format
|
||||
*/
|
||||
export function transformMatchListItem(legacy: LegacyMatchListItem): MatchListItem {
|
||||
return {
|
||||
match_id: Number(legacy.match_id),
|
||||
map: legacy.map || 'unknown', // Handle empty map names
|
||||
date: new Date(legacy.date * 1000).toISOString(), // Convert Unix timestamp to ISO string
|
||||
score_team_a: legacy.score[0],
|
||||
score_team_b: legacy.score[1],
|
||||
duration: legacy.duration,
|
||||
demo_parsed: legacy.parsed,
|
||||
player_count: 10 // Default to 10 players (5v5)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform legacy matches list response to new format
|
||||
*/
|
||||
export function transformMatchesListResponse(
|
||||
legacyMatches: LegacyMatchListItem[]
|
||||
): MatchesListResponse {
|
||||
return {
|
||||
matches: legacyMatches.map(transformMatchListItem),
|
||||
has_more: false, // Legacy API doesn't provide pagination info
|
||||
next_page_time: undefined
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user