UNPKG

whodis-mcp-server

Version:

Whodis MCP Server for checking the availability of domain names using WHOIS lookups.

63 lines (62 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const logger_util_js_1 = require("../utils/logger.util.js"); const error_util_js_1 = require("../utils/error.util.js"); const transport_util_js_1 = require("../utils/transport.util.js"); // Create a contextualized logger for this file const serviceLogger = logger_util_js_1.Logger.forContext('services/vendor.ip-api.com.service.ts'); // Log service initialization serviceLogger.debug('IP API service initialized'); /** * @namespace VendorIpApiService * @description Service layer for interacting directly with the ip-api.com vendor API. * Responsible for constructing API requests based on provided parameters * and handling the raw response from the `fetchIpApi` utility. */ /** * @function get * @description Fetches details for a specific IP address or the current device's IP from ip-api.com. * It uses the `fetchIpApi` utility and handles the specific success/failure status returned by ip-api.com. * @memberof VendorIpApiService * @param {string} [ipAddress] - Optional IP address to look up. If omitted, fetches details for the current device's public IP. * @param {IPApiRequestOptions} [options={}] - Optional request options for the ip-api.com service, such as `useHttps`, `fields`, and `lang`. * @returns {Promise<IPDetail>} A promise that resolves to the detailed IP information if the API call is successful. * @throws {McpError} Throws an `McpError` (specifically `ApiError` or `UnexpectedError`) if: * - The `fetchIpApi` call fails (network error, non-2xx response). * - The ip-api.com response status is not 'success'. * - An unexpected error occurs during processing. * @example * // Get basic details for 8.8.8.8 * const details = await get('8.8.8.8'); * // Get extended details using HTTPS * const extendedDetails = await get('1.1.1.1', { useHttps: true, fields: [...] }); */ async function get(ipAddress, options = {}) { const methodLogger = logger_util_js_1.Logger.forContext('services/vendor.ip-api.com.service.ts', 'get'); methodLogger.debug(`Calling IP API for IP: ${ipAddress || 'current'}`); try { // Use the centralized fetchIpApi utility const data = await (0, transport_util_js_1.fetchIpApi)(ipAddress || '', { useHttps: options.useHttps, fields: options.fields, lang: options.lang, }); // Handle API-level success/failure specific to ip-api.com if (data.status !== 'success') { throw (0, error_util_js_1.createApiError)(`IP API error: ${data.message || 'Unknown error'}`); } methodLogger.debug(`Received successful data from IP API`); return data; // Already validated as IPDetail structure implicitly by status check } catch (error) { // Log the error caught at the service level methodLogger.error(`Service error fetching IP data`, error); // Rethrow McpErrors (could be from fetchApi or the status check) if (error instanceof error_util_js_1.McpError) { throw error; } // Wrap any other unexpected errors throw (0, error_util_js_1.createUnexpectedError)('Unexpected service error while fetching IP data', error); } } exports.default = { get };