Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 3x 3x 3x 3x 3x 33x 33x 33x 15x 15x 12x 2x 10x 12x 11x 5x 5x 5x 5x 5x 5x 6x 5x 2x 2x 4x 4x 3x 1x 2x 16x 16x 16x 16x 13x 1x 12x 4x 1x 1x 1x 3x 16x 3x 3x 1x 3x | /**
* @module WorldBank/Client
* @description TypeScript REST client for the World Bank Open Data API.
* Provides direct HTTP access to World Bank economic indicators for Sweden
* and Nordic comparison countries, used to enrich political intelligence
* with economic context.
*
* Based on the World Bank MCP Server pattern (https://github.com/anshumax/world_bank_mcp_server)
* but implemented as a native TypeScript HTTP client for build-time data fetching.
*
* @author Hack23 AB
* @license Apache-2.0
* @see https://datahelpdesk.worldbank.org/knowledgebase/articles/889392-about-the-indicators-api-documentation
*/
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/** A single World Bank indicator data point */
export interface WorldBankDataPoint {
readonly countryId: string;
readonly countryName: string;
readonly indicatorId: string;
readonly indicatorName: string;
readonly date: string;
readonly value: number;
}
/** Metadata about a World Bank indicator */
export interface WorldBankIndicator {
readonly id: string;
readonly name: string;
readonly description: string;
readonly unit: string;
}
/** Response from the World Bank API (paginated JSON) */
interface WorldBankApiResponse {
readonly page: number;
readonly pages: number;
readonly per_page: string;
readonly total: number;
}
/** Raw indicator value from the API */
interface RawIndicatorValue {
indicator?: { id?: string; value?: string };
country?: { id?: string; value?: string };
date?: string;
value?: number | null;
}
/** Client configuration */
export interface WorldBankClientConfig {
readonly baseURL?: string;
readonly timeout?: number;
readonly maxRetries?: number;
}
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const DEFAULT_BASE_URL = 'https://api.worldbank.org/v2';
const DEFAULT_TIMEOUT = 15_000;
const DEFAULT_MAX_RETRIES = 2;
/** ISO 3166-1 alpha-3 codes for Sweden and comparison countries */
export const COUNTRY_CODES = {
sweden: 'SWE',
denmark: 'DNK',
norway: 'NOR',
finland: 'FIN',
germany: 'DEU',
eu: 'EUU',
} as const;
/**
* Key World Bank indicator IDs relevant to Swedish political intelligence.
* These indicators provide economic context for policy analysis.
*/
export const INDICATOR_IDS = {
/** GDP growth (annual %) */
gdpGrowth: 'NY.GDP.MKTP.KD.ZG',
/** Unemployment, total (% of total labor force) */
unemployment: 'SL.UEM.TOTL.ZS',
/** Inflation, consumer prices (annual %) */
inflation: 'FP.CPI.TOTL.ZG',
/** Population, total */
population: 'SP.POP.TOTL',
/** Trade (% of GDP) */
tradeGdpPct: 'NE.TRD.GNFS.ZS',
/** Government expenditure (% of GDP) */
govExpenditure: 'GC.XPN.TOTL.GD.ZS',
/** GDP per capita, PPP (current international $) */
gdpPerCapitaPpp: 'NY.GDP.PCAP.PP.CD',
/** Current account balance (% of GDP) */
currentAccountBalance: 'BN.CAB.XOKA.GD.ZS',
/** Military expenditure (% of GDP) */
militaryExpenditure: 'MS.MIL.XPND.GD.ZS',
/** CO2 emissions (metric tons per capita) */
co2Emissions: 'EN.ATM.CO2E.PC',
/** GINI index */
giniIndex: 'SI.POV.GINI',
/** Research and development expenditure (% of GDP) */
rdExpenditure: 'GB.XPD.RSDV.GD.ZS',
} as const;
// ---------------------------------------------------------------------------
// WorldBankClient class
// ---------------------------------------------------------------------------
/**
* HTTP client for the World Bank Open Data API.
* Fetches economic indicator data for Sweden and comparison countries.
*/
export class WorldBankClient {
readonly baseURL: string;
readonly timeout: number;
readonly maxRetries: number;
constructor(config: WorldBankClientConfig = {}) {
this.baseURL = config.baseURL ?? DEFAULT_BASE_URL;
this.timeout = config.timeout ?? DEFAULT_TIMEOUT;
this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
}
/**
* Fetch indicator data for a specific country.
*
* @param countryCode - ISO 3166-1 alpha-3 country code (e.g., 'SWE')
* @param indicatorId - World Bank indicator ID (e.g., 'NY.GDP.MKTP.KD.ZG')
* @param perPage - Maximum number of records to fetch (default: 50)
* @returns Array of data points sorted by date descending
*/
async getIndicator(
countryCode: string,
indicatorId: string,
perPage = 50,
): Promise<WorldBankDataPoint[]> {
const url = `${this.baseURL}/country/${encodeURIComponent(countryCode)}/indicator/${encodeURIComponent(indicatorId)}?format=json&per_page=${perPage}`;
const data = await this.fetchWithRetry(url);
if (!Array.isArray(data) || data.length < 2 || !Array.isArray(data[1])) {
return [];
}
return (data[1] as RawIndicatorValue[])
.filter((item): item is RawIndicatorValue & { value: number } => item.value !== null && item.value !== undefined)
.map((item) => ({
countryId: item.country?.id ?? countryCode,
countryName: item.country?.value ?? countryCode,
indicatorId: item.indicator?.id ?? indicatorId,
indicatorName: item.indicator?.value ?? indicatorId,
date: item.date ?? '',
value: item.value,
}))
.sort((a, b) => {
const yearA = parseInt(a.date, 10);
const yearB = parseInt(b.date, 10);
Iif (isNaN(yearA) && isNaN(yearB)) return 0;
Iif (isNaN(yearA)) return 1;
Iif (isNaN(yearB)) return -1;
return yearB - yearA;
});
}
/**
* Fetch the latest available value for an indicator.
*
* @param countryCode - ISO 3166-1 alpha-3 country code
* @param indicatorId - World Bank indicator ID
* @returns Most recent data point, or null if no data
*/
async getLatestIndicator(
countryCode: string,
indicatorId: string,
): Promise<WorldBankDataPoint | null> {
const results = await this.getIndicator(countryCode, indicatorId, 10);
return results.length > 0 ? results[0] : null;
}
/**
* Compare an indicator across multiple countries.
*
* @param countryCodes - Array of ISO 3166-1 alpha-3 codes
* @param indicatorId - World Bank indicator ID
* @returns Map of country code → latest data point
*/
async compareCountries(
countryCodes: readonly string[],
indicatorId: string,
): Promise<Map<string, WorldBankDataPoint | null>> {
const results = new Map<string, WorldBankDataPoint | null>();
// Fetch sequentially to respect API rate limits
for (const code of countryCodes) {
try {
const latest = await this.getLatestIndicator(code, indicatorId);
results.set(code, latest);
} catch {
results.set(code, null);
}
}
return results;
}
// -----------------------------------------------------------------------
// Internal helpers
// -----------------------------------------------------------------------
private async fetchWithRetry(url: string, attempt = 0): Promise<unknown> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
try {
const response = await fetch(url, {
signal: controller.signal,
headers: { Accept: 'application/json' },
});
if (!response.ok) {
throw new Error(`World Bank API error: ${response.status} ${response.statusText}`);
}
return await response.json();
} catch (error) {
if (attempt < this.maxRetries) {
const delay = 1000 * (attempt + 1);
await new Promise((resolve) => setTimeout(resolve, delay));
return this.fetchWithRetry(url, attempt + 1);
}
throw error;
} finally {
clearTimeout(timeoutId);
}
}
}
// ---------------------------------------------------------------------------
// Singleton
// ---------------------------------------------------------------------------
let defaultWorldBankClient: WorldBankClient | null = null;
/** Get or create the default singleton WorldBankClient */
export function getDefaultWorldBankClient(): WorldBankClient {
if (!defaultWorldBankClient) {
defaultWorldBankClient = new WorldBankClient();
}
return defaultWorldBankClient;
}
|