UNPKG

oxylabs-ai-studio

Version:

JavaScript SDK for Oxylabs AI Studio API services

131 lines 4.41 kB
import axios from 'axios'; /** * HTTP Client for Oxylabs API */ export class OxylabsAIStudioClient { constructor(config) { this.config = { apiUrl: config.apiUrl || 'https://api-aistudio.oxylabs.io', apiKey: config.apiKey || '', timeout: config.timeout || 30000, retryAttempts: config.retryAttempts || 3, debug: config.debug || false, ...config }; // Create axios instance this.http = axios.create({ baseURL: this.config.apiUrl, timeout: this.config.timeout, headers: { 'Content-Type': 'application/json', 'User-Agent': 'sdk' } }); // Setup request interceptor for authentication this.http.interceptors.request.use((config) => { if (this.config.apiKey) { config.headers['x-api-key'] = this.config.apiKey; } if (this.config.debug) { console.log(`[DEBUG] ${config.method?.toUpperCase()} ${config.url}`); console.log('[DEBUG] Headers:', config.headers); if (config.data) { console.log('[DEBUG] Data:', config.data); } } return config; }, (error) => { return Promise.reject(error); }); // Setup response interceptor for error handling this.http.interceptors.response.use((response) => { if (this.config.debug) { console.log(`[DEBUG] Response ${response.status}:`, response.data); } return response; }, (error) => { if (this.config.debug) { console.error('[DEBUG] Error:', error.response?.data || error.message); } return Promise.reject(this.handleError(error)); }); } /** * Handle and format API errors */ handleError(error) { if (error.response) { // Server responded with error status const { status, data } = error.response; const errorData = data; return new Error(`API Error ${status}: ${errorData.message || errorData.error || 'Unknown error'}`); } else if (error.request) { // Request was made but no response received return new Error('Network Error: No response from server'); } else { // Something else happened return new Error(`Request Error: ${error.message}`); } } /** * Retry wrapper for API calls */ async withRetry(fn, attempts = this.config.retryAttempts) { for (let i = 0; i < attempts; i++) { try { return await fn(); } catch (error) { if (i === attempts - 1) throw error; // Wait before retry (exponential backoff) const delay = Math.pow(2, i) * 1000; await new Promise(resolve => setTimeout(resolve, delay)); if (this.config.debug) { console.log(`[DEBUG] Retrying request (attempt ${i + 2}/${attempts})`); } } } // This should never be reached, but TypeScript requires a return throw new Error('Unexpected error in retry logic'); } /** * GET request */ async get(url, config = {}) { return this.withRetry(async () => { const response = await this.http.get(url, config); return response.data; }); } /** * POST request */ async post(url, data = {}, config = {}) { return this.withRetry(async () => { const response = await this.http.post(url, data, config); return response.data; }); } /** * PUT request */ async put(url, data = {}, config = {}) { return this.withRetry(async () => { const response = await this.http.put(url, data, config); return response.data; }); } /** * DELETE request */ async delete(url, config = {}) { return this.withRetry(async () => { const response = await this.http.delete(url, config); return response.data; }); } } //# sourceMappingURL=client.js.map