@fett/synology-api 0.0.1-beta.0 → 0.0.1-beta.2

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.
Files changed (58) hide show
  1. package/README.md +20 -0
  2. package/dist/cjs/constants.js +7 -0
  3. package/dist/cjs/core.js +109 -0
  4. package/dist/cjs/helpers.js +88 -0
  5. package/dist/cjs/index.js +19 -0
  6. package/dist/cjs/modules/Api/Auth.js +50 -0
  7. package/dist/cjs/modules/Api/Info.js +32 -0
  8. package/dist/cjs/modules/Api/index.js +18 -0
  9. package/dist/cjs/modules/AudioStation/Song/index.js +28 -0
  10. package/dist/cjs/modules/AudioStation/Song/types.js +2 -0
  11. package/dist/cjs/modules/AudioStation/index.js +8 -0
  12. package/dist/cjs/modules/index.js +24 -0
  13. package/dist/cjs/types/API.js +3 -0
  14. package/dist/cjs/types/index.js +17 -0
  15. package/dist/cjs/utils.js +19 -0
  16. package/dist/esm/constants.js +4 -0
  17. package/dist/{core.js → esm/core.js} +15 -36
  18. package/dist/esm/helpers.js +80 -0
  19. package/dist/esm/index.js +3 -0
  20. package/dist/esm/modules/Api/Auth.js +43 -0
  21. package/dist/esm/modules/Api/Info.js +26 -0
  22. package/dist/esm/modules/Api/index.js +2 -0
  23. package/dist/{modules/AudioStation.js → esm/modules/AudioStation/Song/index.js} +4 -5
  24. package/dist/esm/modules/AudioStation/index.js +5 -0
  25. package/dist/esm/modules/index.js +20 -0
  26. package/dist/esm/types/API.js +2 -0
  27. package/dist/esm/types/index.js +1 -0
  28. package/dist/esm/utils.js +14 -0
  29. package/dist/types/constants.d.ts +4 -0
  30. package/dist/{core.d.ts → types/core.d.ts} +1 -1
  31. package/dist/types/helpers.d.ts +2 -0
  32. package/dist/types/index.d.ts +3 -0
  33. package/dist/types/modules/Api/Auth.d.ts +3 -0
  34. package/dist/types/modules/Api/Info.d.ts +2 -0
  35. package/dist/types/modules/Api/index.d.ts +2 -0
  36. package/dist/types/modules/AudioStation/Song/index.d.ts +5 -0
  37. package/dist/types/{AudioStation.Song.d.ts → modules/AudioStation/Song/types.d.ts} +5 -4
  38. package/dist/types/modules/AudioStation/index.d.ts +5 -0
  39. package/dist/types/modules/index.d.ts +5 -0
  40. package/dist/types/types/API.d.ts +9 -0
  41. package/dist/types/types/index.d.ts +1 -0
  42. package/dist/types/utils.d.ts +3 -0
  43. package/package.json +24 -11
  44. package/dist/constants.d.ts +0 -2
  45. package/dist/constants.js +0 -2
  46. package/dist/index.d.ts +0 -2
  47. package/dist/index.js +0 -2
  48. package/dist/modules/AudioStation.d.ts +0 -7
  49. package/dist/modules/index.d.ts +0 -5
  50. package/dist/modules/index.js +0 -15
  51. package/dist/types/API.d.ts +0 -0
  52. package/dist/types/API.js +0 -1
  53. package/dist/types/FileStation.d.ts +0 -0
  54. package/dist/types/FileStation.js +0 -0
  55. package/dist/types/index.js +0 -0
  56. package/dist/utils.d.ts +0 -2
  57. package/dist/utils.js +0 -11
  58. /package/dist/{types/AudioStation.Song.js → esm/modules/AudioStation/Song/types.js} +0 -0
package/README.md CHANGED
@@ -1,2 +1,22 @@
1
1
  # synology-api
2
+
2
3
  nodejs synology api wrapper
