@smartbear/mcp
Version:
MCP server for interacting SmartBear Products
45 lines (44 loc) • 1.65 kB
JavaScript
import { MCP_SERVER_NAME, MCP_SERVER_VERSION } from "../../../common/info.js";
import { QMETRY_DEFAULTS } from "../../config/constants.js";
import { handleQMetryApiError, handleQMetryFetchError, } from "./error-handler.js";
/**
* QMetry API request function with centralized error handling.
*
* Handles authentication, project access, CORS, and generic API errors with
* user-friendly messages and troubleshooting guidance.
*
* @param options Request configuration including authentication token
* @returns Parsed JSON response from QMetry API
* @throws User-friendly errors with detailed troubleshooting steps for various scenarios
*/
export async function qmetryRequest({ method = "GET", path, token, project, baseUrl, body, }) {
const url = `${baseUrl}${path}`;
const headers = {
apikey: token,
project: project || QMETRY_DEFAULTS.PROJECT_KEY,
"User-Agent": `${MCP_SERVER_NAME}/${MCP_SERVER_VERSION}`,
};
if (body) {
headers["Content-Type"] = "application/json";
}
const init = {
method,
headers,
};
if (body && ["POST", "PUT", "PATCH"].includes(method)) {
init.body = JSON.stringify(body);
}
let res;
try {
res = await fetch(url, init);
}
catch (error) {
// Handle fetch errors (CORS, network issues, SSL certificate issues, etc.)
handleQMetryFetchError(error instanceof Error ? error : new Error(String(error)), baseUrl, project, path);
}
if (!res.ok) {
// Use centralized error handling
await handleQMetryApiError(res, baseUrl, project, path);
}
return (await res.json());
}