UNPKG

npmplus-mcp-server

Version:

Production-ready MCP server for intelligent JavaScript package management. Works with Claude, Windsurf, Cursor, VS Code, and any MCP-compatible AI editor.

82 lines 3.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.httpClient = exports.HttpClient = void 0; const undici_1 = require("undici"); const constants_js_1 = require("./constants.js"); const p_limit_1 = __importDefault(require("p-limit")); // Rate limiter - 5 concurrent requests const limit = (0, p_limit_1.default)(5); class HttpClient { baseHeaders; constructor() { this.baseHeaders = { "User-Agent": "mcp-package-manager/1.0.0", "Accept": "application/json" }; } async request(url, options = {}) { return limit(async () => { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), options.timeout || 30000); try { const response = await (0, undici_1.fetch)(url, { method: options.method || "GET", headers: { ...this.baseHeaders, ...options.headers }, body: options.body ? JSON.stringify(options.body) : undefined, signal: controller.signal }); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } const data = await response.json(); return data; } catch (error) { if (error.name === "AbortError") { throw new Error(`Request timeout after ${options.timeout || 30000}ms`); } throw error; } finally { clearTimeout(timeout); } }); } // NPM Registry specific methods async npmRegistry(endpoint) { const url = `${constants_js_1.URLS.NPM_REGISTRY}${endpoint}`; return this.request(url); } async npmApi(endpoint) { const url = `${constants_js_1.URLS.NPM_API}${endpoint}`; return this.request(url); } // Bundle size service requests async bundlephobia(packageSpec) { const url = `${constants_js_1.URLS.BUNDLEPHOBIA_API}/size?package=${encodeURIComponent(packageSpec)}`; return this.request(url); } async packagephobia(packageName) { const url = `${constants_js_1.URLS.BUNDLEPHOBIA_API}?p=${encodeURIComponent(packageName)}`; return this.request(url); } // NPM search method async npmSearch(query, options = {}) { const params = new URLSearchParams({ text: query, size: (options.size || 25).toString(), from: (options.from || 0).toString() }); const url = `${constants_js_1.URLS.NPM_API}/search?${params}`; return this.request(url); } } exports.HttpClient = HttpClient; exports.httpClient = new HttpClient(); //# sourceMappingURL=http-client.js.map