import HTTPClient from "./HTTPClient.ts";
import { ServerSocket } from "./ServerSocket.ts";
import { Account, Allocation, AllocationList, AllPermissions, APIKeyList, Backup, BackupList, DatabaseList, DatabaseParameters, File, FileList, FolderOptions, MultiFileOptions, NewAPIKey, NewAPIKeyOptions, NewBackupOptions, NewDatabase, NewDatabaseOptions, NewScheduleOptions, NewUserOptions, PowerActionType, RenameFileOptions, Schedule, ScheduleList, Server, ServerList, ServerParameters, ServerResources, SignedURL, SingleFileOptions, SocketCredentials, SubUser, SubUserList, Task, TaskOptions, TwoFactorQR, TwoFactorTokenList, UpdateEmailOptions, UpdateFileCHMODOptions, UpdateFilesCHMODOptions, UpdatePasswordOptions, UpdateScheduleOptions, UpdateUserOptions, UpdateVariableOptions, UploadFileOptions, Variable, VariableList,} from "../types/client.ts";import { ContentType, Method } from "../types/http.ts";import { SocketOptions } from "../types/serversocket.ts";
export default class Client { private http: HTTPClient;
constructor(public endpoint: string, api_token: string) { this.http = new HTTPClient(endpoint, api_token); }
async getAccount() { return (await this.http.request<Account>(Method.GET, "/client/account")) .attributes; }
async get2FAImage() { return ( await this.http.request<TwoFactorQR>( Method.GET, "/client/account/two-factor" ) ).data; }
async enable2FA(code: string) { return ( await this.http.request<TwoFactorTokenList>( Method.POST, "/client/account/two-factor", { body: { code } } ) ).attributes; }
async delete2FA(password: string) { await this.http.request(Method.DELETE, "/client/account/two-factor", { body: { password }, }); }
async updateEmail(options: UpdateEmailOptions) { await this.http.request(Method.PUT, "/client/account/email", { body: { ...options }, }); }
async updatePassword(options: UpdatePasswordOptions) { await this.http.request(Method.PUT, "/client/account/password", { body: { current_password: options.oldPassword, password: options.newPassword, password_confirmation: options.newPassword, }, }); }
async getAPIKeys() { return ( await this.http.request<APIKeyList>( Method.GET, "/client/account/api-keys" ) ).data; }
async createAPIKey(options: NewAPIKeyOptions) { return await this.http.request<NewAPIKey>( Method.POST, "/client/account/api-keys", { body: { description: options.description, allowed_ips: options.allowedIPs, }, } ); }
async deleteAPIKey(id: number) { await this.http.request(Method.DELETE, `/client/account/api-keys/${id}`); }
async getDatabases(server: string, params?: DatabaseParameters) { return ( await this.http.request<DatabaseList>( Method.GET, `/client/servers/${server}/databases`, { params, } ) ).data; }
async createDatabase(server: string, options: NewDatabaseOptions) { return ( await this.http.request<NewDatabase>( Method.POST, `/client/servers/${server}/databases`, { body: { database: options.name, remote: options.remote || "%" } } ) ).attributes; }
async rotateDatabasePWD(server: string, database: string) { return ( await this.http.request<NewDatabase>( Method.POST, `/client/servers/${server}/databases/${database}/rotate-password` ) ).attributes; }
async deleteDatabase(server: string, database: string) { await this.http.request( Method.DELETE, `/client/servers/${server}/databases/${database}` ); }
async getFiles(server: string, directory: string = "/") { return ( await this.http.request<FileList>( Method.GET, `/client/servers/${server}/files/list`, { query: { directory } } ) ).data; }
async getFileContents(server: string, file: string) { return await this.http.request<string>( Method.GET, `/client/servers/${server}/files/contents`, { query: { file }, type: ContentType.PLAIN, } ); }
async downloadFile(server: string, file: string) { return ( await this.http.request<SignedURL>( Method.GET, `/client/servers/${server}/files/download`, { query: { file }, } ) ).attributes; }
async renameFile(server: string, options: RenameFileOptions) { await this.http.request( Method.PUT, `/client/servers/${server}/files/rename`, { body: { root: options.directory || "%", files: [ { from: options.oldName, to: options.newName, }, ], }, } ); }
async copyFile(server: string, file: string) { await this.http.request( Method.POST, `/client/servers/${server}/files/copy`, { body: { location: file }, } ); }
async writeFile(server: string, target: string, data: string | Uint8Array) { await this.http.request( Method.POST, `/client/servers/${server}/files/write`, { body: data, query: { file: target, }, type: ContentType.PLAIN, } ); }
async uploadFile(server: string, { name, filePath }: UploadFileOptions) { const file = await Deno.readFile(filePath); await this.writeFile(server, name, file); }
private async uploadFileMP( server: string, { name, filePath }: UploadFileOptions ) { throw new Error("This method has not been implemented."); }
async compressFile(server: string, { file, directory }: SingleFileOptions) { return await this.compressFiles(server, { files: [file], directory, }); }
async compressFiles(server: string, { files, directory }: MultiFileOptions) { return ( await this.http.request<File>( Method.POST, `/client/servers/${server}/files/compress`, { body: { root: directory, files: files, }, } ) ).attributes; }
async decompressFile(server: string, { file, directory }: SingleFileOptions) { await this.http.request( Method.POST, `/client/servers/${server}/files/decompress`, { body: { root: directory, file: file, }, } ); }
async deleteFile(server: string, { file, directory }: SingleFileOptions) { await this.deleteFiles(server, { files: [file], directory, }); }
async deleteFiles(server: string, { files, directory }: MultiFileOptions) { await this.http.request( Method.POST, `/client/servers/${server}/files/delete`, { body: { root: directory, files: files, }, } ); }
async createFolder(server: string, { name, directory }: FolderOptions) { await this.http.request( Method.POST, `/client/servers/${server}/files/create-folder`, { body: { root: directory, name, }, } ); }
async updateFilesCHMOD(server: string, { files, directory }: UpdateFilesCHMODOptions) { await this.http.request( Method.POST, `/client/servers/${server}/files/chmod`, { body: { root: directory || "/", files } } ); }
async updateFileCHMOD(server: string, { file, directory }: UpdateFileCHMODOptions) { await this.updateFilesCHMOD(server, { directory, files: [file] }); }
async getFTPServer(server: string) { return ( await this.http.request<SignedURL>( Method.GET, `/client/servers/${server}/files/upload` ) ).attributes; }
async getSchedules(server: string) { return ( await this.http.request<ScheduleList>( Method.GET, `/client/servers/${server}/schedules` ) ).data; }
async createSchedule( server: string, { name, minute, hour, dayOfWeek, dayOfMonth, is_active }: NewScheduleOptions ) { return ( await this.http.request<Schedule>( Method.POST, `/client/servers/${server}/schedules`, { body: { name, minute, hour, day_of_week: dayOfWeek, day_of_month: dayOfMonth, is_active, }, } ) ).attributes; }
async getSchedule(server: string, id: number) { return ( await this.http.request<Schedule>( Method.GET, `/client/servers/${server}/schedules/${id}` ) ).attributes; }
async updateSchedule( server: string, id: number, { minute, hour, dayOfWeek, dayOfMonth, is_active }: UpdateScheduleOptions ) { return ( await this.http.request<Schedule>( Method.POST, `/client/servers/${server}/schedules/${id}`, { body: { minute, hour, day_of_week: dayOfWeek, day_of_month: dayOfMonth, is_active, }, } ) ).attributes; }
async deleteSchedule(server: string, id: number) { await this.http.request( Method.DELETE, `/client/servers/${server}/schedules/${id}` ); }
async createTask( server: string, schedule: number, { timeOffset, action, payload }: TaskOptions ) { if (payload && action === "backup") { payload = undefined; }
return ( await this.http.request<Task>( Method.POST, `/client/servers/${server}/schedules/${schedule}/tasks`, { body: { time_offset: timeOffset, action, payload, }, } ) ).attributes; }
async updateTask( server: string, schedule: number, id: number, { timeOffset, action, payload }: TaskOptions ) { if (payload && action === "backup") { payload = undefined; }
return ( await this.http.request<Task>( Method.POST, `/client/servers/${server}/schedules/${schedule}/tasks/${id}`, { body: { time_offset: timeOffset, action, payload, }, } ) ).attributes; }
async deleteTask(server: string, schedule: number, id: number) { await this.http.request( Method.DELETE, `/client/servers/${server}/schedules/${schedule}/tasks/${id}` ); }
async getAllocations(server: string) { return ( await this.http.request<AllocationList>( Method.GET, `/client/servers/${server}/network/allocations` ) ).data; }
async setAllocationNote(server: string, id: number, note: string) { return ( await this.http.request<Allocation>( Method.POST, `/client/servers/${server}/network/allocations/${id}`, { body: { notes: note, }, } ) ).attributes; }
async setPrimaryAllocation(server: string, id: number) { return ( await this.http.request<Allocation>( Method.POST, `/client/servers/${server}/network/allocations/${id}/primary` ) ).attributes; }
async deleteAllocation(server: string, id: number) { await this.http.request( Method.DELETE, `/client/servers/${server}/network/allocations/${id}` ); }
async getUsers(server: string) { return ( await this.http.request<SubUserList>( Method.GET, `/client/servers/${server}/users` ) ).data; }
async createUser(server: string, { email, perms }: NewUserOptions) { return ( await this.http.request<SubUser>( Method.POST, `/client/servers/${server}/users`, { body: { email, permissions: perms, }, } ) ).attributes; }
async getUser(server: string, uuid: string) { return ( await this.http.request<SubUser>( Method.GET, `/client/servers/${server}/users/${uuid}` ) ).attributes; }
async updateUser(server: string, uuid: string, { perms }: UpdateUserOptions) { return ( await this.http.request<SubUser>( Method.POST, `/client/servers/${server}/users/${uuid}`, { body: { permissions: perms, }, } ) ).attributes; }
async deleteUser(server: string, uuid: string) { await this.http.request( Method.DELETE, `/client/servers/${server}/users/${uuid}` ); }
async getBackups(server: string) { return await this.http.request<BackupList>( Method.GET, `/client/servers/${server}/backups` ); }
async createBackup(server: string, options?: NewBackupOptions) { return ( await this.http.request<Backup>( Method.POST, `/client/servers/${server}/backups`, { body: { name: options?.name, ignored_files: options?.ignoredFiles, }, } ) ).attributes; }
async getBackup(server: string, uuid: string) { return ( await this.http.request<Backup>( Method.GET, `/client/servers/${server}/backups/${uuid}` ) ).attributes; }
async getBackupDownload(server: string, uuid: string) { return ( await this.http.request<SignedURL>( Method.GET, `/client/servers/${server}/backups/${uuid}/download` ) ).attributes; }
async lockBackup(server: string, uuid: string) { return ( await this.http.request<Backup>( Method.POST, `/client/servers/${server}/backups/${uuid}/lock` ) ).attributes; }
async restoreBackup(server: string, uuid: string) { return ( await this.http.request<Backup>( Method.POST, `/client/servers/${server}/backups/${uuid}/restore` ) ).attributes; }
async deleteBackup(server: string, uuid: string) { await this.http.request( Method.DELETE, `/client/servers/${server}/backups/${uuid}` ); }
async getVariables(server: string) { return ( await this.http.request<VariableList>( Method.GET, `/client/servers/${server}/startup` ) ).data; }
async updateVariable(server: string, { key, value }: UpdateVariableOptions) { return ( await this.http.request<Variable>( Method.PUT, `/client/servers/${server}/startup/variable`, { body: { key, value: value || null, }, } ) ).attributes; }
async renameServer(server: string, name: string) { await this.http.request( Method.POST, `/client/servers/${server}/settings/rename`, { body: { name, }, } ); }
async reinstallServer(server: string) { await this.http.request( Method.POST, `/client/servers/${server}/settings/reinstall` ); }
async changeDockerImage(server: string, dockerImage: string) { await this.http.request( Method.PUT, `/client/servers/${server}/settings/docker-image`, { body: { "docker_image": dockerImage } } ) }
async getServer(server: string, params?: ServerParameters) { return await this.http.request<Server>( Method.GET, `/client/servers/${server}`, { params, } ); }
async getServerSocket(server: string) { return ( await this.http.request<SocketCredentials>( Method.GET, `/client/servers/${server}/websocket` ) ).data; }
async joinServerSocket(server: string, options?: SocketOptions) { return new ServerSocket(this, server, options); }
async getResources(server: string) { return ( await this.http.request<ServerResources>( Method.GET, `/client/servers/${server}/resources` ) ).attributes; }
async sendCommand(server: string, command: string) { await this.http.request(Method.POST, `/client/servers/${server}/command`, { body: { command, }, }); }
async sendPowerAction(server: string, action: PowerActionType) { await this.http.request(Method.POST, `/client/servers/${server}/power`, { body: { signal: action, }, }); }
async getServers(params?: ServerParameters) { return await this.http.request<ServerList>(Method.GET, "/client", { params, }); }
async getAllPermissions() { return ( await this.http.request<AllPermissions>(Method.GET, `/client/permissions`) ).attributes; }}