Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Add Likes.ts Query
Browse files Browse the repository at this point in the history
  • Loading branch information
RLAlpha49 committed Apr 28, 2024
1 parent 4a0af61 commit a3bd127
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 27 deletions.
90 changes: 69 additions & 21 deletions __tests__/anilist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>, retryAfter = 60) {
try {
const response = await apiCall();
Expand Down Expand Up @@ -31,28 +50,32 @@ describe('Anilist API Query', () => {
aniLink = new AniLink(token);
});

test('User Query', async () => {
test('User Query', async (): Promise<UserResponse> => {
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<MediaResponse> => {
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<MediaTrendResponse> => {
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<AiringScheduleResponse> => {
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<CharacterResponse> => {
const response = await handleRateLimit(() => aniLink.anilist.query.character({
id: 1,
asHtml: true,
Expand All @@ -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<StaffResponse> => {
const response = await handleRateLimit(() => aniLink.anilist.query.staff({
id: 132186,
asHtml: true,
Expand All @@ -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<MediaListResponse> => {
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<MediaListCollectionResponse> => {
const response = await handleRateLimit(() => aniLink.anilist.query.mediaListCollection({
userId: 542244,
type: 'ANIME',
Expand All @@ -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<String> => {
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<MediaTagCollectionResponse> => {
const response = await handleRateLimit(() => aniLink.anilist.query.mediaTagCollection())
expect(response).toBeDefined()
return response.data.MediaTagCollection;
})

test('Viewer Query', async () => {
test('Viewer Query', async (): Promise<UserResponse> => {
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<NotificationResponse> => {
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<StudioResponse> => {
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<ReviewResponse> => {
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<Activity> => {
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<ActivityReply> => {
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<UserResponse> => {
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<UserResponse> => {
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<ThreadResponse> => {
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<ThreadCommentResponse> => {
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<RecommendationResponse> => {
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<BasicUser> => {
const response = await handleRateLimit(() => aniLink.anilist.query.page.likes({likeableId: 723422275, type: 'ACTIVITY', asHtml: true}))
expect(response).toBeDefined()
return response.data.Page.likes;
})
})

Expand Down Expand Up @@ -195,5 +242,6 @@ describe('Anilist API Mutation', () => {
disabledListActivity: [{type: 'CURRENT', disabled: false}]
}))
expect(response).toBeDefined()
return response;
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
16 changes: 14 additions & 2 deletions src/AniLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -69,6 +71,10 @@ class AniLink {
thread: () => Promise<ThreadResponse>
threadComment: () => Promise<ThreadCommentResponse>
recommendation: () => Promise<RecommendationResponse>

page: {
likes: () => Promise<BasicUser>
}
}
mutation: {
updateUser: (variables: {
Expand Down Expand Up @@ -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: {
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/apis/anilist/interfaces/responses/Recommendation.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,4 +23,4 @@ export const RecommendationSchema = `
user {
${BasicUserSchema}
}
`
`
41 changes: 41 additions & 0 deletions src/apis/anilist/query/page/Likes.ts
Original file line number Diff line number Diff line change
@@ -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<BasicUser> {
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)
}
}

0 comments on commit a3bd127

Please sign in to comment.