4
+
5
+ ## Usage
6
+
7
+ ```js
8
+ import SynologyApi from '@fett/synology-api';
9
+
10
+ // Create a new instance
11
+ const synologyApi = new SynologyApi(
12
+ server: "https:192.168.1.1:5001",
13
+ username: "admin",
14
+ password: "password",
15
+ );
16
+
17
+ // Connect to the server
18
+ await synologyApi.connect();
19
+
20
+ ```
21
+
22
+ ## API
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QUICK_CONNECT_PINGPANG_API = exports.GLOBAL_QUICK_CONNECT_URL = exports.SYNOLOGY_API_INFO = exports.SYNOLOGY_API_AUTH = void 0;
4
+ exports.SYNOLOGY_API_AUTH = "SYNO.API.Auth";
5
+ exports.SYNOLOGY_API_INFO = "SYNO.API.Info";
6
+ exports.GLOBAL_QUICK_CONNECT_URL = "https://global.quickconnect.cn/Serv.php";
7
+ exports.QUICK_CONNECT_PINGPANG_API = "/webman/pingpong.cgi?action=cors&quickconnect=true";
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.SynologyApi = void 0;
16
+ // reference: https://kb.synology.com/zh-tw/DSM/tutorial/What_websites_does_Synology_NAS_connect_to_when_running_services_or_updating_software
17
+ const axios_1 = __importDefault(require("axios"));
18
+ const modules_1 = require("./modules");
19
+ const utils_1 = require("./utils");
20
+ const helpers_1 = require("./helpers");
21
+ const Api_1 = require("./modules/Api");
22
+ class SynologyApi extends modules_1.BaseSynologyApi {
23
+ constructor(options) {
24
+ super();
25
+ this.isConnecting = false;
26
+ this.authInfo = null;
27
+ this.apiInfo = {};
28
+ this.server = options.server;
29
+ this.username = options.username;
30
+ this.password = options.password;
31
+ this.baseUrl = `${this.server}/webapi/`;
32
+ }
33
+ connect() {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ // if quickconnect id
36
+ if (!(0, utils_1.isHttpUrl)(this.server)) {
37
+ this.server = yield (0, helpers_1.getServerInfo)(this.server);
38
+ // reset base url
39
+ this.baseUrl = `${this.server}/webapi/`;
40
+ }
41
+ try {
42
+ const result = yield (0, Api_1.login)(this);
43
+ this.authInfo = result.data;
44
+ this.isConnecting = true;
45
+ yield this._getApiInfo();
46
+ return true;
47
+ }
48
+ catch (err) {
49
+ console.error(err);
50
+ return false;
51
+ }
52
+ });
53
+ }
54
+ disconnect() {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ try {
57
+ yield (0, Api_1.logout)(this);
58
+ this.authInfo = null;
59
+ this.apiInfo = {};
60
+ this.isConnecting = false;
61
+ return true;
62
+ }
63
+ catch (err) {
64
+ console.error(err);
65
+ return false;
66
+ }
67
+ });
68
+ }
69
+ _getApiInfo() {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ try {
72
+ const result = yield (0, Api_1.getApiInfo)(this);
73
+ this.apiInfo = result.data;
74
+ }
75
+ catch (err) {
76
+ console.error(err);
77
+ }
78
+ });
79
+ }
80
+ hasApi(apiName) {
81
+ if (!this.isConnecting) {
82
+ throw new Error("Not connected");
83
+ }
84
+ return Object.prototype.hasOwnProperty.call(this.apiInfo, apiName);
85
+ }
86
+ run(apiName, options) {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ if (!this.isConnecting) {
89
+ throw new Error("Not connected");
90
+ }
91
+ if (!this.hasApi(apiName)) {
92
+ throw new Error(`${apiName} not found`);
93
+ }
94
+ const { method = "get", params, data, headers } = options;
95
+ const api = this.apiInfo[apiName];
96
+ const url = `${this.baseUrl}${api.path}`;
97
+ const externalParams = Object.assign({ api: apiName, version: api.maxVersion, _sid: this.authInfo.sid }, params);
98
+ let result = null;
99
+ if (method === "get") {
100
+ result = yield axios_1.default.get(url, { params: externalParams, data, headers });
101
+ }
102
+ if (method === "post") {
103
+ result = yield axios_1.default.post(url, { params: externalParams, data, headers });
104
+ }
105
+ return result;
106
+ });
107
+ }
108
+ }
109
+ exports.SynologyApi = SynologyApi;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.pingpang = exports.getServerInfo = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const constants_1 = require("./constants");
18
+ const getServersFromServerInfo = (serverInfo) => __awaiter(void 0, void 0, void 0, function* () {
19
+ var _a, _b;
20
+ // proxy server
21
+ if (serverInfo.service.relay_ip) {
22
+ const server = `http://${serverInfo.service.relay_ip}:${serverInfo.service.relay_port}`;
23
+ const res = yield (0, exports.pingpang)(server);
24
+ if (res) {
25
+ return server;
26
+ }
27
+ }
28
+ // WAN IP
29
+ if (serverInfo.server.external.ip) {
30
+ const server = `http://${serverInfo.server.external.ip}:${serverInfo.service.port}`;
31
+ if (yield (0, exports.pingpang)(server)) {
32
+ return server;
33
+ }
34
+ }
35
+ // lan ip
36
+ if ((_a = serverInfo.server.interface) === null || _a === void 0 ? void 0 : _a[0]) {
37
+ const server = `http://${(_b = serverInfo.server.interface) === null || _b === void 0 ? void 0 : _b[0].ip}:${serverInfo.service.port}`;
38
+ if (yield (0, exports.pingpang)(server)) {
39
+ return server;
40
+ }
41
+ }
42
+ });
43
+ // get server ip
44
+ const getServerInfo = (quickConnectId) => __awaiter(void 0, void 0, void 0, function* () {
45
+ var _a, _b, _c, _d;
46
+ const params = {
47
+ version: 1,
48
+ id: "dsm",
49
+ serverID: quickConnectId,
50
+ get_ca_fingerprints: true,
51
+ command: "get_server_info",
52
+ };
53
+ const serverInfo = yield axios_1.default.post(constants_1.GLOBAL_QUICK_CONNECT_URL, params);
54
+ if (!((_b = (_a = serverInfo.data) === null || _a === void 0 ? void 0 : _a.service) === null || _b === void 0 ? void 0 : _b.relay_ip) && !((_d = (_c = serverInfo.data) === null || _c === void 0 ? void 0 : _c.service) === null || _d === void 0 ? void 0 : _d.relay_port)) {
55
+ const relayRequestParams = {
56
+ version: 1,
57
+ id: "dsm",
58
+ serverID: quickConnectId,
59
+ platform: "web",
60
+ command: "request_tunnel",
61
+ };
62
+ const result = yield axios_1.default.post(`https://${serverInfo.data.env.control_host}/Serv.php`, relayRequestParams);
63
+ return getServersFromServerInfo(result.data);
64
+ }
65
+ else {
66
+ return getServersFromServerInfo(serverInfo.data);
67
+ }
68
+ });
69
+ exports.getServerInfo = getServerInfo;
70
+ // pingpang
71
+ const pingpang = (server) => __awaiter(void 0, void 0, void 0, function* () {
72
+ try {
73
+ const result = yield axios_1.default.get(`${server}/${constants_1.QUICK_CONNECT_PINGPANG_API}`, {
74
+ timeout: 3000,
75
+ });
76
+ if (result.data.success) {
77
+ return true;
78
+ }
79
+ else {
80
+ return false;
81
+ }
82
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
83
+ }
84
+ catch (_err) {
85
+ return false;
86
+ }
87
+ });
88
+ exports.pingpang = pingpang;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ const core_1 = require("./core");
18
+ __exportStar(require("./core"), exports);
19
+ exports.default = core_1.SynologyApi;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.login = login;
16
+ exports.logout = logout;
17
+ const axios_1 = __importDefault(require("axios"));
18
+ const constants_1 = require("@/constants");
19
+ function login(core) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const params = {
22
+ api: constants_1.SYNOLOGY_API_AUTH,
23
+ version: 6,
24
+ method: "login",
25
+ account: core.username,
26
+ passwd: core.password,
27
+ format: "sid",
28
+ };
29
+ const url = `${core.baseUrl}entry.cgi`;
30
+ const result = yield axios_1.default.get(url, { params });
31
+ if (!result.data.success) {
32
+ throw new Error(result.data.error.message);
33
+ }
34
+ return result.data;
35
+ });
36
+ }
37
+ function logout(core) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const params = {
40
+ api: constants_1.SYNOLOGY_API_AUTH,
41
+ version: 6,
42
+ method: "logout",
43
+ };
44
+ const url = `${core.baseUrl}entry.cgi`;
45
+ const result = yield axios_1.default.get(url, { params });
46
+ if (!result.data.success) {
47
+ throw new Error(result.data.error.message);
48
+ }
49
+ });
50
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getApiInfo = getApiInfo;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const constants_1 = require("@/constants");
18
+ function getApiInfo(core) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ const params = {
21
+ api: constants_1.SYNOLOGY_API_INFO,
22
+ version: 1,
23
+ method: "query",
24
+ };
25
+ const url = `${core.baseUrl}entry.cgi`;
26
+ const result = yield axios_1.default.get(url, { params });
27
+ if (!result.data.success) {
28
+ throw new Error(result.data.error.message);
29
+ }
30
+ return result.data;
31
+ });
32
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Auth"), exports);
18
+ __exportStar(require("./Info"), exports);
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getSongList = getSongList;
13
+ /**
14
+ * fetch song list
15
+ * */
16
+ function getSongList(params) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ const res = yield this.run("SYNO.AudioStation.Song", {
19
+ params: {
20
+ method: "list",
21
+ library: "all",
22
+ limit: params.limit,
23
+ offset: params.offset,
24
+ },
25
+ });
26
+ return res.data;
27
+ });
28
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AudioStationKey = exports.AudioStationMethods = void 0;
4
+ const Song_1 = require("./Song");
5
+ exports.AudioStationMethods = {
6
+ getSongList: Song_1.getSongList,
7
+ };
8
+ exports.AudioStationKey = "AudioStation";
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseSynologyApi = void 0;
4
+ const AudioStation_1 = require("./AudioStation");
5
+ // bind methods to BaseSynologyApi instance
6
+ function methodsBundler(instance, methods) {
7
+ const output = {};
8
+ for (const key in methods) {
9
+ output[key] = methods[key].bind(instance);
10
+ }
11
+ return output;
12
+ }
13
+ class BaseSynologyApi {
14
+ constructor() { }
15
+ }
16
+ exports.BaseSynologyApi = BaseSynologyApi;
17
+ // proxy methods namespace to BaseSynologyApi instance
18
+ Object.defineProperties(BaseSynologyApi.prototype, {
19
+ [AudioStation_1.AudioStationKey]: {
20
+ get() {
21
+ return methodsBundler(this, AudioStation_1.AudioStationMethods);
22
+ },
23
+ },
24
+ });
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // export response
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./API"), exports);
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isObjEmpty = isObjEmpty;
4
+ exports.queryObjToString = queryObjToString;
5
+ exports.isHttpUrl = isHttpUrl;
6
+ function isObjEmpty(obj) {
7
+ return obj && typeof obj === "object" && Object.keys(obj).length === 0;
8
+ }
9
+ function queryObjToString(params) {
10
+ if (isObjEmpty(params)) {
11
+ return "";
12
+ }
13
+ return Object.keys(params)
14
+ .map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(params[key]))
15
+ .join("&");
16
+ }
17
+ function isHttpUrl(url) {
18
+ return /^https?:\/\//.test(url);
19
+ }
@@ -0,0 +1,4 @@
1
+ export const SYNOLOGY_API_AUTH = "SYNO.API.Auth";
2
+ export const SYNOLOGY_API_INFO = "SYNO.API.Info";
3
+ export const GLOBAL_QUICK_CONNECT_URL = "https://global.quickconnect.cn/Serv.php";
4
+ export const QUICK_CONNECT_PINGPANG_API = "/webman/pingpong.cgi?action=cors&quickconnect=true";
@@ -7,9 +7,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ // reference: https://kb.synology.com/zh-tw/DSM/tutorial/What_websites_does_Synology_NAS_connect_to_when_running_services_or_updating_software
10
11
  import axios from "axios";
