@fett/synology-api 0.0.1-beta.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 +2 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +2 -0
- package/dist/core.d.ts +38 -0
- package/dist/core.js +123 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/modules/AudioStation.d.ts +7 -0
- package/dist/modules/AudioStation.js +26 -0
- package/dist/modules/index.d.ts +5 -0
- package/dist/modules/index.js +15 -0
- package/dist/types/API.d.ts +0 -0
- package/dist/types/API.js +1 -0
- package/dist/types/AudioStation.Song.d.ts +17 -0
- package/dist/types/AudioStation.Song.js +1 -0
- package/dist/types/FileStation.d.ts +0 -0
- package/dist/types/FileStation.js +0 -0
- package/dist/types/index.d.ts +0 -0
- package/dist/types/index.js +0 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +11 -0
- package/package.json +48 -0
package/README.md
ADDED
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { BaseSynologyApi } from "./modules";
|
|
2
|
+
export interface SynologyApiOptions {
|
|
3
|
+
server: string;
|
|
4
|
+
username: string;
|
|
5
|
+
password: string;
|
|
6
|
+
}
|
|
7
|
+
export interface SynologyApiInfo {
|
|
8
|
+
maxVersion: number;
|
|
9
|
+
minVersion: number;
|
|
10
|
+
path: string;
|
|
11
|
+
requestFormat?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface SynologyApiAuthInfo {
|
|
14
|
+
sid: string;
|
|
15
|
+
did: number;
|
|
16
|
+
is_portal_port: boolean;
|
|
17
|
+
synotoken: string;
|
|
18
|
+
}
|
|
19
|
+
export declare class SynologyApi extends BaseSynologyApi {
|
|
20
|
+
server: string;
|
|
21
|
+
username: string;
|
|
22
|
+
password: string;
|
|
23
|
+
baseUrl: string;
|
|
24
|
+
isConnecting: boolean;
|
|
25
|
+
private authInfo;
|
|
26
|
+
private apiInfo;
|
|
27
|
+
constructor(options: SynologyApiOptions);
|
|
28
|
+
connect(): Promise<boolean>;
|
|
29
|
+
disconnect(): Promise<boolean>;
|
|
30
|
+
_getApiInfo(): Promise<void>;
|
|
31
|
+
hasApi(apiName: string): any;
|
|
32
|
+
run(apiName: string, options: {
|
|
33
|
+
method?: "get" | "post";
|
|
34
|
+
params?: Record<string, any>;
|
|
35
|
+
data?: Record<string, any>;
|
|
36
|
+
headers?: Record<string, any>;
|
|
37
|
+
}): Promise<any>;
|
|
38
|
+
}
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
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, SYNOLOGY_API_INFO } from "./constants";
|
|
12
|
+
import { BaseSynologyApi } from "./modules";
|
|
13
|
+
export class SynologyApi extends BaseSynologyApi {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
super();
|
|
16
|
+
this.isConnecting = false;
|
|
17
|
+
this.authInfo = null;
|
|
18
|
+
this.apiInfo = {};
|
|
19
|
+
this.server = options.server;
|
|
20
|
+
this.username = options.username;
|
|
21
|
+
this.password = options.password;
|
|
22
|
+
this.baseUrl = `${this.server}/webapi/`;
|
|
23
|
+
}
|
|
24
|
+
connect() {
|
|
25
|
+
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
|
+
};
|
|
34
|
+
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;
|
|
41
|
+
this.isConnecting = true;
|
|
42
|
+
yield this._getApiInfo();
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
console.error(err);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
disconnect() {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const params = {
|
|
54
|
+
api: SYNOLOGY_API_AUTH,
|
|
55
|
+
version: 6,
|
|
56
|
+
method: "logout",
|
|
57
|
+
};
|
|
58
|
+
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
|
+
}
|
|
64
|
+
this.authInfo = null;
|
|
65
|
+
this.apiInfo = {};
|
|
66
|
+
this.isConnecting = false;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
console.error(err);
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
_getApiInfo() {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
const params = {
|
|
78
|
+
api: SYNOLOGY_API_INFO,
|
|
79
|
+
version: 1,
|
|
80
|
+
method: "query",
|
|
81
|
+
};
|
|
82
|
+
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;
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
console.error(err);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
hasApi(apiName) {
|
|
96
|
+
if (!this.isConnecting) {
|
|
97
|
+
throw new Error("Not connected");
|
|
98
|
+
}
|
|
99
|
+
return Object.prototype.hasOwnProperty.call(this.apiInfo, apiName);
|
|
100
|
+
}
|
|
101
|
+
run(apiName, options) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
if (!this.isConnecting) {
|
|
104
|
+
throw new Error("Not connected");
|
|
105
|
+
}
|
|
106
|
+
if (!this.hasApi(apiName)) {
|
|
107
|
+
throw new Error(`${apiName} not found`);
|
|
108
|
+
}
|
|
109
|
+
const { method = "get", params, data, headers } = options;
|
|
110
|
+
const api = this.apiInfo[apiName];
|
|
111
|
+
const url = `${this.baseUrl}${api.path}`;
|
|
112
|
+
const externalParams = Object.assign({ api: apiName, version: api.maxVersion, _sid: this.authInfo.sid }, params);
|
|
113
|
+
let result = null;
|
|
114
|
+
if (method === "get") {
|
|
115
|
+
result = yield axios.get(url, { params: externalParams, data, headers });
|
|
116
|
+
}
|
|
117
|
+
if (method === "post") {
|
|
118
|
+
result = yield axios.post(url, { params: externalParams, data, headers });
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
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 {};
|
|
@@ -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
|
+
function getSongList(params) {
|
|
11
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
12
|
+
const res = yield this.run("SYNO.AudioStation.Song", {
|
|
13
|
+
params: {
|
|
14
|
+
method: "list",
|
|
15
|
+
library: "all",
|
|
16
|
+
limit: params.limit,
|
|
17
|
+
offset: params.offset,
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
return res.data;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export const AudioStationMethods = {
|
|
24
|
+
getSongList,
|
|
25
|
+
};
|
|
26
|
+
export const AudioStationProp = "AudioStation";
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// export type
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type AudioStationSongListRequest = {
|
|
2
|
+
limit?: number;
|
|
3
|
+
offset?: number;
|
|
4
|
+
method?: number;
|
|
5
|
+
library?: number;
|
|
6
|
+
additional?: string;
|
|
7
|
+
version?: number;
|
|
8
|
+
sort_by?: string;
|
|
9
|
+
sort_direction?: string;
|
|
10
|
+
};
|
|
11
|
+
export type AudioStationSongListResponse = {
|
|
12
|
+
offset: number;
|
|
13
|
+
total: number;
|
|
14
|
+
songs: {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fett/synology-api",
|
|
3
|
+
"version": "0.0.1-beta.0",
|
|
4
|
+
"description": "synology api for nodejs",
|
|
5
|
+
"module": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"author": "ChrisSong1994 <https://github.com/ChrisSong1994>",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"synology",
|
|
11
|
+
"api",
|
|
12
|
+
"nodejs",
|
|
13
|
+
"typescript",
|
|
14
|
+
"esmodule"
|
|
15
|
+
],
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"dev": "tsc --sourceMap --watch",
|
|
21
|
+
"build": "npm run clean && tsc",
|
|
22
|
+
"lint": "eslint src --ext .ts --fix",
|
|
23
|
+
"prettier": "prettier --write 'src/**/*.ts'",
|
|
24
|
+
"format": "npm run prettier && npm run lint",
|
|
25
|
+
"prepublish": "npm run build",
|
|
26
|
+
"pub": "npm publish --access=public",
|
|
27
|
+
"test": "vitest",
|
|
28
|
+
"coverage": "vitest run --coverage",
|
|
29
|
+
"clean": "rimraf dist"
|
|
30
|
+
},
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"packageManager": "pnpm@10.8.1",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@eslint/js": "^9.26.0",
|
|
35
|
+
"eslint": "9.25.1",
|
|
36
|
+
"globals": "^16.1.0",
|
|
37
|
+
"prettier": "3.5.3",
|
|
38
|
+
"rimraf": "^6.0.1",
|
|
39
|
+
"ts-node": "^10.9.2",
|
|
40
|
+
"typescript": "~5.8.3",
|
|
41
|
+
"typescript-eslint": "^8.32.0",
|
|
42
|
+
"vite": "^6.3.5",
|
|
43
|
+
"vitest": "^3.1.3"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"axios": "^1.9.0"
|
|
47
|
+
}
|
|
48
|
+
}
|