diff --git a/src/client.ts b/src/client.ts index 9552d01..308de1a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -14,6 +14,7 @@ import { Profile } from './profile.js' export interface SearchConfig { abortController?: AbortController + abortSignal?: AbortSignal fresh?: boolean debounce?: number } @@ -61,6 +62,10 @@ export type AnswerSessionParams = { export { AnswerSession, Message } +function isAbortController(signal: AbortSignal | AbortController | undefined): signal is AbortController { + return signal !== undefined && (signal as AbortController)?.signal !== undefined +} + export class OramaClient { private readonly id = createId() private readonly api_key: string @@ -149,7 +154,12 @@ export class OramaClient { const performSearch = async () => { try { const timeStart = Date.now() - searchResults = await this.fetch>('search', 'POST', { q: query, sst: this.searchToken }, config?.abortController) + searchResults = await this.fetch>( + 'search', + 'POST', + { q: query, sst: this.searchToken }, + config?.abortSignal ?? config?.abortController + ) const timeEnd = Date.now() searchResults.elapsed = await formatElapsedTime(BigInt(timeEnd * CONST.MICROSECONDS_BASE - timeStart * CONST.MICROSECONDS_BASE)) roundTripTime = timeEnd - timeStart @@ -242,7 +252,7 @@ export class OramaClient { cached = true } else { const timeStart = Date.now() - searchResults = await this.fetch>('vector-search2', 'POST', { q: query }, config?.abortController) + searchResults = await this.fetch>('vector-search2', 'POST', { q: query }, config?.abortSignal ?? config?.abortController) const timeEnd = Date.now() searchResults.elapsed = await formatElapsedTime(BigInt(timeEnd * CONST.MICROSECONDS_BASE - timeStart * CONST.MICROSECONDS_BASE)) @@ -346,10 +356,11 @@ export class OramaClient { path: Endpoint, method: Method, body?: object, - abortController?: AbortController, + abort?: AbortController | AbortSignal, queryParams?: Record ): Promise { - if (abortController?.signal.aborted === true) { + const abortSignal = isAbortController(abort) ? abort?.signal : abort + if (abortSignal?.aborted === true) { throw new Error('Request aborted') } @@ -361,7 +372,7 @@ export class OramaClient { // 'x-orama-instance-id': this.id, // 'x-orama-version': version }, - signal: abortController?.signal + signal: abortSignal } if (method === 'POST' && body !== undefined) { diff --git a/src/manager/index-manager.ts b/src/manager/index-manager.ts index f7f749f..defe7a9 100644 --- a/src/manager/index-manager.ts +++ b/src/manager/index-manager.ts @@ -16,8 +16,8 @@ type CallWebhookPayload = E extends EndpointSnapshot ? any[] : E extends EndpointDeploy ? undefined - : E extends EndpointUpdateSchema - ? { schema: { [key: string]: any }, embeddings?: any } + : E extends EndpointUpdateSchema + ? { schema: { [key: string]: any }; embeddings?: any } : never export class IndexManager { diff --git a/src/manager/index.ts b/src/manager/index.ts index f966834..dc2eaca 100644 --- a/src/manager/index.ts +++ b/src/manager/index.ts @@ -4,7 +4,7 @@ import { IndexManager } from './index-manager.js' import { API_V1_BASE_URL } from './constants.js' type CloudManagerConfig = { - api_key: string, + api_key: string baseURL?: string }