@ctrl/transmission 7.1.0 → 7.2.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.
package/README.md CHANGED
@@ -71,6 +71,15 @@ const res = await client.removeTorrent('torrent_id', true);
71
71
  console.log(res);
72
72
  ```
73
73
 
74
+ ##### export and create from state
75
+
76
+ If you're shutting down the server often (serverless?) you can export the state
77
+
78
+ ```ts
79
+ const state = client.exportState()
80
+ const client = Transmission.createFromState(config, state);
81
+ ```
82
+
74
83
  ### See Also
75
84
  All of the following npm modules provide the same normalized functions along with supporting the unique apis for each client.
76
85
 
@@ -1,10 +1,18 @@
1
1
  import { ofetch } from 'ofetch';
2
- import type { AddTorrentOptions as NormalizedAddTorrentOptions, AllClientData, NormalizedTorrent, TorrentClient, TorrentSettings } from '@ctrl/shared-torrent';
2
+ import type { Jsonify } from 'type-fest';
3
+ import type { AddTorrentOptions as NormalizedAddTorrentOptions, AllClientData, NormalizedTorrent, TorrentClient, TorrentClientConfig, TorrentClientState } from '@ctrl/shared-torrent';
3
4
  import type { AddTorrentOptions, AddTorrentResponse, DefaultResponse, FreeSpaceResponse, GetTorrentRepsonse, NormalizedTorrentIds, RenamePathOptions, SessionArguments, SessionResponse, SetTorrentOptions } from './types.js';
5
+ interface TransmissionState extends TorrentClientState {
6
+ auth?: {
7
+ sessionId: string;
8
+ };
9
+ }
4
10
  export declare class Transmission implements TorrentClient {
5
- config: TorrentSettings;
6
- sessionId?: string;
7
- constructor(options?: Partial<TorrentSettings>);
11
+ static createFromState(config: Readonly<TorrentClientConfig>, state: Readonly<Jsonify<TransmissionState>>): Transmission;
12
+ config: TorrentClientConfig;
13
+ state: TransmissionState;
14
+ constructor(options?: Partial<TorrentClientConfig>);
15
+ exportState(): Jsonify<TransmissionState>;
8
16
  getSession(): Promise<SessionResponse>;
9
17
  setSession(args: Partial<SessionArguments>): Promise<SessionResponse>;
10
18
  queueTop(id: NormalizedTorrentIds): Promise<DefaultResponse>;
@@ -56,3 +64,4 @@ export declare class Transmission implements TorrentClient {
56
64
  request<T>(method: string, args?: any): Promise<ReturnType<typeof ofetch.raw<T>>>;
57
65
  private _handleNormalizedIds;
58
66
  }
67
+ export {};
@@ -11,11 +11,24 @@ const defaults = {
11
11
  timeout: 5000,
12
12
  };
13
13
  export class Transmission {
14
+ static createFromState(config, state) {
15
+ const client = new Transmission(config);
16
+ client.state = {
17
+ ...state,
18
+ auth: {
19
+ sessionId: state.auth?.sessionId,
20
+ },
21
+ };
22
+ return client;
23
+ }
14
24
  config;
15
- sessionId;
25
+ state = {};
16
26
  constructor(options = {}) {
17
27
  this.config = { ...defaults, ...options };
18
28
  }
29
+ exportState() {
30
+ return JSON.parse(JSON.stringify(this.state));
31
+ }
19
32
  async getSession() {
20
33
  const res = await this.request('session-get');
21
34
  return res._data;
@@ -276,11 +289,11 @@ export class Transmission {
276
289
  return res._data;
277
290
  }
278
291
  async request(method, args = {}) {
279
- if (!this.sessionId && method !== 'session-get') {
292
+ if (!this.state.auth?.sessionId && method !== 'session-get') {
280
293
  await this.getSession();
281
294
  }
282
295
  const headers = {
283
- 'X-Transmission-Session-Id': this.sessionId,
296
+ 'X-Transmission-Session-Id': this.state.auth?.sessionId,
284
297
  'Content-Type': 'application/json',
285
298
  };
286
299
  if (this.config.username || this.config.password) {
@@ -313,7 +326,9 @@ export class Transmission {
313
326
  }
314
327
  catch (error) {
315
328
  if (error instanceof FetchError && error.response.status === 409) {
316
- this.sessionId = error.response.headers.get('x-transmission-session-id');
329
+ this.state.auth = {
330
+ sessionId: error.response.headers.get('x-transmission-session-id'),
331
+ };
317
332
  // eslint-disable-next-line no-return-await, @typescript-eslint/return-await
318
333
  return await this.request(method, args);
319
334
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctrl/transmission",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "description": "TypeScript api wrapper for transmission using got",
5
5
  "author": "Scott Cooper <scttcper@gmail.com>",
6
6
  "license": "MIT",
@@ -32,23 +32,24 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@ctrl/magnet-link": "^4.0.2",
35
- "@ctrl/shared-torrent": "^6.1.0",
35
+ "@ctrl/shared-torrent": "^6.2.1",
36
36
  "ofetch": "^1.4.1",
37
+ "type-fest": "^4.30.2",
37
38
  "ufo": "^1.5.4",
38
39
  "uint8array-extras": "^1.4.0"
39
40
  },
40
41
  "devDependencies": {
41
42
  "@biomejs/biome": "1.9.4",
42
- "@ctrl/eslint-config-biome": "4.2.11",
43
+ "@ctrl/eslint-config-biome": "4.3.1",
43
44
  "@ctrl/magnet-link": "^4.0.2",
44
- "@ctrl/shared-torrent": "^6.1.0",
45
- "@sindresorhus/tsconfig": "6.0.0",
46
- "@types/node": "22.9.0",
47
- "@vitest/coverage-v8": "2.1.4",
45
+ "@ctrl/shared-torrent": "^6.2.1",
46
+ "@sindresorhus/tsconfig": "7.0.0",
47
+ "@types/node": "22.10.2",
48
+ "@vitest/coverage-v8": "2.1.8",
48
49
  "p-wait-for": "5.0.2",
49
- "typedoc": "0.26.11",
50
- "typescript": "5.6.3",
51
- "vitest": "2.1.4"
50
+ "typedoc": "0.27.5",
51
+ "typescript": "5.7.2",
52
+ "vitest": "2.1.8"
52
53
  },
53
54
  "publishConfig": {
54
55
  "access": "public",