Skip to content

Commit 02f4c05

Browse files
committed
feat: implement DedicatedServerApiService
1 parent f90d11c commit 02f4c05

File tree

4 files changed

+298
-4
lines changed

4 files changed

+298
-4
lines changed

src/structures/ApiClient.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { $Fetch } from 'ofetch';
2-
import { createFetch, UserApiService, VpsApiService, type RestClientOptions } from '.';
2+
import {
3+
createFetch,
4+
UserApiService,
5+
VpsApiService,
6+
DedicatedServerApiService,
7+
type RestClientOptions,
8+
} from '.';
39

410
export class NodestyAPIClient {
511
public apiFetch: $Fetch;
@@ -15,4 +21,8 @@ export class NodestyAPIClient {
1521
public get vps() {
1622
return new VpsApiService(this.apiFetch);
1723
}
24+
25+
public get dedicatedServer() {
26+
return new DedicatedServerApiService(this.apiFetch);
27+
}
1828
}
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
import type { $Fetch } from 'ofetch';
2+
import type { ApiResponse } from '..';
3+
4+
export class DedicatedServerApiService {
5+
public constructor(private apiFetch: $Fetch) {}
6+
7+
/**
8+
* Perform an action on a dedicated server
9+
* @param id Service ID
10+
* @param action Action to perform
11+
*/
12+
public performAction(id: string, action: DedicatedServerAction) {
13+
return this.apiFetch<ApiResponse<void>>(`/services/${id}/dedicated/action`, {
14+
method: 'POST',
15+
body: { action },
16+
});
17+
}
18+
19+
/**
20+
* Get hardware components of a dedicated server
21+
* @param id Service ID
22+
*/
23+
public getHardwareComponents(id: string) {
24+
return this.apiFetch<ApiResponse<DedicatedServerHardwareComponent>>(
25+
`/services/${id}/dedicated/hardware`,
26+
);
27+
}
28+
29+
/**
30+
* Get dedicated server details
31+
* @param id Service ID
32+
*/
33+
public getDetails(id: string) {
34+
return this.apiFetch<ApiResponse<DedicatedServerDetails>>(`/services/${id}/dedicated/info`);
35+
}
36+
37+
/**
38+
* Get available OS templates for a dedicated server
39+
* @param id Service ID
40+
*/
41+
public getOsTemplates(id: string) {
42+
return this.apiFetch<ApiResponse<DedicatedServerOsTemplate[]>>(
43+
`/services/${id}/dedicated/os-templates`,
44+
);
45+
}
46+
47+
public getReinstallStatus(id: string) {
48+
return this.apiFetch<ApiResponse<DedicatedServerReinstallStatus>>(
49+
`/services/${id}/dedicated/reinstall-status`,
50+
);
51+
}
52+
53+
/**
54+
* Reinstall a dedicated server with a new OS
55+
* @param id Service ID
56+
*/
57+
public reinstall(id: string, data: DedicatedServerReinstallData) {
58+
return this.apiFetch<ApiResponse<void>>(`/services/${id}/dedicated/reinstall`, {
59+
method: 'POST',
60+
body: data,
61+
});
62+
}
63+
64+
/**
65+
* Get dedicated server tasks
66+
* @param id Service ID
67+
*/
68+
public getTasks(id: string) {
69+
return this.apiFetch<ApiResponse<DedicatedServerTask[]>>(`/services/${id}/dedicated/tasks`);
70+
}
71+
}
72+
73+
/**
74+
* Represents the action to be performed on a dedicated server.
75+
*/
76+
export enum DedicatedServerAction {
77+
Start = 'setPowerOn',
78+
Stop = 'setPowerOff',
79+
Restart = 'setPowerReset',
80+
}
81+
82+
/**
83+
* Represents CPU details and usage for a dedicated server.
84+
*/
85+
export interface DedicatedServerCpuDetails {
86+
/**
87+
* Model of the CPU.
88+
* @example "AMD Ryzen 9 5950X"
89+
*/
90+
model: string;
91+
92+
/**
93+
* Base speed of the CPU in MHz.
94+
* @example 3500
95+
*/
96+
speed: number;
97+
98+
/**
99+
* Turbo speed of the CPU in MHz.
100+
* @example 4900
101+
*/
102+
turboSpeed: number;
103+
104+
/**
105+
* Number of CPU cores.
106+
* @example 16
107+
*/
108+
cores: number;
109+
110+
/**
111+
* Number of CPU threads.
112+
* @example 32
113+
*/
114+
threads: number;
115+
}
116+
117+
/**
118+
* Represents the detailed information about a dedicated server.
119+
*/
120+
export interface DedicatedServerDetails {
121+
/**
122+
* Unique identifier for the dedicated server.
123+
* @example "1"
124+
*/
125+
dedicatedId: string;
126+
127+
/**
128+
* Current status of the dedicated server.
129+
* @example true
130+
*/
131+
status: boolean;
132+
133+
/**
134+
* List of available actions for the dedicated server.
135+
*/
136+
availableActions: DedicatedServerAction[];
137+
138+
/**
139+
* Model of the mainboard.
140+
* @example "ASUS ROG Strix X570-E Gaming"
141+
*/
142+
mainboard: string;
143+
144+
/**
145+
* Amount of RAM in GB.
146+
* @example 128
147+
*/
148+
ram: number;
149+
150+
/**
151+
* Total disk space in GB.
152+
* @example 1000
153+
*/
154+
disk: number;
155+
156+
/**
157+
* CPU details for the dedicated server.
158+
*/
159+
cpu: DedicatedServerCpuDetails;
160+
}
161+
162+
/**
163+
* Represents an operating system template available for a dedicated server.
164+
*/
165+
export interface DedicatedServerOsTemplate {
166+
/**
167+
* OS version ID.
168+
* @example 1
169+
*/
170+
id: number;
171+
172+
/**
173+
* OS version name.
174+
* @example "Debian 9.5"
175+
*/
176+
name: string;
177+
}
178+
179+
/**
180+
* Represents the data required to reinstall a dedicated server with a new OS.
181+
*/
182+
export interface DedicatedServerReinstallData {
183+
/**
184+
* New root password for the dedicated server
185+
* @example "StrongPassword123!"
186+
*/
187+
password: string;
188+
189+
/**
190+
* ID of the new OS to install
191+
* @example 1
192+
*/
193+
osId: number;
194+
}
195+
196+
/**
197+
* Represents a task related to a dedicated server, such as rebooting or restoring a backup.
198+
*/
199+
export interface DedicatedServerTask {
200+
/**
201+
* Description of the task action.
202+
* @example "Server reboot initiated"
203+
*/
204+
action: string;
205+
206+
/**
207+
* Unix timestamp when the task started.
208+
* @example 1625251200000
209+
*/
210+
startedAt: number;
211+
212+
/**
213+
* Unix timestamp when the task was last updated.
214+
* @example 1625254800000
215+
*/
216+
updatedAt: number;
217+
}
218+
219+
/**
220+
* Represents a hardware component of a dedicated server, including its model and specifications.
221+
*/
222+
export interface DedicatedServerHardwareComponent {
223+
/**
224+
* Hardware component name.
225+
* @example "CPU Model"
226+
*/
227+
component: string;
228+
229+
/**
230+
* Model of the hardware component.
231+
* @example "AMD Ryzen 9 9950X"
232+
*/
233+
model: string;
234+
235+
/**
236+
* Value of the hardware component.
237+
* @example 5000
238+
*/
239+
value: number;
240+
241+
/**
242+
* Unit of the value.
243+
* @example " MHz"
244+
*/
245+
valueSuffix: string;
246+
}
247+
248+
/**
249+
* Enum representing the steps of the reinstall process for a dedicated server.
250+
*/
251+
export enum DedicatedServerReinstallStep {
252+
RebootingServer = 0,
253+
PreparingBootEnvironment = 1,
254+
InstallingOperatingSystem = 2,
255+
InstallationCompleted = 3,
256+
}
257+
258+
/**
259+
* Represents the status of the reinstall process for a dedicated server.
260+
*/
261+
export interface DedicatedServerReinstallStatus {
262+
/**
263+
* Whether the reinstall process is completed.
264+
* @example true
265+
*/
266+
completed: boolean;
267+
268+
/**
269+
* Current step of the reinstall process.
270+
* @example 1
271+
* @enum {0, 1, 2, 3}
272+
*/
273+
step: DedicatedServerReinstallStep;
274+
}

src/structures/services/VpsApiService.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class VpsApiService {
77
/**
88
* Perform an action on a VPS
99
* @param id Service ID
10-
* @param action Action to perform (start, stop, restart, poweroff)
10+
* @param action Action to perform
1111
*/
1212
public performAction(id: string, action: VpsAction) {
1313
return this.apiFetch<ApiResponse<void>>(`/services/${id}/vps/action`, {
@@ -87,6 +87,10 @@ export class VpsApiService {
8787
});
8888
}
8989

90+
/**
91+
* Get VPS tasks
92+
* @param id Service ID
93+
*/
9094
public getTasks(id: string) {
9195
return this.apiFetch<ApiResponse<VpsTask[]>>(`/services/${id}/vps/tasks`);
9296
}
@@ -95,7 +99,12 @@ export class VpsApiService {
9599
/**
96100
* Represents the action to be performed on a VPS.
97101
*/
98-
export type VpsAction = 'start' | 'stop' | 'restart' | 'poweroff';
102+
export enum VpsAction {
103+
Start = 'start',
104+
Stop = 'stop',
105+
Restart = 'restart',
106+
ForceStop = 'poweroff',
107+
}
99108

100109
/**
101110
* Represents a backup of a VPS.
@@ -520,7 +529,7 @@ export interface VpsBandwidthDetails {
520529
}
521530

522531
/**
523-
* Represents the detailed information about a Virtual Private Server (VPS).
532+
* Represents the detailed information about a VPS.
524533
*/
525534
export interface VpsDetails {
526535
/**

src/structures/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './UserApiService';
22
export * from './VpsApiService';
3+
export * from './DedicatedServerApiService';

0 commit comments

Comments
 (0)