(params: {
query: string
logger: Logger
fetch: typeof globalThis.fetch
})
| 43 | * @returns Array of projects with their metadata, or null if the request fails |
| 44 | */ |
| 45 | export async function searchLibraries(params: { |
| 46 | query: string |
| 47 | logger: Logger |
| 48 | fetch: typeof globalThis.fetch |
| 49 | }): Promise<SearchResult[] | null> { |
| 50 | const { query, logger, fetch } = params |
| 51 | |
| 52 | const searchStartTime = Date.now() |
| 53 | const searchContext = { |
| 54 | query, |
| 55 | queryLength: query.length, |
| 56 | } |
| 57 | |
| 58 | try { |
| 59 | const url = new URL(`${CONTEXT7_API_BASE_URL}/search`) |
| 60 | url.searchParams.set('query', query) |
| 61 | |
| 62 | const fetchStartTime = Date.now() |
| 63 | const response = await withTimeout( |
| 64 | fetch(url, { |
| 65 | headers: { |
| 66 | Authorization: `Bearer ${process.env['CONTEXT7_API_KEY']}`, |
| 67 | }, |
| 68 | }), |
| 69 | FETCH_TIMEOUT_MS, |
| 70 | ) |
| 71 | const fetchDuration = Date.now() - fetchStartTime |
| 72 | |
| 73 | if (!response.ok) { |
| 74 | logger.error( |
| 75 | { |
| 76 | ...searchContext, |
| 77 | status: response.status, |
| 78 | statusText: response.statusText, |
| 79 | fetchDuration, |
| 80 | totalDuration: Date.now() - searchStartTime, |
| 81 | }, |
| 82 | `Library search failed with status ${response.status}`, |
| 83 | ) |
| 84 | return null |
| 85 | } |
| 86 | |
| 87 | const parseStartTime = Date.now() |
| 88 | const responseBody = await response.json() |
| 89 | const projects = responseBody as SearchResponse |
| 90 | const parseDuration = Date.now() - parseStartTime |
| 91 | const totalDuration = Date.now() - searchStartTime |
| 92 | |
| 93 | logger.debug( |
| 94 | { |
| 95 | ...searchContext, |
| 96 | fetchDuration, |
| 97 | parseDuration, |
| 98 | totalDuration, |
| 99 | resultsCount: projects.results?.length || 0, |
| 100 | success: true, |
| 101 | }, |
| 102 | 'Library search completed successfully', |
no test coverage detected