Skip to content

Context7 Platform -- Up-to-date docs for AI agents

refactor(api): make param handling more terse

Kent C. Doddsa83042c6bcb0
diff --git a/src/lib/api.ts b/src/lib/api.ts
index f11d95c7..ddda0115 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -46,20 +46,10 @@ export async function fetchLibraryDocumentation(
basePath = path;
folders = foldersParam;
}
- let contextURL = `${CONTEXT7_BASE_URL}/${basePath}/llms.txt`;
- const params = [];
- if (folders) {
- params.push(`folders=${encodeURIComponent(folders)}`);
- }
- if (tokens) {
- params.push(`tokens=${tokens}`);
- }
- if (topic) {
- params.push(`topic=${encodeURIComponent(topic)}`);
- }
- if (params.length > 0) {
- contextURL += `?${params.join("&")}`;
- }
+ const contextURL = new URL(`${CONTEXT7_BASE_URL}/${basePath}/llms.txt`);
+ if (folders) contextURL.set('folders', folders)
+ if (tokens) contextURL.set('tokens', tokens)
+ if (topic) contextURL.set('topic', topic)
const response = await fetch(contextURL);
if (!response.ok) {