npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

anilink-api-wrapper

v1.29.2

Published

API wrapper for AniManga sites such as Anilist, MyAnimeList, Kitsu.

Downloads

89

Readme

AniLink

For full documentation on methods and parameters, please refer to the AniLink documentation.

AniLink is a TypeScript library for interacting with the AniList API. It provides methods for querying and mutating data, making it easier to integrate AniList's features into your own applications.

Installation

To install AniLink, you can use npm or yarn.:

npm install anilink-api-wrapper

or

yarn add anilink-api-wrapper

Initialization

AniList

To start using AniLink, you need to import it and initialize it with an optional authentication token. You can get an authentication token by registering your application on the AniList website.

Typescript

import AniLink from 'anilink-api-wrapper';

Javascript

const AniLink = require('anilink-api-wrapper');

Initialization

const authToken = 'your-auth-token';
const aniLink = new AniLink(authToken);

You can also initialize AniLink without an authentication token

const aniLink = new AniLink();

AniList API

Structure

AniLink for AniList is divided into two main sections: anilist.query and anilist.mutation. The anilist.query section contains methods for querying data from the AniList API, while the anilist.mutation section contains methods for mutating data.

Custom Queries and Mutations

If needed there is a custom section anilist.custom that allows the user to pass a custom query or mutation to the AniList API.

The method accepts two parameters: the query or mutation string and an optional variables object.

const viewer = await aniLink.anilist.custom('query {Viewer {id}}');

const mutation = 'mutation ($about: String) {UpdateUser (about: $about) {id}}';
const variables = { about: "New about text" };
const response = await aniLink.anilist.custom(mutation, variables);

Query Methods

The anilist.query section is further divided into main query methods and page query methods. The main query methods return a single piece of data, while the page query methods return pages of data.

List of main query methods in anilist.query:

  • user
  • media
  • mediaTrend
  • airingSchedule
  • character
  • staff
  • mediaList
  • mediaListCollection
  • genreCollection
  • mediaTagCollection
  • viewer
  • notification
  • studio
  • review
  • activity
  • activityReply
  • following
  • follower
  • thread
  • threadComment
  • recommendation
  • markdown
  • aniChartUser
  • siteStatistics
  • externalLinkSourceCollection

List of page query methods in anilist.query.page:

  • users
  • medias
  • characters
  • staffs
  • studios
  • mediaLists
  • airingSchedules
  • mediaTrends
  • notifications
  • followers
  • following
  • activities
  • activityReplies
  • threads
  • threadComments
  • reviews
  • recommendations
  • likes

Mutation Methods

List of methods in anilist.mutation:

  • updateUser
  • saveMediaListEntry
  • updateMediaListEntries
  • deleteMediaListEntries
  • deleteCustomLists
  • saveTextActivity
  • saveMessageActivity
  • deleteActivity
  • toggleActivityPin
  • toggleActivitySubscription
  • saveActivityReply
  • deleteActivityReply
  • toggleLike
  • toggleLikeV2
  • toggleFollow
  • toggleFavourite
  • updateFavouriteOrder
  • saveReview
  • deleteReview
  • saveRecommendation
  • saveThread
  • deleteThread
  • toggleThreadSubscription
  • saveThreadComment
  • deleteThreadComment
  • updateAniChartSettings
  • updateAniChartHighlights

Error Handling

AniLink will throw an error if the AniList API returns an error. You can catch these errors using a try-catch block.

try {
  const user = await aniLink.anilist.query.user({id: 542244});
  console.log(user);
} catch (error) {
  console.error(error);
}

This includes status codes and error messages returned by the AniList API. Here is an example rate limit handler to catch the errors thrown by AniLink:

Typescript
async function handleRateLimit(apiCall: () => Promise<any>, retryAfter = 60) {
  try {
    let response;
    try {
      response = await apiCall();
    } catch (error) {
      throw error;
    }
    console.log(response.data);
    return response;
  } catch (error: any) {
    if (error.response && error.response.status === 429) {
      console.log('Rate limit exceeded, waiting for 1 minute before retrying...');
      await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
      console.log('Retrying...');
      return handleRateLimit(apiCall, retryAfter);
    } else {
      if (error.response && error.response.data) {
        throw error.response.data;
      } else {
        throw error.response || error;
      }
    }
  }
}
Javascript
async function handleRateLimit(apiCall, retryAfter = 60) {
    // Same as above
}

The possible error codes returned by the AniList API are:

  • 400: Bad Request (e.g. missing variables, invalid variables, or invalid query)
  • 401: Unauthorized (e.g. invalid authentication token)
  • 404: Not Found (e.g. user not found)
  • 429: Too Many Requests (e.g. rate limit exceeded)
  • 500: Internal Server Error (e.g. AniList server error)

Missing or Invalid Variables

AniLink will also throw an error if any variables are missing or invalid. For example, if you try to query a user providing a string instead of a number for ID, AniLink will throw an error. Most variables are optional however there a few that are required.

try {
  const user = await aniLink.anilist.query.user({id: '542244'});
  console.log(user);
} catch (error) {
  console.error(error);
}

Example Error Thrown:

  Invalid id: 542244. Expected type: number

Examples

See the ANILIST_API_EXAMPLES for examples of how to use AniLink to interact with the AniList API.

License

This project is licensed under the MIT License - see the LICENSE file for details.