UNPKG

aniki

Version:

Aniki is an easy-to-use NPM module that gets information about your favorite anime and manga.

471 lines (469 loc) 22.9 kB
"use strict"; /** * @file This file contain the AnimeKitsu class, used to get anime informations from the Kitsu.app API. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnimeKitsu = void 0; // The url var url = "https://kitsu.app/api/edge"; // Main class /** * @class * @since 1.0.2 * @description AnimeKitsu is a class that's using the Kitsu.app API, with this class you can find animes informations in different ways * @example * Basic usage: * ```js * // CJS * const { AnimeKitsu } = require("aniki") * // JS ESM or TS * import { AnimeKitsu } from "aniki"; * * const anime = new AnimeKitsu(); * * // Normal * anime.find({ query: "Oshi no Ko" }).then(a => console.log(a.data[0])); * * // Find by an id * anime.findById(3600).then(a => console.log(a.data)); * * // Handling errors * * anime.find( * { query: "Oshi no ko" }, * async ({ apiError, moduleError }, status) => { * if (apiError) console.error(await apiError); * if (moduleError) console.error(await moduleError); * }); * * // Best practice to avoid using .then() method is by using asynchronous function * * async function getAnime(query) { * // ... * const a = await anime.find({ query: query }) * // ... * } * * ``` */ var AnimeKitsu = /** @class */ (function () { function AnimeKitsu() { var _this = this; this.defaultHandleError = function (error) { return __awaiter(_this, void 0, void 0, function () { var _a, _b, _c, _d, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: if (!error.apiError) return [3 /*break*/, 2]; _b = (_a = console).error; _c = ["Aniki: Unhandled API error:"]; return [4 /*yield*/, error.apiError]; case 1: _b.apply(_a, _c.concat([(_g.sent()).errors])); _g.label = 2; case 2: if (!error.moduleError) return [3 /*break*/, 4]; _e = (_d = console).error; _f = ["Aniki: Unhandled error:"]; return [4 /*yield*/, error.moduleError]; case 3: _e.apply(_d, _f.concat([_g.sent()])); _g.label = 4; case 4: return [2 /*return*/]; } }); }); }; } // Methods /** * @method * @param {IKitsuAnimeFind} params - The parameters for the request. * @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API. * @description The find method is used to find animes with different parameters. * @returns {Promise<IKitsuAnime | undefined>} - Returns a Promise with the IKitsuAnime inteface. * @example * ```js * // Searching an anime * anime.find({ query: "Oshi no ko", offset: 0 }).then(r=> console.log(r.data[0])) * ``` * @since 1.0.2 */ AnimeKitsu.prototype.find = function (params, handleError) { return __awaiter(this, void 0, void 0, function () { var parameters, p, res, _a; var _b; var _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: parameters = {}; if (!!params.query) return [3 /*break*/, 2]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'query' in AnimeKitsu#find is empty.", }, 400)]; case 1: _e.sent(); return [2 /*return*/]; case 2: Object.assign(parameters, { "filter[text]": params.query }); if (!params.offset) return [3 /*break*/, 4]; if (!isNaN(params.offset)) return [3 /*break*/, 4]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'offset' in AnimeKitsu#find is not a number.", }, 400)]; case 3: _e.sent(); return [2 /*return*/]; case 4: Object.assign(parameters, { "page[offset]": (_c = params.offset) !== null && _c !== void 0 ? _c : 0 }); if (!params.perPage) return [3 /*break*/, 8]; if (!isNaN(params.perPage)) return [3 /*break*/, 6]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'perPage' in AnimeKitsu#find is not a number.", }, 400)]; case 5: _e.sent(); return [2 /*return*/]; case 6: if (!(params.perPage > 20)) return [3 /*break*/, 8]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'perPage' in MangaKitsu#find should be less than or equal to 20.", }, 400)]; case 7: _e.sent(); return [2 /*return*/]; case 8: Object.assign(parameters, { "page[limit]": (_d = params.perPage) !== null && _d !== void 0 ? _d : 10 }); if (params.averageRating) Object.assign(parameters, { "filter[averageRating]": params.averageRating, }); if (params.season) Object.assign(parameters, { "filter[season]": params.season }); if (params.ageRating) Object.assign(parameters, { "filter[ageRating]": params.ageRating }); if (params.year) Object.assign(parameters, { "filter[year]": params.year }); if (params.streamers) Object.assign(parameters, { "filter[streamers]": params.streamers }); if (params.categories) Object.assign(parameters, { "filter[categories]": params.categories }); p = new URLSearchParams(parameters); return [4 /*yield*/, fetch("".concat(url, "/anime?").concat(p), { headers: { "Content-Type": "application/vnd.api+json", Accept: "application/vnd.api+json", }, })]; case 9: res = _e.sent(); if (!!res.ok) return [3 /*break*/, 12]; _a = (handleError || this.defaultHandleError); _b = {}; return [4 /*yield*/, res.json()]; case 10: return [4 /*yield*/, _a.apply(void 0, [(_b.apiError = (_e.sent()), _b), res.status])]; case 11: _e.sent(); return [2 /*return*/]; case 12: return [2 /*return*/, res.json()]; } }); }); }; /** * @method * @param {number | `${number}`} id - The ID of the anime. * @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API. * @description Get an anime with the ID. * @returns {Promise<IKitsuAnimeSingle | undefined>} - Return a Promise. * @example * ```js * anime.findById(30).then(r => console.log(r.data.id)); * ``` * @since 1.3.0 */ AnimeKitsu.prototype.findById = function (id, handleError) { return __awaiter(this, void 0, void 0, function () { var res, _a; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!!id) return [3 /*break*/, 2]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'id' in AnimeKitsu#findById is empty.", }, 400)]; case 1: _c.sent(); return [2 /*return*/]; case 2: if (!isNaN(id)) return [3 /*break*/, 4]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'id' in AnimeKitsu#findById is not a number.", }, 400)]; case 3: _c.sent(); return [2 /*return*/]; case 4: return [4 /*yield*/, fetch("".concat(url, "/anime/").concat(id), { headers: { "Content-Type": "application/vnd.api+json", Accept: "application/vnd.api+json", }, })]; case 5: res = _c.sent(); if (!!res.ok) return [3 /*break*/, 8]; _a = (handleError || this.defaultHandleError); _b = {}; return [4 /*yield*/, res.json()]; case 6: return [4 /*yield*/, _a.apply(void 0, [(_b.apiError = (_c.sent()), _b), res.status])]; case 7: _c.sent(); return [2 /*return*/]; case 8: return [2 /*return*/, res.json()]; } }); }); }; /** * * @method * @param {IKitsuAnimeList} params - The parameters for the request. * @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API. * @description Get an list of animes, you can choose the page, and the number of animes per page. * @returns {Promise<IKitsuAnime | undefined>} - Return a Promise. * @example * ```js * anime.list({ offset: 0, perPage: 10 }).then(a => console.log(a)); * ``` * @since 1.0.2 * */ AnimeKitsu.prototype.list = function (params, handleError) { return __awaiter(this, void 0, void 0, function () { var parameters, p, res, _a; var _b; var _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: parameters = {}; if (!params.offset) return [3 /*break*/, 2]; if (!isNaN(params.offset)) return [3 /*break*/, 2]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'offset' in AnimeKitsu#list is not a number.", }, 400)]; case 1: _e.sent(); return [2 /*return*/]; case 2: Object.assign(parameters, { "page[offset]": (_c = params.offset) !== null && _c !== void 0 ? _c : 0 }); if (!params.perPage) return [3 /*break*/, 6]; if (!isNaN(params.perPage)) return [3 /*break*/, 4]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'perPage' in AnimeKitsu#list is not a number.", }, 400)]; case 3: _e.sent(); return [2 /*return*/]; case 4: if (!(params.perPage > 20)) return [3 /*break*/, 6]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'perPage' in MangaKitsu#find should be less than or equal to 20.", }, 400)]; case 5: _e.sent(); return [2 /*return*/]; case 6: Object.assign(parameters, { "page[limit]": (_d = params.perPage) !== null && _d !== void 0 ? _d : 10 }); if (params.averageRating) Object.assign(parameters, { "filter[averageRating]": params.averageRating, }); if (params.ageRating) Object.assign(parameters, { "filter[averageRating]": params.ageRating }); if (params.season) Object.assign(parameters, { "filter[season]": params.season }); if (params.year) Object.assign(parameters, { "filter[year]": params.year }); if (params.streamers) Object.assign(parameters, { "filter[streamers]": params.streamers }); if (params.categories) Object.assign(parameters, { "filter[categories]": params.categories }); p = new URLSearchParams(parameters); return [4 /*yield*/, fetch("".concat(url, "/anime?").concat(p), { headers: { "Content-Type": "application/vnd.api+json", Accept: "application/vnd.api+json", }, })]; case 7: res = _e.sent(); if (!!res.ok) return [3 /*break*/, 10]; _a = (handleError || this.defaultHandleError); _b = {}; return [4 /*yield*/, res.json()]; case 8: return [4 /*yield*/, _a.apply(void 0, [(_b.apiError = (_e.sent()), _b), res.status])]; case 9: _e.sent(); return [2 /*return*/]; case 10: return [2 /*return*/, res.json()]; } }); }); }; /** * @method * @param {number | `${number}`} id - The parameters to find a specific episode of an anime using the episode ID. * @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API. * @description Get an episode with the ID. * @returns {Promise<IKitsuEpisode | undefined>} - Return a IKitsuEpisode Promise interface or undefined if it has no result. * @example * ```js * anime.episode(30).then(r => console.log(r.data.attributes.titles.en)); * ``` * @since 1.3.0 */ AnimeKitsu.prototype.episode = function (id, handleError) { return __awaiter(this, void 0, void 0, function () { var res, _a; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!!id) return [3 /*break*/, 2]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'id' in AnimeKitsu#episode is empty.", }, 400)]; case 1: _c.sent(); return [2 /*return*/]; case 2: if (!isNaN(id)) return [3 /*break*/, 4]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'id' in AnimeKitsu#episode is not a number.", }, 400)]; case 3: _c.sent(); return [2 /*return*/]; case 4: return [4 /*yield*/, fetch("".concat(url, "/episodes/").concat(id), { headers: { "Content-Type": "application/vnd.api+json", Accept: "application/vnd.api+json", }, })]; case 5: res = _c.sent(); if (!!res.ok) return [3 /*break*/, 8]; _a = (handleError || this.defaultHandleError); _b = {}; return [4 /*yield*/, res.json()]; case 6: return [4 /*yield*/, _a.apply(void 0, [(_b.apiError = (_c.sent()), _b), res.status])]; case 7: _c.sent(); return [2 /*return*/]; case 8: return [2 /*return*/, res.json()]; } }); }); }; /** * @method * @param {number | `${number}`} mediaId - The parameters to find all episodes of an anime using its ID. * @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API. * @description Get an episode with the ID. * @returns {Promise<IKitsuEpisodes | undefined>} - Return a IKitsuEpisode Promise interface or undefined if it has no result. * @example * ```js * anime.episodes(7442).then(r => console.log(r.data.attributes.titles.en)); * ``` * @since 1.3.5 */ AnimeKitsu.prototype.episodes = function (mediaId, handleError) { return __awaiter(this, void 0, void 0, function () { var res, _a; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!!mediaId) return [3 /*break*/, 2]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'mediaId' in AnimeKitsu#episodes is empty.", }, 400)]; case 1: _c.sent(); return [2 /*return*/]; case 2: if (!isNaN(mediaId)) return [3 /*break*/, 4]; return [4 /*yield*/, (handleError || this.defaultHandleError)({ moduleError: "'mediaId' in AnimeKitsu#episodes is not a number.", }, 400)]; case 3: _c.sent(); return [2 /*return*/]; case 4: return [4 /*yield*/, fetch("".concat(url, "/episodes?filter[media_id]=").concat(mediaId), { headers: { "Content-Type": "application/vnd.api+json", Accept: "application/vnd.api+json", }, })]; case 5: res = _c.sent(); if (!!res.ok) return [3 /*break*/, 8]; _a = (handleError || this.defaultHandleError); _b = {}; return [4 /*yield*/, res.json()]; case 6: return [4 /*yield*/, _a.apply(void 0, [(_b.apiError = (_c.sent()), _b), res.status])]; case 7: _c.sent(); return [2 /*return*/]; case 8: return [2 /*return*/, res.json()]; } }); }); }; return AnimeKitsu; }()); exports.AnimeKitsu = AnimeKitsu; exports.default = AnimeKitsu;