All files / scripts/parliamentary-data/persistence imf.ts

100% Statements 8/8
85.71% Branches 6/7
100% Functions 1/1
100% Lines 8/8

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                                                                                        5x 5x 5x   5x 5x           5x 5x                         5x    
/**
 * @module parliamentary-data/persistence/imf
 * @description IMF (Datamapper / SDMX 3.0) response persistence.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import fs from 'node:fs';
import path from 'node:path';
 
import { sanitizeDokId } from './shared/sanitize.js';
import { DATA_ROOT, ensureDir } from './shared/meta-sidecar.js';
 
/**
 * Persist IMF API response data (Datamapper JSON or SDMX 3.0).
 *
 * Stored under `analysis/data/imf/{indicator}/{country}.json`, mirroring the
 * World Bank persistence layout. Introduced by the hybrid IMF integration
 * (Economic Data Contract v2.0, 2026-04). Supports all IMF providers:
 * Datamapper (WEO) and SDMX 3.0 (IFS/BOP/FM/GFS/DOTS/MFS), all accessed
 * through the pure-TypeScript client `scripts/imf-client.ts` (there is no
 * Python MCP / `uvx` runtime; agentic workflows invoke the `tsx
 * scripts/imf-fetch.ts` CLI via the `bash` tool).
 *
 * @param indicator  - IMF indicator code (e.g. 'NGDP_RPCH', 'GGXWDG_NGDP',
 *                     'PCPIPCH', 'LUR', 'FM_EXP_G01_GDP_PT').
 * @param country    - IMF AREA code (ISO3 for Datamapper; varies by dataset
 *                     per `scripts/imf-codes.ts`).
 * @param response   - Raw IMF response payload.
 * @param options    - Optional provenance (`database`, `projectionVintage`,
 *                     `dataRoot` override for testing).
 * @returns Absolute path to the persisted data file.
 */
export function persistIMFData(
  indicator: string,
  country: string,
  response: unknown,
  options: {
    database?: string;
    projectionVintage?: string;
    dataRoot?: string;
  } = {},
): string {
  const dataRoot = options.dataRoot ?? DATA_ROOT;
  const dir = path.join(dataRoot, 'imf', sanitizeDokId(indicator));
  ensureDir(dir);
 
  const filename = `${sanitizeDokId(country)}.json`;
  fs.writeFileSync(
    path.join(dir, filename),
    JSON.stringify(response, null, 2),
    'utf8',
  );
 
  const metaFilename = `${sanitizeDokId(country)}.meta.json`;
  fs.writeFileSync(
    path.join(dir, metaFilename),
    JSON.stringify({
      fetchedAt: new Date().toISOString(),
      mcpTool: 'imf-ts-client',
      indicator,
      country,
      ...(options.database ? { database: options.database } : {}),
      ...(options.projectionVintage ? { projectionVintage: options.projectionVintage } : {}),
    }, null, 2),
    'utf8',
  );
 
  return path.join(dir, filename);
}