UNPKG

transmission-rpc-client

Version:
93 lines (92 loc) 2.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransmissionClient = void 0; const axios_1 = __importDefault(require("axios")); class TransmissionClient { constructor(config) { this.csrfHeader = 'X-Transmission-Session-Id'; this.separator = '/'; this.token = ''; this.config = config; this.protocol = this.config.isHttps ? 'https' : 'http'; const host = this.config.host; this.host = host.endsWith(this.separator) ? host.substring(0, host.length - 1) : host; this.url = `${this.protocol}://${this.host}/transmission/rpc`; } request(req) { const getWithToken = (token) => { const headers = this.config.headers; const config = { headers: Object.assign({ [this.csrfHeader]: token }, headers), }; return axios_1.default .post(this.url, req, config) .then((res) => res.data) .catch((err) => { var _a; console.log(err.response); if (((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) === 409) { const csrfToken = err.response.headers[`${this.csrfHeader.toLowerCase()}`]; if (csrfToken) { this.token = csrfToken; return getWithToken(this.token); } else { console.log("No CSRF Header is present!"); } } return Promise.reject(err); }); }; return getWithToken(this.token); } getTorrents(req) { return this.request(req); } addTorrent(req) { return this.request(req); } moveTorrent(req) { return this.request(req); } removeTorrent(req) { return this.request(req); } updateTorrent(req) { return this.request(req); } stopTorrent(req) { return this.request(req); } startTorrent(req) { return this.request(req); } /** * Start torrent bypassing queueing */ startTorrentNow(req) { return this.request(req); } verifyTorrent(req) { return this.request(req); } torrentReannounce(req) { return this.request(req); } getSession(req) { return this.request(req); } setSession(req) { return this.request(req); } getFreeSpace(req) { return this.request(req); } getSessionStats(req) { return this.request(req); } } exports.TransmissionClient = TransmissionClient;