import Anime, { IAnimeResponse } from "./anime"; import Artist, { IArtistListItemResponse } from "./artist"; import { ISeasonListItemResponse, Season, SeasonInfo, SeasonType } from "./season"; import fetch, { RequestInit } from "node-fetch"; class AnimeThemes { public readonly host: string; private readonly options: RequestInit; constructor(options: IAnimeThemesOptions = { host: "https://themes.moe/api" }) { this.host = options.host; this.options = { headers: { "Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip,deflate,br", "Content-Type": "application/json", "Transfer-Encoding": "chunked", "User-Agent": options.userAgent || `anime-themes/${require("../package.json").version} (+https://github.com/LeNitrous/anime-themes)` } }; } public async search(title: string): Promise { title = title.replace(/!/g, "%21"); const search = await this.query(`/anime/search/${title}`); const response = await this.query("/themes/search", { method: "POST", body: JSON.stringify(search) }); return response.map((item) => new Anime(item, this)); } public async getAnime(id: number): Promise { const response = await this.query(`/themes/${id}`); return new Anime(response[0], this); } public async getArtists(): Promise { const response = await this.query("/artists"); return response.map((item) => new Artist(item, this)); } public async getSeasons(): Promise { const response = await this.query("/seasons"); return response.map((item) => new SeasonInfo(item, this)); } public async getSeason(year: number, season: SeasonType): Promise { const response = await this.query(`/seasons/${year}/${season}`); return new Season(new SeasonInfo({ year: year.toString(), season: season }, this), response); } public async getYears(): Promise { const response = await this.query("/years"); return response.map((item) => parseInt(item)); } public async getYear(year: number): Promise { const response = await this.query(`/seasons/${year}`); return response.map((item) => new Anime(item, this)); } public async getSongCount(): Promise { const response = await this.query("/songcount"); return response.count; } public async query(path: string, options?: RequestInit): Promise { return await (await fetch(this.host + path, { ...this.options, ...options })).json(); } } interface IAnimeThemesOptions { host: string; userAgent?: string; } interface ISongCountResponse { count: number; } export = AnimeThemes;