fix: retry loading user if 429 returned
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user