@adbjs/sdk 0.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/dist/index.cjs ADDED
@@ -0,0 +1,687 @@
1
+ 'use strict';
2
+
3
+ var wretch = require('wretch');
4
+
5
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
+
7
+ var wretch__default = /*#__PURE__*/_interopDefault(wretch);
8
+
9
+ // src/client.ts
10
+
11
+ // src/errors.ts
12
+ var AllDebridError = class _AllDebridError extends Error {
13
+ code;
14
+ isAllDebridError = true;
15
+ constructor(code, message) {
16
+ super(message);
17
+ this.name = "AllDebridError";
18
+ this.code = code;
19
+ if ("captureStackTrace" in Error && typeof Error.captureStackTrace === "function") {
20
+ Error.captureStackTrace(this, _AllDebridError);
21
+ }
22
+ }
23
+ /**
24
+ * Create an AllDebridError from an API error response
25
+ */
26
+ static fromApiError(error) {
27
+ return new _AllDebridError(error.code, error.message);
28
+ }
29
+ };
30
+ var AuthenticationError = class extends AllDebridError {
31
+ constructor(code, message) {
32
+ super(code, message);
33
+ this.name = "AuthenticationError";
34
+ }
35
+ };
36
+ var LinkError = class extends AllDebridError {
37
+ constructor(code, message) {
38
+ super(code, message);
39
+ this.name = "LinkError";
40
+ }
41
+ };
42
+ var MagnetError = class extends AllDebridError {
43
+ constructor(code, message) {
44
+ super(code, message);
45
+ this.name = "MagnetError";
46
+ }
47
+ };
48
+ var NetworkError = class extends AllDebridError {
49
+ constructor(message, statusCode) {
50
+ super("NETWORK_ERROR", message);
51
+ this.statusCode = statusCode;
52
+ this.name = "NetworkError";
53
+ }
54
+ };
55
+ function createTypedError(code, message) {
56
+ if (code.startsWith("AUTH_")) {
57
+ return new AuthenticationError(code, message);
58
+ }
59
+ if (code.startsWith("LINK_") || code.startsWith("REDIRECTOR_") || code.startsWith("STREAM_") || code.startsWith("DELAYED_")) {
60
+ return new LinkError(code, message);
61
+ }
62
+ if (code.startsWith("MAGNET_")) {
63
+ return new MagnetError(code, message);
64
+ }
65
+ return new AllDebridError(code, message);
66
+ }
67
+
68
+ // src/base-resource.ts
69
+ var BaseResource = class {
70
+ constructor(client) {
71
+ this.client = client;
72
+ }
73
+ /**
74
+ * Make a GET request
75
+ * @param T - The generated response type (e.g., GetLinkUnlockResponse)
76
+ * @returns The extracted data from the response (without the { status, data } wrapper)
77
+ */
78
+ async get(path, params) {
79
+ const clientAny = this.client;
80
+ return clientAny.get(path, params);
81
+ }
82
+ /**
83
+ * Make a POST request
84
+ * @param T - The generated response type
85
+ * @returns The extracted data from the response (without the { status, data } wrapper)
86
+ */
87
+ async post(path, body, params) {
88
+ const clientAny = this.client;
89
+ return clientAny.post(path, body, params);
90
+ }
91
+ /**
92
+ * Make a POST request with FormData (multipart/form-data)
93
+ * @param T - The generated response type
94
+ * @returns The extracted data from the response (without the { status, data } wrapper)
95
+ */
96
+ async postFormData(path, formData, params) {
97
+ const clientAny = this.client;
98
+ return clientAny.postFormData(path, formData, params);
99
+ }
100
+ };
101
+
102
+ // src/resources/host.ts
103
+ var HostResource = class extends BaseResource {
104
+ /**
105
+ * Get list of all supported hosts with their information
106
+ *
107
+ * @param hostOnly - If true, only return hosts (exclude streams and redirectors)
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const hosts = await client.host.list()
112
+ * console.log(hosts.hosts) // All supported file hosts
113
+ * console.log(hosts.streams) // Streaming hosts
114
+ * console.log(hosts.redirectors) // Link redirectors
115
+ * ```
116
+ */
117
+ async list(hostOnly) {
118
+ const params = hostOnly ? { hostOnly: "1" } : void 0;
119
+ return this.get("/hosts", params);
120
+ }
121
+ /**
122
+ * Get array of all supported domain names
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * const domains = await client.host.domains()
127
+ * console.log(domains) // ['rapidgator.net', 'uploaded.net', ...]
128
+ * ```
129
+ */
130
+ async domains() {
131
+ return this.get("/hosts/domains");
132
+ }
133
+ /**
134
+ * Get hosts ordered by restriction level (priority list)
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * const priority = await client.host.priority()
139
+ * console.log(priority.hosts) // Ordered list with restriction levels
140
+ * ```
141
+ */
142
+ async priority() {
143
+ return this.get("/hosts/priority");
144
+ }
145
+ };
146
+
147
+ // src/resources/link.ts
148
+ var LinkResource = class extends BaseResource {
149
+ /**
150
+ * Get information about one or more links
151
+ *
152
+ * @param links - Single link or array of links to get info for
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * const info = await client.link.infos('https://example.com/file.zip')
157
+ * console.log(info)
158
+ * ```
159
+ */
160
+ async infos(links) {
161
+ const linksArray = Array.isArray(links) ? links : [links];
162
+ const data = await this.get("/link/infos", {
163
+ link: linksArray
164
+ });
165
+ return data?.infos;
166
+ }
167
+ /**
168
+ * Extract links from redirectors/link protectors
169
+ *
170
+ * @param link - The redirector link to extract from
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * const links = await client.link.redirector('https://linkprotector.com/abc123')
175
+ * ```
176
+ */
177
+ async redirector(link) {
178
+ const data = await this.get("/link/redirector", {
179
+ link
180
+ });
181
+ return data?.links;
182
+ }
183
+ /**
184
+ * Unlock a download link
185
+ *
186
+ * @param link - The link to unlock
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * const result = await client.link.unlock('https://example.com/file.zip')
191
+ * if (result.link) {
192
+ * console.log('Direct link:', result.link)
193
+ * } else if (result.delayed) {
194
+ * // Handle delayed generation
195
+ * const delayedResult = await client.link.delayed(result.delayed)
196
+ * }
197
+ * ```
198
+ */
199
+ async unlock(link) {
200
+ return this.get("/link/unlock", {
201
+ link
202
+ });
203
+ }
204
+ /**
205
+ * Unlock a link and automatically poll if delayed
206
+ * Note: The API response format doesn't include a delayed field in the current OpenAPI spec.
207
+ * This method will be updated once the delayed mechanism is documented.
208
+ *
209
+ * @param link - The link to unlock
210
+ * @param _options - Polling options (currently unused)
211
+ *
212
+ * @example
213
+ * ```ts
214
+ * const result = await client.link.unlockWithPolling('https://example.com/file.zip')
215
+ * console.log('Direct link:', result.link)
216
+ * ```
217
+ */
218
+ async unlockWithPolling(link, _options = {}) {
219
+ return this.unlock(link);
220
+ }
221
+ /**
222
+ * Get streaming options for a generated link
223
+ *
224
+ * @param id - The generated link ID or streaming ID
225
+ *
226
+ * @example
227
+ * ```ts
228
+ * const streams = await client.link.streaming('abc123')
229
+ * ```
230
+ */
231
+ async streaming(id) {
232
+ return this.get("/link/streaming", {
233
+ id
234
+ });
235
+ }
236
+ /**
237
+ * Get the status/result of a delayed link generation
238
+ *
239
+ * @param id - The delayed generation ID
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * const result = await client.link.delayed('delayed_id_123')
244
+ * ```
245
+ */
246
+ async delayed(id) {
247
+ return this.get("/link/delayed", {
248
+ id
249
+ });
250
+ }
251
+ };
252
+
253
+ // src/resources/magnet.ts
254
+ var MagnetResource = class extends BaseResource {
255
+ /**
256
+ * Upload magnets by URI or hash
257
+ *
258
+ * @param magnets - Single magnet URI/hash or array of magnets
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * const result = await client.magnet.upload('magnet:?xt=urn:btih:...')
263
+ * console.log('Magnet ID:', result.magnets[0].id)
264
+ * ```
265
+ */
266
+ async upload(magnets) {
267
+ const magnetsArray = Array.isArray(magnets) ? magnets : [magnets];
268
+ return this.get("/magnet/upload", {
269
+ magnets: magnetsArray
270
+ });
271
+ }
272
+ /**
273
+ * Upload a torrent file
274
+ *
275
+ * @param file - The torrent file (Blob or File)
276
+ * @param filename - Optional filename (defaults to 'torrent.torrent' for Blob, or file.name for File)
277
+ *
278
+ * @example
279
+ * ```ts
280
+ * const file = new File([buffer], 'torrent.torrent')
281
+ * const result = await client.magnet.uploadFile(file)
282
+ *
283
+ * // Or with a Blob and custom filename
284
+ * const blob = new Blob([buffer])
285
+ * const result = await client.magnet.uploadFile(blob, 'my-torrent.torrent')
286
+ * ```
287
+ */
288
+ async uploadFile(file, filename) {
289
+ const formData = new FormData();
290
+ const actualFilename = filename || (file instanceof File ? file.name : "torrent.torrent");
291
+ formData.append("files[]", file, actualFilename);
292
+ return this.postFormData("/magnet/upload/file", formData);
293
+ }
294
+ /**
295
+ * Get the status of one or more magnets
296
+ *
297
+ * @param id - Optional magnet ID to get status for a specific magnet
298
+ *
299
+ * @example
300
+ * ```ts
301
+ * // Get all magnets status
302
+ * const allStatus = await client.magnet.status()
303
+ *
304
+ * // Get specific magnet status
305
+ * const status = await client.magnet.status('123')
306
+ * console.log(status.magnets[0]?.status) // 'Downloading', 'Ready', etc.
307
+ * ```
308
+ */
309
+ async status(id) {
310
+ const params = id ? { id } : void 0;
311
+ const data = await this.get("/magnet/status", params);
312
+ if (data?.magnets && !Array.isArray(data.magnets)) {
313
+ return {
314
+ ...data,
315
+ magnets: [data.magnets]
316
+ };
317
+ }
318
+ return data;
319
+ }
320
+ /**
321
+ * Delete a magnet
322
+ *
323
+ * @param id - The magnet ID to delete (string)
324
+ *
325
+ * @example
326
+ * ```ts
327
+ * await client.magnet.delete('123')
328
+ * ```
329
+ */
330
+ async delete(id) {
331
+ return this.get("/magnet/delete", {
332
+ id
333
+ });
334
+ }
335
+ /**
336
+ * Restart one or more failed magnets
337
+ *
338
+ * @param ids - Single magnet ID or array of magnet IDs to restart (strings)
339
+ *
340
+ * @example
341
+ * ```ts
342
+ * // Restart single magnet
343
+ * await client.magnet.restart('123')
344
+ *
345
+ * // Restart multiple magnets
346
+ * await client.magnet.restart(['123', '456'])
347
+ * ```
348
+ */
349
+ async restart(ids) {
350
+ const idsArray = Array.isArray(ids) ? ids : [ids];
351
+ return this.get("/magnet/restart", {
352
+ ids: idsArray
353
+ });
354
+ }
355
+ /**
356
+ * Check instant availability of magnets
357
+ *
358
+ * @param magnets - Single magnet hash or array of hashes
359
+ *
360
+ * @example
361
+ * ```ts
362
+ * const available = await client.magnet.instant(['hash1', 'hash2'])
363
+ * console.log(available.magnets)
364
+ * ```
365
+ */
366
+ async instant(magnets) {
367
+ const magnetsArray = Array.isArray(magnets) ? magnets : [magnets];
368
+ return this.get("/magnet/instant", {
369
+ magnets: magnetsArray
370
+ });
371
+ }
372
+ /**
373
+ * Watch a magnet's status with automatic polling
374
+ *
375
+ * @param id - The magnet ID to watch (string)
376
+ * @param options - Watch options
377
+ *
378
+ * @example
379
+ * ```ts
380
+ * await client.magnet.watch('123', {
381
+ * onUpdate: (status) => console.log('Status:', status.magnets[0]?.status),
382
+ * stopOnStatus: 'Ready'
383
+ * })
384
+ * ```
385
+ */
386
+ async watch(id, options = {}) {
387
+ const { interval = 3e3, maxAttempts = 0, onUpdate, stopOnStatus = "Ready" } = options;
388
+ let attempt = 0;
389
+ while (maxAttempts === 0 || attempt < maxAttempts) {
390
+ attempt++;
391
+ const status = await this.status(id);
392
+ onUpdate?.(status);
393
+ const magnet = status?.magnets?.[0];
394
+ if (magnet?.status === stopOnStatus) {
395
+ return status;
396
+ }
397
+ await new Promise((resolve) => setTimeout(resolve, interval));
398
+ }
399
+ throw new Error(
400
+ `Watch timeout: magnet did not reach '${stopOnStatus}' status after ${maxAttempts} attempts`
401
+ );
402
+ }
403
+ };
404
+
405
+ // src/resources/user.ts
406
+ var UserResource = class extends BaseResource {
407
+ /**
408
+ * Get user profile information including premium status and quotas
409
+ *
410
+ * @example
411
+ * ```ts
412
+ * const user = await client.user.getInfo()
413
+ * console.log(user.username, user.isPremium)
414
+ * ```
415
+ */
416
+ async getInfo() {
417
+ const data = await this.get("/user");
418
+ return data?.user;
419
+ }
420
+ /**
421
+ * Get available hosts for the current user based on their subscription
422
+ *
423
+ * @param hostOnly - If true, only return hosts data (exclude streams and redirectors)
424
+ *
425
+ * @example
426
+ * ```ts
427
+ * const hosts = await client.user.getHosts()
428
+ * console.log(Object.keys(hosts.hosts))
429
+ * ```
430
+ */
431
+ async getHosts(hostOnly) {
432
+ const params = hostOnly ? { hostOnly: "1" } : void 0;
433
+ return this.get("/user/hosts", params);
434
+ }
435
+ /**
436
+ * Clear a specific notification by code
437
+ *
438
+ * @param code - The notification code to clear
439
+ *
440
+ * @example
441
+ * ```ts
442
+ * await client.user.clearNotification('SOME_NOTIF_CODE')
443
+ * ```
444
+ */
445
+ async clearNotification(code) {
446
+ await this.get("/user/notification/clear", { code });
447
+ }
448
+ // ============================================
449
+ // Saved Links Management
450
+ // ============================================
451
+ /**
452
+ * Get all saved links
453
+ *
454
+ * @example
455
+ * ```ts
456
+ * const savedLinks = await client.user.getLinks()
457
+ * console.log(savedLinks.links)
458
+ * ```
459
+ */
460
+ async getLinks() {
461
+ return this.get("/user/links");
462
+ }
463
+ /**
464
+ * Save a link for later use
465
+ *
466
+ * @param link - The link to save
467
+ *
468
+ * @example
469
+ * ```ts
470
+ * await client.user.saveLink('https://example.com/file.zip')
471
+ * ```
472
+ */
473
+ async saveLink(link) {
474
+ return this.get("/user/links/save", { link });
475
+ }
476
+ /**
477
+ * Delete a saved link
478
+ *
479
+ * @param link - The link to delete (can be the saved link ID or URL)
480
+ *
481
+ * @example
482
+ * ```ts
483
+ * await client.user.deleteLink('saved-link-id')
484
+ * ```
485
+ */
486
+ async deleteLink(link) {
487
+ return this.get("/user/links/delete", { link });
488
+ }
489
+ // ============================================
490
+ // History Management
491
+ // ============================================
492
+ /**
493
+ * Get user history (if enabled in account settings)
494
+ *
495
+ * @example
496
+ * ```ts
497
+ * const history = await client.user.getHistory()
498
+ * console.log(history.links)
499
+ * ```
500
+ */
501
+ async getHistory() {
502
+ return this.get("/user/history");
503
+ }
504
+ /**
505
+ * Clear user history
506
+ *
507
+ * @example
508
+ * ```ts
509
+ * await client.user.clearHistory()
510
+ * ```
511
+ */
512
+ async clearHistory() {
513
+ return this.get("/user/history/delete");
514
+ }
515
+ };
516
+
517
+ // src/client.ts
518
+ var DEFAULT_BASE_URL = "https://api.alldebrid.com/v4";
519
+ var DEFAULT_AGENT = "@alldebrid/sdk";
520
+ var DEFAULT_TIMEOUT = 3e4;
521
+ var DEFAULT_MAX_RETRIES = 3;
522
+ var AllDebridClient = class {
523
+ config;
524
+ /**
525
+ * User resource for managing user account
526
+ */
527
+ user;
528
+ /**
529
+ * Link resource for unlocking and managing download links
530
+ */
531
+ link;
532
+ /**
533
+ * Magnet resource for managing torrents
534
+ */
535
+ magnet;
536
+ /**
537
+ * Host resource for getting information about supported hosts
538
+ */
539
+ host;
540
+ constructor(config) {
541
+ this.config = {
542
+ apiKey: config.apiKey,
543
+ agent: config.agent ?? DEFAULT_AGENT,
544
+ baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
545
+ timeout: config.timeout ?? DEFAULT_TIMEOUT,
546
+ retry: config.retry ?? true,
547
+ maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES
548
+ };
549
+ this.user = new UserResource(this);
550
+ this.link = new LinkResource(this);
551
+ this.magnet = new MagnetResource(this);
552
+ this.host = new HostResource(this);
553
+ }
554
+ /**
555
+ * Build query string from params
556
+ */
557
+ buildUrl(path, params) {
558
+ const allParams = {
559
+ agent: this.config.agent,
560
+ ...params
561
+ };
562
+ const query = new URLSearchParams();
563
+ for (const [key, value] of Object.entries(allParams)) {
564
+ if (value !== void 0 && value !== null) {
565
+ if (Array.isArray(value)) {
566
+ value.forEach((v) => query.append(`${key}[]`, String(v)));
567
+ } else {
568
+ query.append(key, String(value));
569
+ }
570
+ }
571
+ }
572
+ const queryString = query.toString();
573
+ return queryString ? `${path}?${queryString}` : path;
574
+ }
575
+ /**
576
+ * Make a GET request
577
+ * @param T - The generated response type (e.g., GetLinkUnlockResponse)
578
+ * @returns The extracted data from the response (without the { status, data } wrapper)
579
+ */
580
+ async get(path, params) {
581
+ try {
582
+ const url = this.buildUrl(path, params);
583
+ const json = await wretch__default.default(this.config.baseUrl).auth(`Bearer ${this.config.apiKey}`).url(url).get().json();
584
+ if (json.status === "error" && json.error) {
585
+ throw createTypedError(json.error.code, json.error.message);
586
+ }
587
+ return json.data;
588
+ } catch (error) {
589
+ if (error.status === 401) {
590
+ throw createTypedError("AUTH_BAD_APIKEY", "Invalid API key or unauthorized access");
591
+ }
592
+ if (error.status === 403) {
593
+ throw createTypedError("AUTH_BLOCKED", "Access forbidden");
594
+ }
595
+ if (error.status === 429) {
596
+ throw createTypedError("RATE_LIMITED", "Too many requests");
597
+ }
598
+ if (error.isAllDebridError) {
599
+ throw error;
600
+ }
601
+ throw new NetworkError(error.message || "Network error occurred", error.status);
602
+ }
603
+ }
604
+ /**
605
+ * Make a POST request
606
+ * @param T - The generated response type
607
+ * @returns The extracted data from the response (without the { status, data } wrapper)
608
+ */
609
+ async post(path, body, params) {
610
+ try {
611
+ const url = this.buildUrl(path, params);
612
+ const json = await wretch__default.default(this.config.baseUrl).auth(`Bearer ${this.config.apiKey}`).url(url).post(body).json();
613
+ if (json.status === "error" && json.error) {
614
+ throw createTypedError(json.error.code, json.error.message);
615
+ }
616
+ return json.data;
617
+ } catch (error) {
618
+ if (error.status === 401) {
619
+ throw createTypedError("AUTH_BAD_APIKEY", "Invalid API key or unauthorized access");
620
+ }
621
+ if (error.status === 403) {
622
+ throw createTypedError("AUTH_BLOCKED", "Access forbidden");
623
+ }
624
+ if (error.status === 429) {
625
+ throw createTypedError("RATE_LIMITED", "Too many requests");
626
+ }
627
+ if (error.isAllDebridError) {
628
+ throw error;
629
+ }
630
+ throw new NetworkError(error.message || "Network error occurred", error.status);
631
+ }
632
+ }
633
+ /**
634
+ * Make a POST request with FormData (multipart/form-data)
635
+ * @param T - The generated response type
636
+ * @returns The extracted data from the response (without the { status, data } wrapper)
637
+ */
638
+ async postFormData(path, formData, params) {
639
+ try {
640
+ const url = this.buildUrl(path, params);
641
+ const json = await wretch__default.default(this.config.baseUrl).auth(`Bearer ${this.config.apiKey}`).url(url).body(formData).post().json();
642
+ if (json.status === "error" && json.error) {
643
+ throw createTypedError(json.error.code, json.error.message);
644
+ }
645
+ return json.data;
646
+ } catch (error) {
647
+ if (error.status === 401) {
648
+ throw createTypedError("AUTH_BAD_APIKEY", "Invalid API key or unauthorized access");
649
+ }
650
+ if (error.status === 403) {
651
+ throw createTypedError("AUTH_BLOCKED", "Access forbidden");
652
+ }
653
+ if (error.status === 429) {
654
+ throw createTypedError("RATE_LIMITED", "Too many requests");
655
+ }
656
+ if (error.isAllDebridError) {
657
+ throw error;
658
+ }
659
+ throw new NetworkError(error.message || "Network error occurred", error.status);
660
+ }
661
+ }
662
+ /**
663
+ * Test the API connection
664
+ */
665
+ async ping() {
666
+ try {
667
+ await this.get("/user");
668
+ return true;
669
+ } catch {
670
+ return false;
671
+ }
672
+ }
673
+ };
674
+
675
+ exports.AllDebridClient = AllDebridClient;
676
+ exports.AllDebridError = AllDebridError;
677
+ exports.AuthenticationError = AuthenticationError;
678
+ exports.HostResource = HostResource;
679
+ exports.LinkError = LinkError;
680
+ exports.LinkResource = LinkResource;
681
+ exports.MagnetError = MagnetError;
682
+ exports.MagnetResource = MagnetResource;
683
+ exports.NetworkError = NetworkError;
684
+ exports.UserResource = UserResource;
685
+ exports.createTypedError = createTypedError;
686
+ //# sourceMappingURL=index.cjs.map
687
+ //# sourceMappingURL=index.cjs.map