Skip to content

Commit 7b2ff45

Browse files
committed
inc version, add types, fix errors of moving packages
1 parent 9f18a68 commit 7b2ff45

File tree

6 files changed

+158
-120
lines changed

6 files changed

+158
-120
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@carbonhost/typescript",
3-
"version": "1.0.8",
3+
"version": "1.1.0",
44
"main": "dist/index.js",
55
"module": "dist/index.mjs",
66
"types": "dist/index.d.ts",

src/carbon-star.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import type { Carbon } from "./carbon";
55
import { FileManager } from "./file-manager";
66
import { MinecraftManager } from "./managers/minecraft-manager";
77
import { StatManager } from "./managers/stat-manager";
8-
import { PortManager } from "./ports";
8+
import { NetworkManager } from "./network";
99
import { UserManager } from "./stars/users";
1010
import type { JavaVersion, UpdateStarType } from "./types/create-star";
11-
import type { CarbonStarType, CarbonSubdomainType, StarResources } from "./types/star";
11+
import type {
12+
CarbonStarType,
13+
CarbonSubdomainType,
14+
StarResources,
15+
} from "./types/star";
1216

1317
export class CarbonStar {
1418
// @ts-ignore
@@ -63,13 +67,10 @@ export class CarbonStar {
6367

6468
billingCycle: "monthly" | "hourly";
6569

66-
constructor(
67-
carbonClient: Carbon,
68-
carbonStar: CarbonStarType,
69-
) {
70+
constructor(carbonClient: Carbon, carbonStar: CarbonStarType) {
7071
this.carbonClient = carbonClient;
71-
this.axios = axios.create(carbonClient.getAxios().defaults)
72-
this.axios.defaults.baseURL = `${this.axios.defaults.baseURL}/v1/stars/${carbonStar._id}`
72+
this.axios = axios.create(carbonClient.getAxios().defaults);
73+
this.axios.defaults.baseURL = `${this.axios.defaults.baseURL}/v1/stars/${carbonStar._id}`;
7374

7475
this._id = carbonStar._id;
7576
this.ownerId = carbonStar.ownerId;
@@ -104,7 +105,7 @@ export class CarbonStar {
104105
this.resources = {
105106
storage: carbonStar.resources.storage,
106107
memory: carbonStar.resources.memory,
107-
vCPU: carbonStar.resources.vCPU
108+
vCPU: carbonStar.resources.vCPU,
108109
};
109110

110111
this.suspended = carbonStar.suspended;
@@ -116,7 +117,7 @@ export class CarbonStar {
116117
}
117118

118119
get users() {
119-
return new UserManager(this, this.axios)
120+
return new UserManager(this, this.axios);
120121
}
121122

122123
get minecraft() {
@@ -135,30 +136,39 @@ export class CarbonStar {
135136
return new BackupManager(this, this.axios, this.carbonClient.getAxios());
136137
}
137138

138-
get ports() {
139-
return new PortManager(this, this.axios);
139+
get network() {
140+
return new NetworkManager(this, this.axios);
140141
}
141142

142143
async delete() {
143-
return this.carbonClient.getAxios().delete(`/v1/stars/${this._id}`).then((res) => res.data);
144+
return this.carbonClient
145+
.getAxios()
146+
.delete(`/v1/stars/${this._id}`)
147+
.then((res) => res.data);
144148
}
145149

146150
async update(request: UpdateStarType) {
147-
return this.carbonClient.getAxios().patch(`/v1/stars/${this._id}`, request).then((res) => res.data);
151+
return this.carbonClient
152+
.getAxios()
153+
.patch(`/v1/stars/${this._id}`, request)
154+
.then((res) => res.data);
148155
}
149156

150157
async rename(name: string) {
151-
return this.carbonClient.getAxios().put(`/v1/stars/${this._id}/name`, {
152-
name,
153-
}).then((res) => res.data);
158+
return this.carbonClient
159+
.getAxios()
160+
.put(`/v1/stars/${this._id}/name`, {
161+
name,
162+
})
163+
.then((res) => res.data);
154164
}
155165

156166
// async getWebsocketInfo() {
157167
// return this.axios.get
158168
// }
159169

160170
async getResources() {
161-
return this.axios.get<StarResources>("/resources").then((res) => res.data);
171+
return this.axios.get<StarResources>("/resources").then((res) => res.data);
162172
}
163173

164174
async setPower(action: "start" | "stop" | "restart" | "kill") {
@@ -192,4 +202,4 @@ export class CarbonStar {
192202
// console.log(err.response.data);
193203
// });
194204
// }
195-
}
205+
}

src/file-manager/index.ts

Lines changed: 112 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,117 @@ import type { CarbonStar } from "../carbon-star";
33
import type { ChmodOptions, FileInfo } from "./types";
44

55
export class FileManager {
6-
private star: CarbonStar;
7-
private axios: AxiosInstance;
8-
9-
constructor(star: CarbonStar, axios: AxiosInstance) {
10-
this.star = star;
11-
this.axios = axios;
12-
}
13-
14-
private async fetchFiles(directory: string) {
15-
return this.axios.get<FileInfo[]>(`/files?path=${directory}`).then(res => res.data)
16-
}
17-
18-
async getFiles(directory: string) {
19-
return await this.fetchFiles(directory)
20-
}
21-
22-
async getFile(path: string) {
23-
return this.axios.get<{ content?: string, imageSrc?: string }>(`/files/content?path=${path}`).then(res => res.data)
24-
}
25-
26-
async saveFile(path: string, content: string) {
27-
return this.axios.put("/files/write", { path, content })
28-
}
29-
30-
async chmod(options: ChmodOptions) {
31-
return this.axios.post("/files/chmod", options)
32-
}
33-
34-
async moveFiles(path: string, files: { from: string, to: string }[]) {
35-
return this.axios.post("/files/move", { path, files })
36-
}
37-
38-
async renameFile(path: string, name: string) {
39-
return this.axios.put("/files/rename", { path, name })
40-
}
41-
42-
async duplicateFile(path: string) {
43-
return this.axios.post("/files/duplicate", { path })
44-
}
45-
46-
async downloadFile(path: string) {
47-
return this.axios.get("/files/download", { params: { path }, responseType: 'blob' })
48-
}
49-
50-
async deleteFile(params: { path?: string, paths?: string[] }) {
51-
const requestParams: { path?: string, paths?: string } = {};
52-
53-
if (params.path) {
54-
requestParams.path = params.path;
55-
}
56-
57-
if (params.paths?.length) {
58-
requestParams.paths = `[${params.paths.map(path => `"${path}"`).join(",")}]`;
6+
private star: CarbonStar;
7+
private axios: AxiosInstance;
8+
9+
constructor(star: CarbonStar, axios: AxiosInstance) {
10+
this.star = star;
11+
this.axios = axios;
12+
}
13+
14+
private async fetchFiles(directory: string) {
15+
return this.axios
16+
.get<FileInfo[]>(`/files?path=${directory}`)
17+
.then((res) => res.data);
18+
}
19+
20+
async getFiles(directory: string) {
21+
return await this.fetchFiles(directory);
22+
}
23+
24+
async getFile(path: string) {
25+
return this.axios
26+
.get<{ content?: string; imageSrc?: string }>(
27+
`/files/content?path=${path}`,
28+
)
29+
.then((res) => res.data);
30+
}
31+
32+
async saveFile(path: string, content: string) {
33+
return this.axios.put("/files/write", { path, content });
34+
}
35+
36+
async chmod(options: ChmodOptions) {
37+
return this.axios.post("/files/chmod", options);
38+
}
39+
40+
async moveFiles(path: string, files: { from: string; to: string }[]) {
41+
return this.axios.post("/files/move", { path, files });
42+
}
43+
44+
async renameFile(path: string, name: string) {
45+
return this.axios.put("/files/rename", { path, name });
46+
}
47+
48+
async duplicateFile(path: string) {
49+
return this.axios.post("/files/duplicate", { path });
50+
}
51+
52+
async downloadFile(path: string) {
53+
return this.axios.get("/files/download", {
54+
params: { path },
55+
responseType: "blob",
56+
});
57+
}
58+
59+
async deleteFile(params: { path?: string; paths?: string[] }) {
60+
const requestParams: { path?: string; paths?: string } = {};
61+
62+
if (params.path) {
63+
requestParams.path = params.path;
64+
}
65+
66+
if (params.paths?.length) {
67+
requestParams.paths = `[${params.paths.map((path) => `"${path}"`).join(",")}]`;
68+
}
69+
70+
return this.axios.delete("/files", { params: requestParams });
71+
}
72+
73+
async createFile(parentDirectory: string, fileName: string) {
74+
return this.axios.post("/files", {
75+
type: "file",
76+
path: `${parentDirectory}/${fileName}`,
77+
});
78+
}
79+
80+
async createDirectory(parentDirectory: string, folderName: string) {
81+
return this.axios.post("/files", {
82+
type: "directory",
83+
path: `${parentDirectory}/${folderName}`,
84+
});
85+
}
86+
87+
async decompressFile(root: string, file: string) {
88+
return this.axios.post("/files/decompress", { root, file });
89+
}
90+
91+
async compressFiles(root: string, files: string[]) {
92+
return this.axios.post("/files/compress", { root, files });
93+
}
94+
95+
async uploadFile(path: string, file: File) {
96+
try {
97+
// get url
98+
const url = await this.axios
99+
.get("/files/upload", { params: { path } })
100+
.then((res) => res.data.url);
101+
102+
const formData = new FormData();
103+
formData.append("files", file);
104+
105+
const updatedURL = `${url}&directory=${path}`;
106+
107+
// Send the FormData directly, not wrapped in an object
108+
return await this.axios.post(updatedURL, formData, {
109+
headers: {
110+
// Don't set Content-Type manually - axios will set it automatically with boundary
111+
'Content-Type': 'multipart/form-data'
59112
}
60-
61-
return this.axios.delete("/files", { params: requestParams })
62-
}
63-
64-
async createFile(parentDirectory: string, fileName: string) {
65-
return this.axios.post("/files", {
66-
type: "file",
67-
path: `${parentDirectory}/${fileName}`
68-
})
69-
}
70-
71-
async createDirectory(parentDirectory: string, folderName: string) {
72-
return this.axios.post("/files", {
73-
type: "directory",
74-
path: `${parentDirectory}/${folderName}`
75-
})
76-
}
77-
78-
async decompressFile(root: string, file: string) {
79-
return this.axios.post("/files/decompress", { root, file })
80-
}
81-
82-
async compressFiles(root: string, files: string[]) {
83-
return this.axios.post("/files/compress", { root, files })
84-
}
85-
86-
async uploadFile(path: string, file: File) {
87-
88-
// get url
89-
const url = await this.axios.get("/files/upload", { params: { path } })
90-
.then(res => res.data.url)
91-
92-
93-
// upload file
94-
// files form data
95-
// directory
96-
const formData = new FormData();
97-
formData.append("files", file);
98-
99-
const updatedURL = `${url}?directory=${path}`;
100-
101-
return this.axios.post(updatedURL, formData)
113+
});
114+
} catch (err) {
115+
console.error('Upload error:', err);
116+
throw err;
102117
}
103-
}
118+
}
119+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export * from "./file-manager/types";
99
export * from "./carbon-plugin/types";
1010
export * from "./backups/types";
1111
export * from "./invites/types";
12-
export * from "./stars/users/types"
13-
export * from "./ports/types";
12+
export * from "./stars/users/types";
13+
export * from "./network/types";
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { AxiosInstance } from "axios";
22
import type { CarbonStar } from "../carbon-star";
3-
import type { Port } from "./types";
3+
import type { Port, SFTPDetails } from "./types";
44

5-
export class PortManager {
5+
export class NetworkManager {
66
private star: CarbonStar;
77
private axios: AxiosInstance;
88

@@ -23,4 +23,8 @@ export class PortManager {
2323
return this.axios.post<Port>("/ports", { notes }).then(res => res.data)
2424
}
2525

26+
async getSFTPDetails() {
27+
return this.axios.get<SFTPDetails>("/network/sftp").then(res => res.data)
28+
}
29+
2630
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ export type Port = {
55
notes: string | null;
66
isDefault: boolean;
77
}
8+
9+
10+
export type SFTPDetails = {
11+
host: string;
12+
port: number;
13+
username: string;
14+
password?: string;
15+
}

0 commit comments

Comments
 (0)