python-proxy-scraper-client
Version:
A TypeScript client for interacting with a Python proxy scraper service
51 lines (50 loc) • 1.69 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseClient = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
class BaseClient {
constructor(config) {
this.baseUrl = config.baseUrl;
}
async handleResponse(response) {
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || `HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
}
async makeRequest(endpoint, config, options = {}) {
try {
const url = `${this.baseUrl}${endpoint}`;
const response = await (0, node_fetch_1.default)(url, {
...config,
headers: {
'Content-Type': 'application/json',
...options.headers,
},
});
return this.handleResponse(response);
}
catch (error) {
console.error(`Error making ${config.method} request to ${endpoint}:`, error);
throw error;
}
}
async get(endpoint, options = {}) {
return this.makeRequest(endpoint, { method: 'GET' }, options);
}
async post(endpoint, body, options = {}) {
return this.makeRequest(endpoint, {
method: 'POST',
body: JSON.stringify(body)
}, options);
}
getFullUrl(endpoint) {
return `${this.baseUrl}${endpoint}`;
}
}
exports.BaseClient = BaseClient;
;