feat: create route to handle updating watch status in AniList
This commit is contained in:
30
src/controllers/watch-status/anilist.ts
Normal file
30
src/controllers/watch-status/anilist.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user