Skip to content

Commit

Permalink
fix: check if is an orama client
Browse files Browse the repository at this point in the history
  • Loading branch information
raiindev committed Nov 21, 2024
1 parent 1d25adb commit 5dc824d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/switch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export type SearchConfig = {
debounce?: number
}

function isOramaClient(client: any): client is OramaClient {
return client && typeof client === 'object' && 'api_key' in client && 'endpoint' in client;
}

export class Switch<T = OramaSwitchClient> {
private invalidClientError = 'Invalid client. Expected either an OramaClient or an Orama OSS database.'
client: OramaSwitchClient
Expand All @@ -22,7 +26,7 @@ export class Switch<T = OramaSwitchClient> {
constructor(client: OramaSwitchClient) {
this.client = client

if (client instanceof OramaClient) {
if (isOramaClient(client)) {
this.clientType = 'cloud'
this.isCloud = true
} else if (typeof client === 'object' && 'id' in client && 'tokenizer' in client) {
Expand All @@ -44,10 +48,10 @@ export class Switch<T = OramaSwitchClient> {
}
}

createAnswerSession(params: T extends OramaClient ? CloudAnswerSessionConfig : OSSAnswerSessionConfig): T extends OramaClient ? CloudAnswerSession : OSSAnswerSession {
createAnswerSession(params: T extends OramaClient ? CloudAnswerSessionConfig : OSSAnswerSessionConfig): T extends OramaClient ? CloudAnswerSession<true> : OSSAnswerSession {
if (this.isCloud) {
const p = params as CloudAnswerSessionConfig
return (this.client as OramaClient).createAnswerSession(p) as unknown as T extends OramaClient ? CloudAnswerSession : OSSAnswerSession
return (this.client as OramaClient).createAnswerSession(p) as unknown as T extends OramaClient ? CloudAnswerSession<true> : OSSAnswerSession
}

if (this.isOSS) {
Expand All @@ -58,7 +62,7 @@ export class Switch<T = OramaSwitchClient> {
events: p.events,
userContext: p.userContext,
systemPrompt: p.systemPrompt,
}) as unknown as T extends OramaClient ? CloudAnswerSession : OSSAnswerSession
}) as unknown as T extends OramaClient ? CloudAnswerSession<true> : OSSAnswerSession
}

throw new Error(this.invalidClientError)
Expand Down

0 comments on commit 5dc824d

Please sign in to comment.