ani-client 2.1.0 → 2.1.1
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.
- package/README.md +134 -60
- package/dist/index.js +14 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -7,13 +7,10 @@
|
|
|
7
7
|
[](https://www.typescriptlang.org/)
|
|
8
8
|
[](LICENSE)
|
|
9
9
|
|
|
10
|
-
> A fully typed, zero-dependency client for the [AniList](https://anilist.co) GraphQL API.
|
|
10
|
+
> A fully typed, zero-dependency client for the [AniList](https://anilist.co) GraphQL API.
|
|
11
|
+
> Supports Node.js, Bun, Deno, and modern browsers.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
**Support server** [Discord server](https://discord.gg/3P7twDurUD)
|
|
15
|
-
|
|
16
|
-
## Highlights
|
|
13
|
+
## Features
|
|
17
14
|
|
|
18
15
|
- **Zero dependencies** — uses the native `fetch` API
|
|
19
16
|
- **Universal** — Node.js ≥ 20, Bun, Deno, and modern browsers
|
|
@@ -21,24 +18,23 @@
|
|
|
21
18
|
- **LRU cache** with TTL, stale-while-revalidate, and hit/miss stats
|
|
22
19
|
- **Rate-limit protection** with exponential backoff, retries, and custom strategies
|
|
23
20
|
- **Request deduplication** — concurrent identical queries share a single in-flight request
|
|
24
|
-
- **Batch queries** — fetch up to 50 media
|
|
25
|
-
- **
|
|
26
|
-
- **
|
|
27
|
-
- **AbortSignal support** — cancel globally or per-request with `withSignal()`
|
|
21
|
+
- **Batch queries** — fetch up to 50 media, characters, or staff in a single API call
|
|
22
|
+
- **Auto-pagination** — async iterator that yields items across all pages
|
|
23
|
+
- **AbortSignal support** — cancel globally or per-request via `withSignal()`
|
|
28
24
|
- **Injectable logger** — plug in `console`, pino, winston, or any compatible logger
|
|
29
|
-
- **Redis-ready** — swap the cache
|
|
25
|
+
- **Redis-ready** — swap the in-memory cache with the built-in `RedisCache` adapter
|
|
30
26
|
|
|
31
|
-
##
|
|
27
|
+
## Installation
|
|
32
28
|
|
|
33
29
|
```bash
|
|
34
30
|
npm install ani-client
|
|
35
|
-
#
|
|
31
|
+
# pnpm
|
|
36
32
|
pnpm add ani-client
|
|
37
|
-
#
|
|
33
|
+
# yarn
|
|
38
34
|
yarn add ani-client
|
|
39
35
|
```
|
|
40
36
|
|
|
41
|
-
## Quick
|
|
37
|
+
## Quick Start
|
|
42
38
|
|
|
43
39
|
```ts
|
|
44
40
|
import { AniListClient, MediaType } from "ani-client";
|
|
@@ -57,56 +53,51 @@ const results = await client.searchMedia({
|
|
|
57
53
|
perPage: 10,
|
|
58
54
|
});
|
|
59
55
|
|
|
60
|
-
//
|
|
56
|
+
// Lookup by MyAnimeList ID
|
|
61
57
|
const fma = await client.getMediaByMalId(5114);
|
|
62
|
-
|
|
63
|
-
// Paginated media relationships
|
|
64
|
-
const characters = await client.getMediaCharacters(1, { page: 1, perPage: 25, voiceActors: true });
|
|
65
|
-
const staff = await client.getMediaStaff(1, { page: 1, perPage: 25 });
|
|
66
58
|
```
|
|
67
59
|
|
|
68
|
-
##
|
|
60
|
+
## Usage
|
|
69
61
|
|
|
70
|
-
### Caching
|
|
62
|
+
### Caching
|
|
63
|
+
|
|
64
|
+
The client caches every response in memory by default. You can tune TTL, capacity, and enable stale-while-revalidate:
|
|
71
65
|
|
|
72
66
|
```ts
|
|
73
67
|
const client = new AniListClient({
|
|
74
68
|
cache: {
|
|
75
|
-
ttl: 1000 * 60 * 5,
|
|
76
|
-
maxSize: 200,
|
|
77
|
-
staleWhileRevalidateMs: 60_000,
|
|
69
|
+
ttl: 1000 * 60 * 5, // 5 min TTL
|
|
70
|
+
maxSize: 200, // LRU capacity
|
|
71
|
+
staleWhileRevalidateMs: 60_000, // serve stale for 1 min after expiry
|
|
78
72
|
},
|
|
79
73
|
});
|
|
80
74
|
|
|
81
|
-
// Check cache performance
|
|
82
75
|
console.log(client.cacheStats);
|
|
83
76
|
// { hits: 42, misses: 8, stales: 2, hitRate: 0.84 }
|
|
84
77
|
```
|
|
85
78
|
|
|
86
|
-
|
|
79
|
+
For distributed setups, swap to the built-in Redis adapter:
|
|
87
80
|
|
|
88
81
|
```ts
|
|
89
|
-
|
|
90
|
-
|
|
82
|
+
import { AniListClient, RedisCache } from "ani-client";
|
|
83
|
+
import { createClient } from "redis";
|
|
91
84
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
### Structured logging
|
|
85
|
+
const redis = createClient();
|
|
86
|
+
await redis.connect();
|
|
97
87
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
// debug: "Request complete" { durationMs: 120, status: 200 }
|
|
88
|
+
const client = new AniListClient({
|
|
89
|
+
cacheAdapter: new RedisCache(redis),
|
|
90
|
+
});
|
|
102
91
|
```
|
|
103
92
|
|
|
104
|
-
### Rate
|
|
93
|
+
### Rate Limiting
|
|
94
|
+
|
|
95
|
+
The client is pre-configured to stay within AniList's 30 req/min limit. You can override the defaults and provide a custom retry strategy:
|
|
105
96
|
|
|
106
97
|
```ts
|
|
107
98
|
const client = new AniListClient({
|
|
108
99
|
rateLimit: {
|
|
109
|
-
maxRequests:
|
|
100
|
+
maxRequests: 25,
|
|
110
101
|
windowMs: 60_000,
|
|
111
102
|
maxRetries: 3,
|
|
112
103
|
retryOnNetworkError: true,
|
|
@@ -114,17 +105,26 @@ const client = new AniListClient({
|
|
|
114
105
|
},
|
|
115
106
|
});
|
|
116
107
|
|
|
108
|
+
// Inspect the rate limit state after any request
|
|
117
109
|
console.log(client.rateLimitInfo);
|
|
118
|
-
// { remaining:
|
|
110
|
+
// { remaining: 22, limit: 25, reset: 1741104000 }
|
|
119
111
|
```
|
|
120
112
|
|
|
121
|
-
### Batch
|
|
113
|
+
### Batch Requests
|
|
114
|
+
|
|
115
|
+
Fetch multiple entries in a single API call (chunks of 50):
|
|
122
116
|
|
|
123
117
|
```ts
|
|
124
|
-
|
|
125
|
-
const
|
|
118
|
+
const anime = await client.getMediaBatch([1, 5, 6, 20]);
|
|
119
|
+
const characters = await client.getCharacterBatch([1, 2, 3]);
|
|
120
|
+
const staff = await client.getStaffBatch([95269, 95270]);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Auto-Pagination
|
|
124
|
+
|
|
125
|
+
Use the built-in async iterator to walk through all pages automatically:
|
|
126
126
|
|
|
127
|
-
|
|
127
|
+
```ts
|
|
128
128
|
for await (const anime of client.paginate(
|
|
129
129
|
(page) => client.searchMedia({ query: "Gundam", page, perPage: 50 }),
|
|
130
130
|
5, // max 5 pages
|
|
@@ -133,32 +133,106 @@ for await (const anime of client.paginate(
|
|
|
133
133
|
}
|
|
134
134
|
```
|
|
135
135
|
|
|
136
|
-
###
|
|
136
|
+
### Request Cancellation
|
|
137
|
+
|
|
138
|
+
Scope any client instance to an `AbortSignal` for per-request cancellation:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const controller = new AbortController();
|
|
142
|
+
const scoped = client.withSignal(controller.signal);
|
|
143
|
+
|
|
144
|
+
setTimeout(() => controller.abort(), 3_000);
|
|
145
|
+
const anime = await scoped.getMedia(1); // cancelled after 3s
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Logging
|
|
149
|
+
|
|
150
|
+
Pass any `console`-compatible logger to trace requests and cache events:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
const client = new AniListClient({ logger: console });
|
|
154
|
+
// debug: "API request" { variables: { id: 1 } }
|
|
155
|
+
// debug: "Request complete" { durationMs: 120 }
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Media Relationships
|
|
159
|
+
|
|
160
|
+
Paginated access to a media's characters and staff:
|
|
137
161
|
|
|
138
162
|
```ts
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
163
|
+
const characters = await client.getMediaCharacters(1, {
|
|
164
|
+
page: 1,
|
|
165
|
+
perPage: 25,
|
|
166
|
+
voiceActors: true,
|
|
167
|
+
});
|
|
168
|
+
|
|
143
169
|
const staff = await client.getMediaStaff(1, { page: 1, perPage: 25 });
|
|
144
|
-
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Users, Characters, Studios & More
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
const user = await client.getUser("AniList");
|
|
176
|
+
const favs = await client.getUserFavorites("AniList", { perPage: 50 });
|
|
177
|
+
const char = await client.getCharacter(1, { voiceActors: true });
|
|
178
|
+
const studio = await client.getStudio(21, { media: { perPage: 50 } });
|
|
145
179
|
const schedule = await client.getWeeklySchedule();
|
|
180
|
+
const review = await client.getReview(760);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Error Handling
|
|
184
|
+
|
|
185
|
+
All API errors throw an `AniListError` with a `status` code and the raw GraphQL `errors` array:
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
import { AniListError } from "ani-client";
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
await client.getMedia(999999999);
|
|
192
|
+
} catch (e) {
|
|
193
|
+
if (e instanceof AniListError) {
|
|
194
|
+
console.error(e.message); // "Not Found"
|
|
195
|
+
console.error(e.status); // 404
|
|
196
|
+
console.error(e.errors); // raw GraphQL errors array
|
|
197
|
+
}
|
|
198
|
+
}
|
|
146
199
|
```
|
|
147
200
|
|
|
201
|
+
### Lifecycle Hooks
|
|
202
|
+
|
|
203
|
+
Intercept requests, responses, cache events, and errors:
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
const client = new AniListClient({
|
|
207
|
+
hooks: {
|
|
208
|
+
onRequest: (query, variables) => console.log("→", variables),
|
|
209
|
+
onResponse: (query, durationMs, fromCache) => console.log(`← ${durationMs}ms`),
|
|
210
|
+
onCacheHit: (key) => console.log("cache hit", key),
|
|
211
|
+
onRateLimit: (retryAfterMs) => console.warn(`rate limited, retrying in ${retryAfterMs}ms`),
|
|
212
|
+
onError: (error) => console.error(error.message),
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Requirements
|
|
218
|
+
|
|
219
|
+
| Runtime | Version |
|
|
220
|
+
|----------|--------------------|
|
|
221
|
+
| Node.js | ≥ 20 |
|
|
222
|
+
| Bun | ≥ 1.0 |
|
|
223
|
+
| Deno | ≥ 1.28 |
|
|
224
|
+
| Browsers | `fetch` + `AbortController` required |
|
|
225
|
+
|
|
148
226
|
## Documentation
|
|
149
227
|
|
|
150
|
-
Full API reference, guides (caching, pagination,
|
|
228
|
+
Full API reference, configuration options, and guides (caching, pagination, hooks, Redis, etc.):
|
|
151
229
|
|
|
152
230
|
**[ani-client.js.org](https://ani-client.js.org)**
|
|
153
231
|
|
|
154
|
-
##
|
|
232
|
+
## Community
|
|
155
233
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
| Node.js | ≥ 20 |
|
|
159
|
-
| Bun | ≥ 1.0 |
|
|
160
|
-
| Deno | ≥ 1.28 |
|
|
161
|
-
| Browsers | Any with `fetch` + `AbortController` |
|
|
234
|
+
- 💬 [Discord server](https://discord.gg/3P7twDurUD)
|
|
235
|
+
- ✨ [Showcase — see who's using ani-client](https://ani-client.js.org/showcase)
|
|
162
236
|
|
|
163
237
|
## Contributing
|
|
164
238
|
|
|
@@ -166,4 +240,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, coding standards,
|
|
|
166
240
|
|
|
167
241
|
## License
|
|
168
242
|
|
|
169
|
-
[MIT](LICENSE) © gonzyui
|
|
243
|
+
[MIT](LICENSE) © [gonzyui](https://github.com/gonzyui)
|
package/dist/index.js
CHANGED
|
@@ -351,7 +351,7 @@ var AniListError = class _AniListError extends Error {
|
|
|
351
351
|
this.status = status;
|
|
352
352
|
this.errors = errors;
|
|
353
353
|
Object.setPrototypeOf(this, _AniListError.prototype);
|
|
354
|
-
if (Error
|
|
354
|
+
if ("captureStackTrace" in Error) {
|
|
355
355
|
Error.captureStackTrace(this, _AniListError);
|
|
356
356
|
}
|
|
357
357
|
}
|
|
@@ -468,18 +468,24 @@ var MEDIA_RECOMMENDATION_FIELDS = `
|
|
|
468
468
|
title { romaji english native userPreferred }
|
|
469
469
|
type
|
|
470
470
|
format
|
|
471
|
-
coverImage { large medium }
|
|
471
|
+
coverImage { extraLarge large medium color }
|
|
472
472
|
averageScore
|
|
473
473
|
meanScore
|
|
474
474
|
episodes
|
|
475
475
|
chapters
|
|
476
476
|
volumes
|
|
477
|
-
nextAiringEpisode
|
|
477
|
+
nextAiringEpisode {
|
|
478
|
+
id
|
|
479
|
+
airingAt
|
|
480
|
+
episode
|
|
481
|
+
mediaId
|
|
482
|
+
timeUntilAiring
|
|
483
|
+
}
|
|
478
484
|
season
|
|
479
485
|
seasonYear
|
|
480
|
-
startDate
|
|
481
|
-
|
|
482
|
-
studios
|
|
486
|
+
startDate { year month day }
|
|
487
|
+
endDate { year month day }
|
|
488
|
+
studios { nodes { id name isAnimationStudio siteUrl } }
|
|
483
489
|
genres
|
|
484
490
|
siteUrl
|
|
485
491
|
}
|
|
@@ -841,7 +847,7 @@ query (
|
|
|
841
847
|
timeUntilAiring
|
|
842
848
|
episode
|
|
843
849
|
mediaId
|
|
844
|
-
media
|
|
850
|
+
media {
|
|
845
851
|
${MEDIA_FIELDS_BASE}
|
|
846
852
|
}
|
|
847
853
|
}
|
|
@@ -2117,7 +2123,7 @@ function mapFavorites(fav) {
|
|
|
2117
2123
|
|
|
2118
2124
|
// src/client/index.ts
|
|
2119
2125
|
var DEFAULT_API_URL = "https://graphql.anilist.co";
|
|
2120
|
-
var LIB_VERSION = "2.1.
|
|
2126
|
+
var LIB_VERSION = "2.1.1" ;
|
|
2121
2127
|
var AniListClient = class {
|
|
2122
2128
|
apiUrl;
|
|
2123
2129
|
headers;
|