Skip to content

Commit 9ca78dc

Browse files
committed
added projects services
1 parent 8f4cc99 commit 9ca78dc

File tree

4 files changed

+199
-1
lines changed

4 files changed

+199
-1
lines changed

.changeset/unlucky-fans-jam.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 the projects services

services/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function getNewToken(): Promise<void> {
3535

3636
try {
3737
const response: AxiosResponse = await axios.post(
38-
"/oauth/access_token",
38+
"https://api.cloudways.com/api/v1/oauth/access_token",
3939
config
4040
);
4141

services/projects/index.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type {
2+
CreateProjectParams,
3+
CreateProjectResponse,
4+
DeleteProjectParams,
5+
GetProjectListResponse,
6+
UpdateProjectParams,
7+
UpdateProjectResponse,
8+
} from "./types";
9+
import { apiCall } from "../core";
10+
import { HttpMethod } from "../core/types";
11+
12+
/**
13+
* Creates a new project by making a POST request to the specified API endpoint.
14+
*
15+
* @param {CreateProjectParams} params The parameters required for creating a new project, including the project name and associated application IDs.
16+
* @returns {Promise<CreateProjectResponse>} A promise that resolves to the response of the project creation process.
17+
* @throws {Error} Throws an error if the request fails.
18+
*/
19+
export async function createProject(
20+
params: CreateProjectParams
21+
): Promise<CreateProjectResponse> {
22+
return apiCall("/project", HttpMethod.POST, params);
23+
}
24+
25+
/**
26+
* Deletes a project by making a DELETE request to the specified API endpoint.
27+
*
28+
* @param {DeleteProjectParams} params The parameters required for deleting a project, including the numeric project ID.
29+
* @returns {Promise<void>} A promise that resolves when the project is successfully deleted.
30+
* @throws {Error} Throws an error if the request fails.
31+
*/
32+
export async function deleteProject(
33+
params: DeleteProjectParams
34+
): Promise<void> {
35+
return apiCall(`/project/${params.id}`, HttpMethod.DELETE);
36+
}
37+
38+
/**
39+
* Retrieves a list of projects by making a GET request to the specified API endpoint.
40+
*
41+
* @returns {Promise<GetProjectListResponse>} A promise that resolves to the response containing the list of projects.
42+
* @throws {Error} Throws an error if the request fails.
43+
*/
44+
export async function getProjectList(): Promise<GetProjectListResponse> {
45+
return apiCall("/project");
46+
}
47+
48+
/**
49+
* Updates a project by making a PUT request to the specified API endpoint.
50+
*
51+
* @param {UpdateProjectParams} params The parameters required for updating the project, including the project ID, new name, and associated application IDs.
52+
* @returns {Promise<UpdateProjectResponse>} A promise that resolves to the response of the project update process.
53+
* @throws {Error} Throws an error if the request fails.
54+
*/
55+
export async function updateProject(
56+
params: UpdateProjectParams
57+
): Promise<UpdateProjectResponse> {
58+
return apiCall(`/project/${params.id}`, HttpMethod.PUT, params);
59+
}

services/projects/types.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* Represents the parameters required to create a new project.
3+
*/
4+
export interface CreateProjectParams {
5+
/**
6+
* Name of the project.
7+
*/
8+
name: string;
9+
10+
/**
11+
* Comma separated list of application IDs to be attached to this project.
12+
*/
13+
app_ids: string;
14+
}
15+
16+
/**
17+
* Represents the response structure for the create project API.
18+
*/
19+
export interface CreateProjectResponse {
20+
/**
21+
* The project object.
22+
*/
23+
project: {
24+
/**
25+
* Unique identifier of the project.
26+
*/
27+
id: string;
28+
29+
/**
30+
* Name of the project.
31+
*/
32+
name: string;
33+
34+
/**
35+
* User ID associated with the project.
36+
*/
37+
user_id: string;
38+
39+
/**
40+
* Indicates if this is the default project.
41+
*/
42+
is_default: string;
43+
44+
/**
45+
* Date and time when the project was created.
46+
*/
47+
created_at: string;
48+
49+
/**
50+
* Date and time when the project was last updated.
51+
*/
52+
updated_at: string;
53+
54+
/**
55+
* Image associated with the project, if any.
56+
*/
57+
image: string | null;
58+
};
59+
}
60+
61+
/**
62+
* Represents the parameters required to delete a project.
63+
*/
64+
export interface DeleteProjectParams {
65+
/**
66+
* Numeric ID of the project to be deleted.
67+
*/
68+
id: number;
69+
}
70+
71+
/**
72+
* Represents a single project in the project list.
73+
*/
74+
export interface Project {
75+
/**
76+
* Unique identifier of the project.
77+
*/
78+
id: string;
79+
80+
/**
81+
* Name of the project.
82+
*/
83+
name: string;
84+
85+
/**
86+
* Date and time when the project was created.
87+
*/
88+
created_at: string;
89+
90+
/**
91+
* URL of the project's image.
92+
*/
93+
image: string;
94+
}
95+
96+
/**
97+
* Represents the response structure for the get project list API.
98+
*/
99+
export interface GetProjectListResponse {
100+
/**
101+
* Array of projects.
102+
*/
103+
projects: Project[];
104+
}
105+
106+
/**
107+
* Represents the parameters required to update a project.
108+
*/
109+
export interface UpdateProjectParams {
110+
/**
111+
* Numeric ID of the project to be updated.
112+
*/
113+
id: number;
114+
115+
/**
116+
* New name of the project.
117+
*/
118+
name: string;
119+
120+
/**
121+
* Comma separated list of application IDs to be attached to this project.
122+
*/
123+
app_ids: string;
124+
}
125+
126+
/**
127+
* Represents the response structure for the update project API.
128+
*/
129+
export interface UpdateProjectResponse {
130+
/**
131+
* The updated project object.
132+
*/
133+
project: Project;
134+
}

0 commit comments

Comments
 (0)