UNPKG

anil-brd-typescript-sdk

Version:

TypeScript SDK for Bright Data APIs - Web Unlocker, SERP, and Scraper APIs

85 lines (84 loc) 3.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClient = void 0; const axios_1 = __importDefault(require("axios")); const index_1 = require("../index"); class HttpClient { constructor(config) { this.config = config; this.client = axios_1.default.create({ baseURL: 'https://api.brightdata.com', // Ensure correct base URL timeout: config.timeout, headers: { 'Authorization': `Bearer ${config.apiKey}`, 'Content-Type': 'application/json' } }); this.setupInterceptors(); } setupInterceptors() { // Request interceptor this.client.interceptors.request.use((config) => { // Add request ID for tracing if (config.headers) { config.headers['X-Request-ID'] = this.generateRequestId(); } return config; }, (error) => Promise.reject(error)); // Response interceptor this.client.interceptors.response.use((response) => response, (error) => { var _a, _b, _c, _d, _e, _f; const axiosError = error; const message = ((_b = (_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || axiosError.message; const statusCode = (_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.status; const requestId = (_e = (_d = axiosError.config) === null || _d === void 0 ? void 0 : _d.headers) === null || _e === void 0 ? void 0 : _e['X-Request-ID']; throw new index_1.BrightDataError(message, statusCode, (_f = axiosError.response) === null || _f === void 0 ? void 0 : _f.data, requestId); }); } generateRequestId() { return `bd_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; } async post(url, data, config) { var _a; try { // Ensure data is properly formatted as JSON string const requestData = JSON.stringify(data); const response = await this.client.post(url, requestData, { ...config, headers: { ...config === null || config === void 0 ? void 0 : config.headers, 'Content-Type': 'application/json' } }); return { data: response.data, status: response.status, headers: response.headers, requestId: response.headers['x-request-id'] }; } catch (error) { if (error.response) { throw new index_1.BrightDataError(`API request failed: ${((_a = error.response.data) === null || _a === void 0 ? void 0 : _a.message) || error.message}`, error.response.status, error.response.data); } throw new index_1.BrightDataError(`Request failed: ${error.message}`); } } async get(url, config) { const response = await this.client.get(url, config); return this.formatResponse(response); } formatResponse(response) { var _a, _b; return { data: response.data, status: response.status, headers: response.headers, requestId: (_b = (_a = response.config) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b['X-Request-ID'] }; } } exports.HttpClient = HttpClient;