Skip to content

Commit 6394ebd

Browse files
committed
allow some methods at BaseApplication
1 parent 0dad50b commit 6394ebd

File tree

3 files changed

+146
-124
lines changed

3 files changed

+146
-124
lines changed

.changeset/yellow-steaks-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/api": minor
3+
---
4+
5+
Allow some app methods at BaseApplication

src/structures/application/application.ts

Lines changed: 3 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { assertApplication } from "@/assertions/application";
2-
import { assertPathLike, assertString } from "@/assertions/literal";
3-
import { ApplicationStatus, SquareCloudAPI } from "@/index";
2+
import { ApplicationStatus, BaseApplication, SquareCloudAPI } from "@/index";
43
import {
54
ApplicationBackupManager,
65
ApplicationCacheManager,
76
ApplicationDeploysManager,
87
ApplicationFilesManager,
98
} from "@/managers";
109
import { APIApplication, ApplicationLanguage } from "@squarecloud/api-types/v2";
11-
import FormData from "form-data";
12-
import { readFile } from "fs/promises";
1310
import { WebsiteApplication } from "./website";
1411

1512
/**
@@ -19,7 +16,7 @@ import { WebsiteApplication } from "./website";
1916
* @param client - The client for this application
2017
* @param data - The data from this application
2118
*/
22-
export class Application {
19+
export class Application extends BaseApplication {
2320
/** The application ID */
2421
id: string;
2522
/** The application display name */
@@ -59,6 +56,7 @@ export class Application {
5956
data: APIApplication,
6057
) {
6158
assertApplication(data);
59+
super(client, { ...data, tag: data.name, lang: data.language });
6260

6361
const { id, name, desc, cluster, ram, language } = data;
6462

@@ -93,118 +91,6 @@ export class Application {
9391
return logs;
9492
}
9593

96-
/**
97-
* Starts up the application
98-
* @returns `true` for success or `false` for fail
99-
*/
100-
async start(): Promise<boolean> {
101-
const data = await this.client.api.application(
102-
"start",
103-
this.id,
104-
undefined,
105-
"POST",
106-
);
107-
108-
return data?.status === "success";
109-
}
110-
111-
/**
112-
* Stops the application
113-
* @returns `true` for success or `false` for fail
114-
*/
115-
async stop(): Promise<boolean> {
116-
const data = await this.client.api.application(
117-
"stop",
118-
this.id,
119-
undefined,
120-
"POST",
121-
);
122-
123-
return data?.status === "success";
124-
}
125-
126-
/**
127-
* Restarts the application
128-
* @returns `true` for success or `false` for fail
129-
*/
130-
async restart(): Promise<boolean> {
131-
const data = await this.client.api.application(
132-
"restart",
133-
this.id,
134-
undefined,
135-
"POST",
136-
);
137-
138-
return data?.status === "success";
139-
}
140-
141-
/**
142-
* Deletes your whole application
143-
*
144-
* - This action is irreversible.
145-
* @returns `true` for success or `false` for fail
146-
*/
147-
async delete(): Promise<boolean> {
148-
const data = await this.client.api.application(
149-
"delete",
150-
this.id,
151-
undefined,
152-
"DELETE",
153-
);
154-
155-
return data?.status === "success";
156-
}
157-
158-
/**
159-
* Commit files to your application folder
160-
*
161-
* - This action is irreversible.
162-
*
163-
* - Tip: use this to get an absolute path.
164-
* ```ts
165-
* require('path').join(__dirname, 'fileName')
166-
* ```
167-
* - Tip2: use a zip file to commit more than one archive
168-
*
169-
* @param file - Buffer or absolute path to the file
170-
* @param fileName - The file name (e.g.: "index.js")
171-
* @param restart - Whether the application should be restarted after the commit
172-
* @returns `true` for success or `false` for fail
173-
*/
174-
async commit(
175-
file: string | Buffer,
176-
fileName?: string,
177-
restart?: boolean,
178-
): Promise<boolean> {
179-
assertPathLike(file, "COMMIT_DATA");
180-
181-
if (fileName) {
182-
assertString(fileName, "FILE_NAME");
183-
}
184-
185-
if (typeof file === "string") {
186-
file = await readFile(file);
187-
}
188-
189-
const formData = new FormData();
190-
formData.append("file", file, { filename: fileName || "app.zip" });
191-
192-
const data = await this.client.api.application(
193-
"commit",
194-
this.id,
195-
{
196-
restart: `${Boolean(restart)}`,
197-
},
198-
{
199-
method: "POST",
200-
body: formData.getBuffer(),
201-
headers: formData.getHeaders(),
202-
},
203-
);
204-
205-
return data?.status === "success";
206-
}
207-
20894
isWebsite(): this is WebsiteApplication {
20995
const domain = Reflect.get(this, "domain");
21096
return Boolean(domain);

src/structures/application/base.ts

Lines changed: 138 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { SquareCloudAPI } from "@/index";
1+
import { assertPathLike, assertString } from "@/assertions/literal";
2+
import { ApplicationStatus, SquareCloudAPI } from "@/index";
23
import {
34
APIUserApplication,
45
APIWebsiteApplication,
56
ApplicationLanguage,
67
} from "@squarecloud/api-types/v2";
8+
import FormData from "form-data";
9+
import { readFile } from "fs/promises";
710
import { Application } from "./application";
811
import { WebsiteApplication } from "./website";
912

@@ -16,8 +19,8 @@ import { WebsiteApplication } from "./website";
1619
*/
1720
export class BaseApplication {
1821
id: string;
19-
tag: string;
20-
description?: string | undefined;
22+
name: string;
23+
description?: string;
2124
url: string;
2225
ram: number;
2326
/**
@@ -34,7 +37,7 @@ export class BaseApplication {
3437
*/
3538
language: ApplicationLanguage;
3639
cluster: string;
37-
isWebsite: boolean;
40+
private _isWebsite: boolean;
3841

3942
constructor(
4043
public readonly client: SquareCloudAPI,
@@ -43,16 +46,16 @@ export class BaseApplication {
4346
const { id, tag, desc, ram, lang, cluster, isWebsite } = data;
4447

4548
this.id = id;
46-
this.tag = tag;
49+
this.name = tag;
4750
this.description = desc;
4851
this.ram = ram;
4952
this.language = lang;
5053
this.cluster = cluster;
51-
this.isWebsite = isWebsite;
54+
this._isWebsite = isWebsite;
5255
this.url = `https://squarecloud.app/dashboard/app/${id}`;
5356
}
5457

55-
async fetch() {
58+
async fetch(): Promise<Application> {
5659
const data = await this.client.api.application("", this.id);
5760

5861
if (data.response.isWebsite) {
@@ -63,4 +66,132 @@ export class BaseApplication {
6366
}
6467
return new Application(this.client, data.response);
6568
}
69+
70+
/** @returns The application current status information */
71+
async getStatus(): Promise<ApplicationStatus> {
72+
const data = await this.client.api.application("status", this.id);
73+
const status = new ApplicationStatus(this.client, data.response, this.id);
74+
75+
return status;
76+
}
77+
78+
/** @returns The application logs */
79+
async getLogs(): Promise<string> {
80+
const data = await this.client.api.application("logs", this.id);
81+
const { logs } = data.response;
82+
83+
return logs;
84+
}
85+
86+
/**
87+
* Starts up the application
88+
* @returns `true` for success or `false` for fail
89+
*/
90+
async start(): Promise<boolean> {
91+
const data = await this.client.api.application(
92+
"start",
93+
this.id,
94+
undefined,
95+
"POST",
96+
);
97+
98+
return data?.status === "success";
99+
}
100+
101+
/**
102+
* Stops the application
103+
* @returns `true` for success or `false` for fail
104+
*/
105+
async stop(): Promise<boolean> {
106+
const data = await this.client.api.application(
107+
"stop",
108+
this.id,
109+
undefined,
110+
"POST",
111+
);
112+
113+
return data?.status === "success";
114+
}
115+
116+
/**
117+
* Restarts the application
118+
* @returns `true` for success or `false` for fail
119+
*/
120+
async restart(): Promise<boolean> {
121+
const data = await this.client.api.application(
122+
"restart",
123+
this.id,
124+
undefined,
125+
"POST",
126+
);
127+
128+
return data?.status === "success";
129+
}
130+
131+
/**
132+
* Deletes your whole application
133+
*
134+
* - This action is irreversible.
135+
* @returns `true` for success or `false` for fail
136+
*/
137+
async delete(): Promise<boolean> {
138+
const data = await this.client.api.application(
139+
"delete",
140+
this.id,
141+
undefined,
142+
"DELETE",
143+
);
144+
145+
return data?.status === "success";
146+
}
147+
148+
/**
149+
* Commit files to your application folder
150+
*
151+
* - This action is irreversible.
152+
*
153+
* - Tip: use this to get an absolute path.
154+
* ```ts
155+
* require('path').join(__dirname, 'fileName')
156+
* ```
157+
* - Tip2: use a zip file to commit more than one archive
158+
*
159+
* @param file - Buffer or absolute path to the file
160+
* @param fileName - The file name (e.g.: "index.js")
161+
* @param restart - Whether the application should be restarted after the commit
162+
* @returns `true` for success or `false` for fail
163+
*/
164+
async commit(
165+
file: string | Buffer,
166+
fileName?: string,
167+
restart?: boolean,
168+
): Promise<boolean> {
169+
assertPathLike(file, "COMMIT_DATA");
170+
171+
if (fileName) {
172+
assertString(fileName, "FILE_NAME");
173+
}
174+
175+
if (typeof file === "string") {
176+
file = await readFile(file);
177+
}
178+
179+
const formData = new FormData();
180+
formData.append("file", file, { filename: fileName || "app.zip" });
181+
182+
const data = await this.client.api.application(
183+
"commit",
184+
this.id,
185+
{
186+
restart: `${Boolean(restart)}`,
187+
},
188+
{
189+
method: "POST",
190+
body: formData.getBuffer(),
191+
headers: formData.getHeaders(),
192+
},
193+
);
194+
195+
return data?.status === "success";
196+
}
66197
}

0 commit comments

Comments
 (0)