UNPKG

@kareemaly/researcher

Version:
55 lines (54 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SearchApiProvider = void 0; const httpClients_1 = require("../utils/httpClients"); const logger_1 = require("../utils/logger"); const log = (0, logger_1.createLogger)("search-api"); class SearchApiProvider { constructor(config) { this.config = config; } async search(query, options = {}) { if (this.rateLimiter) { await this.rateLimiter.acquire(); } try { const params = new URLSearchParams(); params.append('api_key', this.config.apiKey); params.append('q', query); params.append('engine', 'google'); // Using Google as the default engine if (options.type) params.append('type', options.type); if (options.location) params.append('location', options.location); if (options.language) params.append('language', options.language); if (options.limit) params.append('limit', options.limit.toString()); const response = await httpClients_1.searchClient.get(`${this.config.endpoint}?${params}`); const data = response.data; const result = { id: "", // Will be set by storage timestamp: Date.now(), query, type: options.type || "web", location: options.location, language: options.language, results: data.organic_results.map((item) => ({ title: item.title, url: item.link, snippet: item.snippet, language: item.language, published: item.date })) }; return result; } finally { if (this.rateLimiter) { await this.rateLimiter.release(); } } } } exports.SearchApiProvider = SearchApiProvider;