UNPKG

myanimelist-wrapper

Version:

A comprehensive TypeScript wrapper for the Jikan API v4 (unofficial MyAnimeList API)

94 lines 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JikanClient = void 0; const anime_1 = require("./endpoints/anime"); const manga_1 = require("./endpoints/manga"); const characters_1 = require("./endpoints/characters"); const people_1 = require("./endpoints/people"); const clubs_1 = require("./endpoints/clubs"); const seasons_1 = require("./endpoints/seasons"); const schedules_1 = require("./endpoints/schedules"); const top_1 = require("./endpoints/top"); const genres_1 = require("./endpoints/genres"); const producers_1 = require("./endpoints/producers"); const magazines_1 = require("./endpoints/magazines"); const users_1 = require("./endpoints/users"); const reviews_1 = require("./endpoints/reviews"); const recommendations_1 = require("./endpoints/recommendations"); const random_1 = require("./endpoints/random"); const error_1 = require("./types/error"); /** * Main client for interacting with the Jikan API */ class JikanClient { /** * Create a new JikanClient instance * @param options Client configuration options */ constructor(options = {}) { this.baseUrl = options.baseUrl || "https://api.jikan.moe/v4"; this.timeout = options.timeout || 30000; this.headers = { Accept: "application/json", ...options.headers, }; // Initialize endpoints this.anime = new anime_1.AnimeEndpoint(this); this.manga = new manga_1.MangaEndpoint(this); this.characters = new characters_1.CharactersEndpoint(this); this.people = new people_1.PeopleEndpoint(this); this.clubs = new clubs_1.ClubsEndpoint(this); this.seasons = new seasons_1.SeasonsEndpoint(this); this.schedules = new schedules_1.SchedulesEndpoint(this); this.top = new top_1.TopEndpoint(this); this.genres = new genres_1.GenresEndpoint(this); this.producers = new producers_1.ProducersEndpoint(this); this.magazines = new magazines_1.MagazinesEndpoint(this); this.users = new users_1.UsersEndpoint(this); this.reviews = new reviews_1.ReviewsEndpoint(this); this.recommendations = new recommendations_1.RecommendationsEndpoint(this); this.random = new random_1.RandomEndpoint(this); } /** * Make a request to the Jikan API * @param endpoint API endpoint path * @param params Query parameters * @returns Promise with the API response */ async request(endpoint, params) { try { const url = new URL(`${this.baseUrl}${endpoint}`); if (params) { Object.entries(params).forEach(([key, value]) => { if (value !== undefined && value !== null) { url.searchParams.append(key, String(value)); } }); } const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), this.timeout); const response = await fetch(url.toString(), { method: "GET", headers: this.headers, signal: controller.signal, }); clearTimeout(timeoutId); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new error_1.JikanError(`API request failed with status ${response.status}`, response.status, errorData); } return (await response.json()); } catch (error) { if (error instanceof error_1.JikanError) { throw error; } if (error instanceof Error && error.name === "AbortError") { throw new error_1.JikanError("Request timeout", 408); } throw new error_1.JikanError(error instanceof Error ? error.message : "Unknown error occurred", 500); } } } exports.JikanClient = JikanClient; //# sourceMappingURL=client.js.map