UNPKG

@botwall/sdk

Version:

BotWall SDK for site protection and bot crawling

111 lines 4.36 kB
"use strict"; // BotWall SDK Core Client Object.defineProperty(exports, "__esModule", { value: true }); exports.BotWallClient = void 0; const errors_1 = require("./errors"); class BotWallClient { constructor(apiUrl) { // Use BACKEND_URL from environment, fallback to '/api' for relative proxy in dev // To set: add BACKEND_URL=http://localhost:3001/api (or your prod URL) to your .env file this.apiUrl = apiUrl || process.env.BACKEND_URL || 'https://botwall-api.onrender.com/api'; } /** * Verify bot and site credentials, check credits, and deduct 1 credit * @param botConfig - Bot configuration with ID and API key * @param siteConfig - Site configuration with ID * @param options - Additional options like path and user agent * @returns Promise<VerifyResponse> */ async verify(botConfig, siteConfig, options = {}) { try { const response = await fetch(`${this.apiUrl}/verify`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ botId: botConfig.botId, botApiKey: botConfig.apiKey, siteId: siteConfig.id, path: options.path || '/', userAgent: options.userAgent }), }); if (!response.ok) { throw await (0, errors_1.createErrorFromResponse)(response); } const data = await response.json(); return data; } catch (error) { if (error instanceof errors_1.NetworkError) { throw error; } throw new errors_1.NetworkError(`Verification failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Manually deduct credits from a bot (alternative to automatic deduction) * @param botConfig - Bot configuration with ID and API key * @param creditsToDeduct - Number of credits to deduct (default: 1) * @returns Promise<{ creditsDeducted: number; remainingCredits: number }> */ async deductCredits(botConfig, creditsToDeduct = 1) { try { const response = await fetch(`${this.apiUrl}/verify/deduct-credits`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ botId: botConfig.botId, botApiKey: botConfig.apiKey, creditsToDeduct }), }); if (!response.ok) { throw await (0, errors_1.createErrorFromResponse)(response); } const data = await response.json(); return data; } catch (error) { if (error instanceof errors_1.NetworkError) { throw error; } throw new errors_1.NetworkError(`Credit deduction failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Get bot information including current credit balance * @param botConfig - Bot configuration with ID and API key * @returns Promise<{ botId: string; botName: string; credits: number }> */ async getBotInfo(botConfig) { try { const response = await fetch(`${this.apiUrl}/bots/info`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ botId: botConfig.botId, botApiKey: botConfig.apiKey }), }); if (!response.ok) { throw await (0, errors_1.createErrorFromResponse)(response); } const data = await response.json(); return data; } catch (error) { if (error instanceof errors_1.NetworkError) { throw error; } throw new errors_1.NetworkError(`Failed to get bot info: ${error instanceof Error ? error.message : 'Unknown error'}`); } } } exports.BotWallClient = BotWallClient; //# sourceMappingURL=client.js.map