ani-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,461 @@
1
+ declare enum MediaType {
2
+ ANIME = "ANIME",
3
+ MANGA = "MANGA"
4
+ }
5
+ declare enum MediaFormat {
6
+ TV = "TV",
7
+ TV_SHORT = "TV_SHORT",
8
+ MOVIE = "MOVIE",
9
+ SPECIAL = "SPECIAL",
10
+ OVA = "OVA",
11
+ ONA = "ONA",
12
+ MUSIC = "MUSIC",
13
+ MANGA = "MANGA",
14
+ NOVEL = "NOVEL",
15
+ ONE_SHOT = "ONE_SHOT"
16
+ }
17
+ declare enum MediaStatus {
18
+ FINISHED = "FINISHED",
19
+ RELEASING = "RELEASING",
20
+ NOT_YET_RELEASED = "NOT_YET_RELEASED",
21
+ CANCELLED = "CANCELLED",
22
+ HIATUS = "HIATUS"
23
+ }
24
+ declare enum MediaSeason {
25
+ WINTER = "WINTER",
26
+ SPRING = "SPRING",
27
+ SUMMER = "SUMMER",
28
+ FALL = "FALL"
29
+ }
30
+ declare enum MediaSort {
31
+ ID = "ID",
32
+ TITLE_ROMAJI = "TITLE_ROMAJI",
33
+ TITLE_ENGLISH = "TITLE_ENGLISH",
34
+ TITLE_NATIVE = "TITLE_NATIVE",
35
+ TYPE = "TYPE",
36
+ FORMAT = "FORMAT",
37
+ START_DATE = "START_DATE",
38
+ END_DATE = "END_DATE",
39
+ SCORE = "SCORE",
40
+ POPULARITY = "POPULARITY",
41
+ TRENDING = "TRENDING",
42
+ EPISODES = "EPISODES",
43
+ DURATION = "DURATION",
44
+ STATUS = "STATUS",
45
+ FAVOURITES = "FAVOURITES",
46
+ UPDATED_AT = "UPDATED_AT",
47
+ SEARCH_MATCH = "SEARCH_MATCH"
48
+ }
49
+ declare enum CharacterSort {
50
+ ID = "ID",
51
+ ROLE = "ROLE",
52
+ SEARCH_MATCH = "SEARCH_MATCH",
53
+ FAVOURITES = "FAVOURITES"
54
+ }
55
+ interface MediaTitle {
56
+ romaji: string | null;
57
+ english: string | null;
58
+ native: string | null;
59
+ userPreferred: string | null;
60
+ }
61
+ interface MediaCoverImage {
62
+ extraLarge: string | null;
63
+ large: string | null;
64
+ medium: string | null;
65
+ color: string | null;
66
+ }
67
+ interface FuzzyDate {
68
+ year: number | null;
69
+ month: number | null;
70
+ day: number | null;
71
+ }
72
+ interface MediaTrailer {
73
+ id: string | null;
74
+ site: string | null;
75
+ thumbnail: string | null;
76
+ }
77
+ interface MediaTag {
78
+ id: number;
79
+ name: string;
80
+ description: string | null;
81
+ category: string | null;
82
+ rank: number | null;
83
+ isMediaSpoiler: boolean | null;
84
+ }
85
+ interface Studio {
86
+ id: number;
87
+ name: string;
88
+ isAnimationStudio: boolean;
89
+ siteUrl: string | null;
90
+ }
91
+ interface StudioConnection {
92
+ nodes: Studio[];
93
+ }
94
+ interface CharacterName {
95
+ first: string | null;
96
+ middle: string | null;
97
+ last: string | null;
98
+ full: string | null;
99
+ native: string | null;
100
+ alternative: string[];
101
+ }
102
+ interface CharacterImage {
103
+ large: string | null;
104
+ medium: string | null;
105
+ }
106
+ interface Media {
107
+ id: number;
108
+ idMal: number | null;
109
+ title: MediaTitle;
110
+ type: MediaType;
111
+ format: MediaFormat | null;
112
+ status: MediaStatus | null;
113
+ description: string | null;
114
+ startDate: FuzzyDate | null;
115
+ endDate: FuzzyDate | null;
116
+ season: MediaSeason | null;
117
+ seasonYear: number | null;
118
+ episodes: number | null;
119
+ duration: number | null;
120
+ chapters: number | null;
121
+ volumes: number | null;
122
+ countryOfOrigin: string | null;
123
+ isLicensed: boolean | null;
124
+ source: string | null;
125
+ hashtag: string | null;
126
+ trailer: MediaTrailer | null;
127
+ coverImage: MediaCoverImage;
128
+ bannerImage: string | null;
129
+ genres: string[];
130
+ synonyms: string[];
131
+ averageScore: number | null;
132
+ meanScore: number | null;
133
+ popularity: number | null;
134
+ favourites: number | null;
135
+ trending: number | null;
136
+ tags: MediaTag[];
137
+ studios: StudioConnection;
138
+ isAdult: boolean | null;
139
+ siteUrl: string | null;
140
+ }
141
+ interface Character {
142
+ id: number;
143
+ name: CharacterName;
144
+ image: CharacterImage;
145
+ description: string | null;
146
+ gender: string | null;
147
+ dateOfBirth: FuzzyDate | null;
148
+ age: string | null;
149
+ bloodType: string | null;
150
+ favourites: number | null;
151
+ siteUrl: string | null;
152
+ media: {
153
+ nodes: Pick<Media, "id" | "title" | "type" | "coverImage" | "siteUrl">[];
154
+ } | null;
155
+ }
156
+ interface StaffName {
157
+ first: string | null;
158
+ middle: string | null;
159
+ last: string | null;
160
+ full: string | null;
161
+ native: string | null;
162
+ }
163
+ interface StaffImage {
164
+ large: string | null;
165
+ medium: string | null;
166
+ }
167
+ interface Staff {
168
+ id: number;
169
+ name: StaffName;
170
+ language: string | null;
171
+ image: StaffImage;
172
+ description: string | null;
173
+ primaryOccupations: string[];
174
+ gender: string | null;
175
+ dateOfBirth: FuzzyDate | null;
176
+ dateOfDeath: FuzzyDate | null;
177
+ age: number | null;
178
+ yearsActive: number[];
179
+ homeTown: string | null;
180
+ bloodType: string | null;
181
+ favourites: number | null;
182
+ siteUrl: string | null;
183
+ }
184
+ interface UserAvatar {
185
+ large: string | null;
186
+ medium: string | null;
187
+ }
188
+ interface UserStatistics {
189
+ count: number;
190
+ meanScore: number;
191
+ minutesWatched: number;
192
+ episodesWatched: number;
193
+ chaptersRead: number;
194
+ volumesRead: number;
195
+ }
196
+ interface User {
197
+ id: number;
198
+ name: string;
199
+ about: string | null;
200
+ avatar: UserAvatar;
201
+ bannerImage: string | null;
202
+ isFollowing: boolean | null;
203
+ isFollower: boolean | null;
204
+ donatorTier: number | null;
205
+ donatorBadge: string | null;
206
+ createdAt: number | null;
207
+ siteUrl: string | null;
208
+ statistics: {
209
+ anime: UserStatistics;
210
+ manga: UserStatistics;
211
+ } | null;
212
+ }
213
+ interface PageInfo {
214
+ total: number | null;
215
+ perPage: number | null;
216
+ currentPage: number | null;
217
+ lastPage: number | null;
218
+ hasNextPage: boolean | null;
219
+ }
220
+ interface SearchMediaOptions {
221
+ query?: string;
222
+ type?: MediaType;
223
+ format?: MediaFormat;
224
+ status?: MediaStatus;
225
+ season?: MediaSeason;
226
+ seasonYear?: number;
227
+ genre?: string;
228
+ tag?: string;
229
+ isAdult?: boolean;
230
+ sort?: MediaSort[];
231
+ page?: number;
232
+ perPage?: number;
233
+ }
234
+ interface SearchCharacterOptions {
235
+ query?: string;
236
+ sort?: CharacterSort[];
237
+ page?: number;
238
+ perPage?: number;
239
+ }
240
+ interface SearchStaffOptions {
241
+ query?: string;
242
+ page?: number;
243
+ perPage?: number;
244
+ }
245
+ interface PagedResult<T> {
246
+ pageInfo: PageInfo;
247
+ results: T[];
248
+ }
249
+ interface AniListClientOptions {
250
+ /** Optional AniList OAuth token for authenticated requests */
251
+ token?: string;
252
+ /** Custom API endpoint (defaults to https://graphql.anilist.co) */
253
+ apiUrl?: string;
254
+ /** Cache configuration (enabled by default, 24h TTL) */
255
+ cache?: {
256
+ /** Time-to-live in milliseconds (default: 86 400 000 = 24h) */
257
+ ttl?: number;
258
+ /** Maximum number of cached entries (default: 500, 0 = unlimited) */
259
+ maxSize?: number;
260
+ /** Set to false to disable caching entirely */
261
+ enabled?: boolean;
262
+ };
263
+ /** Rate limiter configuration (enabled by default, 85 req/min) */
264
+ rateLimit?: {
265
+ /** Max requests per window (default: 85) */
266
+ maxRequests?: number;
267
+ /** Window size in ms (default: 60 000) */
268
+ windowMs?: number;
269
+ /** Max retries on 429 (default: 3) */
270
+ maxRetries?: number;
271
+ /** Retry delay in ms when Retry-After header is absent (default: 2000) */
272
+ retryDelayMs?: number;
273
+ /** Set to false to disable rate limiting entirely */
274
+ enabled?: boolean;
275
+ };
276
+ }
277
+
278
+ /**
279
+ * Lightweight AniList GraphQL client with built-in caching and rate limiting.
280
+ *
281
+ * @example
282
+ * ```ts
283
+ * import { AniListClient } from "ani-client";
284
+ *
285
+ * const client = new AniListClient();
286
+ * const anime = await client.getMedia(1); // Cowboy Bebop
287
+ * console.log(anime.title.romaji);
288
+ *
289
+ * // Custom cache & rate limit options
290
+ * const client2 = new AniListClient({
291
+ * cache: { ttl: 1000 * 60 * 60 }, // 1 hour cache
292
+ * rateLimit: { maxRequests: 60 },
293
+ * });
294
+ * ```
295
+ */
296
+ declare class AniListClient {
297
+ private readonly apiUrl;
298
+ private readonly headers;
299
+ private readonly cache;
300
+ private readonly rateLimiter;
301
+ constructor(options?: AniListClientOptions);
302
+ /**
303
+ * @internal
304
+ */
305
+ private request;
306
+ /**
307
+ * Fetch a single media entry by its AniList ID.
308
+ *
309
+ * @param id - The AniList media ID
310
+ * @returns The media object
311
+ */
312
+ getMedia(id: number): Promise<Media>;
313
+ /**
314
+ * Search for anime or manga.
315
+ *
316
+ * @param options - Search / filter parameters
317
+ * @returns Paginated results with matching media
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * const results = await client.searchMedia({
322
+ * query: "Naruto",
323
+ * type: MediaType.ANIME,
324
+ * perPage: 5,
325
+ * });
326
+ * ```
327
+ */
328
+ searchMedia(options?: SearchMediaOptions): Promise<PagedResult<Media>>;
329
+ /**
330
+ * Get currently trending anime or manga.
331
+ *
332
+ * @param type - `MediaType.ANIME` or `MediaType.MANGA` (defaults to ANIME)
333
+ * @param page - Page number (default 1)
334
+ * @param perPage - Results per page (default 20, max 50)
335
+ */
336
+ getTrending(type?: MediaType, page?: number, perPage?: number): Promise<PagedResult<Media>>;
337
+ /**
338
+ * Fetch a character by AniList ID.
339
+ */
340
+ getCharacter(id: number): Promise<Character>;
341
+ /**
342
+ * Search for characters by name.
343
+ */
344
+ searchCharacters(options?: SearchCharacterOptions): Promise<PagedResult<Character>>;
345
+ /**
346
+ * Fetch a staff member by AniList ID.
347
+ */
348
+ getStaff(id: number): Promise<Staff>;
349
+ /**
350
+ * Search for staff (voice actors, directors, etc.).
351
+ */
352
+ searchStaff(options?: SearchStaffOptions): Promise<PagedResult<Staff>>;
353
+ /**
354
+ * Fetch a user by AniList ID.
355
+ */
356
+ getUser(id: number): Promise<User>;
357
+ /**
358
+ * Fetch a user by username.
359
+ */
360
+ getUserByName(name: string): Promise<User>;
361
+ /**
362
+ * Execute an arbitrary GraphQL query against the AniList API.
363
+ * Useful for advanced use-cases not covered by the built-in methods.
364
+ *
365
+ * @param query - A valid GraphQL query string
366
+ * @param variables - Optional variables object
367
+ */
368
+ raw<T = unknown>(query: string, variables?: Record<string, unknown>): Promise<T>;
369
+ /**
370
+ * Clear the entire response cache.
371
+ */
372
+ clearCache(): void;
373
+ /**
374
+ * Number of entries currently in the cache.
375
+ */
376
+ get cacheSize(): number;
377
+ }
378
+
379
+ /**
380
+ * Custom error class for AniList API errors.
381
+ */
382
+ declare class AniListError extends Error {
383
+ /** HTTP status code returned by the API */
384
+ readonly status: number;
385
+ /** Raw error body from the API response */
386
+ readonly errors: unknown[];
387
+ constructor(message: string, status: number, errors?: unknown[]);
388
+ }
389
+
390
+ /**
391
+ * Simple in-memory cache with configurable TTL.
392
+ * Used internally by AniListClient to avoid redundant API calls.
393
+ */
394
+ interface CacheOptions {
395
+ /** Time-to-live in milliseconds (default: 24 hours) */
396
+ ttl?: number;
397
+ /** Maximum number of entries to keep (default: 500, 0 = unlimited) */
398
+ maxSize?: number;
399
+ /** Disable caching entirely (default: false) */
400
+ enabled?: boolean;
401
+ }
402
+ declare class MemoryCache {
403
+ private readonly ttl;
404
+ private readonly maxSize;
405
+ private readonly enabled;
406
+ private readonly store;
407
+ constructor(options?: CacheOptions);
408
+ /** Build a deterministic cache key from a query + variables pair. */
409
+ static key(query: string, variables: Record<string, unknown>): string;
410
+ /** Retrieve a cached value, or `undefined` if missing / expired. */
411
+ get<T>(key: string): T | undefined;
412
+ /** Store a value in the cache. */
413
+ set<T>(key: string, data: T): void;
414
+ /** Remove a specific entry. */
415
+ delete(key: string): boolean;
416
+ /** Clear the entire cache. */
417
+ clear(): void;
418
+ /** Number of entries currently stored. */
419
+ get size(): number;
420
+ }
421
+
422
+ /**
423
+ * Rate limiter with automatic retry for AniList API.
424
+ *
425
+ * AniList allows 90 requests per minute.
426
+ * When a 429 (Too Many Requests) is received, the client
427
+ * waits for the Retry-After header and retries automatically.
428
+ */
429
+ interface RateLimitOptions {
430
+ /** Max requests per window (default: 85, conservative under AniList's 90/min) */
431
+ maxRequests?: number;
432
+ /** Window size in milliseconds (default: 60 000 = 1 minute) */
433
+ windowMs?: number;
434
+ /** Max number of retries on 429 responses (default: 3) */
435
+ maxRetries?: number;
436
+ /** Default retry delay in ms when Retry-After header is missing (default: 2000) */
437
+ retryDelayMs?: number;
438
+ /** Disable rate limiting entirely (default: false) */
439
+ enabled?: boolean;
440
+ }
441
+ declare class RateLimiter {
442
+ private readonly maxRequests;
443
+ private readonly windowMs;
444
+ private readonly maxRetries;
445
+ private readonly retryDelayMs;
446
+ private readonly enabled;
447
+ /** @internal */
448
+ private timestamps;
449
+ constructor(options?: RateLimitOptions);
450
+ /**
451
+ * Wait until it's safe to make a request (respects rate limit window).
452
+ */
453
+ acquire(): Promise<void>;
454
+ /**
455
+ * Execute a fetch with automatic retry on 429 responses.
456
+ */
457
+ fetchWithRetry(url: string, init: RequestInit): Promise<Response>;
458
+ private sleep;
459
+ }
460
+
461
+ export { AniListClient, type AniListClientOptions, AniListError, type CacheOptions, type Character, type CharacterImage, type CharacterName, CharacterSort, type FuzzyDate, type Media, type MediaCoverImage, MediaFormat, MediaSeason, MediaSort, MediaStatus, type MediaTag, type MediaTitle, type MediaTrailer, MediaType, MemoryCache, type PageInfo, type PagedResult, type RateLimitOptions, RateLimiter, type SearchCharacterOptions, type SearchMediaOptions, type SearchStaffOptions, type Staff, type StaffImage, type StaffName, type Studio, type StudioConnection, type User, type UserAvatar, type UserStatistics };