feat: create route to handle updating watch status in AniList

This commit is contained in:
2024-06-12 09:33:55 -04:00
parent 09ffff7c56
commit 0a859e0f16
8 changed files with 284 additions and 10 deletions

View File

@@ -0,0 +1,30 @@
import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import type { WatchStatus } from "~/types/title";
const UpdateWatchStatusQuery = graphql(`
mutation UpdateWatchStatus($titleId: Int!, $watchStatus: MediaListStatus!) {
SaveMediaListEntry(mediaId: $titleId, status: $watchStatus) {
id
}
}
`);
/** Updates the watch status for a title on Anilist. If the token is null, the watch status will not be updated. */
export async function maybeUpdateWatchStatusOnAnilist(
titleId: number,
watchStatus: WatchStatus,
aniListToken: string | undefined,
) {
if (!aniListToken) {
return;
}
const client = new GraphQLClient("https://graphql.anilist.co/");
const headers = new Headers({ Authorization: `Bearer ${aniListToken}` });
return client
.request(UpdateWatchStatusQuery, { titleId, watchStatus }, headers)
.then((data) => !!data?.SaveMediaListEntry?.id);
}