@ctrl/rqbit 2.0.0 → 2.0.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 CHANGED
@@ -111,6 +111,22 @@ const result = await client.normalizedAddTorrent('magnet:?xt=urn:btih:...', {
111
111
  console.log(result);
112
112
  ```
113
113
 
114
+ ##### Errors
115
+
116
+ Failed requests throw a `TorrentClientError` from [@ctrl/shared-torrent](https://github.com/scttcper/shared-torrent) with a `code` of `torrent_not_found`, `unauthorized`, `request_failed` or `client_error`, the http `status` when there is one and the original error as the `cause`. rqbit errors are a `RqbitApiError`, a `TorrentClientError` that also has rqbit's error `kind`.
117
+
118
+ ```ts
119
+ import { TorrentClientError } from '@ctrl/rqbit';
120
+
121
+ try {
122
+ await client.getTorrent('torrent-hash');
123
+ } catch (error) {
124
+ if (error instanceof TorrentClientError && error.code === 'torrent_not_found') {
125
+ // not in the client
126
+ }
127
+ }
128
+ ```
129
+
114
130
  ##### export and create from state
115
131
 
116
132
  rqbit uses basic auth on every request so there is no session to save, this exists to match the other clients.
@@ -1,3 +1,4 @@
1
1
  export * from './rqbit.js';
2
2
  export * from './normalizeTorrentData.js';
3
3
  export type * from './types.js';
4
+ export { TorrentClientError, type TorrentClientErrorCode } from '@ctrl/shared-torrent';
package/dist/src/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './rqbit.js';
2
2
  export * from './normalizeTorrentData.js';
3
+ export { TorrentClientError } from '@ctrl/shared-torrent';
@@ -1,4 +1,4 @@
1
- import type { AddTorrentOptions as NormalizedAddTorrentOptions, AllClientData, NormalizedTorrent, TorrentClient, TorrentClientConfig, TorrentClientState } from '@ctrl/shared-torrent';
1
+ import { type AddTorrentOptions as NormalizedAddTorrentOptions, type AllClientData, type NormalizedTorrent, type TorrentClient, type TorrentClientConfig, TorrentClientError, type TorrentClientState } from '@ctrl/shared-torrent';
2
2
  import { type FetchOptions, type MappedResponseType, type ResponseType } from 'ofetch';
3
3
  import type { Jsonify } from 'type-fest';
4
4
  import type { AddTorrentOptions, AddTorrentResponse, ApiRootResponse, CreateTorrentOptions, CreateTorrentResponse, DhtStats, EmptyResponse, ListTorrentsResponse, PeerStatsResponse, RateLimits, RqbitErrorResponse, SessionStats, TorrentDetails, TorrentIdOrHash, TorrentListItem, TorrentStats, TorrentWithStats } from './types.js';
@@ -21,12 +21,11 @@ export type ResolvedConfig = RqbitConfig & {
21
21
  /**
22
22
  * Error thrown when a request to rqbit fails, either an error response or a timeout/network error
23
23
  */
24
- export declare class RqbitApiError extends Error {
24
+ /**
25
+ * A {@link TorrentClientError} with rqbit's error details
26
+ */
27
+ export declare class RqbitApiError extends TorrentClientError {
25
28
  name: string;
26
- /**
27
- * HTTP status code, undefined when no response was received (timeout or network error)
28
- */
29
- status?: number;
30
29
  /**
31
30
  * rqbit error kind, ex - `torrent_not_found`. Undefined when rqbit returned a plain text error.
32
31
  */
package/dist/src/rqbit.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { TorrentClientError, } from '@ctrl/shared-torrent';
1
2
  import { FetchError, ofetch, } from 'ofetch';
2
3
  import { joinURL } from 'ufo';
3
4
  import { base64ToUint8Array, stringToBase64 } from 'uint8array-extras';
@@ -13,24 +14,28 @@ const defaults = {
13
14
  /**
14
15
  * Error thrown when a request to rqbit fails, either an error response or a timeout/network error
15
16
  */
16
- export class RqbitApiError extends Error {
17
+ /**
18
+ * A {@link TorrentClientError} with rqbit's error details
19
+ */
20
+ export class RqbitApiError extends TorrentClientError {
17
21
  name = 'RqbitApiError';
18
- /**
19
- * HTTP status code, undefined when no response was received (timeout or network error)
20
- */
21
- status;
22
22
  /**
23
23
  * rqbit error kind, ex - `torrent_not_found`. Undefined when rqbit returned a plain text error.
24
24
  */
25
25
  kind;
26
26
  response;
27
27
  constructor(status, message, response, cause) {
28
- super(message, { cause });
29
- this.status = status;
28
+ super(message, rqbitErrorCode(status, response?.error_kind), { status, cause });
30
29
  this.kind = response?.error_kind;
31
30
  this.response = response;
32
31
  }
33
32
  }
33
+ function rqbitErrorCode(status, kind) {
34
+ if (status === 401 || status === 403) {
35
+ return 'unauthorized';
36
+ }
37
+ return kind === 'torrent_not_found' ? 'torrent_not_found' : 'request_failed';
38
+ }
34
39
  /**
35
40
  * rqbit has no published api reference. `GET /` on the server lists every endpoint, see {@link Rqbit.getApiInfo}.
36
41
  * Routes {@link https://github.com/ikatson/rqbit/blob/v9.0.1/crates/librqbit/src/http_api/handlers/mod.rs}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctrl/rqbit",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "TypeScript api wrapper for rqbit using ofetch",
5
5
  "keywords": [
6
6
  "rqbit",
@@ -42,7 +42,7 @@
42
42
  "test:watch": "vitest"
43
43
  },
44
44
  "dependencies": {
45
- "@ctrl/shared-torrent": "^6.5.4",
45
+ "@ctrl/shared-torrent": "^6.6.0",
46
46
  "ofetch": "^1.5.1",
47
47
  "type-fest": "^5.10.0",
48
48
  "ufo": "^1.6.4",