MCPcopy
hub / github.com/lissy93/web-check / enrichCountry

Function enrichCountry

api/location.js:125–152  ·  view source on GitHub ↗
(code)

Source from the content-addressed store, hash-verified

123
124// Fetch country-level metadata to fill fields not provided by every geo source
125const enrichCountry = async (code) => {
126 if (!code) return {};
127 try {
128 const data = await getJson(
129 `https://restcountries.com/v3.1/alpha/${code}` +
130 '?fields=tld,languages,currencies,area,population',
131 );
132 const c = Array.isArray(data) ? data[0] : data;
133 if (!c) {
134 log.debug(`restcountries returned no entry for ${code}`);
135 return {};
136 }
137 const languages = c.languages ? Object.values(c.languages).join(', ') : undefined;
138 const currCode = c.currencies ? Object.keys(c.currencies)[0] : undefined;
139 const curr = currCode ? c.currencies[currCode] : null;
140 return {
141 country_tld: c.tld?.[0],
142 languages,
143 currency: currCode,
144 currency_name: curr?.name,
145 country_area: c.area,
146 country_population: c.population,
147 };
148 } catch (error) {
149 log.debug(`restcountries enrichment failed for ${code}`, error.message);
150 return {};
151 }
152};
153
154// Strip empty values so they don't shadow enrichment defaults during merge
155const compact = (o) =>

Callers 1

locationHandlerFunction · 0.85

Calls 1

getJsonFunction · 0.85

Tested by

no test coverage detected