( libraryId: string, query: string, options?: GetContextOptions, accessToken?: string )
| 314 | } |
| 315 | |
| 316 | export async function getLibraryContext( |
| 317 | libraryId: string, |
| 318 | query: string, |
| 319 | options?: GetContextOptions, |
| 320 | accessToken?: string |
| 321 | ): Promise<ContextResponse | string> { |
| 322 | const params = new URLSearchParams({ libraryId, query }); |
| 323 | if (options?.type) { |
| 324 | params.set("type", options.type); |
| 325 | } |
| 326 | const headers = getAuthHeaders(accessToken); |
| 327 | const response = await fetch(`${baseUrl}/api/v2/context?${params}`, { |
| 328 | headers, |
| 329 | }); |
| 330 | |
| 331 | if (!response.ok) { |
| 332 | const errorData = (await response.json().catch(() => ({}))) as { |
| 333 | error?: string; |
| 334 | message?: string; |
| 335 | redirectUrl?: string; |
| 336 | }; |
| 337 | |
| 338 | if (response.status === 301 && errorData.redirectUrl) { |
| 339 | return { |
| 340 | codeSnippets: [], |
| 341 | infoSnippets: [], |
| 342 | error: errorData.error || "library_redirected", |
| 343 | message: errorData.message, |
| 344 | redirectUrl: errorData.redirectUrl, |
| 345 | }; |
| 346 | } |
| 347 | |
| 348 | return { |
| 349 | codeSnippets: [], |
| 350 | infoSnippets: [], |
| 351 | error: errorData.error || `HTTP error ${response.status}`, |
| 352 | message: errorData.message, |
| 353 | }; |
| 354 | } |
| 355 | |
| 356 | if (options?.type === "txt") { |
| 357 | return await response.text(); |
| 358 | } |
| 359 | |
| 360 | return (await response.json()) as ContextResponse; |
| 361 | } |
no test coverage detected