11
- import { SYNOLOGY_API_AUTH, SYNOLOGY_API_INFO } from "./constants";
12
12
  import { BaseSynologyApi } from "./modules";
13
+ import { isHttpUrl } from "./utils";
14
+ import { getServerInfo } from "./helpers";
15
+ import { login, logout, getApiInfo } from "./modules/Api";
13
16
  export class SynologyApi extends BaseSynologyApi {
14
17
  constructor(options) {
15
18
  super();
@@ -23,21 +26,15 @@ export class SynologyApi extends BaseSynologyApi {
23
26
  }
24
27
  connect() {
25
28
  return __awaiter(this, void 0, void 0, function* () {
26
- const params = {
27
- api: SYNOLOGY_API_AUTH,
28
- version: 6,
29
- method: "login",
30
- account: this.username,
31
- passwd: this.password,
32
- format: "sid",
33
- };
29
+ // if quickconnect id
30
+ if (!isHttpUrl(this.server)) {
31
+ this.server = yield getServerInfo(this.server);
32
+ // reset base url
33
+ this.baseUrl = `${this.server}/webapi/`;
34
+ }
34
35
  try {
35
- const url = `${this.baseUrl}entry.cgi`;
36
- const result = yield axios.get(url, { params });
37
- if (!result.data.success) {
38
- throw new Error(result.data.error.message);
39
- }
40
- this.authInfo = result.data.data;
36
+ const result = yield login(this);
37
+ this.authInfo = result.data;
41
38
  this.isConnecting = true;
42
39
  yield this._getApiInfo();
43
40
  return true;
@@ -50,17 +47,8 @@ export class SynologyApi extends BaseSynologyApi {
50
47
  }
51
48
  disconnect() {
52
49
  return __awaiter(this, void 0, void 0, function* () {
53
- const params = {
54
- api: SYNOLOGY_API_AUTH,
55
- version: 6,
56
- method: "logout",
57
- };
58
50
  try {
59
- const url = `${this.baseUrl}entry.cgi`;
60
- const result = yield axios.get(url, { params });
61
- if (!result.data.success) {
62
- throw new Error(result.data.error.message);
63
- }
51
+ yield logout(this);
64
52
  this.authInfo = null;
65
53
  this.apiInfo = {};
66
54
  this.isConnecting = false;
@@ -74,18 +62,9 @@ export class SynologyApi extends BaseSynologyApi {
74
62
  }
75
63
  _getApiInfo() {
76
64
  return __awaiter(this, void 0, void 0, function* () {
77
- const params = {
78
- api: SYNOLOGY_API_INFO,
79
- version: 1,
80
- method: "query",
81
- };
82
65
  try {
83
- const url = `${this.baseUrl}entry.cgi`;
84
- const result = yield axios.get(url, { params });
85
- if (!result.data.success) {
86
- throw new Error(result.data.error.message);
87
- }
88
- this.apiInfo = result.data.data;
66
+ const result = yield getApiInfo(this);
67
+ this.apiInfo = result.data;
89
68
  }
90
69
  catch (err) {
91
70
  console.error(err);
@@ -0,0 +1,80 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import axios from "axios";
11
+ import { GLOBAL_QUICK_CONNECT_URL, QUICK_CONNECT_PINGPANG_API } from "./constants";
12
+ const getServersFromServerInfo = (serverInfo) => __awaiter(void 0, void 0, void 0, function* () {
13
+ var _a, _b;
14
+ // proxy server
15
+ if (serverInfo.service.relay_ip) {
16
+ const server = `http://${serverInfo.service.relay_ip}:${serverInfo.service.relay_port}`;
17
+ const res = yield pingpang(server);
18
+ if (res) {
19
+ return server;
20
+ }
21
+ }
22
+ // WAN IP
23
+ if (serverInfo.server.external.ip) {
24
+ const server = `http://${serverInfo.server.external.ip}:${serverInfo.service.port}`;
25
+ if (yield pingpang(server)) {
26
+ return server;
27
+ }
28
+ }
29
+ // lan ip
30
+ if ((_a = serverInfo.server.interface) === null || _a === void 0 ? void 0 : _a[0]) {
31
+ const server = `http://${(_b = serverInfo.server.interface) === null || _b === void 0 ? void 0 : _b[0].ip}:${serverInfo.service.port}`;
32
+ if (yield pingpang(server)) {
33
+ return server;
34
+ }
35
+ }
36
+ });
37
+ // get server ip
38
+ export const getServerInfo = (quickConnectId) => __awaiter(void 0, void 0, void 0, function* () {
39
+ var _a, _b, _c, _d;
40
+ const params = {
41
+ version: 1,
42
+ id: "dsm",
43
+ serverID: quickConnectId,
44
+ get_ca_fingerprints: true,
45
+ command: "get_server_info",
46
+ };
47
+ const serverInfo = yield axios.post(GLOBAL_QUICK_CONNECT_URL, params);
48
+ if (!((_b = (_a = serverInfo.data) === null || _a === void 0 ? void 0 : _a.service) === null || _b === void 0 ? void 0 : _b.relay_ip) && !((_d = (_c = serverInfo.data) === null || _c === void 0 ? void 0 : _c.service) === null || _d === void 0 ? void 0 : _d.relay_port)) {
49
+ const relayRequestParams = {
50
+ version: 1,
51
+ id: "dsm",
52
+ serverID: quickConnectId,
53
+ platform: "web",
54
+ command: "request_tunnel",
55
+ };
56
+ const result = yield axios.post(`https://${serverInfo.data.env.control_host}/Serv.php`, relayRequestParams);
57
+ return getServersFromServerInfo(result.data);
58
+ }
59
+ else {
60
+ return getServersFromServerInfo(serverInfo.data);
61
+ }
62
+ });
63
+ // pingpang
64
+ export const pingpang = (server) => __awaiter(void 0, void 0, void 0, function* () {
65
+ try {
66
+ const result = yield axios.get(`${server}/${QUICK_CONNECT_PINGPANG_API}`, {
67
+ timeout: 3000,
68
+ });
69
+ if (result.data.success) {
70
+ return true;
71
+ }
72
+ else {
73
+ return false;
74
+ }
75
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
76
+ }
77
+ catch (_err) {
78
+ return false;
79
+ }
80
+ });
@@ -0,0 +1,3 @@
1
+ import { SynologyApi } from "./core";
2
+ export * from "./core";
3
+ export default SynologyApi;
@@ -0,0 +1,43 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import axios from "axios";
11
+ import { SYNOLOGY_API_AUTH } from "../../constants";
12
+ export function login(core) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ const params = {
15
+ api: SYNOLOGY_API_AUTH,
16
+ version: 6,
17
+ method: "login",
18
+ account: core.username,
19
+ passwd: core.password,
20
+ format: "sid",
21
+ };
22
+ const url = `${core.baseUrl}entry.cgi`;
23
+ const result = yield axios.get(url, { params });
24
+ if (!result.data.success) {
25
+ throw new Error(result.data.error.message);
26
+ }
27
+ return result.data;
28
+ });
29
+ }
30
+ export function logout(core) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ const params = {
33
+ api: SYNOLOGY_API_AUTH,
34
+ version: 6,
35
+ method: "logout",
36
+ };
37
+ const url = `${core.baseUrl}entry.cgi`;
38
+ const result = yield axios.get(url, { params });
39
+ if (!result.data.success) {
40
+ throw new Error(result.data.error.message);
41
+ }
42
+ });
43
+ }
@@ -0,0 +1,26 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import axios from "axios";
11
+ import { SYNOLOGY_API_INFO } from "../../constants";
12
+ export function getApiInfo(core) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ const params = {
15
+ api: SYNOLOGY_API_INFO,
16
+ version: 1,
17
+ method: "query",
18
+ };
19
+ const url = `${core.baseUrl}entry.cgi`;
20
+ const result = yield axios.get(url, { params });
21
+ if (!result.data.success) {
22
+ throw new Error(result.data.error.message);
23
+ }
24
+ return result.data;
25
+ });
26
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./Auth";
2
+ export * from "./Info";
@@ -7,7 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- function getSongList(params) {
10
+ /**
11
+ * fetch song list
12
+ * */
13
+ export function getSongList(params) {
11
14
  return __awaiter(this, void 0, void 0, function* () {
12
15
  const res = yield this.run("SYNO.AudioStation.Song", {
13
16
  params: {
@@ -20,7 +23,3 @@ function getSongList(params) {
20
23
  return res.data;
21
24
  });
22
25
  }
23
- export const AudioStationMethods = {
24
- getSongList,
25
- };
26
- export const AudioStationProp = "AudioStation";
@@ -0,0 +1,5 @@
1
+ import { getSongList } from "./Song";
2
+ export const AudioStationMethods = {
3
+ getSongList: getSongList,
4
+ };
5
+ export const AudioStationKey = "AudioStation";
@@ -0,0 +1,20 @@
1
+ import { AudioStationKey, AudioStationMethods } from "./AudioStation";
2
+ // bind methods to BaseSynologyApi instance
3
+ function methodsBundler(instance, methods) {
4
+ const output = {};
5
+ for (const key in methods) {
6
+ output[key] = methods[key].bind(instance);
7
+ }
8
+ return output;
9
+ }
10
+ export class BaseSynologyApi {
11
+ constructor() { }
12
+ }
13
+ // proxy methods namespace to BaseSynologyApi instance
14
+ Object.defineProperties(BaseSynologyApi.prototype, {
15
+ [AudioStationKey]: {
16
+ get() {
17
+ return methodsBundler(this, AudioStationMethods);
18
+ },
19
+ },
20
+ });
@@ -0,0 +1,2 @@
1
+ // export response
2
+ export {};
@@ -0,0 +1 @@
1
+ export * from "./API";
@@ -0,0 +1,14 @@
1
+ export function isObjEmpty(obj) {
2
+ return obj && typeof obj === "object" && Object.keys(obj).length === 0;
3
+ }
4
+ export function queryObjToString(params) {
5
+ if (isObjEmpty(params)) {
6
+ return "";
7
+ }
8
+ return Object.keys(params)
9
+ .map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(params[key]))
10
+ .join("&");
11
+ }
12
+ export function isHttpUrl(url) {
13
+ return /^https?:\/\//.test(url);
14
+ }
@@ -0,0 +1,4 @@
1
+ export declare const SYNOLOGY_API_AUTH = "SYNO.API.Auth";
2
+ export declare const SYNOLOGY_API_INFO = "SYNO.API.Info";
3
+ export declare const GLOBAL_QUICK_CONNECT_URL = "https://global.quickconnect.cn/Serv.php";
4
+ export declare const QUICK_CONNECT_PINGPANG_API = "/webman/pingpong.cgi?action=cors&quickconnect=true";
@@ -27,7 +27,7 @@ export declare class SynologyApi extends BaseSynologyApi {
27
27
  constructor(options: SynologyApiOptions);
28
28
  connect(): Promise<boolean>;
29
29
  disconnect(): Promise<boolean>;
30
- _getApiInfo(): Promise<void>;
30
+ private _getApiInfo;
31
31
  hasApi(apiName: string): any;
32
32
  run(apiName: string, options: {
33
33
  method?: "get" | "post";
@@ -0,0 +1,2 @@
1
+ export declare const getServerInfo: (quickConnectId: string) => Promise<string>;
2
+ export declare const pingpang: (server: string) => Promise<boolean>;
@@ -0,0 +1,3 @@
1
+ import { SynologyApi } from "./core";
2
+ export * from "./core";
3
+ export default SynologyApi;
@@ -0,0 +1,3 @@
1
+ import { SynologyApi } from "../../core";
2
+ export declare function login(core: SynologyApi): Promise<any>;
3
+ export declare function logout(core: SynologyApi): Promise<void>;
@@ -0,0 +1,2 @@
1
+ import { SynologyApi } from "../../core";
2
+ export declare function getApiInfo(core: SynologyApi): Promise<any>;
@@ -0,0 +1,2 @@
1
+ export * from "./Auth";
2
+ export * from "./Info";
@@ -0,0 +1,5 @@
1
+ import { AudioStationSongListRequest, AudioStationSongListResponse } from "./types";
2
+ /**
3
+ * fetch song list
4
+ * */
5
+ export declare function getSongList(params: AudioStationSongListRequest): Promise<AudioStationSongListResponse>;
@@ -1,17 +1,18 @@
1
+ import { SynologyApiResponse } from "../../../types";
1
2
  export type AudioStationSongListRequest = {
2
3
  limit?: number;
3
4
  offset?: number;
4
- method?: number;
5
- library?: number;
5
+ method?: string;
6
+ library?: string;
6
7
  additional?: string;
7
8
  version?: number;
8
9
  sort_by?: string;
9
10
  sort_direction?: string;
10
11
  };
11
- export type AudioStationSongListResponse = {
12
+ export type AudioStationSongListResponse = SynologyApiResponse<{
12
13
  offset: number;
13
14
  total: number;
14
15
  songs: {
15
16
  [key: string]: any;
16
17
  };
17
- };
18
+ }>;
@@ -0,0 +1,5 @@
1
+ import { getSongList } from "./Song";
2
+ export declare const AudioStationMethods: {
3
+ getSongList: typeof getSongList;
4
+ };
5
+ export declare const AudioStationKey = "AudioStation";
@@ -0,0 +1,5 @@
1
+ import { AudioStationKey, AudioStationMethods } from "./AudioStation";
2
+ export declare class BaseSynologyApi {
3
+ [AudioStationKey]: typeof AudioStationMethods;
4
+ constructor();
5
+ }
@@ -0,0 +1,9 @@
1
+ export type SynologyApiResponse<T = any> = {
2
+ data: T;
3
+ error: {
4
+ code: number;
5
+ errors: Record<string, string>;
6
+ message: string;
7
+ };
8
+ success: boolean;
9
+ };
@@ -0,0 +1 @@
1
+ export * from "./API";
@@ -0,0 +1,3 @@
1
+ export declare function isObjEmpty(obj: Record<string, any>): boolean;
2
+ export declare function queryObjToString(params: Record<string, any>): string;
3
+ export declare function isHttpUrl(url: string): boolean;
package/package.json CHANGED
@@ -1,32 +1,44 @@
1
1
  {
2
2
  "name": "@fett/synology-api",
3
- "version": "0.0.1-beta.0",
3
+ "version": "0.0.1-beta.2",
4
4
  "description": "synology api for nodejs",
5
- "module": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
5
+ "module": "./dist/esm/index.js",
6
+ "main": "./dist/cjs/index.js",
7
+ "types": "./dist/types/index.d.ts",
7
8
  "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "require": "./dist/cjs/index.js",
12
+ "import": "./dist/esm/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
8
18
  "author": "ChrisSong1994 <https://github.com/ChrisSong1994>",
9
19
  "keywords": [
10
20
  "synology",
11
21
  "api",
12
22
  "nodejs",
23
+ "browser",
13
24
  "typescript",
14
- "esmodule"
15
- ],
16
- "files": [
17
- "dist"
25
+ "esmodule",
26
+ "cjs",
27
+ "commonjs"
18
28
  ],
19
29
  "scripts": {
20
- "dev": "tsc --sourceMap --watch",
21
- "build": "npm run clean && tsc",
30
+ "dev": "tsc --sourceMap --watch && tsc-alias",
31
+ "build:cjs": "tsc --module commonjs --outDir dist/cjs && tsc-alias",
32
+ "build:esm": "tsc --module esnext --outDir dist/esm && tsc-alias",
33
+ "build": "npm run clean && npm run build:cjs && npm run build:esm",
34
+ "clean": "rimraf dist",
22
35
  "lint": "eslint src --ext .ts --fix",
23
36
  "prettier": "prettier --write 'src/**/*.ts'",
24
37
  "format": "npm run prettier && npm run lint",
25
38
  "prepublish": "npm run build",
26
39
  "pub": "npm publish --access=public",
27
40
  "test": "vitest",
28
- "coverage": "vitest run --coverage",
29
- "clean": "rimraf dist"
41
+ "coverage": "vitest run --coverage"
30
42
  },
31
43
  "license": "ISC",
32
44
  "packageManager": "pnpm@10.8.1",
@@ -37,6 +49,7 @@
37
49
  "prettier": "3.5.3",
38
50
  "rimraf": "^6.0.1",
39
51
  "ts-node": "^10.9.2",
52
+ "tsc-alias": "^1.8.16",
40
53
  "typescript": "~5.8.3",
41
54
  "typescript-eslint": "^8.32.0",
42
55
  "vite": "^6.3.5",
@@ -1,2 +0,0 @@
1
- export declare const SYNOLOGY_API_AUTH = "SYNO.API.Auth";
2
- export declare const SYNOLOGY_API_INFO = "SYNO.API.Info";
package/dist/constants.js DELETED
@@ -1,2 +0,0 @@
1
- export const SYNOLOGY_API_AUTH = "SYNO.API.Auth";
2
- export const SYNOLOGY_API_INFO = "SYNO.API.Info";
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./core";
2
- export * from "./modules";
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- export * from "./core";
2
- export * from "./modules";
@@ -1,7 +0,0 @@
1
- import { AudioStationSongListRequest, AudioStationSongListResponse } from "../types/AudioStation.Song";
2
- declare function getSongList(params: AudioStationSongListRequest): Promise<AudioStationSongListResponse>;
3
- export declare const AudioStationMethods: {
4
- getSongList: typeof getSongList;
5
- };
6
- export declare const AudioStationProp = "AudioStation";
7
- export {};
@@ -1,5 +0,0 @@
1
- import { AudioStationProp, AudioStationMethods } from "./AudioStation";
2
- export declare class BaseSynologyApi {
3
- [AudioStationProp]: typeof AudioStationMethods;
4
- constructor();
5
- }
@@ -1,15 +0,0 @@
1
- import { AudioStationProp, AudioStationMethods } from "./AudioStation";
2
- export class BaseSynologyApi {
3
- constructor() { }
4
- }
5
- const instanceBindings = new WeakMap();
6
- Object.defineProperty(BaseSynologyApi.prototype, AudioStationProp, {
7
- get() {
8
- if (!instanceBindings.has(this)) {
9
- instanceBindings.set(this, {
10
- getSongList: AudioStationMethods.getSongList.bind(this),
11
- });
12
- }
13
- return instanceBindings.get(this);
14
- },
15
- });
File without changes
package/dist/types/API.js DELETED
@@ -1 +0,0 @@
1
- // export type
File without changes
File without changes
File without changes
package/dist/utils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare function isEmpty(obj: Record<string, any>): boolean;
2
- export declare function queryObjToString(params: Record<string, any>): string;
package/dist/utils.js DELETED
@@ -1,11 +0,0 @@
1
- export function isEmpty(obj) {
2
- return Object.keys(obj).length === 0;
3
- }
4
- export function queryObjToString(params) {
5
- if (isEmpty(params)) {
6
- return "";
7
- }
8
- return Object.keys(params)
9
- .map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(params[key]))
10
- .join("&");
11
- }