country.services.js•1.86 kB
import { ERROR_MESSAGES } from "../shared/constants/index.js";
/**
* Fetches and formats general information about Colombia from the API Colombia endpoint.
* @returns {Promise<ApiColombiaResponse>} A formatted text response containing country details
* or an error message if the request fails.
*/
export async function getCountry() {
try {
const apiUrl = process.env.API_COLOMBIA_URL || 'https://api-colombia.com/api/';
const response = await fetch(`${apiUrl}v1/Country/Colombia`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const countryData = await response.json();
const countryInfo = `
Country: ${countryData.name}
Capital: ${countryData.stateCapital}
Population: ${countryData.population.toLocaleString()}
Area: ${countryData.surface.toLocaleString()} km²
Region: ${countryData.region} - ${countryData.subRegion}
Languages: ${countryData.languages.join(', ')}
Currency: ${countryData.currency} (${countryData.currencyCode} ${countryData.currencySymbol})
Time Zone: ${countryData.timeZone}
ISO Code: ${countryData.isoCode}
Internet Domain: ${countryData.internetDomain}
Phone Prefix: ${countryData.phonePrefix}
Borders: ${countryData.borders.join(', ')}
Description:
${countryData.description}
Flag URLs:
${countryData.flags.join('\n')}
`.trim();
return {
content: [{
type: 'text',
text: countryInfo
}]
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : ERROR_MESSAGES.UNKNOWN_ERROR;
return {
content: [{
type: 'text',
text: `${ERROR_MESSAGES.FETCH_ERROR}: ${errorMessage}`
}]
};
}
}