mirror of
https://github.com/upstash/context7.git
synced 2025-06-26 23:50:04 +00:00
feat: improve search result formatting to handle missing data gracefully
This commit is contained in:
parent
171b298f7a
commit
459e63d771
@ -1,22 +1,40 @@
|
|||||||
import { SearchResponse, SearchResult } from "./types.js";
|
import { SearchResponse, SearchResult } from "./types.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format a search result into a string representation
|
* Formats a search result into a human-readable string representation.
|
||||||
* @param result SearchResult to format
|
* Only shows code snippet count and GitHub stars when available (not equal to -1).
|
||||||
* @returns Formatted search result string
|
*
|
||||||
|
* @param result The SearchResult object to format
|
||||||
|
* @returns A formatted string with library information
|
||||||
*/
|
*/
|
||||||
export function formatSearchResult(result: SearchResult): string {
|
export function formatSearchResult(result: SearchResult): string {
|
||||||
return `- Title: ${result.title}
|
// Always include these basic details
|
||||||
- Context7-compatible library ID: ${result.id}
|
const formattedResult = [
|
||||||
- Description: ${result.description}
|
`- Title: ${result.title}`,
|
||||||
- Code Snippets: ${result.totalSnippets}
|
`- Context7-compatible library ID: ${result.id}`,
|
||||||
- GitHub Stars: ${result.stars}`;
|
`- Description: ${result.description}`,
|
||||||
|
];
|
||||||
|
|
||||||
|
// Only add code snippets count if it's a valid value
|
||||||
|
if (result.totalSnippets !== -1 && result.totalSnippets !== undefined) {
|
||||||
|
formattedResult.push(`- Code Snippets: ${result.totalSnippets}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only add GitHub stars if it's a valid value
|
||||||
|
if (result.stars !== -1 && result.stars !== undefined) {
|
||||||
|
formattedResult.push(`- GitHub Stars: ${result.stars}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join all parts with newlines
|
||||||
|
return formattedResult.join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format search results into a string representation
|
* Formats a search response into a human-readable string representation.
|
||||||
* @param searchResponse The search response to format
|
* Each result is formatted using formatSearchResult.
|
||||||
* @returns Formatted search results string
|
*
|
||||||
|
* @param searchResponse The SearchResponse object to format
|
||||||
|
* @returns A formatted string with search results
|
||||||
*/
|
*/
|
||||||
export function formatSearchResults(searchResponse: SearchResponse): string {
|
export function formatSearchResults(searchResponse: SearchResponse): string {
|
||||||
if (!searchResponse.results || searchResponse.results.length === 0) {
|
if (!searchResponse.results || searchResponse.results.length === 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user