jspteroapi
Version:
A pterodactyl v1 api using undici
201 lines (200 loc) • 7.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const Request_1 = require("../modules/Request");
const server_1 = require("./methods/server");
const console_1 = require("./methods/console");
const file_1 = require("./methods/file");
const database_1 = require("./methods/database");
const account_1 = require("./methods/account");
const schedule_1 = require("./methods/schedule");
const Error_1 = require("../modules/Error");
const network_1 = require("./methods/network");
const subUser_1 = require("./methods/subUser");
const backups_1 = require("./methods/backups");
const settings_1 = require("./methods/settings");
class Client {
host;
key;
errorHandler;
/**
* @param host - Panels address
* @param key - Api key to use
* @param errorHandler - A custom function to handle errors
* @param fast - Fast login (No credential check)
*/
constructor(host, key, errorHandler = (error) => {
throw error;
}, fast = false) {
this.host = host;
this.key = key;
this.errorHandler = errorHandler;
host = host.trim();
if (host.endsWith('/'))
host = host.slice(0, -1);
this.host = host;
this.request = new Request_1.Request(this.host, this.key, this.errorHandler).request;
// Server
const servermethods = new server_1.serverMethods(this);
this.getAllServers = servermethods.getAllServers;
this.getServerInfo = servermethods.getServerInfo;
this.getServerResources = servermethods.getServerResources;
this.sendCommand = servermethods.sendCommand;
this.setPowerState = servermethods.setPowerState;
// Console
const consolemethods = new console_1.consoleMethods(this, this.errorHandler);
this.startConsoleConnection = consolemethods.startConsoleConnection;
// File
const filemethods = new file_1.fileMethods(this);
this.getAllFiles = filemethods.getAllFiles;
this.getFileContents = filemethods.getFileContents;
this.writeFile = filemethods.writeFile;
this.renameFile = filemethods.renameFile;
this.copyFile = filemethods.copyFile;
this.getFileDownloadLink = filemethods.getFileDownloadLink;
this.compressFile = filemethods.compressFile;
this.decompressFile = filemethods.decompressFile;
this.deleteFile = filemethods.deleteFile;
this.createFolder = filemethods.createFolder;
this.getFileUploadLink = filemethods.getFileUploadLink;
// Database
const databasemethods = new database_1.databaseMethods(this);
this.getAllDatabases = databasemethods.getAllDatabases;
this.createDatabase = databasemethods.createDatabase;
this.deleteDatabase = databasemethods.deleteDatabase;
this.rotateDatabasePass = databasemethods.rotateDatabasePass;
// Account
const accountmethods = new account_1.accountMethods(this);
this.getAllPermissions = accountmethods.getAllPermissions;
this.getAccountInfo = accountmethods.getAccountInfo;
this.getAccount2FADetails = accountmethods.getAccount2FADetails;
this.enable2FA = accountmethods.enable2FA;
this.updateEmail = accountmethods.updateEmail;
this.updatePassword = accountmethods.updatePassword;
this.getAllApiKeys = accountmethods.getAllApiKeys;
this.createApiKey = accountmethods.createApiKey;
this.deleteApiKey = accountmethods.deleteApiKey;
// Schedule
const schedulemethods = new schedule_1.scheduleMethods(this);
this.getAllSchedules = schedulemethods.getAllSchedules;
this.createSchedule = schedulemethods.createSchedule;
this.getScheduleInfo = schedulemethods.getScheduleInfo;
this.editSchedule = schedulemethods.editSchedule;
// Network
const networkmethods = new network_1.networkMethods(this);
this.getAllAlocations = networkmethods.getAllAlocations;
this.assignAllocation = networkmethods.assignAllocation;
this.setAllocationNote = networkmethods.setAllocationNote;
this.setAllocationPrimary = networkmethods.setAllocationPrimary;
this.deleteAllocation = networkmethods.deleteAllocation;
// SubUsers
const subusermethods = new subUser_1.subUserMethods(this);
this.getAllSubUsers = subusermethods.getAllSubUsers;
this.createSubUser = subusermethods.createSubUser;
this.getSubUserInfo = subusermethods.getSubUserInfo;
this.updateSubUserPermissions = subusermethods.updateSubUserPermissions;
this.deleteSubUser = subusermethods.deleteSubUser;
// Backups
const backupmethods = new backups_1.backupMethods(this);
this.getAllBackups = backupmethods.getAllBackups;
this.createBackup = backupmethods.createBackup;
this.getBackupInfo = backupmethods.getBackupInfo;
this.getBackupDownloadLink = backupmethods.getBackupDownloadLink;
this.deleteBackup = backupmethods.deleteBackup;
this.toggleLockBackup = backupmethods.toggleLockBackup;
this.restoreBackup = backupmethods.restoreBackup;
// Settings
const settingsmethods = new settings_1.settingsMethods(this);
this.renameServer = settingsmethods.renameServer;
this.reinstallServer = settingsmethods.reinstallServer;
if (!fast)
this.testAPI();
}
/**
* @param throwError - Whether to throw an error or return bool
* @remarks Will not work if using custom error handler.
*/
testAPI = async (throwError = true) => {
try {
await this.getAllServers();
return true;
}
catch (e) {
if (e instanceof Error_1.JSPteroAPIError) {
if (throwError) {
if (e.HTML_STATUS === 403)
e.ERRORS = ['Invalid Client API key'];
throw e;
}
return false;
}
else {
if (throwError)
throw e;
return false;
}
}
};
/**
@internal
*/
request;
// Get
getAllServers;
getServerInfo;
getServerResources;
getAllPermissions;
getAllDatabases;
getAllFiles;
getFileContents;
getFileDownloadLink;
getFileUploadLink;
getAccountInfo;
getAccount2FADetails;
getAllApiKeys;
getAllSchedules;
getScheduleInfo;
getAllAlocations;
getAllSubUsers;
getSubUserInfo;
getAllBackups;
getBackupInfo;
getBackupDownloadLink;
// POST
sendCommand;
setPowerState;
createDatabase;
rotateDatabasePass;
copyFile;
writeFile;
compressFile;
decompressFile;
deleteFile;
createFolder;
enable2FA;
createApiKey;
createSchedule;
editSchedule;
assignAllocation;
setAllocationNote;
setAllocationPrimary;
createSubUser;
updateSubUserPermissions;
createBackup;
renameServer;
reinstallServer;
toggleLockBackup;
restoreBackup;
// Delete
deleteDatabase;
deleteApiKey;
deleteAllocation;
deleteSubUser;
deleteBackup;
// PUT
renameFile;
updateEmail;
updatePassword;
startConsoleConnection;
}
exports.Client = Client;