mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-30 00:53:14 +00:00
(feat/search): debounce and abort previous pending req
This commit is contained in:
@@ -1,34 +1,37 @@
|
||||
import { Result } from "@/types";
|
||||
|
||||
async function getSearchResults(question: string, apiKey:string, apiHost:string): Promise<Result[]> {
|
||||
|
||||
const payload = {
|
||||
question,
|
||||
api_key:apiKey
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(`${apiHost}/api/search`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Error: ${response.status}`);
|
||||
}
|
||||
|
||||
const data: Result[] = await response.json();
|
||||
return data;
|
||||
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch documents:", error);
|
||||
throw error;
|
||||
async function getSearchResults(question: string, apiKey: string, apiHost: string, signal: AbortSignal): Promise<Result[]> {
|
||||
|
||||
const payload = {
|
||||
question,
|
||||
api_key: apiKey
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(`${apiHost}/api/search`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
signal: signal
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Error: ${response.status}`);
|
||||
}
|
||||
|
||||
const data: Result[] = await response.json();
|
||||
return data;
|
||||
|
||||
} catch (error) {
|
||||
if (!(error instanceof DOMException && error.name == "AbortError")) {
|
||||
console.error("Failed to fetch documents:", error);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
export {
|
||||
getSearchResults
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
getSearchResults
|
||||
}
|
||||
Reference in New Issue
Block a user