UNPKG

chonkie

Version:

🦛 CHONK your texts in TS with Chonkie!✨The no-nonsense lightweight and efficient chunking library.

50 lines • 2.31 kB
"use strict"; /** Base cloud client for Chonkie API. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CloudClient = void 0; class CloudClient { constructor(config) { this.apiKey = config.apiKey; this.baseUrl = config.baseUrl || "https://api.chonkie.ai"; } request(endpoint_1) { return __awaiter(this, arguments, void 0, function* (endpoint, options = {}) { const { method = "POST", body, headers = {} } = options; // Don't set Content-Type or stringify body if it's FormData const isFormData = body instanceof FormData; const requestHeaders = Object.assign({ "Authorization": `Bearer ${this.apiKey}` }, headers); const response = yield fetch(`${this.baseUrl}${endpoint}`, { method, headers: requestHeaders, body: isFormData ? body : (body ? JSON.stringify(body) : undefined), }); if (!response.ok) { const error = yield response.json().catch(() => ({ message: "Unknown error" })); throw new Error(`API request failed: ${error.message}`); } return response.json(); }); } validateAuth() { return __awaiter(this, void 0, void 0, function* () { try { const response = yield this.request("/v1/auth/validate"); return response.status === 200; } catch (error) { return false; } }); } } exports.CloudClient = CloudClient; //# sourceMappingURL=base.js.map