Context7 Platform -- Up-to-date docs for AI agents
feat: create seperate files for helper functions and api calls
Abdusshh27d5887a1b07
diff --git a/src/lib/api.ts b/src/lib/api.tsnew file mode 100644index 00000000..1e881747--- /dev/null+++ b/src/lib/api.ts@@ -0,0 +1,68 @@+import { Project } from "./types.js";++const CONTEXT7_BASE_URL = "https://context7.com";++/**+ * Fetches projects from the Context7 API+ * @returns Array of projects or null if the request fails+ */+export async function fetchProjects(): Promise<Project[] | null> {+ try {+ const response = await fetch(`${CONTEXT7_BASE_URL}/api/projects`);+ if (!response.ok) {+ console.error(`Failed to fetch projects: ${response.status}`);+ return null;+ }+ return await response.json();+ } catch (error) {+ console.error("Error fetching projects:", error);+ return null;+ }+}++/**+ * Fetches documentation context for a specific package+ * @param packageName The package name to fetch documentation for+ * @param tokens Number of tokens to retrieve (default: 5000)+ * @param topic Optional topic to rerank context for+ * @returns The documentation text or null if the request fails+ */+export async function fetchPackageDocumentation(+ packageName: string,+ tokens: number = 5000,+ topic: string = ""+): Promise<string | null> {+ try {+ let contextURL = `${CONTEXT7_BASE_URL}/${packageName}/llm.txt`;+ const params = [];++ if (tokens) {+ params.push(`tokens=${tokens}`);+ }+ if (topic) {+ params.push(`topic=${encodeURIComponent(topic)}`);+ }++ if (params.length > 0) {+ contextURL += `?${params.join("&")}`;+ }++ const response = await fetch(contextURL);++ if (!response.ok) {+ console.error(`Failed to fetch documentation: ${response.status}`);+ return null;+ }++ const text = await response.text();++ if (!text || text === "No content available" || text === "No context data available") {+ return null;+ }++ return text;+ } catch (error) {+ console.error("Error fetching package documentation:", error);+ return null;+ }+}diff --git a/src/lib/utils.ts b/src/lib/utils.tsnew file mode 100644index 00000000..a12fb6b1--- /dev/null+++ b/src/lib/utils.ts@@ -0,0 +1,20 @@+import { Project } from "./types.js";++/**+ * Format a project for display+ * @param project The project to format+ * @returns Formatted project string+ */+export function formatProject(project: Project): string {+ return `Title: ${project.settings.title}\nPackage name: ${project.settings.project}\n`;+}++/**+ * Format a list of projects for display+ * @param projects List of projects to format+ * @returns Formatted text with all projects+ */+export function formatProjectsList(projects: Project[]): string {+ const formattedProjects = projects.map(formatProject);+ return `${formattedProjects.length} available documentation packages:\n\n${formattedProjects.join("\n")}`;+}