oxylabs-ai-studio
Version:
JavaScript SDK for Oxylabs AI Studio API services
65 lines • 2.5 kB
JavaScript
/**
* AI-Map Service
* Handles all AI-Map related API calls
*/
export class AiMapService {
constructor(client) {
this.client = client;
}
/**
* Submit map request (POST /map)
*/
async submitMapRequest(options) {
const payload = {
url: options.url,
search_keywords: options.search_keywords ?? undefined,
user_prompt: options.user_prompt ?? undefined,
limit: options.limit ?? undefined,
geo_location: options.geo_location ?? undefined,
render_javascript: options.render_javascript ?? undefined,
max_crawl_depth: options.max_crawl_depth ?? undefined,
include_sitemap: options.include_sitemap ?? undefined,
allow_subdomains: options.allow_subdomains ?? undefined,
allow_external_domains: options.allow_external_domains ?? undefined,
max_credits: options.max_credits ?? undefined,
};
return await this.client.post('/map', payload);
}
/**
* Get map run data/results (GET /map/run/data)
*/
async getMapRunData(runId) {
if (!runId) {
throw new Error('run_id is required');
}
const params = new URLSearchParams();
params.append('run_id', runId);
const url = `/map/run/data?${params.toString()}`;
return await this.client.get(url);
}
/**
* Synchronous mapping (wait for results)
*/
async map(options, timeout = 300000, pollInterval = 5000) {
const submitResult = await this.submitMapRequest(options);
const runId = submitResult.run_id || submitResult.id;
if (!runId) {
throw new Error('No run ID returned from map request');
}
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const response = await this.getMapRunData(runId);
const status = response.status;
console.log('Run status:', status);
if (status === 'completed' || status === 'success') {
return response.data;
}
else if (status === 'failed' || status === 'error') {
throw new Error(`Mapping failed: ${response.error_code || response.message || 'Unknown error'}`);
}
await new Promise(resolve => setTimeout(resolve, pollInterval));
}
throw new Error(`Mapping timeout after ${timeout}ms`);
}
}
//# sourceMappingURL=aiMap.js.map