Skip to content

Commit 5720337

Browse files
committed
added server and application services
1 parent bc8801f commit 5720337

File tree

7 files changed

+728
-13
lines changed

7 files changed

+728
-13
lines changed

.changeset/long-plums-stare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cloudways-js-client": patch
3+
---
4+
5+
added server and applications services

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ export * from "./services/Lists";
33
export * from "./services/core";
44
export * from "./services/projects";
55
export * from "./services/operation";
6+
export * from "./services/application";
7+
export * from "./services/server";
68

79
// export all the types
810
export * from "./services/Lists/types";
911
export * from "./services/core/types";
1012
export * from "./services/projects/types";
13+
export * from "./services/server/types";

src/services/Lists/index.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import { apiCall } from "../core";
22
import type {
33
BackupFrequency,
44
Country,
5-
App,
5+
AppInfo,
66
Provider,
77
Region,
88
PackageList,
99
ServerSize,
1010
Setting,
11+
ServerMonitoringTarget,
1112
} from "./types";
1213

1314
/**
1415
* Gets a list of available apps and their versions.
15-
* @returns {Promise<App[]>} A promise resolving with an array of apps and their versions.
16+
* @returns {Promise<AppInfo[]>} A promise resolving with an array of apps and their versions.
1617
* @example
1718
* ```
1819
* [
@@ -34,7 +35,7 @@ import type {
3435
* ]
3536
* ```
3637
*/
37-
export function getAppList(): Promise<App[]> {
38+
export function getAppList(): Promise<AppInfo[]> {
3839
return apiCall("/apps");
3940
}
4041

@@ -78,23 +79,32 @@ export function getCountriesList(): Promise<Country[]> {
7879
* ```
7980
*/
8081
export function getMonitorDurations(): Promise<string[]> {
81-
return apiCall("/monitor_durations");
82+
return apiCall("/monitor_durations").then((response) => response.durations);
8283
}
8384

8485
/**
8586
* Gets a list of server monitoring graph types.
86-
* @returns {Promise<string[]>} A promise resolving with monitoring targets.
87+
* @returns {Promise<ServerMonitoringTarget[]>} A promise resolving with monitoring targets for each provider.
8788
* @example
8889
* ```
89-
* {
90-
* "amazon": ["Idle CPU", "Free Disk (DB)", ...],
91-
* "do": ["Idle CPU", "Free Disk", ...],
90+
* [
91+
* { provider: "amazon", targets: ["Idle CPU", "Free Disk (DB)", ...] },
92+
* { provider: "do", targets: ["Idle CPU", "Free Disk", ...] },
9293
* ...
93-
* }
94+
* ]
9495
* ```
9596
*/
96-
export function getMonitorTargets(): Promise<{ [provider: string]: string[] }> {
97-
return apiCall("/monitor_targets");
97+
export function getMonitorTargets(): Promise<ServerMonitoringTarget[]> {
98+
return apiCall("/monitor_targets").then((response) => {
99+
const monitorTargetsList: ServerMonitoringTarget[] = [];
100+
for (const provider in response) {
101+
monitorTargetsList.push({
102+
provider: provider,
103+
targets: response[provider],
104+
});
105+
}
106+
return monitorTargetsList;
107+
});
98108
}
99109

100110
/**

src/services/Lists/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export interface AppVersion {
5050
/**
5151
* Represents an app with its versions.
5252
*/
53-
export interface App {
53+
export interface AppInfo {
5454
label: string;
5555
versions: AppVersion[];
5656
}
@@ -59,7 +59,7 @@ export interface App {
5959
* Represents the response for getting a list of apps.
6060
*/
6161
export interface GetAppListResponse {
62-
[appName: string]: App;
62+
[appName: string]: AppInfo;
6363
}
6464

6565
/**
@@ -141,3 +141,11 @@ export interface Setting {
141141
setting: string;
142142
values: string[];
143143
}
144+
145+
/**
146+
* Represents a server monitoring target for a provider.
147+
*/
148+
export interface ServerMonitoringTarget {
149+
provider: string;
150+
targets: string[];
151+
}

src/services/application/index.ts

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import { apiCall } from "../core";
2+
import { HttpMethod } from "../core/types";
3+
4+
/**
5+
* Start the process of adding an app to a server.
6+
* @param {number} serverId Numeric id of the server.
7+
* @param {string} application App to be installed (e.g., "wordpress", "joomla").
8+
* @param {string} appLabel Name of the app.
9+
* @param {string} projectName (Optional) Name of the project to tag the newly created app with.
10+
* @returns {Promise< number >} A promise resolving with the operation id.
11+
* @example
12+
* ```
13+
*
14+
* 12345
15+
*
16+
* ```
17+
*/
18+
export function startAddAppProcess(
19+
serverId: number,
20+
application: string,
21+
appLabel: string,
22+
projectName?: string
23+
): Promise<number> {
24+
const data = {
25+
server_id: serverId,
26+
application: application,
27+
app_label: appLabel,
28+
project_name: projectName || "",
29+
};
30+
return apiCall("/app", HttpMethod.POST, data).then(
31+
(response) => response.operation_id
32+
);
33+
}
34+
35+
/**
36+
* Clone an app to the same server.
37+
* @param {number} serverId Numeric id of the server.
38+
* @param {number} appId Numeric id of the application.
39+
* @param {string} appLabel Name of the app.
40+
* @returns {Promise<{ appId: number, operationId: number }>} A promise resolving with the cloned app's id and operation id.
41+
* @example
42+
* ```
43+
* {
44+
* "appId": 1111,
45+
* "operationId": 12345
46+
* }
47+
* ```
48+
*/
49+
export function cloneApp(
50+
serverId: number,
51+
appId: number,
52+
appLabel: string
53+
): Promise<{ appId: number; operationId: number }> {
54+
const data = {
55+
server_id: serverId,
56+
app_id: appId,
57+
app_label: appLabel,
58+
};
59+
return apiCall("/app/clone", HttpMethod.POST, data).then((response) => ({
60+
appId: response.app_id,
61+
operationId: response.operation_id,
62+
}));
63+
}
64+
65+
/**
66+
* Clone an app to another existing server.
67+
* @param {number} serverId Numeric id of the source server.
68+
* @param {number} appId Numeric id of the application.
69+
* @param {number} destinationServerId Numeric id of the destination server.
70+
* @returns {Promise<{ sourceOperationId: number, destinationOperationId: number, appId: number }>} A promise resolving with the source and destination operation ids along with the cloned app's id.
71+
* @example
72+
* ```
73+
* {
74+
* "sourceOperationId": 12345,
75+
* "destinationOperationId": 12346,
76+
* "appId": 1111
77+
* }
78+
* ```
79+
*/
80+
export function cloneAppToOtherServer(
81+
serverId: number,
82+
appId: number,
83+
destinationServerId: number
84+
): Promise<{
85+
sourceOperationId: number;
86+
destinationOperationId: number;
87+
appId: number;
88+
}> {
89+
const data = {
90+
server_id: serverId,
91+
app_id: appId,
92+
destination_server_id: destinationServerId,
93+
};
94+
return apiCall("/app/cloneToOtherServer", HttpMethod.POST, data).then(
95+
(response) => ({
96+
sourceOperationId: response.source_operation_id,
97+
destinationOperationId: response.destination_operation_id,
98+
appId: response.app_id,
99+
})
100+
);
101+
}
102+
103+
/**
104+
* Clone a staging app to the same server.
105+
* @param {number} serverId Numeric id of the server.
106+
* @param {number} appId Numeric id of the staging application.
107+
* @returns {Promise<{ appId: number, operationId: number }>} A promise resolving with the cloned staging app's id and operation id.
108+
* @example
109+
* ```
110+
* {
111+
* "appId": 1111,
112+
* "operationId": 12345
113+
* }
114+
* ```
115+
*/
116+
export function cloneStagingApp(
117+
serverId: number,
118+
appId: number
119+
): Promise<{ appId: number; operationId: number }> {
120+
const data = {
121+
server_id: serverId,
122+
app_id: appId,
123+
};
124+
return apiCall("/staging/app/cloneApp", HttpMethod.POST, data).then(
125+
(response) => ({
126+
appId: response.app_id,
127+
operationId: response.operation_id,
128+
})
129+
);
130+
}
131+
132+
/**
133+
* Clone a staging app to another existing server.
134+
* @param {number} serverId Numeric id of the source server.
135+
* @param {number} appId Numeric id of the staging application.
136+
* @param {number} destinationServerId Numeric id of the destination server.
137+
* @returns {Promise<{ sourceOperationId: number, destinationOperationId: number, appId: number }>} A promise resolving with the operation ids and the cloned staging app's id.
138+
* @example
139+
* ```
140+
* {
141+
* "sourceOperationId": 12345,
142+
* "destinationOperationId": 12346,
143+
* "appId": 1111
144+
* }
145+
* ```
146+
*/
147+
export function cloneStagingAppToOtherServer(
148+
serverId: number,
149+
appId: number,
150+
destinationServerId: number
151+
): Promise<{
152+
sourceOperationId: number;
153+
destinationOperationId: number;
154+
appId: number;
155+
}> {
156+
const data = {
157+
server_id: serverId,
158+
app_id: appId,
159+
destination_server_id: destinationServerId,
160+
};
161+
return apiCall("/staging/app/cloneToOtherServer", HttpMethod.POST, data).then(
162+
(response) => ({
163+
sourceOperationId: response.source_operation_id,
164+
destinationOperationId: response.destination_operation_id,
165+
appId: response.app_id,
166+
})
167+
);
168+
}
169+
170+
/**
171+
* Start the process of removing an app.
172+
* @param {number} serverId Numeric id of the server.
173+
* @param {number} appId Numeric id of the application.
174+
* @returns {Promise<number>} A promise resolving with the operation id.
175+
* @example
176+
* ```
177+
* 12345
178+
* ```
179+
*/
180+
export function startRemoveAppProcess(
181+
serverId: number,
182+
appId: number
183+
): Promise<number> {
184+
return apiCall(`/app/${appId}`, HttpMethod.DELETE, {
185+
server_id: serverId,
186+
}).then((response) => response.operation_id);
187+
}
188+
189+
/**
190+
* Update the label of an application.
191+
* @param {number} appId Numeric id of the application.
192+
* @param {number} serverId Numeric id of the server.
193+
* @param {string} label New label of the application.
194+
* @returns {Promise<boolean>} A promise resolving with a boolean indicating the update status.
195+
* @example
196+
* ```
197+
* true
198+
* ```
199+
*/
200+
export function updateAppLabel(
201+
appId: number,
202+
serverId: number,
203+
label: string
204+
): Promise<boolean> {
205+
const data = {
206+
server_id: serverId,
207+
label: label,
208+
};
209+
return apiCall(`/app/${appId}`, HttpMethod.PUT, data).then(
210+
(response) => response.status
211+
);
212+
}

0 commit comments

Comments
 (0)