All files / scripts/imf/transport path-normaliser.ts

100% Statements 10/10
100% Branches 10/10
100% Functions 1/1
100% Lines 10/10

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                                        17x 1x   16x 1x   15x 15x 15x 2x   13x 13x    
/**
 * @module imf/transport/path-normaliser
 * @description SDMX path normalisation for the IMF SDMX 3.0 gateway.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
/**
 * Translate a comma-form SDMX dataflow reference
 * (`/data/AGENCY,FLOW,VERSION/key` — the SDMX 2.x URN style still used
 * by all of our docs / CLI / `weoSdmxPath` for human readability) into
 * the slash form (`/data/dataflow/AGENCY/FLOW/VERSION/key`) that the
 * IMF SDMX 3.0 REST gateway requires. The 3.0 endpoint silently 404s
 * the comma form. Verified live against `api.imf.org` 2026-05-10.
 *
 * SDMX 3.0 is the only IMF SDMX surface this client targets; legacy
 * SDMX surfaces are not configured anywhere in the repo.
 */
export function normalizeSdmxPathForBase(baseURL: string, pathWithQuery: string): string {
  if (!baseURL.includes('/sdmx/3.0')) {
    return pathWithQuery;
  }
  if (pathWithQuery.includes('/data/dataflow/')) {
    return pathWithQuery;
  }
  const re = /^(\/?data)\/([^/,?#]+),([^/,?#]+),([^/,?#]+)(\/[^?#]*)?(\?.*)?$/;
  const m = re.exec(pathWithQuery);
  if (!m) {
    return pathWithQuery;
  }
  const [, dataPrefix, agency, flow, version, keyPart, query] = m;
  return `${dataPrefix}/dataflow/${agency}/${flow}/${version}${keyPart ?? ''}${query ?? ''}`;
}