UNPKG

bunactyl

Version:

TypeScript SDK for Pterodactyl

52 lines (51 loc) 1.78 kB
export class BunactylClient { 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()}`); } // For 204 No Content responses 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); } }