virtualizorjs 1.0.5 → 2.1.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 kkMihai
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 kkMihai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,126 +1,212 @@
1
- # VirtualizorJS
2
-
3
- Since there is no SDK's for Node.js for the Virtualizor API, I decided to create one, and one that is actually easy to use and useful with **0 Dependencies** keeping it `Lightweight` and `Fast`.
4
-
5
- VirtualizorJS simplifies the management of Virtualizor servers with a streamlined and developer-friendly API for Node.js. Perform actions such as creating, starting, stopping, and restarting virtual servers effortlessly. Ideal for seamless integration into your Node.js applications, providing a powerful toolkit for Virtualizor server management.
6
-
7
-
8
- [![GitHub stars](https://img.shields.io/github/stars/kkMihai/virtualizorjs.svg?style=social&label=Star&maxAge=2592000)](https://github.com/kkMihai/virtualizorjs)
9
-
10
- [![Npm package version](https://badgen.net/npm/v/virtualizorjs)](https://npmjs.com/package/virtualizorjs) [![Maintenance](https://img.shields.io/badge/Maintained%3F-Yes-green.svg)](https://github.com/kkMihai/virtualizorjs/graphs/commit-activity)
11
-
12
- ## Important
13
- - This library is still in development and there is more to add but for now it's stable and ready to use, all the methods have been tested and work as expected.
14
-
15
- ## Table of Contents
16
- - [Installation](#installation)
17
- - [Usage](#usage)
18
- - [Examples](#examples)
19
- - [API Documentation](#api-documentation)
20
- - [Roadmap](#roadmap)
21
- - [Contributing](#contributing)
22
- - [License](#license)
23
-
24
- ## Installation
25
-
26
- ```bash
27
- npm i virtualizorjs@latest
28
- ```
29
-
30
- ## Usage
31
-
32
- ```javascript
33
-
34
- const VirtualizorClient = require('virtualizorjs');
35
-
36
- // Initialize VirtualizorClient
37
- const { ListVPS } = new VirtualizorClient({
38
- host: '< IP or Hostname of Virtualizor Server >',
39
- port: 4085, // or 4085 for SSL
40
- adminapikey: "< Your API KEY >",
41
- adminapipass: "< Your API PASS >",
42
- });
43
-
44
- // Using const client = new VirtualizorClient({ ... }) is also valid, but you will have to use client.ListVPS() instead of ListVPS() which just looks ugly.
45
- // Example: Get a list of all VPSs
46
- ListVPS().then((data) => {
47
- console.log(data);
48
- }).catch((err) => {
49
- console.log(err);
50
- });
51
- ```
52
-
53
- # Event Handling Usage
54
-
55
- VirtualizorJS uses the [EventEmitter](https://nodejs.org/api/events.html) class to handle events. You can attach **`Event Listeners`** to different events provided by the VirtualizorClient.
56
-
57
- - Note: To use **`Event Listeners`** we need to define the **`VirtualizorClient`** as a `const` named preferably **`Client`** and then use **`Client.on()`** to attach **`Event Listeners`** to different events.
58
-
59
- ```javascript
60
- const VirtualizorClient = require('virtualizorjs');
61
-
62
- const Client = new VirtualizorClient({
63
- host: '< IP or Hostname of Virtualizor Server >',
64
- port: 4085,
65
- adminapikey: "< Your API KEY >",
66
- adminapipass: "< Your API PASS >",
67
- });
68
-
69
- //Get the methods you need
70
- const { StartVPS } = Client;
71
-
72
- // - Event Types - :
73
- // 1. vpsCreated
74
- // 2. vpsStarted
75
- // 3. vpsStopped
76
- // 4. vpsRestarted
77
-
78
- // Event listener for when a virtual server is created
79
- Client.on('vpsCreated', (response) => {
80
- console.log(`Virtual Server Created! Details:`, response);
81
- });
82
-
83
- // Event listener for when a virtual server is started
84
- Client.on('vpsStarted', (response) => {
85
- console.log(`Virtual Server Started! Details:`, response);
86
- });
87
-
88
- // Event listener for when a virtual server is stopped
89
- Client.on('vpsStopped', (response) => {
90
- console.log(`Virtual Server Stopped! Details:`, response);
91
- });
92
-
93
- // Event listener for when a virtual server is restarted
94
- Client.on('vpsRestarted', (response) => {
95
- console.log(`Virtual Server Restarted! Details:`, response);
96
- });
97
- ```
98
-
99
- ## What's the point of using **`Event Listeners`**?
100
- - **`Event Listeners`** are useful when you want to perform an action when a certain event occurs without modifying the source code of the **`VirtualizorJS`** library to avoid breaking changes.
101
- - For example, you can use **`Event Listeners`** to send a notification to your `users` when a event is triggered.
102
- - You can also use **`Event Listeners`** to perform an action when a event is triggered.
103
-
104
- ## Examples
105
-
106
- - [Get VPS's List](/examples/listvps.js)
107
- - [Create VPS](/examples/createvps.js)
108
- - [Using Event Handling](/examples/eventhandling.js)
109
-
110
-
111
- ## Documentation
112
-
113
- - Check the [Wiki](https://github.com/kkMihai/virtualizorjs/wiki) for detailed documentation.
114
- - If you use frameworks such as [Next.js](https://nextjs.org/) make sure to use it only Server-Side for security reasons.
115
-
116
- ## Roadmap
117
- - [ ] Add Proxmox KVM Support
118
- - [ ] Add Virtualization Types enums
119
-
120
- ## Contributing
121
-
122
- - Feel free to contribute by opening issues or submitting pull requests. See [CONTRIBUTING](/CONTRIBUTING.md) for details.
123
-
124
- ## License
125
-
126
- - This project is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details.
1
+ # VirtualizorJS
2
+
3
+ [![npm version](https://img.shields.io/npm/v/virtualizorjs)](https://www.npmjs.com/package/virtualizorjs)
4
+ [![CI Status](https://img.shields.io/github/actions/workflow/status/kkMihai/virtualizorjs/ci.yml?branch=main)](https://github.com/kkMihai/virtualizorjs/actions)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+
7
+ A **TypeScript-first** SDK for the [Virtualizor](https://www.virtualizor.com/) server management API. Manage VPS instances, users, and plans with a clean, namespaced interface and zero production dependencies.
8
+
9
+ ## Features
10
+
11
+ - ✅ **TypeScript-first**: Full type safety and IDE autocomplete
12
+ - ✅ **Namespaced API**: Logical organization (`client.vps.*`, `client.users.*`, `client.plans.*`, `client.tasks.*`)
13
+ - **SHA-256 Authentication**: Secure API communication
14
+ - ✅ **Zero Production Dependencies**: Lightweight and fast
15
+ - **Async Task Polling**: Built-in support for long-running operations
16
+ - ✅ **Self-signed SSL Ready**: Pre-configured for Virtualizor's typical certificate setup
17
+ - ✅ **Dual Output**: Outputs both CommonJS and ESM modules
18
+
19
+ ## Installation
20
+
21
+ **npm:**
22
+ ```bash
23
+ npm install virtualizorjs
24
+ ```
25
+
26
+ **bun:**
27
+ ```bash
28
+ bun add virtualizorjs
29
+ ```
30
+
31
+ ## Quick Start
32
+
33
+ ### Create a Client
34
+
35
+ ```typescript
36
+ import { createVirtualizorClient } from 'virtualizorjs';
37
+
38
+ const client = createVirtualizorClient({
39
+ host: 'virtualizor.example.com',
40
+ apiKey: 'your-api-key',
41
+ apiPass: 'your-api-pass',
42
+ // Optional: port (default 4085), https (default true),
43
+ // rejectUnauthorized (default false), timeout (default 30000ms)
44
+ // 4085 for Https, 4084 for Http
45
+ });
46
+ ```
47
+
48
+ ### List All VPS
49
+
50
+ ```typescript
51
+ const vpsList = await client.vps.list();
52
+
53
+ for (const [vpsId, vps] of Object.entries(vpsList)) {
54
+ console.log(`${vpsId}: ${vps.hostname} (${vps.status})`);
55
+ }
56
+ ```
57
+
58
+ ### Start a VPS and Wait for Completion
59
+
60
+ Many Virtualizor operations are asynchronous. Use `client.tasks.wait()` to poll for completion:
61
+
62
+ ```typescript
63
+ const result = await client.vps.start('123');
64
+ const task = await client.tasks.wait(result.taskid!);
65
+
66
+ console.log(`VPS started! Task status: ${task.status}`);
67
+ ```
68
+
69
+ ## Error Handling
70
+
71
+ All SDK errors are instances of `VirtualizorApiError`. Catch and inspect them:
72
+
73
+ ```typescript
74
+ import { VirtualizorApiError } from 'virtualizorjs';
75
+
76
+ try {
77
+ await client.vps.start('invalid-id');
78
+ } catch (err) {
79
+ if (err instanceof VirtualizorApiError) {
80
+ console.error(`API Error [${err.code}]: ${err.message}`);
81
+ } else {
82
+ console.error('Unexpected error:', err);
83
+ }
84
+ }
85
+ ```
86
+
87
+ ## API Reference
88
+
89
+ ### VPS Management
90
+
91
+ | Method | Parameters | Returns | Notes |
92
+ |--------|-----------|---------|-------|
93
+ | `list()` | | `Record<string, VPS>` | List all VPS |
94
+ | `get(vpsId)` | `vpsId: string` | `VPS` | Get a single VPS |
95
+ | `create(params)` | `CreateVPSParams` | `AsyncTaskResult` | Async |
96
+ | `delete(vpsId)` | `vpsId: string` | `AsyncTaskResult` | Async |
97
+ | `start(vpsId)` | `vpsId: string` | `AsyncTaskResult` | Async |
98
+ | `stop(vpsId)` | `vpsId: string` | `AsyncTaskResult` | Async |
99
+ | `restart(vpsId)` | `vpsId: string` | `AsyncTaskResult` | Async |
100
+ | `poweroff(vpsId)` | `vpsId: string` | `AsyncTaskResult` | Async |
101
+ | `suspend(vpsId)` | `vpsId: string` | `AsyncTaskResult` | Async |
102
+ | `unsuspend(vpsId)` | `vpsId: string` | `AsyncTaskResult` | Async |
103
+ | `rebuild(vpsId, params)` | `vpsId: string, RebuildVPSParams` | `AsyncTaskResult` | Async |
104
+ | `clone(vpsId, params)` | `vpsId: string, CloneVPSParams` | `AsyncTaskResult` | Async |
105
+ | `migrate(vpsId, params)` | `vpsId: string, MigrateVPSParams` | `AsyncTaskResult` | Async |
106
+ | `status(vpsId)` | `vpsId: string` | `unknown` | Current status snapshot |
107
+ | `vnc(vpsId)` | `vpsId: string` | `VNCInfo` | Get VNC connection details |
108
+ | `stats(vpsId)` | `vpsId: string` | `VPSStatsResponse` | Get resource usage statistics |
109
+
110
+ ### User Management
111
+
112
+ | Method | Parameters | Returns | Notes |
113
+ |--------|-----------|---------|-------|
114
+ | `list()` | | `Record<string, User>` | List all users |
115
+ | `create(params)` | `CreateUserParams` | `AsyncTaskResult` | Async |
116
+ | `delete(uid)` | `uid: string` | `AsyncTaskResult` | Async |
117
+ | `suspend(uid)` | `uid: string` | `AsyncTaskResult` | Async |
118
+ | `unsuspend(uid)` | `uid: string` | `AsyncTaskResult` | Async |
119
+
120
+ ### Plan Management
121
+
122
+ | Method | Parameters | Returns | Notes |
123
+ |--------|-----------|---------|-------|
124
+ | `list()` | — | `Record<string, Plan>` | List all plans |
125
+ | `create(params)` | `CreatePlanParams` | `AsyncTaskResult` | Async |
126
+ | `delete(planId)` | `planId: string` | `AsyncTaskResult` | Async |
127
+
128
+ ### Task Polling
129
+
130
+ | Method | Parameters | Returns | Notes |
131
+ |--------|-----------|---------|-------|
132
+ | `get(taskId)` | `taskId: string` | `Task \| undefined` | Get task status once |
133
+ | `wait(taskId, options?)` | `taskId: string, { pollIntervalMs?, timeoutMs? }?` | `Promise<Task>` | Poll until complete or timeout |
134
+
135
+ ## Task Polling Pattern
136
+
137
+ Many API calls return `AsyncTaskResult` with a `taskid` field. Poll for completion:
138
+
139
+ ```typescript
140
+ // Example: Create a VPS and wait for it to be ready
141
+ const createResult = await client.vps.create({
142
+ hostname: 'my-vps.example.com',
143
+ // ... other params
144
+ });
145
+
146
+ const completedTask = await client.tasks.wait(createResult.taskid!, {
147
+ pollIntervalMs: 5000, // Check every 5 seconds (default: 2000ms)
148
+ timeoutMs: 300000, // Give up after 5 minutes (default: 120000ms)
149
+ });
150
+
151
+ if (completedTask.status === '1' || completedTask.status === 'done') {
152
+ console.log('VPS creation completed successfully');
153
+ }
154
+ ```
155
+
156
+ ## Configuration
157
+
158
+ When creating a client, the following options are available:
159
+
160
+ ```typescript
161
+ interface VirtualizorConfig {
162
+ host: string; // Virtualizor server hostname or IP
163
+ apiKey: string; // API key from Virtualizor panel
164
+ apiPass: string; // API password from Virtualizor panel
165
+ port?: number; // Server port (default: 4085)
166
+ https?: boolean; // Use HTTPS (default: true)
167
+ rejectUnauthorized?: boolean; // Reject self-signed certs (default: false)
168
+ timeout?: number; // Request timeout in ms (default: 30000)
169
+ }
170
+ ```
171
+
172
+ ### Self-Signed SSL Certificates
173
+
174
+ Virtualizor typically uses self-signed SSL certificates. The SDK handles this by default with `rejectUnauthorized: false`. If you need to enforce certificate validation, set `rejectUnauthorized: true` and ensure your Virtualizor instance has a valid certificate.
175
+
176
+ ## TypeScript Types
177
+
178
+ All resources are fully typed. Import types as needed:
179
+
180
+ ```typescript
181
+ import type {
182
+ VPS,
183
+ CreateVPSParams,
184
+ RebuildVPSParams,
185
+ CloneVPSParams,
186
+ MigrateVPSParams,
187
+ User,
188
+ CreateUserParams,
189
+ Plan,
190
+ CreatePlanParams,
191
+ Task,
192
+ AsyncTaskResult,
193
+ } from 'virtualizorjs';
194
+ ```
195
+
196
+ ## Contributing
197
+
198
+ We welcome contributions! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines on how to help.
199
+
200
+ ## Security
201
+
202
+ Please report security vulnerabilities to the maintainers privately. See [SECURITY.md](./SECURITY.md) for more details.
203
+
204
+ ## License
205
+
206
+ This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
207
+
208
+ ---
209
+
210
+ **Author**: [kkMihai](https://github.com/kkMihai)
211
+ **Package**: [npm/virtualizorjs](https://www.npmjs.com/package/virtualizorjs)
212
+ **Repository**: [github.com/kkMihai/virtualizorjs](https://github.com/kkMihai/virtualizorjs)