/
client.ts
383 lines (329 loc) · 8.46 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { join } from 'path'
import EventEmitter, { EventInterface } from '@rizzzi/eventemitter'
import { APIClient } from './api'
import { HeartBeatMonitor } from './heartbeat'
import { AnimeManager } from '../manager/anime'
import { MangaManager } from '../manager/manga'
import { ClubManager } from '../manager/club'
import { PersonManager } from '../manager/person'
import { CharacterManager } from '../manager/character'
import { GenreManager } from '../manager/genre'
import { MagazineManager } from '../manager/magazine'
import { ProducerManager } from '../manager/producer'
import { SeasonManager } from '../manager/season'
import { TopManager } from '../manager/top'
import { ScheduleManager } from '../manager/schedule'
import { UserManager } from '../manager/user'
import { RecommendationManager } from '../manager/recommendation'
import { ReviewManager } from '../manager/review'
export interface ClientOptions {
/**
* The hostname of the server.
*
* This option could be useful if you are hosting your own instance
* of Jikan REST API.
*
* Default value: `api.jikan.moe`
*/
host: string
/**
* The base pathname of each request.
*
* Default value: `v4`
*/
baseUri: string
/**
* Whether to use HTTPS protocol instead of HTTP.
*
* Default value: `false`
*/
secure: boolean
/**
* The number of miliseconds to wait before creating another request.
* This is to avoid getting rate-limited by
*
* Default value: `1200` (50 requests per minute)
*/
dataRateLimit: number
/**
* The number of miliseconds before the cache is expired. This is an
* effort to avoid sending multiple requests for the same content to
*
* Default value: `86400000` (1 day)
*/
dataExpiry: number
/**
* The number of items to be returned on each paginated request.
*
* Default value: `25`
*/
dataPaginationMaxSize: number
/**
* The number of miliseconds before giving up on a request.
*
* Default value: `15000` (15 seconds)
*/
requestTimeout: number
/**
* The maximum limit of requests in the queue. This is an effort to
* prevent clogging the queue.
*
* Default value: `100`
*/
requestQueueLimit: number
/**
* Whether to disable cache or not. It's recommended that this option is disabled
* to avoid sending multiple requests for the same content.
*/
disableCaching: boolean
/**
* The number of retries on HTTP 500 errors.
*/
maxApiErrorRetry: number
/**
* Whether to retry on HTTP 500 errors.
*/
retryOnApiError: boolean
/**
* Keep sockets around in a pool to be used by other requests in the future.
* Default value: `true`
*/
keepAlive: boolean
/**
* When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets
* being kept alive. Only relevant if keepAlive is set to true.
*
* Default value: `60000`
*/
keepAliveMsecs: number
/**
* Where to store cache from Jikan API
*/
dataPath?: string
}
export interface ClientEvents extends EventInterface {
debug: [scope: string, message: string]
}
export type ClientEventNames = keyof ClientEvents
export class Client {
/** @hidden */
private static setOptions (options?: Partial<ClientOptions>): ClientOptions {
const defaultOptions: ClientOptions = {
dataPath: (() => { try { return join(__dirname, '..', '..', '..', '.Jikan') } catch {} })(),
host: 'api.jikan.moe',
baseUri: 'v4',
secure: true,
dataRateLimit: 1200, // 50 API requests a minute
dataExpiry: 1000 * 60 * 60 * 24, // 1 day expiration
dataPaginationMaxSize: 25,
requestTimeout: 15000,
requestQueueLimit: 100,
maxApiErrorRetry: 5,
retryOnApiError: true,
keepAlive: true,
keepAliveMsecs: 60000,
disableCaching: false
}
return Object.assign(defaultOptions, options)
}
/**
* Current options of the client.
*
* You can change client options anytime.
*/
public readonly options: ClientOptions
/** @hidden */
public readonly APIClient: APIClient
/**
* Anime resource context.
*
* @example
* ```ts
* const anime = await client.anime.get(5)
* const episodes = await anime.getEpisodes()
*
* console.log(anime, episodes)
* ```
*/
public readonly anime: AnimeManager
/**
* Manga resource context.
*
* @example
* ```ts
* const manga = await client.manga.get(4)
* const characters = await manga.getCharacters()
*
* console.log(manga, characters)
* ```
*/
public readonly manga: MangaManager
/**
* Clubs resource context.
*
* @example
* ```ts
* const club = await client.clubs.get(<id>)
*
* console.log(club.mmeberCount)
* ```
*/
public readonly clubs: ClubManager
/**
* People resource context.
*
* @example
* ```ts
* const person = await client.people.get(<id>)
* const pictures = await person.getPictures()
*
* console.log(`${person.name}`, pictures)
* ```
*/
public readonly people: PersonManager
/**
* Characters resource context.
*
* @example
* ```ts
* const character = await client.characters.get(1)
* const voiceActors = await character.getVoiceActors()
*
* console.log(character, voiceActors)
* ```
*/
public readonly characters: CharacterManager
/**
* Genres resource context.
*
* @example
* ```ts
* const genres = await client.genres.list()
*
* console.log(genres)
* ```
*/
public readonly genres: GenreManager
/**
* Magazines resource context.
*
* @example
* ```ts
* const magazines = await client.magazines.list()
*
* console.log(magazines)
* ```
*/
public readonly magazines: MagazineManager
/**
* Producers resource context.
*
* @example
* ```ts
* const producers = await client.producers.list()
*
* console.log(producers)
* ```
*/
public readonly producers: ProducerManager
public readonly recommendations: RecommendationManager
public readonly reviews: ReviewManager
public readonly users: UserManager
/**
* Seasons resource context.
*
* @example
* ```ts
* const seasons = await client.seasons.list()
*
* console.log(seasons)
* ```
*/
public readonly seasons: SeasonManager
public readonly top: TopManager
public readonly schedules: ScheduleManager
/**
* Check if MAL is down.
*
* @example
* ```ts
* const heartbeat = await client.heartbeat.check()
*
* if (heartbeat.down)
* console.warn('MAL is down!')
* ```
*/
public readonly heartbeat: HeartBeatMonitor
/** @hidden */
public readonly events: EventEmitter<ClientEvents>
/**
* Listen to client events.
*
* @example
* ```ts
* client.on('debug', console.log)
* ```
*/
public on: EventEmitter<ClientEvents>['on']
/**
* Listen to client events once.
*
* @example
* ```ts
* client.once('debug', console.log)
* ```
*/
public once: EventEmitter<ClientEvents>['on']
/**
* Remove a listener.
*
* @example
* ```ts
* client.off('debug', console.log)
* ```
*/
public off: EventEmitter<ClientEvents>['off']
/** @hidden */
public readonly emit: EventEmitter<ClientEvents>['emit']
/** @hidden */
public debug (scope: string, message: string) {
return this.emit('debug', scope, message)
}
/**
* Instantiate new Jikan client
*
* @param options - Client options
* @example
* ```ts
* const Jikan = require('jikan4.js')
* const client = new Client()
*
* console.log(await client.anime.get(5))
* ```
*/
public constructor (options?: Partial<ClientOptions>) {
this.options = Client.setOptions(options)
this.APIClient = new APIClient(this)
this.anime = new AnimeManager(this)
this.manga = new MangaManager(this)
this.clubs = new ClubManager(this)
this.people = new PersonManager(this)
this.characters = new CharacterManager(this)
this.genres = new GenreManager(this)
this.magazines = new MagazineManager(this)
this.producers = new ProducerManager(this)
this.recommendations = new RecommendationManager(this)
this.reviews = new ReviewManager(this)
this.users = new UserManager(this)
this.seasons = new SeasonManager(this)
this.top = new TopManager(this)
this.schedules = new ScheduleManager(this)
this.heartbeat = new HeartBeatMonitor(this)
this.events = new EventEmitter()
const { on, once, emit, off } = this.events.bind()
this.on = on
this.once = once
this.emit = emit
this.off = off
}
}