fix: retry loading user if 429 returned

This commit is contained in:
2024-09-26 04:30:45 -04:00
parent bee8acaca8
commit e4ca45dbdc
2 changed files with 69 additions and 20 deletions

View File

@@ -1,6 +1,8 @@
import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import { sleep } from "../sleep";
const GetNextEpisodeAiringAtQuery = graphql(`
query GetNextEpisodeAiringAt($id: Int!) {
Media(id: $id) {
@@ -13,15 +15,39 @@ const GetNextEpisodeAiringAtQuery = graphql(`
}
`);
export function getNextEpisodeTimeUntilAiring(aniListId: number) {
type NextAiringTime = {
status:
| "FINISHED"
| "RELEASING"
| "NOT_YET_RELEASED"
| "CANCELLED"
| "HIATUS"
| null;
nextAiring: {
episode: number;
airingAt: number;
} | null;
};
export async function getNextEpisodeTimeUntilAiring(
aniListId: number,
): Promise<NextAiringTime> {
const client = new GraphQLClient("https://graphql.anilist.co/");
return client
.request(GetNextEpisodeAiringAtQuery, { id: aniListId })
.then((data) => {
const status = data!.Media!.status;
const nextAiring = data!.Media!.nextAiringEpisode;
try {
const { status, nextAiringEpisode: nextAiring } = await client
.request(GetNextEpisodeAiringAtQuery, {
id: aniListId,
})
.then((data) => data!.Media!);
return { status, nextAiring };
});
return { status, nextAiring };
} catch (error) {
if (error.response.status === 429) {
await sleep(Number(error.response.headers.get("Retry-After")!) * 1000);
return getNextEpisodeTimeUntilAiring(aniListId);
}
throw error;
}
}