42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { ChessComGame } from "@/types/chessCom";
|
|
import { getPaddedMonth } from "./helpers";
|
|
|
|
export const getUserRecentGames = async (
|
|
username: string
|
|
): Promise<ChessComGame[]> => {
|
|
const date = new Date();
|
|
const year = date.getUTCFullYear();
|
|
const month = date.getUTCMonth() + 1;
|
|
const paddedMonth = getPaddedMonth(month);
|
|
|
|
const res = await fetch(
|
|
`https://api.chess.com/pub/player/${username}/games/${year}/${paddedMonth}`
|
|
);
|
|
|
|
if (res.status === 404) return [];
|
|
|
|
const data = await res.json();
|
|
|
|
const games: ChessComGame[] = data?.games ?? [];
|
|
|
|
if (games.length < 50) {
|
|
const previousMonth = month === 1 ? 12 : month - 1;
|
|
const previousPaddedMonth = getPaddedMonth(previousMonth);
|
|
const yearToFetch = previousMonth === 12 ? year - 1 : year;
|
|
|
|
const resPreviousMonth = await fetch(
|
|
`https://api.chess.com/pub/player/${username}/games/${yearToFetch}/${previousPaddedMonth}`
|
|
);
|
|
|
|
const dataPreviousMonth = await resPreviousMonth.json();
|
|
|
|
games.push(...(dataPreviousMonth?.games ?? []));
|
|
}
|
|
|
|
const gamesToReturn = games.slice(0, 50).sort((a, b) => {
|
|
return b.end_time - a.end_time;
|
|
});
|
|
|
|
return gamesToReturn;
|
|
};
|