api.ts•1.34 kB
import axios from "axios";
const BASE_URL = "https://brasilapi.com.br/api";
/**
* Helper function to make GET requests to the Brasil API
* @param endpoint API endpoint path
* @param params Query parameters
* @returns API response data
*/
export async function getBrasilApiData(endpoint: string, params: Record<string, any> = {}) {
try {
const url = `${BASE_URL}${endpoint}`;
console.error(`Making request to: ${url}`);
const response = await axios.get(url, { params });
return {
data: response.data,
success: true
};
} catch (error: any) {
console.error(`Error in API request: ${error.message}`);
// Handle API errors in a structured format
if (error.response) {
return {
success: false,
statusCode: error.response.status,
message: error.response.data?.message || error.message,
error: error.response.data
};
}
return {
success: false,
message: error.message,
error
};
}
}
/**
* Helper function to format error responses for tools
* @param message Error message to display
* @returns Formatted error response for MCP
*/
export function formatErrorResponse(message: string) {
return {
content: [{
type: "text" as const,
text: message
}],
isError: true
};
}