exa-js 1.1.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -53,6 +53,9 @@ type RegularSearchOptions = BaseSearchOptions & {
53
53
  type FindSimilarOptions = BaseSearchOptions & {
54
54
  excludeSourceDomain?: boolean;
55
55
  };
56
+ type ExtrasOptions = {
57
+ links: number;
58
+ };
56
59
  /**
57
60
  * Search options for performing a search query.
58
61
  * @typedef {Object} ContentsOptions
@@ -62,6 +65,8 @@ type FindSimilarOptions = BaseSearchOptions & {
62
65
  * @property {LivecrawlOptions} [livecrawl] - Options for livecrawling contents. Default is "never" for neural/auto search, "fallback" for keyword search.
63
66
  * @property {number} [livecrawlTimeout] - The timeout for livecrawling. Max and default is 10000ms.
64
67
  * @property {boolean} [filterEmptyResults] - If true, filters out results with no contents. Default is true.
68
+ * @property {number} [subpages] - The number of subpages to return for each result, where each subpage is derived from an internal link for the result
69
+ * @property {ExtrasOptions} [extras] - Miscelleneous data for derived from resutls
65
70
  */
66
71
  type ContentsOptions = {
67
72
  text?: TextContentsOptions | true;
@@ -70,6 +75,9 @@ type ContentsOptions = {
70
75
  livecrawl?: LivecrawlOptions;
71
76
  livecrawlTimeout?: number;
72
77
  filterEmptyResults?: boolean;
78
+ subpages?: number;
79
+ subpageTarget?: string | string[];
80
+ extras?: ExtrasOptions;
73
81
  } & (typeof isBeta extends true ? {} : {});
74
82
  /**
75
83
  * Options for livecrawling contents
@@ -129,6 +137,22 @@ type HighlightsResponse = {
129
137
  type SummaryResponse = {
130
138
  summary: string;
131
139
  };
140
+ /**
141
+ * @typedef {Object} ExtrasResponse
142
+ * @property {string[]} links - The links on the page of a result
143
+ */
144
+ type ExtrasResponse = {
145
+ extras: {
146
+ links: string[];
147
+ };
148
+ };
149
+ /**
150
+ * @typedef {Object} SubpagesResponse
151
+ * @property {ContentsResultComponent<T extends ContentsOptions>} subpages - The links on the page of a result
152
+ */
153
+ type SubpagesResponse<T extends ContentsOptions> = {
154
+ subpages: ContentsResultComponent<T>[];
155
+ };
132
156
  type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;
133
157
  /**
134
158
  * @typedef {Object} ContentsResultComponent
@@ -136,10 +160,10 @@ type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;
136
160
  *
137
161
  * @template T - A type extending from 'ContentsOptions'.
138
162
  */
139
- type ContentsResultComponent<T extends ContentsOptions> = Default<(T["text"] extends object | true ? TextResponse : {}) & (T["highlights"] extends object | true ? HighlightsResponse : {}) & (T["summary"] extends object | true ? SummaryResponse : {}), TextResponse>;
163
+ type ContentsResultComponent<T extends ContentsOptions> = Default<(T["text"] extends object | true ? TextResponse : {}) & (T["highlights"] extends object | true ? HighlightsResponse : {}) & (T["summary"] extends object | true ? SummaryResponse : {}) & (T["subpages"] extends number ? SubpagesResponse<T> : {}) & (T["extras"] extends object ? ExtrasResponse : {}), TextResponse>;
140
164
  /**
141
165
  * Represents a search result object.
142
- * @typedef {Object} Result
166
+ * @typedef {Object} SearchResult
143
167
  * @property {string} title - The title of the search result.
144
168
  * @property {string} url - The URL of the search result.
145
169
  * @property {string} [publishedDate] - The estimated creation date of the content.
@@ -147,13 +171,14 @@ type ContentsResultComponent<T extends ContentsOptions> = Default<(T["text"] ext
147
171
  * @property {number} [score] - Similarity score between the query/url and the result.
148
172
  * @property {string} id - The temporary ID for the document.
149
173
  */
150
- type SearchResult<T extends ContentsOptions = {}> = {
174
+ type SearchResult<T extends ContentsOptions> = {
151
175
  title: string | null;
152
176
  url: string;
153
177
  publishedDate?: string;
154
178
  author?: string;
155
179
  score?: number;
156
180
  id: string;
181
+ image?: string;
157
182
  } & ContentsResultComponent<T>;
158
183
  /**
159
184
  * Represents a search response object.
@@ -163,7 +188,7 @@ type SearchResult<T extends ContentsOptions = {}> = {
163
188
  * @property {string} [autoDate] - The autoprompt date, if applicable.
164
189
  * @property {string} requestId - The request ID for the search.
165
190
  */
166
- type SearchResponse<T extends ContentsOptions = {}> = {
191
+ type SearchResponse<T extends ContentsOptions> = {
167
192
  results: SearchResult<T>[];
168
193
  autopromptString?: string;
169
194
  autoDate?: string;
@@ -175,6 +200,7 @@ type SearchResponse<T extends ContentsOptions = {}> = {
175
200
  declare class Exa {
176
201
  private baseURL;
177
202
  private headers;
203
+ private extractContentsOptions;
178
204
  /**
179
205
  * Constructs the Exa API client.
180
206
  * @param {string} apiKey - The API key for authentication.
@@ -195,7 +221,7 @@ declare class Exa {
195
221
  * @param {SearchOptions} [options] - Additional search options.
196
222
  * @returns {Promise<SearchResponse>} A list of relevant search results.
197
223
  */
198
- search(query: string, options?: RegularSearchOptions): Promise<SearchResponse>;
224
+ search(query: string, options?: RegularSearchOptions): Promise<SearchResponse<{}>>;
199
225
  /**
200
226
  * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.
201
227
  * @param {string} query - The query string.
@@ -209,13 +235,8 @@ declare class Exa {
209
235
  * @param {FindSimilarOptions} [options] - Additional options for finding similar links.
210
236
  * @returns {Promise<SearchResponse>} A list of similar search results.
211
237
  */
212
- findSimilar(url: string, options?: FindSimilarOptions): Promise<SearchResponse>;
213
- /**
214
- * Finds similar links to the provided URL and returns the contents of the documents.
215
- * @param {string} url - The URL for which to find similar links.
216
- * @param {FindSimilarOptions} [options] - Additional options for finding similar links.
217
- * @returns {Promise<SearchResponse>} A list of similar search results.
218
- */
238
+ findSimilar(url: string, options?: FindSimilarOptions): Promise<SearchResponse<{}>>;
239
+ /** Finds similar links to the provided URL and returns the contents of the documents. @param {string} url - The URL for which to find similar links. @param {FindSimilarOptions} [options] - Additional options for finding similar links. @returns {Promise<SearchResponse>} A list of similar search results. */
219
240
  findSimilarAndContents<T extends ContentsOptions>(url: string, options?: FindSimilarOptions & T): Promise<SearchResponse<T>>;
220
241
  /**
221
242
  * Retrieves contents of documents based on a list of document IDs.
@@ -223,7 +244,7 @@ declare class Exa {
223
244
  * @param {ContentsOptions} [options] - Additional options for retrieving document contents.
224
245
  * @returns {Promise<GetContentsResponse>} A list of document contents.
225
246
  */
226
- getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>>;
247
+ getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult<T>[], options?: T): Promise<SearchResponse<T>>;
227
248
  }
228
249
 
229
- export { BaseSearchOptions, ContentsOptions, ContentsResultComponent, Default, FindSimilarOptions, HighlightsContentsOptions, HighlightsResponse, LivecrawlOptions, RegularSearchOptions, SearchResponse, SearchResult, SummaryContentsOptions, SummaryResponse, TextContentsOptions, TextResponse, Exa as default };
250
+ export { BaseSearchOptions, ContentsOptions, ContentsResultComponent, Default, ExtrasOptions, ExtrasResponse, FindSimilarOptions, HighlightsContentsOptions, HighlightsResponse, LivecrawlOptions, RegularSearchOptions, SearchResponse, SearchResult, SubpagesResponse, SummaryContentsOptions, SummaryResponse, TextContentsOptions, TextResponse, Exa as default };
package/dist/index.js CHANGED
@@ -35,6 +35,30 @@ __export(src_exports, {
35
35
  module.exports = __toCommonJS(src_exports);
36
36
  var import_cross_fetch = __toESM(require("cross-fetch"));
37
37
  var Exa = class {
38
+ extractContentsOptions(options) {
39
+ const { text, highlights, summary, subpages, subpageTarget, extras, livecrawl, livecrawlTimeout, ...rest } = options;
40
+ const contentsOptions = {};
41
+ if (text !== void 0)
42
+ contentsOptions.text = text;
43
+ if (highlights !== void 0)
44
+ contentsOptions.highlights = highlights;
45
+ if (summary !== void 0)
46
+ contentsOptions.summary = summary;
47
+ if (subpages !== void 0)
48
+ contentsOptions.subpages = subpages;
49
+ if (subpageTarget !== void 0)
50
+ contentsOptions.subpageTarget = subpageTarget;
51
+ if (extras !== void 0)
52
+ contentsOptions.extras = extras;
53
+ if (livecrawl !== void 0)
54
+ contentsOptions.livecrawl = livecrawl;
55
+ if (livecrawlTimeout !== void 0)
56
+ contentsOptions.livecrawlTimeout = livecrawlTimeout;
57
+ return {
58
+ contentsOptions,
59
+ restOptions: rest
60
+ };
61
+ }
38
62
  /**
39
63
  * Constructs the Exa API client.
40
64
  * @param {string} apiKey - The API key for authentication.
@@ -53,7 +77,7 @@ var Exa = class {
53
77
  this.headers = new import_cross_fetch.Headers({
54
78
  "x-api-key": apiKey,
55
79
  "Content-Type": "application/json",
56
- "User-Agent": "exa-node 1.1.0"
80
+ "User-Agent": "exa-node 1.3.0"
57
81
  });
58
82
  }
59
83
  /**
@@ -93,19 +117,11 @@ var Exa = class {
93
117
  * @returns {Promise<SearchResponse>} A list of relevant search results.
94
118
  */
95
119
  async searchAndContents(query, options) {
96
- const { text, highlights, summary, ...rest } = options || {};
120
+ const { contentsOptions, restOptions } = options === void 0 ? { contentsOptions: { text: true }, restOptions: {} } : this.extractContentsOptions(options);
97
121
  return await this.request("/search", "POST", {
98
122
  query,
99
- contents: !text && !highlights && !summary ? {
100
- text: true,
101
- ...options
102
- } : {
103
- ...text ? { text } : {},
104
- ...highlights ? { highlights } : {},
105
- ...summary ? { summary } : {},
106
- ...options
107
- },
108
- ...rest
123
+ contents: contentsOptions,
124
+ ...restOptions
109
125
  });
110
126
  }
111
127
  /**
@@ -117,30 +133,13 @@ var Exa = class {
117
133
  async findSimilar(url, options) {
118
134
  return await this.request("/findSimilar", "POST", { url, ...options });
119
135
  }
120
- /**
121
- * Finds similar links to the provided URL and returns the contents of the documents.
122
- * @param {string} url - The URL for which to find similar links.
123
- * @param {FindSimilarOptions} [options] - Additional options for finding similar links.
124
- * @returns {Promise<SearchResponse>} A list of similar search results.
125
- */
136
+ /** Finds similar links to the provided URL and returns the contents of the documents. @param {string} url - The URL for which to find similar links. @param {FindSimilarOptions} [options] - Additional options for finding similar links. @returns {Promise<SearchResponse>} A list of similar search results. */
126
137
  async findSimilarAndContents(url, options) {
127
- const { text, highlights, summary, ...rest } = options || {};
138
+ const { contentsOptions, restOptions } = options === void 0 ? { contentsOptions: { text: true }, restOptions: {} } : this.extractContentsOptions(options);
128
139
  return await this.request("/findSimilar", "POST", {
129
140
  url,
130
- contents: !text && !highlights && !summary ? {
131
- text: true,
132
- livecrawl: options?.livecrawl,
133
- livecrawlTimeout: options?.livecrawlTimeout,
134
- ...options
135
- } : {
136
- livecrawl: options?.livecrawl,
137
- livecrawlTimeout: options?.livecrawlTimeout,
138
- ...text ? { text } : {},
139
- ...highlights ? { highlights } : {},
140
- ...summary ? { summary } : {},
141
- ...options
142
- },
143
- ...rest
141
+ contents: contentsOptions,
142
+ ...restOptions
144
143
  });
145
144
  }
146
145
  /**
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from \"cross-fetch\";\n\nconst isBeta = false;\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n includeText?: string[];\n excludeText?: string[];\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} RegularSearchOptions\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n};\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.\n * @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.\n * @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.\n * @property {LivecrawlOptions} [livecrawl] - Options for livecrawling contents. Default is \"never\" for neural/auto search, \"fallback\" for keyword search.\n * @property {number} [livecrawlTimeout] - The timeout for livecrawling. Max and default is 10000ms.\n * @property {boolean} [filterEmptyResults] - If true, filters out results with no contents. Default is true.\n */\nexport type ContentsOptions = {\n text?: TextContentsOptions | true;\n highlights?: HighlightsContentsOptions | true;\n summary?: SummaryContentsOptions | true;\n livecrawl?: LivecrawlOptions;\n livecrawlTimeout?: number;\n filterEmptyResults?: boolean;\n} & (typeof isBeta extends true ? {} : {}); // FOR BETA OPTIONS\n\n/**\n * Options for livecrawling contents\n * @typedef {string} LivecrawlOptions\n */\nexport type LivecrawlOptions = \"never\" | \"fallback\" | \"always\";\n\n/**\n * Options for retrieving text from page.\n * @typedef {Object} TextContentsOptions\n * @property {number} [maxCharacters] - The maximum number of characters to return.\n * @property {boolean} [includeHtmlTags] - If true, includes HTML tags in the returned text. Default: false\n */\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n};\n\n/**\n * Options for retrieving highlights from page.\n * @typedef {Object} HighlightsContentsOptions\n * @property {string} [query] - The query string to use for highlights search.\n * @property {number} [numSentences] - The number of sentences to return for each highlight.\n * @property {number} [highlightsPerUrl] - The number of highlights to return for each URL.\n */\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n};\n\n/**\n * Options for retrieving summary from page.\n * @typedef {Object} SummaryContentsOptions\n * @property {string} [query] - The query string to use for summary generation.\n */\nexport type SummaryContentsOptions = {\n query?: string;\n};\n\n/**\n * @typedef {Object} TextResponse\n * @property {string} text - Text from page\n */\nexport type TextResponse = { text: string };\n\n/**\n * @typedef {Object} HighlightsResponse\n * @property {string[]} highlights - The highlights as an array of strings.\n * @property {number[]} highlightScores - The corresponding scores as an array of floats, 0 to 1\n */\nexport type HighlightsResponse = {\n highlights: string[];\n highlightScores: number[];\n};\n\n/**\n * @typedef {Object} SummaryResponse\n * @property {string} summary - The generated summary of the page content.\n */\nexport type SummaryResponse = { summary: string };\n\nexport type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;\n\n/**\n * @typedef {Object} ContentsResultComponent\n * Depending on 'ContentsOptions', this yields a combination of 'TextResponse', 'HighlightsResponse', 'SummaryResponse', or an empty object.\n *\n * @template T - A type extending from 'ContentsOptions'.\n */\nexport type ContentsResultComponent<T extends ContentsOptions> = Default<\n (T[\"text\"] extends object | true ? TextResponse : {}) &\n (T[\"highlights\"] extends object | true ? HighlightsResponse : {}) &\n (T[\"summary\"] extends object | true ? SummaryResponse : {}),\n TextResponse\n>;\n\n/**\n * Represents a search result object.\n * @typedef {Object} Result\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n * @property {string} [autoDate] - The autoprompt date, if applicable.\n * @property {string} requestId - The request ID for the search.\n */\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n autoDate?: string;\n requestId: string;\n};\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(apiKey?: string, baseURL: string = \"https://api.exa.ai\") {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\"\n );\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.1.0\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(\n query: string,\n options?: RegularSearchOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/search\", \"POST\", { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(\n query: string,\n options?: RegularSearchOptions & T\n ): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/search\", \"POST\", {\n query,\n contents:\n !text && !highlights && !summary\n ? {\n text: true,\n ...options,\n }\n : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {}),\n ...options,\n },\n ...rest,\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", \"POST\", { url, ...options });\n }\n\n /**\n * Finds similar links to the provided URL and returns the contents of the documents.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilarAndContents<T extends ContentsOptions>(\n url: string,\n options?: FindSimilarOptions & T\n ): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/findSimilar\", \"POST\", {\n url,\n contents:\n !text && !highlights && !summary\n ? {\n text: true,\n livecrawl: options?.livecrawl,\n livecrawlTimeout: options?.livecrawlTimeout,\n ...options,\n }\n : {\n livecrawl: options?.livecrawl,\n livecrawlTimeout: options?.livecrawlTimeout,\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {}),\n ...options,\n },\n ...rest,\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(\n ids: string | string[] | SearchResult[],\n options?: T\n ): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`, \"POST\", {\n ids: requestIds,\n ...options,\n });\n }\n}\n\nexport default Exa;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA+B;AAiM/B,IAAM,MAAN,MAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASR,YAAY,QAAiB,UAAkB,sBAAsB;AACnE,SAAK,UAAU;AACf,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,SAAK,UAAU,IAAI,2BAAQ;AAAA,MACzB,aAAa;AAAA,MACb,gBAAgB;AAAA,MAChB,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAc,QACZ,UACA,QACA,MACc;AACd,UAAM,WAAW,UAAM,mBAAAA,SAAM,KAAK,UAAU,UAAU;AAAA,MACpD;AAAA,MACA,SAAS,KAAK;AAAA,MACd,MAAM,OAAO,KAAK,U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Q,gBAAgB,QAAQ;AAAA,MAChD;AAAA,MACA,UACE,CAAC,QAAQ,CAAC,cAAc,CAAC,UACrB;AAAA,QACE,MAAM;AAAA,QACN,WAAW,SAAS;AAAA,QACpB,kBAAkB,SAAS;AAAA,QAC3B,GAAG;AAAA,MACL,IACA;AAAA,QACE,WAAW,SAAS;AAAA,QACpB,kBAAkB,SAAS;AAAA,QAC3B,GAAI,OAAO,EAAE,KAAK,IAAI,CAAC;AAAA,QACvB,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;AAAA,QACnC,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;AAAA,QAC7B,GAAG;AAAA,MACL;AAAA,MACN,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,YACJ,KACA,SAC4B;AAC5B,QAAI,IAAI,WAAW,GAAG;AACpB,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,QAAI;AACJ,QAAI,OAAO,QAAQ,UAAU;AAC3B,mBAAa,CAAC,GAAG;AAAA,IACnB,WAAW,OAAO,IAAI,CAAC,MAAM,UAAU;AACrC,mBAAa;AAAA,IACf,OAAO;AACL,mBAAc,IAAuB,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IAChE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ;AAAA,MAC7C,KAAK;AAAA,MACL,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACF;AAEA,IAAO,cAAQ;","names":["fetch"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from \"cross-fetch\";\n\nconst isBeta = false;\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n includeText?: string[];\n excludeText?: string[];\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} RegularSearchOptions\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n};\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n};\n\nexport type ExtrasOptions = {links: number}\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.\n * @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.\n * @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.\n * @property {LivecrawlOptions} [livecrawl] - Options for livecrawling contents. Default is \"never\" for neural/auto search, \"fallback\" for keyword search.\n * @property {number} [livecrawlTimeout] - The timeout for livecrawling. Max and default is 10000ms.\n * @property {boolean} [filterEmptyResults] - If true, filters out results with no contents. Default is true.\n * @property {number} [subpages] - The number of subpages to return for each result, where each subpage is derived from an internal link for the result\n * @property {ExtrasOptions} [extras] - Miscelleneous data for derived from resutls\n */\n\nexport type ContentsOptions = {\n text?: TextContentsOptions | true;\n highlights?: HighlightsContentsOptions | true;\n summary?: SummaryContentsOptions | true;\n livecrawl?: LivecrawlOptions;\n livecrawlTimeout?: number;\n filterEmptyResults?: boolean;\n subpages?: number\n subpageTarget?: string | string[]\n extras?: ExtrasOptions\n} & (typeof isBeta extends true ? {} : {}); // FOR BETA OPTIONS\n\n/**\n * Options for livecrawling contents\n * @typedef {string} LivecrawlOptions\n */\nexport type LivecrawlOptions = \"never\" | \"fallback\" | \"always\";\n\n/**\n * Options for retrieving text from page.\n * @typedef {Object} TextContentsOptions\n * @property {number} [maxCharacters] - The maximum number of characters to return.\n * @property {boolean} [includeHtmlTags] - If true, includes HTML tags in the returned text. Default: false\n */\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n};\n\n/**\n * Options for retrieving highlights from page.\n * @typedef {Object} HighlightsContentsOptions\n * @property {string} [query] - The query string to use for highlights search.\n * @property {number} [numSentences] - The number of sentences to return for each highlight.\n * @property {number} [highlightsPerUrl] - The number of highlights to return for each URL.\n */\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n};\n\n/**\n * Options for retrieving summary from page.\n * @typedef {Object} SummaryContentsOptions\n * @property {string} [query] - The query string to use for summary generation.\n */\nexport type SummaryContentsOptions = {\n query?: string;\n};\n\n/**\n * @typedef {Object} TextResponse\n * @property {string} text - Text from page\n */\nexport type TextResponse = { text: string };\n\n/**\n * @typedef {Object} HighlightsResponse\n * @property {string[]} highlights - The highlights as an array of strings.\n * @property {number[]} highlightScores - The corresponding scores as an array of floats, 0 to 1\n */\nexport type HighlightsResponse = {\n highlights: string[];\n highlightScores: number[];\n};\n\n/**\n * @typedef {Object} SummaryResponse\n * @property {string} summary - The generated summary of the page content.\n */\nexport type SummaryResponse = { summary: string };\n\n/**\n * @typedef {Object} ExtrasResponse\n * @property {string[]} links - The links on the page of a result\n */\nexport type ExtrasResponse = { extras: { links: string[] } };\n\n/**\n * @typedef {Object} SubpagesResponse\n * @property {ContentsResultComponent<T extends ContentsOptions>} subpages - The links on the page of a result\n */\nexport type SubpagesResponse<T extends ContentsOptions> = {subpages: ContentsResultComponent<T>[]}\n\n\n\nexport type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;\n\n/**\n * @typedef {Object} ContentsResultComponent\n * Depending on 'ContentsOptions', this yields a combination of 'TextResponse', 'HighlightsResponse', 'SummaryResponse', or an empty object.\n *\n * @template T - A type extending from 'ContentsOptions'.\n */\nexport type ContentsResultComponent<T extends ContentsOptions> = Default< \n (T[\"text\"] extends object | true ? TextResponse : {}) &\n (T[\"highlights\"] extends object | true ? HighlightsResponse : {}) &\n (T[\"summary\"] extends object | true ? SummaryResponse : {}) &\n (T[\"subpages\"] extends number ? SubpagesResponse<T> : {}) &\n (T[\"extras\"] extends object ? ExtrasResponse : {} ),\n TextResponse\n>;\n\n/**\n * Represents a search result object.\n * @typedef {Object} SearchResult\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n image?: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n * @property {string} [autoDate] - The autoprompt date, if applicable.\n * @property {string} requestId - The request ID for the search.\n */\nexport type SearchResponse<T extends ContentsOptions> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n autoDate?: string;\n requestId: string;\n};\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n private extractContentsOptions<T extends ContentsOptions>(options: T): {\n contentsOptions: ContentsOptions;\n restOptions: Omit<T, keyof ContentsOptions>;\n } {\n\n const { text, highlights, summary, subpages, subpageTarget, extras, livecrawl, livecrawlTimeout, ...rest } = options;\n\n const contentsOptions: ContentsOptions = {};\n if (text !== undefined) contentsOptions.text = text;\n if (highlights !== undefined) contentsOptions.highlights = highlights;\n if (summary !== undefined) contentsOptions.summary = summary;\n if (subpages !== undefined) contentsOptions.subpages = subpages;\n if (subpageTarget !== undefined) contentsOptions.subpageTarget = subpageTarget;\n if (extras !== undefined) contentsOptions.extras = extras;\n if (livecrawl !== undefined) contentsOptions.livecrawl = livecrawl;\n if (livecrawlTimeout !== undefined) contentsOptions.livecrawlTimeout = livecrawlTimeout;\n\n return {\n contentsOptions: contentsOptions,\n restOptions: rest as Omit<T, keyof ContentsOptions>\n };\n }\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(apiKey?: string, baseURL: string = \"https://api.exa.ai\") {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\",\n );\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.3.0\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any,\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`,\n );\n }\n\n return await response.json();\n }\n\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(\n query: string,\n options?: RegularSearchOptions,\n ): Promise<SearchResponse<{}>> {\n return await this.request(\"/search\", \"POST\", { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(\n query: string,\n options?: RegularSearchOptions & T,\n ): Promise<SearchResponse<T>> {\n const { contentsOptions, restOptions } = options === undefined ? \n { contentsOptions: { text: true }, restOptions: {}} :\n this.extractContentsOptions(options);\n\n return await this.request(\"/search\", \"POST\", {\n query,\n contents: contentsOptions,\n ...restOptions,\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions,\n ): Promise<SearchResponse<{}>> {\n return await this.request(\"/findSimilar\", \"POST\", { url, ...options });\n }\n\n /** Finds similar links to the provided URL and returns the contents of the documents. @param {string} url - The URL for which to find similar links. @param {FindSimilarOptions} [options] - Additional options for finding similar links. @returns {Promise<SearchResponse>} A list of similar search results. */ \n async findSimilarAndContents<T extends ContentsOptions>(\n url: string,\n options?: FindSimilarOptions & T,\n ): Promise<SearchResponse<T>> {\n const { contentsOptions, restOptions } = options === undefined ?\n {contentsOptions: {text: true}, restOptions: {}} :\n this.extractContentsOptions(options);\n \n return await this.request(\"/findSimilar\", \"POST\", {\n url,\n contents: contentsOptions,\n ...restOptions,\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(\n ids: string | string[] | SearchResult<T>[],\n options?: T,\n ): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult<T>[]).map((result) => result.id);\n }\n return await this.request(`/contents`, \"POST\", {\n ids: requestIds,\n ...options,\n });\n }\n}\n\nexport default Exa;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA+B;AA0N/B,IAAM,MAAN,MAAU;AAAA,EAIA,uBAAkD,SAGxD;AAEA,UAAM,EAAE,MAAM,YAAY,SAAS,UAAU,eAAe,QAAQ,WAAW,kBAAkB,GAAG,KAAK,IAAI;AAE7G,UAAM,kBAAmC,CAAC;AAC1C,QAAI,SAAS;AAAW,sBAAgB,OAAO;AAC/C,QAAI,eAAe;AAAW,sBAAgB,aAAa;AAC3D,QAAI,YAAY;AAAW,sBAAgB,UAAU;AACrD,QAAI,aAAa;AAAW,sBAAgB,WAAW;AACvD,QAAI,k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1
  // src/index.ts
2
2
  import fetch, { Headers } from "cross-fetch";
3
3
  var Exa = class {
4
+ extractContentsOptions(options) {
5
+ const { text, highlights, summary, subpages, subpageTarget, extras, livecrawl, livecrawlTimeout, ...rest } = options;
6
+ const contentsOptions = {};
7
+ if (text !== void 0)
8
+ contentsOptions.text = text;
9
+ if (highlights !== void 0)
10
+ contentsOptions.highlights = highlights;
11
+ if (summary !== void 0)
12
+ contentsOptions.summary = summary;
13
+ if (subpages !== void 0)
14
+ contentsOptions.subpages = subpages;
15
+ if (subpageTarget !== void 0)
16
+ contentsOptions.subpageTarget = subpageTarget;
17
+ if (extras !== void 0)
18
+ contentsOptions.extras = extras;
19
+ if (livecrawl !== void 0)
20
+ contentsOptions.livecrawl = livecrawl;
21
+ if (livecrawlTimeout !== void 0)
22
+ contentsOptions.livecrawlTimeout = livecrawlTimeout;
23
+ return {
24
+ contentsOptions,
25
+ restOptions: rest
26
+ };
27
+ }
4
28
  /**
5
29
  * Constructs the Exa API client.
6
30
  * @param {string} apiKey - The API key for authentication.
@@ -19,7 +43,7 @@ var Exa = class {
19
43
  this.headers = new Headers({
20
44
  "x-api-key": apiKey,
21
45
  "Content-Type": "application/json",
22
- "User-Agent": "exa-node 1.1.0"
46
+ "User-Agent": "exa-node 1.3.0"
23
47
  });
24
48
  }
25
49
  /**
@@ -59,19 +83,11 @@ var Exa = class {
59
83
  * @returns {Promise<SearchResponse>} A list of relevant search results.
60
84
  */
61
85
  async searchAndContents(query, options) {
62
- const { text, highlights, summary, ...rest } = options || {};
86
+ const { contentsOptions, restOptions } = options === void 0 ? { contentsOptions: { text: true }, restOptions: {} } : this.extractContentsOptions(options);
63
87
  return await this.request("/search", "POST", {
64
88
  query,
65
- contents: !text && !highlights && !summary ? {
66
- text: true,
67
- ...options
68
- } : {
69
- ...text ? { text } : {},
70
- ...highlights ? { highlights } : {},
71
- ...summary ? { summary } : {},
72
- ...options
73
- },
74
- ...rest
89
+ contents: contentsOptions,
90
+ ...restOptions
75
91
  });
76
92
  }
77
93
  /**
@@ -83,30 +99,13 @@ var Exa = class {
83
99
  async findSimilar(url, options) {
84
100
  return await this.request("/findSimilar", "POST", { url, ...options });
85
101
  }
86
- /**
87
- * Finds similar links to the provided URL and returns the contents of the documents.
88
- * @param {string} url - The URL for which to find similar links.
89
- * @param {FindSimilarOptions} [options] - Additional options for finding similar links.
90
- * @returns {Promise<SearchResponse>} A list of similar search results.
91
- */
102
+ /** Finds similar links to the provided URL and returns the contents of the documents. @param {string} url - The URL for which to find similar links. @param {FindSimilarOptions} [options] - Additional options for finding similar links. @returns {Promise<SearchResponse>} A list of similar search results. */
92
103
  async findSimilarAndContents(url, options) {
93
- const { text, highlights, summary, ...rest } = options || {};
104
+ const { contentsOptions, restOptions } = options === void 0 ? { contentsOptions: { text: true }, restOptions: {} } : this.extractContentsOptions(options);
94
105
  return await this.request("/findSimilar", "POST", {
95
106
  url,
96
- contents: !text && !highlights && !summary ? {
97
- text: true,
98
- livecrawl: options?.livecrawl,
99
- livecrawlTimeout: options?.livecrawlTimeout,
100
- ...options
101
- } : {
102
- livecrawl: options?.livecrawl,
103
- livecrawlTimeout: options?.livecrawlTimeout,
104
- ...text ? { text } : {},
105
- ...highlights ? { highlights } : {},
106
- ...summary ? { summary } : {},
107
- ...options
108
- },
109
- ...rest
107
+ contents: contentsOptions,
108
+ ...restOptions
110
109
  });
111
110
  }
112
111
  /**
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from \"cross-fetch\";\n\nconst isBeta = false;\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n includeText?: string[];\n excludeText?: string[];\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} RegularSearchOptions\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n};\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.\n * @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.\n * @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.\n * @property {LivecrawlOptions} [livecrawl] - Options for livecrawling contents. Default is \"never\" for neural/auto search, \"fallback\" for keyword search.\n * @property {number} [livecrawlTimeout] - The timeout for livecrawling. Max and default is 10000ms.\n * @property {boolean} [filterEmptyResults] - If true, filters out results with no contents. Default is true.\n */\nexport type ContentsOptions = {\n text?: TextContentsOptions | true;\n highlights?: HighlightsContentsOptions | true;\n summary?: SummaryContentsOptions | true;\n livecrawl?: LivecrawlOptions;\n livecrawlTimeout?: number;\n filterEmptyResults?: boolean;\n} & (typeof isBeta extends true ? {} : {}); // FOR BETA OPTIONS\n\n/**\n * Options for livecrawling contents\n * @typedef {string} LivecrawlOptions\n */\nexport type LivecrawlOptions = \"never\" | \"fallback\" | \"always\";\n\n/**\n * Options for retrieving text from page.\n * @typedef {Object} TextContentsOptions\n * @property {number} [maxCharacters] - The maximum number of characters to return.\n * @property {boolean} [includeHtmlTags] - If true, includes HTML tags in the returned text. Default: false\n */\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n};\n\n/**\n * Options for retrieving highlights from page.\n * @typedef {Object} HighlightsContentsOptions\n * @property {string} [query] - The query string to use for highlights search.\n * @property {number} [numSentences] - The number of sentences to return for each highlight.\n * @property {number} [highlightsPerUrl] - The number of highlights to return for each URL.\n */\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n};\n\n/**\n * Options for retrieving summary from page.\n * @typedef {Object} SummaryContentsOptions\n * @property {string} [query] - The query string to use for summary generation.\n */\nexport type SummaryContentsOptions = {\n query?: string;\n};\n\n/**\n * @typedef {Object} TextResponse\n * @property {string} text - Text from page\n */\nexport type TextResponse = { text: string };\n\n/**\n * @typedef {Object} HighlightsResponse\n * @property {string[]} highlights - The highlights as an array of strings.\n * @property {number[]} highlightScores - The corresponding scores as an array of floats, 0 to 1\n */\nexport type HighlightsResponse = {\n highlights: string[];\n highlightScores: number[];\n};\n\n/**\n * @typedef {Object} SummaryResponse\n * @property {string} summary - The generated summary of the page content.\n */\nexport type SummaryResponse = { summary: string };\n\nexport type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;\n\n/**\n * @typedef {Object} ContentsResultComponent\n * Depending on 'ContentsOptions', this yields a combination of 'TextResponse', 'HighlightsResponse', 'SummaryResponse', or an empty object.\n *\n * @template T - A type extending from 'ContentsOptions'.\n */\nexport type ContentsResultComponent<T extends ContentsOptions> = Default<\n (T[\"text\"] extends object | true ? TextResponse : {}) &\n (T[\"highlights\"] extends object | true ? HighlightsResponse : {}) &\n (T[\"summary\"] extends object | true ? SummaryResponse : {}),\n TextResponse\n>;\n\n/**\n * Represents a search result object.\n * @typedef {Object} Result\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n * @property {string} [autoDate] - The autoprompt date, if applicable.\n * @property {string} requestId - The request ID for the search.\n */\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n autoDate?: string;\n requestId: string;\n};\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(apiKey?: string, baseURL: string = \"https://api.exa.ai\") {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\"\n );\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.1.0\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(\n query: string,\n options?: RegularSearchOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/search\", \"POST\", { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(\n query: string,\n options?: RegularSearchOptions & T\n ): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/search\", \"POST\", {\n query,\n contents:\n !text && !highlights && !summary\n ? {\n text: true,\n ...options,\n }\n : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {}),\n ...options,\n },\n ...rest,\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", \"POST\", { url, ...options });\n }\n\n /**\n * Finds similar links to the provided URL and returns the contents of the documents.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilarAndContents<T extends ContentsOptions>(\n url: string,\n options?: FindSimilarOptions & T\n ): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/findSimilar\", \"POST\", {\n url,\n contents:\n !text && !highlights && !summary\n ? {\n text: true,\n livecrawl: options?.livecrawl,\n livecrawlTimeout: options?.livecrawlTimeout,\n ...options,\n }\n : {\n livecrawl: options?.livecrawl,\n livecrawlTimeout: options?.livecrawlTimeout,\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {}),\n ...options,\n },\n ...rest,\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(\n ids: string | string[] | SearchResult[],\n options?: T\n ): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`, \"POST\", {\n ids: requestIds,\n ...options,\n });\n }\n}\n\nexport default Exa;\n"],"mappings":";AAAA,OAAO,SAAS,eAAe;AAiM/B,IAAM,MAAN,MAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAS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,OAAO;AACL,mBAAc,IAAuB,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IAChE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ;AAAA,MAC7C,KAAK;AAAA,MACL,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACF;AAEA,IAAO,cAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from \"cross-fetch\";\n\nconst isBeta = false;\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n includeText?: string[];\n excludeText?: string[];\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} RegularSearchOptions\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n};\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n};\n\nexport type ExtrasOptions = {links: number}\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.\n * @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.\n * @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.\n * @property {LivecrawlOptions} [livecrawl] - Options for livecrawling contents. Default is \"never\" for neural/auto search, \"fallback\" for keyword search.\n * @property {number} [livecrawlTimeout] - The timeout for livecrawling. Max and default is 10000ms.\n * @property {boolean} [filterEmptyResults] - If true, filters out results with no contents. Default is true.\n * @property {number} [subpages] - The number of subpages to return for each result, where each subpage is derived from an internal link for the result\n * @property {ExtrasOptions} [extras] - Miscelleneous data for derived from resutls\n */\n\nexport type ContentsOptions = {\n text?: TextContentsOptions | true;\n highlights?: HighlightsContentsOptions | true;\n summary?: SummaryContentsOptions | true;\n livecrawl?: LivecrawlOptions;\n livecrawlTimeout?: number;\n filterEmptyResults?: boolean;\n subpages?: number\n subpageTarget?: string | string[]\n extras?: ExtrasOptions\n} & (typeof isBeta extends true ? {} : {}); // FOR BETA OPTIONS\n\n/**\n * Options for livecrawling contents\n * @typedef {string} LivecrawlOptions\n */\nexport type LivecrawlOptions = \"never\" | \"fallback\" | \"always\";\n\n/**\n * Options for retrieving text from page.\n * @typedef {Object} TextContentsOptions\n * @property {number} [maxCharacters] - The maximum number of characters to return.\n * @property {boolean} [includeHtmlTags] - If true, includes HTML tags in the returned text. Default: false\n */\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n};\n\n/**\n * Options for retrieving highlights from page.\n * @typedef {Object} HighlightsContentsOptions\n * @property {string} [query] - The query string to use for highlights search.\n * @property {number} [numSentences] - The number of sentences to return for each highlight.\n * @property {number} [highlightsPerUrl] - The number of highlights to return for each URL.\n */\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n};\n\n/**\n * Options for retrieving summary from page.\n * @typedef {Object} SummaryContentsOptions\n * @property {string} [query] - The query string to use for summary generation.\n */\nexport type SummaryContentsOptions = {\n query?: string;\n};\n\n/**\n * @typedef {Object} TextResponse\n * @property {string} text - Text from page\n */\nexport type TextResponse = { text: string };\n\n/**\n * @typedef {Object} HighlightsResponse\n * @property {string[]} highlights - The highlights as an array of strings.\n * @property {number[]} highlightScores - The corresponding scores as an array of floats, 0 to 1\n */\nexport type HighlightsResponse = {\n highlights: string[];\n highlightScores: number[];\n};\n\n/**\n * @typedef {Object} SummaryResponse\n * @property {string} summary - The generated summary of the page content.\n */\nexport type SummaryResponse = { summary: string };\n\n/**\n * @typedef {Object} ExtrasResponse\n * @property {string[]} links - The links on the page of a result\n */\nexport type ExtrasResponse = { extras: { links: string[] } };\n\n/**\n * @typedef {Object} SubpagesResponse\n * @property {ContentsResultComponent<T extends ContentsOptions>} subpages - The links on the page of a result\n */\nexport type SubpagesResponse<T extends ContentsOptions> = {subpages: ContentsResultComponent<T>[]}\n\n\n\nexport type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;\n\n/**\n * @typedef {Object} ContentsResultComponent\n * Depending on 'ContentsOptions', this yields a combination of 'TextResponse', 'HighlightsResponse', 'SummaryResponse', or an empty object.\n *\n * @template T - A type extending from 'ContentsOptions'.\n */\nexport type ContentsResultComponent<T extends ContentsOptions> = Default< \n (T[\"text\"] extends object | true ? TextResponse : {}) &\n (T[\"highlights\"] extends object | true ? HighlightsResponse : {}) &\n (T[\"summary\"] extends object | true ? SummaryResponse : {}) &\n (T[\"subpages\"] extends number ? SubpagesResponse<T> : {}) &\n (T[\"extras\"] extends object ? ExtrasResponse : {} ),\n TextResponse\n>;\n\n/**\n * Represents a search result object.\n * @typedef {Object} SearchResult\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n image?: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n * @property {string} [autoDate] - The autoprompt date, if applicable.\n * @property {string} requestId - The request ID for the search.\n */\nexport type SearchResponse<T extends ContentsOptions> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n autoDate?: string;\n requestId: string;\n};\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n private extractContentsOptions<T extends ContentsOptions>(options: T): {\n contentsOptions: ContentsOptions;\n restOptions: Omit<T, keyof ContentsOptions>;\n } {\n\n const { text, highlights, summary, subpages, subpageTarget, extras, livecrawl, livecrawlTimeout, ...rest } = options;\n\n const contentsOptions: ContentsOptions = {};\n if (text !== undefined) contentsOptions.text = text;\n if (highlights !== undefined) contentsOptions.highlights = highlights;\n if (summary !== undefined) contentsOptions.summary = summary;\n if (subpages !== undefined) contentsOptions.subpages = subpages;\n if (subpageTarget !== undefined) contentsOptions.subpageTarget = subpageTarget;\n if (extras !== undefined) contentsOptions.extras = extras;\n if (livecrawl !== undefined) contentsOptions.livecrawl = livecrawl;\n if (livecrawlTimeout !== undefined) contentsOptions.livecrawlTimeout = livecrawlTimeout;\n\n return {\n contentsOptions: contentsOptions,\n restOptions: rest as Omit<T, keyof ContentsOptions>\n };\n }\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(apiKey?: string, baseURL: string = \"https://api.exa.ai\") {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\",\n );\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.3.0\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any,\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`,\n );\n }\n\n return await response.json();\n }\n\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(\n query: string,\n options?: RegularSearchOptions,\n ): Promise<SearchResponse<{}>> {\n return await this.request(\"/search\", \"POST\", { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(\n query: string,\n options?: RegularSearchOptions & T,\n ): Promise<SearchResponse<T>> {\n const { contentsOptions, restOptions } = options === undefined ? \n { contentsOptions: { text: true }, restOptions: {}} :\n this.extractContentsOptions(options);\n\n return await this.request(\"/search\", \"POST\", {\n query,\n contents: contentsOptions,\n ...restOptions,\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions,\n ): Promise<SearchResponse<{}>> {\n return await this.request(\"/findSimilar\", \"POST\", { url, ...options });\n }\n\n /** Finds similar links to the provided URL and returns the contents of the documents. @param {string} url - The URL for which to find similar links. @param {FindSimilarOptions} [options] - Additional options for finding similar links. @returns {Promise<SearchResponse>} A list of similar search results. */ \n async findSimilarAndContents<T extends ContentsOptions>(\n url: string,\n options?: FindSimilarOptions & T,\n ): Promise<SearchResponse<T>> {\n const { contentsOptions, restOptions } = options === undefined ?\n {contentsOptions: {text: true}, restOptions: {}} :\n this.extractContentsOptions(options);\n \n return await this.request(\"/findSimilar\", \"POST\", {\n url,\n contents: contentsOptions,\n ...restOptions,\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(\n ids: string | string[] | SearchResult<T>[],\n options?: T,\n ): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult<T>[]).map((result) => result.id);\n }\n return await this.request(`/contents`,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AAM,8BAA8B;AAAA,IAChD;AACA,QAAI;AACJ,QAAI,OAAO,QAAQ,UAAU;AAC3B,mBAAa,CAAC,GAAG;AAAA,IACnB,WAAW,OAAO,IAAI,CAAC,MAAM,UAAU;AACrC,mBAAa;AAAA,IACf,OAAO;AACL,mBAAc,IAA0B,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IACnE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ;AAAA,MAC7C,KAAK;AAAA,MACL,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACF;AAEA,IAAO,cAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exa-js",
3
- "version": "1.1.3",
3
+ "version": "1.3.0",
4
4
  "description": "Exa SDK for Node.js and the browser",
5
5
  "publishConfig": {
6
6
  "access": "public"