Giter Site home page Giter Site logo

anilist-wrapper's Introduction

anilist-wrapper

A wrapper for the AniList API

npm version Maintenance

Install

npm i anilist-wrapper

or

yarn add anilist-wrapper

Usage

import { Client } from "anilist-wrapper";

//Public usage of the API (only queries)
const AniListClient = new Client();

//Private usage of the API (mutations and queries regarding user's data)
const AuthedAniListClient = new Client("your_access_token");

Queries

Getting personal information and stats of the authenticated user

AnilistClient.fetchUser()
  .then((user) => console.log(JSON.stringify(user)))
  .catch((err) => console.log(err));

Fetching the anime and manga lists of the authenticated user

import { MediaListGroup, MediaListStatus } from "anilist-wrapper";

let lists: MediaListGroup[] = [];
let [animeWatching, animeCompleted, animeDropped, animePaused,
    mangaReading, mangaCompleted, mangaDropped, mangaPaused] = lists;

await AnilistClient.fetchUserAnimeList()
  .then((collection) => {
    collection.lists!.map((l) => {
      if (l.status === MediaListStatus.Current) {
        animeWatching = l;
      }
      else if (l.status === MediaListStatus.Completed) {
        animeCompleted = l;
      }
      else if (l.status === MediaListStatus.Dropped) {
        animeDropped = l;
      }
      else if (l.status === MediaListStatus.Paused) {
        animePaused = l;
      }
    });
  })
  .catch((err) => {
    console.log(err);
  });

await AnilistClient.fetchUserMangaList()
  .then((collection) => {
    collection.lists!.map((l) => {
      if (l.status === MediaListStatus.Current) {
        mangaReading = l;
      }
      else if (l.status === MediaListStatus.Completed) {
        mangaCompleted = l;
      }
      else if (l.status === MediaListStatus.Dropped) {
        mangaDropped = l;
      }
      else if (l.status === MediaListStatus.Paused) {
        mangaPaused = l;
      }
    });
  })
  .catch((err) => {
    console.log(err);
  });

Searching anime and manga

let animes = [] as Media[];
let mangas = [] as Media[];

await AnilistClient.searchAnime("Gintama", {
  page: 1,
  perPage: 10,
})
  .then((data) => animes = data)
  .catch((err) => console.log(err));

await AnilistClient.searchManga("Gintama", {
  page: 1,
  perPage: 10,
})
  .then((data) => mangas = data)
  .catch((err) => console.log(err));

Getting more details of anime and manga

Merges two Media objects, one without details (returned from .search) and the other one with details.

await AnilistClient.animeDetails(animes[0])
  .then((details) => console.log(JSON.stringify(details)))
  .catch((err) => console.log(err));

await AnilistClient.mangaDetails(mangas[0])
  .then((details) => console.log(JSON.stringify(details)))
  .catch((err) => console.log(err));

Searching characters

let characters = [] as Character[];

let nonDetailedCharacter = {} as Character;

let detailedCharacter = {} as Character;

await AnilistClient.searchCharacter("Gintoki", {
  page: 1,
  perPage: 10,
})
  .then((charactersResponse) => {
    characters = charactersResponse;
    nonDetailedCharacter = characters[0];
    })
  .catch((err) => console.log(err));

Getting more details of a character

Merges two Character objects, one without details (returned from .searchCharacter) and the other one with details.

await AnilistClient.characterDetails(nonDetailedCharacter)
  .then(async (detailedCharacterResponse) => {
    detailedCharacter = detailedCharacterResponse;
    console.log(JSON.stringify(detailedCharacter));
  })
  .catch((err) => console.log(err));

Making your own custom function

import { SomeType } from "anilist-wrapper";

AnilistClient.fetch<SomeType>({query: `your query`, variables: {
  somevariable,
  anothervariable
}}).then(...).catch(...)

Mutations

Adding and Updating an entry

If the anime doesn't exist in any of the user's lists, parameter entryId is not needed, otherwise, if the anime exists, the parameter entryId is necesary, if it's not provided the request will return an error.

Returns: id of the entry.

await AniListClient.updateEntry({
  mediaId: 108725,
  status: MediaListStatus.Planning,
})
  .then((entryId) => {
    console.log(entryId);
  })
  .catch((err) => {
    console.log(JSON.stringify(err));
  });

await AniListClient.updateEntry({
  entryId: 158644321,
  status: MediaListStatus.Dropped,
  score: 0,
})
  .then((entryId) => {
    console.log(entryId);
  })
  .catch((err) => {
    console.log(JSON.stringify(err));
  });

Deleting an entry

await AniListClient.deleteEntry(158644321)
  .then((response) => {
    console.log(JSON.stringify(response));
  })
  .catch((err) => {
    console.log(JSON.stringify(err));
  });

License

MIT

anilist-wrapper's People

Contributors

sawa-ko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.