feat: Centralize Anilist GraphQL queries, generalize Durable Object for multiple operations with caching, and add new controllers for search, popular titles, user data, and episode tracking.
This commit is contained in:
@@ -1,55 +1,4 @@
|
||||
import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
|
||||
const MarkEpisodeAsWatchedMutation = graphql(`
|
||||
mutation MarkEpisodeAsWatched($titleId: Int!, $episodeNumber: Int!) {
|
||||
SaveMediaListEntry(
|
||||
mediaId: $titleId
|
||||
status: CURRENT
|
||||
progress: $episodeNumber
|
||||
) {
|
||||
user {
|
||||
id
|
||||
name
|
||||
avatar {
|
||||
medium
|
||||
large
|
||||
}
|
||||
statistics {
|
||||
anime {
|
||||
minutesWatched
|
||||
episodesWatched
|
||||
count
|
||||
meanScore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
const MarkTitleAsWatchedMutation = graphql(`
|
||||
mutation MarkTitleAsWatched($titleId: Int!) {
|
||||
SaveMediaListEntry(mediaId: $titleId, status: COMPLETED) {
|
||||
user {
|
||||
id
|
||||
name
|
||||
avatar {
|
||||
medium
|
||||
large
|
||||
}
|
||||
statistics {
|
||||
anime {
|
||||
minutesWatched
|
||||
episodesWatched
|
||||
count
|
||||
meanScore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
import { env } from "cloudflare:workers";
|
||||
|
||||
export async function markEpisodeAsWatched(
|
||||
aniListToken: string,
|
||||
@@ -57,27 +6,38 @@ export async function markEpisodeAsWatched(
|
||||
episodeNumber: number,
|
||||
markTitleAsComplete: boolean,
|
||||
) {
|
||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||
const durableObjectId = env.ANILIST_DO.idFromName("GLOBAL");
|
||||
const stub = env.ANILIST_DO.get(durableObjectId);
|
||||
|
||||
const mutation = markTitleAsComplete
|
||||
? client.request(
|
||||
MarkTitleAsWatchedMutation,
|
||||
{ titleId },
|
||||
{ Authorization: `Bearer ${aniListToken}` },
|
||||
)
|
||||
: client.request(
|
||||
MarkEpisodeAsWatchedMutation,
|
||||
{ titleId, episodeNumber },
|
||||
{ Authorization: `Bearer ${aniListToken}` },
|
||||
);
|
||||
const operationName = markTitleAsComplete
|
||||
? "MarkTitleAsWatched"
|
||||
: "MarkEpisodeAsWatched";
|
||||
|
||||
return mutation
|
||||
.then((data) => ({
|
||||
...data?.SaveMediaListEntry?.user,
|
||||
statistics: data?.SaveMediaListEntry?.user?.statistics?.anime,
|
||||
}))
|
||||
.catch(async (err) => {
|
||||
console.error(await err.response);
|
||||
throw err;
|
||||
});
|
||||
const variables = markTitleAsComplete
|
||||
? { titleId, token: aniListToken }
|
||||
: { titleId, episodeNumber, token: aniListToken };
|
||||
|
||||
const response = await stub.fetch("http://anilist-do/graphql", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
operationName,
|
||||
variables,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to mark episode as watched: ${response.statusText}`,
|
||||
);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as any;
|
||||
|
||||
return {
|
||||
...data?.user,
|
||||
statistics: data?.user?.statistics?.anime,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user