UNPKG

@monkdb/monkdb

Version:

🚀 Official TypeScript SDK for MonkDB — a unified, AI-native database for diverse data workloads

52 lines • 1.89 kB
// --- src/client/MonkServer.ts --- import { request } from 'undici'; import { MonkConnectionError } from '../errors/MonkErrors.js'; export class MonkServer { constructor(url, options = {}) { this.url = url; this.username = options.username; this.password = options.password; this.schema = options.schema; } async sendRequest(method, path, body, headers = {}) { const fullUrl = new URL(path, this.url); // Add content headers headers['Accept'] = 'application/json'; if (!headers['Content-Type']) { headers['Content-Type'] = 'application/json'; } // Auth headers if (this.username) { const credentials = `${this.username}:${this.password || ''}`; headers['Authorization'] = 'Basic ' + Buffer.from(credentials).toString('base64'); headers['X-User'] = this.username; } // Schema header if (this.schema) { headers['Default-Schema'] = this.schema; } try { const response = await request(fullUrl, { method, body, headers }); const buffer = await response.body.arrayBuffer(); const data = Buffer.from(buffer).toString(); const rawHeaders = response.headers; const responseHeaders = {}; for (const [key, value] of Object.entries(rawHeaders)) { responseHeaders[key] = value; } return { status: response.statusCode, data: data.length > 0 ? JSON.parse(data) : null, headers: responseHeaders, }; } catch (err) { throw new MonkConnectionError(`Failed to send request: ${err.message}`); } } } //# sourceMappingURL=MonkServer.js.map