diff --git a/constants/api.ts b/constants/api.ts index 8289a31..a235020 100644 --- a/constants/api.ts +++ b/constants/api.ts @@ -13,4 +13,5 @@ export const API_ENDPOINTS = { MATCH_DETAIL: (id: string) => `/v1/api/matches/${id}`, ODDS: "/v1/api/odds", SEARCH: "/v1/api/search", + H2H: "/v1/api/h2h", }; diff --git a/lib/api.ts b/lib/api.ts index 4687368..6a0dcb2 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -3,6 +3,7 @@ import { ApiListResponse, ApiResponse, Country, + H2HData, League, LiveScoreMatch, Match, @@ -255,3 +256,58 @@ export const fetchSearch = async ( throw error; } }; + +// 获取两队/两球员之间的历史对战数据 +export const fetchH2H = async ( + sportId: number, + options: { + firstTeamId?: number; + secondTeamId?: number; + firstPlayerId?: number; + secondPlayerId?: number; + timezone?: string; + } +): Promise => { + try { + const params: { + sport_id: number; + first_team_id?: number; + second_team_id?: number; + first_player_id?: number; + second_player_id?: number; + timezone?: string; + } = { + sport_id: sportId, + }; + + if (options.firstTeamId) { + params.first_team_id = options.firstTeamId; + } + if (options.secondTeamId) { + params.second_team_id = options.secondTeamId; + } + if (options.firstPlayerId) { + params.first_player_id = options.firstPlayerId; + } + if (options.secondPlayerId) { + params.second_player_id = options.secondPlayerId; + } + if (options.timezone) { + params.timezone = options.timezone; + } + + const response = await apiClient.get>( + API_ENDPOINTS.H2H, + { params } + ); + + if (response.data.code === 0) { + return response.data.data; + } + + throw new Error(response.data.message); + } catch (error) { + console.error("Fetch H2H error:", error); + throw error; + } +}; diff --git a/types/api.ts b/types/api.ts index 2edda57..4cd4d17 100644 --- a/types/api.ts +++ b/types/api.ts @@ -401,3 +401,31 @@ export interface SearchResult { players: SearchPlayer[]; teams: SearchTeam[]; } + +// H2H 历史对战比赛项 +export interface H2HMatch { + awayTeamKey: string; + countryName: string; + eventAwayTeam: string; + eventCountryKey: string; + eventDate: string; + eventFinalResult: string; + eventHalftimeResult: string; + eventHomeTeam: string; + eventKey: string; + eventLive: string; + eventStatus: string; + eventTime: string; + homeTeamKey: string; + leagueKey: string; + leagueName: string; + leagueRound: string; + leagueSeason: string; +} + +// H2H 历史对战数据 +export interface H2HData { + H2H: H2HMatch[]; + firstTeamResults: H2HMatch[]; + secondTeamResults: H2HMatch[]; +}