分页
This commit is contained in:
84
lib/api.ts
84
lib/api.ts
@@ -115,22 +115,22 @@ export const fetchCountries = async (): Promise<Country[]> => {
|
||||
}
|
||||
};
|
||||
|
||||
export const fetchLeagues = async (
|
||||
sportId: number,
|
||||
countryKey: string
|
||||
): Promise<League[]> => {
|
||||
export const fetchLeagues = async (params: {
|
||||
sportId?: number;
|
||||
countryKey?: string;
|
||||
date?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
sortBy?: "name" | "matchCount" | "createdAt";
|
||||
sortOrder?: "asc" | "desc";
|
||||
}): Promise<ApiListResponse<League>> => {
|
||||
try {
|
||||
const response = await apiClient.get<ApiResponse<ApiListResponse<League>>>(
|
||||
API_ENDPOINTS.LEAGUES,
|
||||
{
|
||||
params: {
|
||||
sportId,
|
||||
countryKey,
|
||||
},
|
||||
}
|
||||
{ params }
|
||||
);
|
||||
if (response.data.code === 0) {
|
||||
return response.data.data.list;
|
||||
return response.data.data;
|
||||
}
|
||||
throw new Error(response.data.message);
|
||||
} catch (error) {
|
||||
@@ -139,48 +139,54 @@ export const fetchLeagues = async (
|
||||
}
|
||||
};
|
||||
|
||||
export const fetchTodayMatches = async (
|
||||
sportId: number,
|
||||
date?: Date | string,
|
||||
timezone?: string
|
||||
): Promise<Match[]> => {
|
||||
export const fetchTodayMatches = async (options: {
|
||||
sportId: number;
|
||||
date?: Date | string;
|
||||
timezone?: string;
|
||||
leagueKey?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}): Promise<ApiListResponse<Match>> => {
|
||||
try {
|
||||
const params: { sportId: number; date?: string; timezone?: string } = {
|
||||
sportId,
|
||||
const params: {
|
||||
sportId: number;
|
||||
date?: string;
|
||||
timezone?: string;
|
||||
leagueKey?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
} = {
|
||||
sportId: options.sportId,
|
||||
leagueKey: options.leagueKey,
|
||||
page: options.page,
|
||||
pageSize: options.pageSize,
|
||||
};
|
||||
|
||||
// 如果提供了日期,格式化为 YYYY-MM-DD 格式
|
||||
if (date) {
|
||||
let dateStr: string;
|
||||
if (date instanceof Date) {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(date.getDate()).padStart(2, "0");
|
||||
dateStr = `${year}-${month}-${day}`;
|
||||
if (options.date) {
|
||||
if (options.date instanceof Date) {
|
||||
const year = options.date.getFullYear();
|
||||
const month = String(options.date.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(options.date.getDate()).padStart(2, "0");
|
||||
params.date = `${year}-${month}-${day}`;
|
||||
} else {
|
||||
dateStr = date;
|
||||
params.date = options.date;
|
||||
}
|
||||
params.date = dateStr;
|
||||
}
|
||||
|
||||
// 如果提供了时区,传给后端;不传则由后端使用本地时区
|
||||
if (timezone) {
|
||||
params.timezone = timezone;
|
||||
if (options.timezone) {
|
||||
params.timezone = options.timezone;
|
||||
}
|
||||
|
||||
const response = await apiClient.get<ApiResponse<ApiListResponse<Match>>>(
|
||||
API_ENDPOINTS.MATCHES_TODAY,
|
||||
{
|
||||
params,
|
||||
}
|
||||
{ params }
|
||||
);
|
||||
if (response.data.code === 0) {
|
||||
return response.data.data.list;
|
||||
return response.data.data;
|
||||
}
|
||||
throw new Error(response.data.message);
|
||||
} catch (error) {
|
||||
console.error("Fetch matches error:", error);
|
||||
// Let the caller handle errors; rethrow the original error
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
@@ -214,9 +220,9 @@ export const fetchLiveScore = async (
|
||||
// });
|
||||
try {
|
||||
const params: { sport_id: number; league_id?: number; timezone?: string } =
|
||||
{
|
||||
sport_id: sportId,
|
||||
};
|
||||
{
|
||||
sport_id: sportId,
|
||||
};
|
||||
|
||||
if (leagueId) {
|
||||
params.league_id = leagueId;
|
||||
|
||||
Reference in New Issue
Block a user