Skip to content

Commit

Permalink
improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-balfe committed Jul 21, 2024
1 parent ac6c0f5 commit e086b3f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@tyr/brave-search",
"version": "0.6.0",
"version": "0.6.1",
"exports": "./src/braveSearch.ts"
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "brave-search",
"version": "0.6.0",
"description": "A TypeScript library for interacting with the Brave Search API, including web search, local POI search, and automatic summarization features.",
"version": "0.6.1",
"description": "A fully typed Brave Search API wrapper, providing easy access to web search, local POI search, and automatic polling for web search summary feature.",
"scripts": {
"build": "tsc",
"prepare": "npm run build"
Expand Down
47 changes: 46 additions & 1 deletion src/braveSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,42 @@ import {
WebSearchApiResponse,
} from "./types";

/**
* An error class specific to BraveSearch API interactions.
* It includes additional information about the response data that caused the error.
*/
class BraveSearchError extends Error {
public responseData: any;

/**
* Initializes a new instance of the BraveSearchError class.
* @param message The error message.
* @param responseData The response data that caused the error.
*/
constructor(message: string, responseData?: any) {
super(message);
this.name = "BraveSearchError";
this.responseData = responseData;
}
}

/**
* The main class for interacting with the Brave Search API.
* It provides methods for web search, local POI search, and summarization.
*/
class BraveSearch {
private apiKey: string;
private baseUrl = "https://api.search.brave.com/res/v1";
private pollInterval = 500;
private maxPollAttempts = DEFAULT_POLLING_TIMEOUT / this.pollInterval;

/**
* Initializes a new instance of the BraveSearch class.
* @param apiKey The API key for accessing the Brave Search API.
* @param options Optional settings to configure the search behavior.
* - maxPollAttempts: Maximum number of attempts to poll for a summary.
* - pollInterval: Interval in milliseconds between polling attempts.
*/
constructor(apiKey: string, options?: { maxPollAttempts?: number; pollInterval?: number }) {
this.apiKey = apiKey;
if (options) {
Expand All @@ -52,6 +72,12 @@ class BraveSearch {
}
}

/**
* Performs a web search using the provided query and options.
* @param query The search query string.
* @param options Optional settings to configure the search behavior.
* @returns A promise that resolves to the search results.
*/
async webSearch(query: string, options: BraveSearchOptions = {}): Promise<WebSearchApiResponse> {
const params = new URLSearchParams({
q: query,
Expand All @@ -72,9 +98,16 @@ class BraveSearch {
}
}

/**
* Executes a web search for the provided query and polls for a summary if available.
* @param query The search query string.
* @param options Optional settings to configure the search behavior.
* @param summarizerOptions Optional settings specific to summarization.
* @returns An object containing promises for the web search results and the summarized answer.
*/
getSummarizedAnswer(
query: string,
options: Omit<BraveSearchOptions, 'summary'> = {},
options: Omit<BraveSearchOptions, "summary"> = {},
summarizerOptions: SummarizerOptions = {},
): {
summary: Promise<SummarizerSearchApiResponse | undefined>;
Expand All @@ -98,6 +131,12 @@ class BraveSearch {
}
}

/**
* Searches for local points of interest using the provided IDs and options.
* @param ids The IDs of the local points of interest.
* @param options Optional settings to configure the search behavior.
* @returns A promise that resolves to the search results.
*/
async localPoiSearch(ids: string[], options: LocalPoiOptions = {}): Promise<LocalPoiSearchApiResponse> {
const params = new URLSearchParams({
ids: ids.join(","),
Expand All @@ -117,6 +156,12 @@ class BraveSearch {
}
}

/**
* Retrieves descriptions for local points of interest using the provided IDs and options.
* @param ids The IDs of the local points of interest.
* @param options Optional settings to configure the search behavior.
* @returns A promise that resolves to the search results.
*/
async localDescriptionsSearch(
ids: string[],
options: LocalDescriptionsOptions = {},
Expand Down

0 comments on commit e086b3f

Please sign in to comment.