aniki
Version:
Aniki is an easy-to-use NPM module that gets information about your favorite anime and manga.
464 lines (463 loc) • 22.3 kB
JavaScript
"use strict";
/**
* @file This file contain the MangaKitsu class, used to get Manga 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.MangaKitsu = void 0;
// The url
var url = "https://kitsu.app/api/edge";
// Main class
/**
* @class
* @description MangaKitsu is a class that's using the Kitsu.app API, with this class you can find Mangas informations in different ways
* @example
* Basic usage:
* ```js
* // CJS
* const { MangaKitsu } = require("aniki")
* // JS ESM or TS
* import { MangaKitsu } from "aniki";
*
* const manga = new MangaKitsu();
*
* // Normal
* manga.find({ query: "Oshi no Ko" }).then(a => console.log(a.data[0]));
*
* // Find by an id
* manga.findById(3600).then(a => console.log(a.data));
*
* // Handling errors
*
* manga.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 functions
*
* async function getManga(query) {
* // ...
* const a = await manga.find({ query: query })
*
* // Tip to avoid multiple awaits
* const a = manga.find({query: ""});
* const b = manga.list({});
* const [A, B] = await Promise.all([a, b])
* }
*
* ```
* @since 1.0.2
*/
var MangaKitsu = /** @class */ (function () {
function MangaKitsu() {
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 {IKitsuMangaFind} 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 mangas with different parameters.
* @returns {Promise<IKitsuManga | undefined>} - Returns a Promise with the IKitsuManga inteface.
* @example
* ```js
* manga.find({ query: "Oshi no ko", offset: 0 }).then(a => console.log(a)); // offset is optional.
* ```
* @since 1.0.2
*/
MangaKitsu.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 MangaKitsu#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 MangaKitsu#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 MangaKitsu#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.season)
Object.assign(parameters, { "filter[season]": params.season });
if (params.year)
Object.assign(parameters, { "filter[year]": params.year });
if (params.categories)
Object.assign(parameters, { "filter[categories]": params.categories });
p = new URLSearchParams(parameters);
return [4 /*yield*/, fetch("".concat(url, "/manga?").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 manga.
* @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API.
* @description Get an Manga with the ID.
* @returns {Promise<IKitsuMangaSingle | undefined>} - Return a Promise.
* @example
* ```js
* manga.findById(30).then(r => console.log(r.data.id));
*
* // Or
* manga.findById("30").then(r => console.log(r.data.id));
* ```
* @since 1.3.0
*/
MangaKitsu.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 MangaKitsu#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 MangaKitsu#findById is not a number.",
}, 400)];
case 3:
_c.sent();
return [2 /*return*/];
case 4: return [4 /*yield*/, fetch("".concat(url, "/manga/").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 {IKitsuMangaList} params - The parameters for the request.
* @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API.
* @description Get an list of Mangas, you can choose the page, and the number of Mangas per page.
* @returns {Promise<IKitsuManga | undefined>} - Return a Promise.
* @example
* ```js
* Manga.list({ offset: 0, perPage: 10 }).then(a => console.log(a));
* ```
* @since 1.0.2
*
*/
MangaKitsu.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 MangaKitsu#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 MangaKitsu#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#list 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.season)
Object.assign(parameters, { "filter[season]": params.season });
if (params.year)
Object.assign(parameters, { "filter[year]": params.year });
if (params.categories)
Object.assign(parameters, { "filter[categories]": params.categories });
p = new URLSearchParams(parameters);
return [4 /*yield*/, fetch("".concat(url, "/manga?").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 chapter of a manga using the chapter ID.
* @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API.
* @description Get an chapter with the ID.
* @returns {Promise<IKitsuChapter | undefined>} - Return a IKitsuChapter Promise interface or undefined if it has no result.
* @example
* ```js
* manga.chapter(30).then(r => console.log(r.data.attributes.titles.en));
* ```
* @since 1.3.0
*/
MangaKitsu.prototype.chapter = 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 MangaKitsu#chapter 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 MangaKitsu#chapter is not a number.",
}, 400)];
case 3:
_c.sent();
return [2 /*return*/];
case 4: return [4 /*yield*/, fetch("".concat(url, "/chapters/").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}`} mangaId - The parameters to find all chapters of an manga using its ID.
* @param {TKitsuHandleError} [handleError] - Used for handling errors from the method and the API.
* @returns {Promise<IKitsuChapters | undefined>} Return a IKitsuChapters Promise interface or undefined if it has no result.
* @example
* ```js
* manga.chapters(7442).then(r => console.log(r.data.attributes.titles.en));
* ```
* @since 1.3.5
*/
MangaKitsu.prototype.chapters = function (mangaId, 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 (!!mangaId) return [3 /*break*/, 2];
return [4 /*yield*/, (handleError || this.defaultHandleError)({
moduleError: "'mangaId' in MangaKitsu#chapters is empty.",
}, 400)];
case 1:
_c.sent();
return [2 /*return*/];
case 2:
if (!isNaN(mangaId)) return [3 /*break*/, 4];
return [4 /*yield*/, (handleError || this.defaultHandleError)({
moduleError: "'mangaId' in MangaKitsu#chapters is not a number.",
}, 400)];
case 3:
_c.sent();
return [2 /*return*/];
case 4: return [4 /*yield*/, fetch("".concat(url, "/chapters?filter[manga_id]=").concat(mangaId), {
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 MangaKitsu;
}());
exports.MangaKitsu = MangaKitsu;
exports.default = MangaKitsu;