All files / src/mcp/ep-open-data utils.ts

100% Statements 19/19
100% Branches 20/20
100% Functions 3/3
100% Lines 13/13

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                                      11x 8x 7x 2x 3x   1x                   17x 17x                   9x 4x 9x 3x 3x    
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module MCP/ep-open-data/utils
 * @description Internal helper utilities for the EP Open Data Portal client.
 */
 
import type { MCPToolResult } from '../../types/index.js';
import type { EPDecisionRecord } from './types.js';
 
/**
 * Unwrap a multilingual JSON-LD label to a plain string.
 * Prefers the English value; falls back to the first available string value.
 * @param raw - Raw label value from the EP Open Data response.
 * @returns Plain string, or empty string when `raw` is empty/undefined.
 * @internal
 */
export function unwrapLabel(raw: string | Record<string, string> | undefined): string {
  if (!raw) return '';
  if (typeof raw === 'string') return raw;
  if (typeof raw['en'] === 'string') return raw['en'];
  for (const v of Object.values(raw)) {
    if (typeof v === 'string') return v;
  }
  return '';
}
 
/**
 * Wrap a value in the canonical MCP tool-result shape.
 * @param payload - Any JSON-serialisable value to embed.
 * @returns MCP tool result with `content[0].type === 'text'`.
 * @internal
 */
export function wrapAsMCPResult(payload: unknown): MCPToolResult {
  const text = typeof payload === 'string' ? payload : JSON.stringify(payload ?? null);
  return { content: [{ type: 'text', text }] };
}
 
/**
 * Extract an identifier from an EP decision record.
 * @param record - EP decision record from the Open Data Portal.
 * @returns The `identifier` field, or the final path segment of `@id`, or empty string.
 * @internal
 */
export function extractIdentifier(record: EPDecisionRecord): string {
  if (record.identifier) return record.identifier;
  const rawId = record['@id'] ?? '';
  if (!rawId) return '';
  const lastSlash = rawId.lastIndexOf('/');
  return lastSlash >= 0 ? rawId.slice(lastSlash + 1) : rawId;
}