refactor: query rewriter

This commit is contained in:
Han Xiao 2025-03-11 15:10:55 +08:00
parent 6cc48d07fd
commit 53cb3a4318

View File

@ -1,42 +1,46 @@
import axios from 'axios';
import { SERPER_API_KEY } from "../config";
import {SERPER_API_KEY} from "../config";
import {SerperSearchResponse, SERPQuery} from '../types';
export async function serperSearch(query: SERPQuery): Promise<{ response: SerperSearchResponse }> {
const response = await axios.post<SerperSearchResponse>('https://google.serper.dev/search', {
...query,
autocorrect: false,
}, {
headers: {
'X-API-KEY': SERPER_API_KEY,
'Content-Type': 'application/json'
},
timeout: 10000
});
const response = await axios.post<SerperSearchResponse>('https://google.serper.dev/search', {
...query,
autocorrect: false,
}, {
headers: {
'X-API-KEY': SERPER_API_KEY,
'Content-Type': 'application/json'
},
timeout: 10000
});
// Maintain the same return structure as the original code
return { response: response.data };
if (response.status !== 200) {
throw new Error(`Serper search failed: ${response.status} ${response.statusText}`)
}
// Maintain the same return structure as the original code
return {response: response.data};
}
export async function serperSearchOld(query: string): Promise<{ response: SerperSearchResponse }> {
const response = await axios.post<SerperSearchResponse>('https://google.serper.dev/search', {
q: query,
autocorrect: false,
}, {
headers: {
'X-API-KEY': SERPER_API_KEY,
'Content-Type': 'application/json'
},
timeout: 10000
});
const response = await axios.post<SerperSearchResponse>('https://google.serper.dev/search', {
q: query,
autocorrect: false,
}, {
headers: {
'X-API-KEY': SERPER_API_KEY,
'Content-Type': 'application/json'
},
timeout: 10000
});
if (response.status !== 200) {
throw new Error(`Serper search failed: ${response.status} ${response.statusText}`)
}
if (response.status !== 200) {
throw new Error(`Serper search failed: ${response.status} ${response.statusText}`)
}
// Maintain the same return structure as the original code
return { response: response.data };
// Maintain the same return structure as the original code
return {response: response.data};
}