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 { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request"; import { GraphQLClient } from "graphql-request";
import { sleep } from "~/libs/sleep";
const GetUserQuery = graphql(` const GetUserQuery = graphql(`
query GetUser { query GetUser {
Viewer { Viewer {
@@ -21,22 +23,43 @@ const GetUserQuery = graphql(`
} }
`); `);
export function getUser(aniListToken: string) { type User = {
statistics: {
minutesWatched?: number | undefined;
episodesWatched?: number | undefined;
count?: number | undefined;
meanScore?: number | undefined;
};
name?: string | undefined;
avatar?:
| {
medium: string | null;
large: string | null;
}
| null
| undefined;
} | null;
export async function getUser(aniListToken: string): Promise<User> {
const client = new GraphQLClient("https://graphql.anilist.co/"); const client = new GraphQLClient("https://graphql.anilist.co/");
return client try {
.request(GetUserQuery, undefined, { const data = await client.request(GetUserQuery, undefined, {
Authorization: `Bearer ${aniListToken}`, Authorization: `Bearer ${aniListToken}`,
}) });
.then((data) => ({ return {
...data?.Viewer, ...data?.Viewer,
statistics: { ...data?.Viewer?.statistics?.anime }, statistics: { ...data?.Viewer?.statistics?.anime },
})) };
.catch((err) => { } catch (err) {
if (err.response?.status === 401 || err.response?.status === 429) { if (err.response?.status === 401) {
return null; return null;
} } else if (err.response?.status === 429) {
return sleep(
Number(err.response.headers.get("Retry-After")!) * 1000,
).then(() => getUser(aniListToken));
}
throw err; throw err;
}); }
} }

View File

@@ -1,6 +1,8 @@
import { graphql } from "gql.tada"; import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request"; import { GraphQLClient } from "graphql-request";
import { sleep } from "../sleep";
const GetNextEpisodeAiringAtQuery = graphql(` const GetNextEpisodeAiringAtQuery = graphql(`
query GetNextEpisodeAiringAt($id: Int!) { query GetNextEpisodeAiringAt($id: Int!) {
Media(id: $id) { 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/"); const client = new GraphQLClient("https://graphql.anilist.co/");
return client try {
.request(GetNextEpisodeAiringAtQuery, { id: aniListId }) const { status, nextAiringEpisode: nextAiring } = await client
.then((data) => { .request(GetNextEpisodeAiringAtQuery, {
const status = data!.Media!.status; id: aniListId,
const nextAiring = data!.Media!.nextAiringEpisode; })
.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;
}
} }