brave-search
Advanced tools
Comparing version 0.5.4 to 0.5.5
import { BraveSearchOptions, LocalDescriptionsOptions, LocalDescriptionsSearchApiResponse, LocalPoiOptions, LocalPoiSearchApiResponse, SummarizerOptions, SummarizerSearchApiResponse, WebSearchApiResponse } from "./types"; | ||
declare class BraveSearchError extends Error { | ||
constructor(message: string); | ||
responseData: any; | ||
constructor(message: string, responseData?: any); | ||
} | ||
@@ -8,4 +9,4 @@ declare class BraveSearch { | ||
private baseUrl; | ||
private pollInterval; | ||
private maxPollAttempts; | ||
private pollInterval; | ||
constructor(apiKey: string, options?: { | ||
@@ -12,0 +13,0 @@ maxPollAttempts?: number; |
@@ -22,6 +22,8 @@ "use strict"; | ||
const axios_1 = __importDefault(require("axios")); | ||
const DEFAULT_POLLING_TIMEOUT = 20000; | ||
class BraveSearchError extends Error { | ||
constructor(message) { | ||
constructor(message, responseData) { | ||
super(message); | ||
this.name = "BraveSearchError"; | ||
this.responseData = responseData; | ||
} | ||
@@ -34,4 +36,4 @@ } | ||
this.baseUrl = "https://api.search.brave.com/res/v1"; | ||
this.maxPollAttempts = 5; | ||
this.pollInterval = 500; | ||
this.maxPollAttempts = DEFAULT_POLLING_TIMEOUT / this.pollInterval; | ||
this.apiKey = apiKey; | ||
@@ -55,3 +57,18 @@ if (options) { | ||
catch (error) { | ||
throw this.handleApiError(error); | ||
const handledError = this.handleApiError(error); | ||
if (handledError.message.includes("Retrying with modified 'result_filter' option in request")) { | ||
// Retry the request with a modified result_filter | ||
const modifiedParams = new URLSearchParams(params); | ||
modifiedParams.set("result_filter", "discussions,faq,news,query,summarizer,videos,web,infobox"); | ||
try { | ||
const response = await axios_1.default.get(`${this.baseUrl}/web/search?${modifiedParams.toString()}`, { | ||
headers: this.getHeaders(), | ||
}); | ||
return response.data; | ||
} | ||
catch (retryError) { | ||
throw this.handleApiError(retryError); | ||
} | ||
} | ||
throw handledError; | ||
} | ||
@@ -148,14 +165,20 @@ } | ||
handleApiError(error) { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d; | ||
if (axios_1.default.isAxiosError(error)) { | ||
const status = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status; | ||
const message = ((_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) || error.message; | ||
const responseData = (_d = error.response) === null || _d === void 0 ? void 0 : _d.data; | ||
if (status === 429) { | ||
return new BraveSearchError(`Rate limit exceeded: ${message}`); | ||
return new BraveSearchError(`Rate limit exceeded: ${message}`, responseData); | ||
} | ||
else if (status === 401) { | ||
return new BraveSearchError(`Authentication error: ${message}`); | ||
return new BraveSearchError(`Authentication error: ${message}`, responseData); | ||
} | ||
else if (status === 422) { | ||
// Handle the specific 422 error related to 'result_filter' param | ||
console.warn("Received 422 error, possibly related to brave server error. Retrying with modified 'result_filter' option in request."); | ||
return new BraveSearchError(`API error (${status}): ${message}. Retrying with modified 'result_filter' option in request.`, responseData); | ||
} | ||
else { | ||
return new BraveSearchError(`API error (${status}): ${message}`); | ||
return new BraveSearchError(`API error (${status}): ${message}`, responseData); | ||
} | ||
@@ -162,0 +185,0 @@ } |
{ | ||
"name": "brave-search", | ||
"version": "0.5.4", | ||
"version": "0.5.5", | ||
"description": "A TypeScript library for interacting with the Brave Search API, including web search, local POI search, and automatic summarization features.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64260
862