feat: add more 429 checks

This commit is contained in:
2024-10-05 15:52:38 -04:00
parent 24c0fa316c
commit 5ea90bda55
5 changed files with 46 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import { sleep } from "~/libs/sleep";
const SearchQuery = graphql(`
query Search($query: String!, $page: Int!, $limit: Int!) {
Page(page: $page, perPage: $limit) {
@@ -27,7 +29,7 @@ export async function fetchSearchResultsFromAnilist(
query: string,
page: number,
limit: number,
) {
): Promise<SearchResultsResponse | undefined> {
const client = new GraphQLClient("https://graphql.anilist.co/");
return client
@@ -40,5 +42,31 @@ export async function fetchSearchResultsFromAnilist(
const { media: results, pageInfo } = page;
return { results, hasNextPage: pageInfo?.hasNextPage };
})
.catch((err) => {
const response = err.response;
if (response.status === 429) {
console.log("429, retrying in", response.headers.get("Retry-After"));
return sleep(Number(response.headers.get("Retry-After")!) * 1000).then(
() => fetchSearchResultsFromAnilist(query, page, limit),
);
}
throw err;
});
}
type SearchResultsResponse = {
results:
| ({
id: number;
title: { userPreferred: string | null; english: string | null } | null;
coverImage: {
extraLarge: string | null;
large: string | null;
medium: string | null;
} | null;
} | null)[]
| null;
hasNextPage: boolean | null | undefined;
};