add functionality and refactor the code
This commit is contained in:
56
app/components/Pterodactyl/files.tsx
Normal file
56
app/components/Pterodactyl/files.tsx
Normal file
@@ -0,0 +1,56 @@
|
||||
import { File } from "./interfaces";
|
||||
|
||||
export default function files(pterodactyl: any) {
|
||||
return {
|
||||
async rename(from: File, newName: string) {
|
||||
console.log("rename", from);
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${process.env.NEXT_PUBLIC_URL}/api/client/servers/${pterodactyl.server_id}/files/rename`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: await pterodactyl.helpers.authHeader(),
|
||||
body: JSON.stringify({
|
||||
root: "/",
|
||||
files: [
|
||||
{
|
||||
from: from.attributes.name,
|
||||
to: newName,
|
||||
},
|
||||
],
|
||||
}),
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
console.log(response);
|
||||
if (response.ok) {
|
||||
// fetchFiles();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
}
|
||||
},
|
||||
|
||||
async fetchFiles() {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${process.env.NEXT_PUBLIC_URL}/api/client/servers/${pterodactyl.server_id}/files/list`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: await pterodactyl.helpers.authHeader(),
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
return data.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
22
app/components/Pterodactyl/helpers.tsx
Normal file
22
app/components/Pterodactyl/helpers.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
export default function helpers(pterodactyl: any) {
|
||||
return {
|
||||
// helper what return auth header
|
||||
async authHeader() {
|
||||
return {
|
||||
Authorization: `Bearer ${pterodactyl.api_key}`,
|
||||
"Content-Type": "application/json",
|
||||
Accept: "Application/vnd.pterodactyl.v1+json",
|
||||
};
|
||||
},
|
||||
|
||||
// setter for api key
|
||||
async setApiKey(apiKey: string) {
|
||||
return (pterodactyl.api_key = apiKey);
|
||||
},
|
||||
|
||||
// setter for server id
|
||||
async setServerID(serverID: string) {
|
||||
return (pterodactyl.server_id = serverID);
|
||||
},
|
||||
};
|
||||
}
|
||||
18
app/components/Pterodactyl/index.tsx
Normal file
18
app/components/Pterodactyl/index.tsx
Normal file
@@ -0,0 +1,18 @@
|
||||
import filesModule from "./files";
|
||||
import helpersModule from "./helpers";
|
||||
|
||||
class Pterodactyl {
|
||||
server_id: string;
|
||||
api_key: string;
|
||||
files: any;
|
||||
helpers: any;
|
||||
|
||||
constructor(server_id: string, api_key: string) {
|
||||
this.server_id = server_id;
|
||||
this.api_key = api_key;
|
||||
this.files = filesModule(this);
|
||||
this.helpers = helpersModule(this);
|
||||
}
|
||||
}
|
||||
|
||||
export default Pterodactyl;
|
||||
17
app/components/Pterodactyl/interfaces.tsx
Normal file
17
app/components/Pterodactyl/interfaces.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
export interface File {
|
||||
attributes: {
|
||||
name: string;
|
||||
modified_at: string;
|
||||
size: number;
|
||||
is_file: boolean;
|
||||
directory: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface Pterodactyl {
|
||||
server_id: string;
|
||||
api_key: string;
|
||||
files: any;
|
||||
|
||||
constructor(server_id: string, api_key: string): void;
|
||||
}
|
||||
Reference in New Issue
Block a user