UNPKG

bunactyl

Version:

TypeScript SDK for Pterodactyl

254 lines (247 loc) 6.56 kB
// src/client.ts class BunactylClient { baseUrl; headers; constructor(options) { this.baseUrl = options.url?.endsWith("/") ? options.url.slice(0, -1) : options.url; this.headers = { Accept: "application/json", "Content-Type": "application/json", Authorization: `Bearer ${options.apiKey}`, "User-Agent": `${options.userAgent}` }; } buildUrl(path, params) { const url = new URL(`${this.baseUrl}/api/application${path}`); if (params) { Object.entries(params).forEach(([key, value]) => { if (value !== undefined && value !== null) { url.searchParams.append(key, String(value)); } }); } return url.toString(); } async request(path, method, options = {}) { const { params, body, ...rest } = options; const response = await fetch(this.buildUrl(path, params), { method, headers: this.headers, body: body ? JSON.stringify(body) : undefined, ...rest }); if (!response.ok) { throw new Error(`HTTP error ${response.status}: ${await response.text()}`); } if (response.status === 204) { return {}; } return response.json(); } async get(path, options) { return this.request(path, "GET", options); } async post(path, data, options) { return this.request(path, "POST", { ...options, body: data }); } async patch(path, data, options) { return this.request(path, "PATCH", { ...options, body: data }); } async delete(path, options) { return this.request(path, "DELETE", options); } } // src/resources/users.ts class UsersResource { client; constructor(client) { this.client = client; } async list(include, filter, sort) { const params = {}; if (include) { params["include"] = include; } if (filter) { Object.entries(filter).forEach(([key, value]) => { if (value) { params[`filter[${key}]`] = value; } }); } if (sort && sort.sort) { params["sort"] = sort.sort; } return await this.client.get("/users", { params }); } async getById(id, include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get(`/users/${id}`, { params }); } async getByExternalId(externalId, include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get(`/users/external/${externalId}`, { params }); } async create(userData) { return await this.client.post("/users", userData); } async update(id, userData) { return await this.client.patch(`/users/${id}`, userData); } async delete(id) { await this.client.delete(`/users/${id}`); } } // src/resources/nodes.ts class NodesResource { client; constructor(client) { this.client = client; } async list(include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get("/nodes", { params }); } async getById(id, include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get(`/nodes/${id}`, { params }); } async getConfiguration(id) { return await this.client.get(`/nodes/${id}/configuration`); } async create(nodeData) { return await this.client.post("/nodes", nodeData); } async update(id, nodeData) { return await this.client.patch(`/nodes/${id}`, nodeData); } async delete(id) { await this.client.delete(`/nodes/${id}`); } } // src/resources/locations.ts class LocationsResource { client; constructor(client) { this.client = client; } async list(include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get("/locations", { params }); } async getById(id, include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get(`/locations/${id}`, { params }); } async create(locationData) { return await this.client.post("/locations", locationData); } async update(id, locationData) { return await this.client.patch(`/locations/${id}`, locationData); } async delete(id) { await this.client.delete(`/locations/${id}`); } } // src/resources/allocations.ts class AllocationsResource { client; constructor(client) { this.client = client; } async list(nodeId, include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get(`/nodes/${nodeId}/allocations`, { params }); } async create(nodeId, allocationData) { await this.client.post(`/nodes/${nodeId}/allocations`, allocationData); } async delete(nodeId, allocationId) { await this.client.delete(`/nodes/${nodeId}/allocations/${allocationId}`); } } // src/resources/servers.ts class ServersResource { client; constructor(client) { this.client = client; } async list(include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get("/servers", { params }); } async getById(id, include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get(`/servers/${id}`, { params }); } async getByExternalId(externalId, include) { const params = {}; if (include) { params["include"] = include; } return await this.client.get(`/servers/external/${externalId}`, { params }); } async create(serverData) { return await this.client.post("/servers", serverData); } async updateDetails(id, details) { return await this.client.patch(`/servers/${id}/details`, details); } async updateBuild(id, buildConfig) { return await this.client.patch(`/servers/${id}/build`, buildConfig); } async updateStartup(id, startupConfig) { return await this.client.patch(`/servers/${id}/startup`, startupConfig); } async delete(id) { await this.client.delete(`/servers/${id}`); } } // src/index.ts class Bunactyl { client; users; nodes; locations; servers; constructor(options) { this.client = new BunactylClient(options); this.users = new UsersResource(this.client); this.nodes = new NodesResource(this.client); this.locations = new LocationsResource(this.client); this.servers = new ServersResource(this.client); } allocations(_nodeId) { return new AllocationsResource(this.client); } } export { Bunactyl };