diff --git a/__tests__/anilist.test.ts b/__tests__/anilist.test.ts index df4887b6..85a34086 100644 --- a/__tests__/anilist.test.ts +++ b/__tests__/anilist.test.ts @@ -2,6 +2,25 @@ require('dotenv').config() import AniLink from '../dist/AniLink.js' // import AniLink from '../src/AniLink' +import {BasicUser} from "../src/apis/anilist/interfaces/BasicUser"; +import {RecommendationResponse} from "../src/apis/anilist/interfaces/responses/Recommendation"; +import {ThreadCommentResponse} from "../src/apis/anilist/interfaces/responses/ThreadComment"; +import {ThreadResponse} from "../src/apis/anilist/interfaces/responses/Thread"; +import {UserResponse} from "../src/apis/anilist/interfaces/responses/User"; +import {Activity} from "../src/apis/anilist/interfaces/Activity"; +import {ActivityReply} from "../src/apis/anilist/interfaces/ActivityReply"; +import {ReviewResponse} from "../src/apis/anilist/interfaces/responses/Review"; +import {StudioResponse} from "../src/apis/anilist/interfaces/responses/Studio"; +import {NotificationResponse} from "../src/apis/anilist/interfaces/responses/Notification"; +import {MediaTagCollectionResponse} from "../src/apis/anilist/interfaces/responses/MediaTagCollection"; +import {MediaListResponse} from "../src/apis/anilist/interfaces/responses/MediaList"; +import {StaffResponse} from "../src/apis/anilist/interfaces/responses/Staff"; +import {CharacterResponse} from "../src/apis/anilist/interfaces/responses/Character"; +import {AiringScheduleResponse} from "../src/apis/anilist/interfaces/responses/AiringSchedule"; +import {MediaListCollectionResponse} from "../src/apis/anilist/interfaces/responses/MediaListCollectionResponse"; +import {MediaTrendResponse} from "../src/apis/anilist/interfaces/responses/MediaTrend"; +import {MediaResponse} from "../src/apis/anilist/interfaces/responses/Media"; + async function handleRateLimit(apiCall: () => Promise, retryAfter = 60) { try { const response = await apiCall(); @@ -31,28 +50,32 @@ describe('Anilist API Query', () => { aniLink = new AniLink(token); }); - test('User Query', async () => { + test('User Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.user({id: 542244, isHTML: true})) expect(response).toBeDefined() + return response.data.User; }) - test('Media Query', async () => { + test('Media Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.media({id: 1, type: 'ANIME'})) expect(response).toBeDefined() + return response.data.Media; }) - test('Media Trend Query', async () => { + test('Media Trend Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.mediaTrend({mediaId: 1, type: 'ANIME'})) expect(response).toBeDefined() + return response.data.MediaTrend; }) - test('Airing Schedule Query', async () => { + test('Airing Schedule Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.airingSchedule({mediaId: 130590})) // id needs to be an airing anime expect(response).toBeDefined() + return response.data.AiringSchedule; }) - test('Character Query', async () => { + test('Character Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.character({ id: 1, asHtml: true, @@ -63,9 +86,10 @@ describe('Anilist API Query', () => { mediaPerPage: 10 })) expect(response).toBeDefined() + return response.data.Character; }) - test('Staff Query', async () => { + test('Staff Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.staff({ id: 132186, asHtml: true, @@ -83,14 +107,16 @@ describe('Anilist API Query', () => { characterMediaPerPage: 10 })) expect(response).toBeDefined() + return response.data.Staff; }) - test('Media List Query', async () => { + test('Media List Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.mediaList({userId: 542244})) expect(response).toBeDefined() + return response.data.MediaList; }) - test('Media List Collection Query', async () => { + test('Media List Collection Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.mediaListCollection({ userId: 542244, type: 'ANIME', @@ -99,71 +125,92 @@ describe('Anilist API Query', () => { perChunk: 10000 })) expect(response).toBeDefined() + return response.data.MediaListCollection; }) - test('Genre Collection Query', async () => { + test('Genre Collection Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.genreCollection()) expect(response).toBeDefined() + return response.data.GenreCollection; }) - test('Media Tag Collection Query', async () => { + test('Media Tag Collection Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.mediaTagCollection()) expect(response).toBeDefined() + return response.data.MediaTagCollection; }) - test('Viewer Query', async () => { + test('Viewer Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.viewer({isHTML: true})) expect(response).toBeDefined() + return response.data.Viewer; }) - test('Notification Query', async () => { + test('Notification Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.notification({asHtml: true})) expect(response).toBeDefined() + return response.data.Notification; }) - test('Studio Query', async () => { + test('Studio Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.studio({id: 561, asHtml: true})) expect(response).toBeDefined() + return response.data.Studio; }) - test('Review Query', async () => { + test('Review Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.review({id: 8008, asHtml: true})) expect(response).toBeDefined() + return response.data.Review; }) - test('Activity Query', async () => { + test('Activity Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.activity({id: 723235883, asHtml: true})) expect(response).toBeDefined() + return response.data.Activity; }) - test('Activity Reply Query', async () => { + test('Activity Reply Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.activityReply({id: 12191046, asHtml: true})) expect(response).toBeDefined() + return response.data.ActivityReply; }) - test('Following Query', async () => { + test('Following Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.following({userId: 542244, asHtml: true})) expect(response).toBeDefined() + return response.data.Following; }) - test('Follower Query', async () => { + test('Follower Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.follower({userId: 542244, asHtml: true})) expect(response).toBeDefined() + return response.data.Follower; }) - test('Thread Query', async () => { + test('Thread Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.thread({id: 71881, asHtml: true})) expect(response).toBeDefined() + return response.data.Thread; }) - test('Thread Comment Query', async () => { + test('Thread Comment Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.threadComment({id: 2555166, asHtml: true})) expect(response).toBeDefined() + return response.data.ThreadComment; }) - test('Reccomendation Query', async () => { + test('Reccomendation Query', async (): Promise => { const response = await handleRateLimit(() => aniLink.anilist.query.recommendation({mediaId: 156822, asHtml: true})) expect(response).toBeDefined() + return response.data.Recommendation; + }) + + + test('Likes Page Query', async (): Promise => { + const response = await handleRateLimit(() => aniLink.anilist.query.page.likes({likeableId: 723422275, type: 'ACTIVITY', asHtml: true})) + expect(response).toBeDefined() + return response.data.Page.likes; }) }) @@ -195,5 +242,6 @@ describe('Anilist API Mutation', () => { disabledListActivity: [{type: 'CURRENT', disabled: false}] })) expect(response).toBeDefined() + return response; }) }) diff --git a/package.json b/package.json index 4b4bd923..0c78c164 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anilink-api-wrapper", - "version": "1.11.0", + "version": "1.12.0", "description": "Anilist API Wrapper", "main": "dist/AniLink.js", "scripts": { diff --git a/src/AniLink.ts b/src/AniLink.ts index a8919b7c..79187752 100644 --- a/src/AniLink.ts +++ b/src/AniLink.ts @@ -42,8 +42,10 @@ import { ThreadResponse } from './apis/anilist/interfaces/responses/Thread' import { ThreadQuery } from './apis/anilist/query/Thread' import { ThreadCommentQuery } from './apis/anilist/query/ThreadComment' import { ThreadCommentResponse } from './apis/anilist/interfaces/responses/ThreadComment' -import {RecommendationResponse} from "./apis/anilist/interfaces/responses/Recommendation"; +import { RecommendationResponse } from './apis/anilist/interfaces/responses/Recommendation' import { RecommendationQuery } from './apis/anilist/query/Recommendation' +import { BasicUser } from './apis/anilist/interfaces/BasicUser' +import { LikesQuery } from './apis/anilist/query/page/Likes' class AniLink { public anilist: { @@ -69,6 +71,10 @@ class AniLink { thread: () => Promise threadComment: () => Promise recommendation: () => Promise + + page: { + likes: () => Promise + } } mutation: { updateUser: (variables: { @@ -115,6 +121,8 @@ class AniLink { const threadCommentQueryInstance = new ThreadCommentQuery(authToken) const recommendationQueryInstance = new RecommendationQuery(authToken) + const likesQueryInstance = new LikesQuery(authToken) + const updateUserMutationInstance = new UpdateUserMutation(authToken) this.anilist = { query: { @@ -138,7 +146,11 @@ class AniLink { follower: followerQueryInstance.follower.bind(followerQueryInstance), thread: threadQueryInstance.thread.bind(threadQueryInstance), threadComment: threadCommentQueryInstance.threadComment.bind(threadCommentQueryInstance), - recommendation: recommendationQueryInstance.recommmendation.bind(recommendationQueryInstance) + recommendation: recommendationQueryInstance.recommmendation.bind(recommendationQueryInstance), + + page: { + likes: likesQueryInstance.likes.bind(likesQueryInstance) + } }, mutation: { updateUser: updateUserMutationInstance.updateUser.bind(updateUserMutationInstance) diff --git a/src/apis/anilist/interfaces/responses/Recommendation.ts b/src/apis/anilist/interfaces/responses/Recommendation.ts index 10ed9626..daf9e724 100644 --- a/src/apis/anilist/interfaces/responses/Recommendation.ts +++ b/src/apis/anilist/interfaces/responses/Recommendation.ts @@ -1,5 +1,5 @@ -import {BasicUser, BasicUserSchema} from "../BasicUser"; -import {MediaResponse, MediaSchema} from "./Media"; +import { BasicUser, BasicUserSchema } from '../BasicUser' +import { MediaResponse, MediaSchema } from './Media' export interface RecommendationResponse { id: number @@ -23,4 +23,4 @@ export const RecommendationSchema = ` user { ${BasicUserSchema} } -` \ No newline at end of file +` diff --git a/src/apis/anilist/query/page/Likes.ts b/src/apis/anilist/query/page/Likes.ts new file mode 100644 index 00000000..176e5283 --- /dev/null +++ b/src/apis/anilist/query/page/Likes.ts @@ -0,0 +1,41 @@ +import { APIWrapper } from '../../../../base/APIWrapper' +import { sendRequest } from '../../../../base/RequestHandler' +import { BasicUser, BasicUserSchema } from '../../interfaces/BasicUser' + +interface LikesVariables { + likeableId?: number + type?: string + page?: number + perPage?: number +} + +export class LikesQuery extends APIWrapper { + private readonly authToken: string + + constructor (authToken: string) { + super('https://graphql.anilist.co') + this.authToken = authToken + } + + async likes (variables?: LikesVariables): Promise { + const query = ` + query ($likeableId: Int, $type: LikeableType, $page: Int, $perPage: Int) { + Page (page: $page, perPage: $perPage) { + pageInfo { + total + perPage + currentPage + lastPage + hasNextPage + } + likes (likeableId: $likeableId, type: $type) { + ${BasicUserSchema} + } + } + } + ` + + const data = { query, variables } + return await sendRequest(this.baseURL, 'POST', data, this.authToken) + } +}