@fett/synology-api 0.0.4 → 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/README.md CHANGED
@@ -5,9 +5,10 @@
5
5
  </p>
6
6
  <h1 align="center">Javascript Synology Api</h1>
7
7
 
8
- ⚠️ The project is under development ...
8
+ ![NPM Version](https://img.shields.io/npm/v/%40fett%2Fsynology-api) ![NPM Downloads](https://img.shields.io/npm/dm/%40fett%2Fsynology-api) ![GitHub](https://img.shields.io/github/license/chrissong1994/synology-api) ![vitest](https://img.shields.io/badge/tested_with-vitest-brightgreen)
9
9
 
10
- **API Document:** [chrissong1994.github.io/synology-api/](chrissong1994.github.io/synology-api/)
10
+ 📖 [中文 README](./README_zh.md)
11
+ 🔎 [API Document ](https://chrissong1994.github.io/synology-api)
11
12
 
12
13
  Synology Api Javascript wrapper can be used in Browser、CLI or Nodejs to interact with Synology NAS.
13
14
  You can use domain or ip address, also supports Synology Quick Connect connect Synology server.
@@ -117,7 +118,7 @@ then you can use it and exec command
117
118
  ```bash
118
119
  syno config use ConnetionName
119
120
 
120
- syno fs getInfo --pretty # print file system info
121
+ syno fs getInfo --beautify # print file system info
121
122
 
122
123
  ```
123
124
 
package/lib/cli/apis.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  export type MethodOptions = {
2
2
  params?: string;
3
- pretty?: boolean;
3
+ beautify?: boolean;
4
4
  list?: boolean;
5
+ output?: string;
5
6
  };
6
7
  export declare const onMethodCall: (module: string) => (method: string, options: MethodOptions) => Promise<void>;
7
8
  export declare const apiCmdRegister: () => void;
package/lib/cli/apis.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { program } from "commander";
2
2
  import chalk from "chalk";
3
3
  import ora from "ora";
4
+ import fse from "fs-extra";
5
+ import path from "path";
4
6
  import { loadConfig } from "./config.js";
5
7
  import { SynologyApi } from "../core.js";
6
8
  import { SynologyApiModules, SynologyApiMethods } from "../modules/index.js";
@@ -8,7 +10,7 @@ import { printMessages } from "./helper.js";
8
10
  export const onMethodCall = (module) => async (method, options) => {
9
11
  const config = await loadConfig();
10
12
  const params = JSON.parse(options.params || "{}");
11
- const pretty = options.pretty;
13
+ const beautify = options.beautify ?? false;
12
14
  const connection = config.connections?.[config.used];
13
15
  if (!connection) {
14
16
  throw new Error("Connection undefined");
@@ -32,12 +34,26 @@ export const onMethodCall = (module) => async (method, options) => {
32
34
  const spinner = ora("waiting...").start();
33
35
  const result = await synologyApi[module]?.[method](params);
34
36
  spinner.stop();
35
- if (pretty) {
37
+ if (beautify) {
36
38
  console.log(JSON.stringify(result, null, 2));
37
39
  }
38
40
  else {
39
41
  console.log(JSON.stringify(result));
40
42
  }
43
+ if (options.output) {
44
+ try {
45
+ let out_path = path.resolve(options.output);
46
+ if (!fse.existsSync(out_path)) {
47
+ fse.ensureFileSync(out_path);
48
+ console.log(chalk.yellowBright(`Output file created: ${out_path}`));
49
+ }
50
+ await fse.writeFile(out_path, JSON.stringify(result, null, 2));
51
+ console.log(chalk.greenBright(`Output file written: ${out_path}`));
52
+ }
53
+ catch (e) {
54
+ console.error(e);
55
+ }
56
+ }
41
57
  synologyApi.disconnect();
42
58
  process.exit(0);
43
59
  };
@@ -52,8 +68,9 @@ export const apiCmdRegister = () => {
52
68
  .command(`${info.cmd} [method]`)
53
69
  .alias(info.alias)
54
70
  .option("-p,--params <params>", `${info.cmd} method params`)
55
- .option("--pretty", "Prettyprint JSON Output")
56
- .option("-l,--list", `list all ${info.cmd} methods`)
71
+ .option("-b,--beautify", "Print beautify JSON data output")
72
+ .option("-l,--list", `List all ${info.cmd} methods`)
73
+ .option("-o,--output <output>", "Specify the output file, no file will be created in the current folder")
57
74
  .description(`Synology ${info.cmd} method call`)
58
75
  .action(onMethodCall(info.cmd));
59
76
  });
package/lib/core.d.ts CHANGED
@@ -17,6 +17,7 @@ export interface SynologyApiOptions {
17
17
  password: string;
18
18
  quickConnectServerType?: QuickConnectServerType;
19
19
  agent?: Agent;
20
+ lanPriority?: boolean;
20
21
  }
21
22
  export interface SynologyApiInfoData {
22
23
  maxVersion: number;
@@ -38,6 +39,7 @@ export declare class SynologyApi extends BaseModuleSynologyApi {
38
39
  baseUrl: string;
39
40
  isConnecting: boolean;
40
41
  quickConnectServerType?: QuickConnectServerType;
42
+ lanPriority?: boolean;
41
43
  private authInfo;
42
44
  private apiInfo;
43
45
  constructor(options: SynologyApiOptions);
package/lib/core.js CHANGED
@@ -17,13 +17,14 @@ export class SynologyApi extends BaseModuleSynologyApi {
17
17
  this.username = options.username;
18
18
  this.password = options.password;
19
19
  this.quickConnectServerType = options.quickConnectServerType ?? QuickConnectServerType.proxy;
20
+ this.lanPriority = options.lanPriority ?? false;
20
21
  this.baseUrl = `${this.server}/webapi/`;
21
22
  this.agent = options.agent ?? undefined;
22
23
  }
23
24
  async connect() {
24
25
  // if quickconnect id
25
26
  if (!isHttpUrl(this.server)) {
26
- this.server = await getServerInfo(this.server, this.quickConnectServerType);
27
+ this.server = await getServerInfo(this.server, this.quickConnectServerType, this.lanPriority);
27
28
  this.baseUrl = `${this.server}/webapi/`;
28
29
  }
29
30
  try {
package/lib/helpers.d.ts CHANGED
@@ -16,5 +16,5 @@ export type ServerInfo = {
16
16
  relay_port: number;
17
17
  };
18
18
  };
19
- export declare const getServerInfo: (quickConnectId: string, quickConnectServerType: QuickConnectServerType) => Promise<any>;
19
+ export declare const getServerInfo: (quickConnectId: string, quickConnectServerType: QuickConnectServerType, lanPriority?: boolean) => Promise<any>;
20
20
  export declare const pingpang: (server: string) => Promise<boolean>;
package/lib/helpers.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import Axios from "axios";
2
2
  import { GLOBAL_QUICK_CONNECT_URL, QUICK_CONNECT_PINGPANG_API } from "./constants.js";
3
3
  import { QuickConnectServerType } from "./types/index.js";
4
- const getServersFromServerInfo = async (serverInfo, quickConnectServerType) => {
4
+ const getServersFromServerInfo = async (serverInfo, quickConnectServerType, lanPriority) => {
5
5
  const serverMap = {
6
6
  [QuickConnectServerType.proxy]: undefined,
7
7
  [QuickConnectServerType.lan]: undefined,
@@ -22,7 +22,13 @@ const getServersFromServerInfo = async (serverInfo, quickConnectServerType) => {
22
22
  serverMap[QuickConnectServerType.lan] =
23
23
  `http://${serverInfo.server.interface?.[0].ip}:${serverInfo.service.port}`;
24
24
  }
25
- const server = serverMap[quickConnectServerType];
25
+ let server = serverMap[quickConnectServerType];
26
+ if (lanPriority && serverMap[QuickConnectServerType.lan]) {
27
+ const lanServer = serverMap[QuickConnectServerType.lan];
28
+ if (lanServer && (await pingpang(lanServer))) {
29
+ return lanServer;
30
+ }
31
+ }
26
32
  if (!server) {
27
33
  return Promise.reject(`${quickConnectServerType} server not found`);
28
34
  }
@@ -34,7 +40,7 @@ const getServersFromServerInfo = async (serverInfo, quickConnectServerType) => {
34
40
  return server;
35
41
  }
36
42
  };
37
- export const getServerInfo = async (quickConnectId, quickConnectServerType) => {
43
+ export const getServerInfo = async (quickConnectId, quickConnectServerType, lanPriority) => {
38
44
  const params = {
39
45
  version: 1,
40
46
  id: "dsm",
@@ -53,10 +59,10 @@ export const getServerInfo = async (quickConnectId, quickConnectServerType) => {
53
59
  };
54
60
  // get replay tunnel
55
61
  const result = (await Axios.post(`https://${serverInfo.env.control_host}/Serv.php`, relayRequestParams)).data;
56
- return getServersFromServerInfo(result, quickConnectServerType);
62
+ return getServersFromServerInfo(result, quickConnectServerType, lanPriority);
57
63
  }
58
64
  else {
59
- return getServersFromServerInfo(serverInfo, quickConnectServerType);
65
+ return getServersFromServerInfo(serverInfo, quickConnectServerType, lanPriority);
60
66
  }
61
67
  };
62
68
  // pingpang
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fett/synology-api",
3
- "version": "0.0.4",
3
+ "version": "0.1.1",
4
4
  "description": "synology api for nodejs",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",