All files / src/clients europeanParliamentClient.ts

90.41% Statements 66/73
78.18% Branches 43/55
100% Functions 48/48
91.66% Lines 66/72

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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802                                                                                                                                                                                                                                                                                                                                          295x 295x 295x 295x 295x 295x 295x 295x 295x                       295x             295x         295x   295x   295x 295x                                                   289x                   5x                                           19x                               6x                     4x                     3x                     3x                     4x                         7x                 4x                                       6x                     8x                     5x                     5x                     16x                     10x               4x                         6x               4x                                                   10x                         8x               4x                                   14x                     4x                                             13x                       4x                       4x                     3x                     4x                       5x               4x               4x               4x               4x               4x                           12x                 4x                     5x                       5x               4x                                               21x                   4x                         4x                   4x                                                   4x   4x 4x             4x     4x 4x       4x      
/**
 * @fileoverview European Parliament API Client – Facade
 *
 * Thin facade that delegates every public method to a bounded-context
 * sub-client while keeping all public method signatures identical.
 * All sub-clients share the same LRU cache and rate-limiter bucket.
 *
 * **Architecture:** Facade pattern over 8 domain sub-clients:
 * MEPClient · PlenaryClient · VotingClient · CommitteeClient ·
 * DocumentClient · LegislativeClient · QuestionClient · VocabularyClient
 *
 * **Intelligence Perspective:** Primary OSINT data collection interface—implements
 * rate-limited, cached access to all EP datasets for intelligence product generation.
 *
 * **Business Perspective:** Core API client powering all data products—reliability,
 * caching, and rate limiting directly impact SLA commitments and customer experience.
 *
 * **Marketing Perspective:** Client performance metrics (cache hit rate, response time)
 * serve as key technical differentiators in developer marketing materials.
 *
 * **ISMS Policies:**
 * - SC-002 (Secure Coding Standards)
 * - PE-001 (Performance Standards)
 * - AU-002 (Audit Logging and Monitoring)
 * - DP-001 (Data Protection and GDPR Compliance)
 *
 * @see https://github.com/Hack23/ISMS-PUBLIC/blob/main/Secure_Development_Policy.md
 * @module clients/europeanParliamentClient
 */
 
import { LRUCache } from 'lru-cache';
import { RateLimiter } from '../utils/rateLimiter.js';
import type {
  MEP,
  MEPDetails,
  PlenarySession,
  VotingRecord,
  LegislativeDocument,
  Committee,
  ParliamentaryQuestion,
  PaginatedResponse,
  Speech,
  Procedure,
  AdoptedText,
  EPEvent,
  MeetingActivity,
  MEPDeclaration,
} from '../types/europeanParliament.js';
 
// Re-export infrastructure types/constants so existing consumers keep working.
export {
  APIError,
  DEFAULT_EP_API_BASE_URL,
  DEFAULT_REQUEST_TIMEOUT_MS,
  DEFAULT_RETRY_ENABLED,
  DEFAULT_MAX_RETRIES,
  DEFAULT_RETRY_BASE_DELAY_MS,
  DEFAULT_RETRY_MAX_DELAY_MS,
  DEFAULT_CACHE_TTL_MS,
  DEFAULT_MAX_CACHE_SIZE,
  DEFAULT_RATE_LIMIT_TOKENS,
  DEFAULT_RATE_LIMIT_INTERVAL,
  DEFAULT_MAX_RESPONSE_BYTES,
} from './ep/baseClient.js';
export type { EPClientConfig } from './ep/baseClient.js';
 
import {
  DEFAULT_EP_API_BASE_URL,
  DEFAULT_REQUEST_TIMEOUT_MS,
  DEFAULT_RETRY_ENABLED,
  DEFAULT_MAX_RETRIES,
  DEFAULT_CACHE_TTL_MS,
  DEFAULT_MAX_CACHE_SIZE,
  DEFAULT_RATE_LIMIT_TOKENS,
  DEFAULT_RATE_LIMIT_INTERVAL,
  DEFAULT_MAX_RESPONSE_BYTES,
  validateApiUrl,
  type EPClientConfig,
  type EPSharedResources,
} from './ep/baseClient.js';
export { validateApiUrl };
 
import {
  MEPClient,
  PlenaryClient,
  VotingClient,
  CommitteeClient,
  DocumentClient,
  LegislativeClient,
  QuestionClient,
  VocabularyClient,
} from './ep/index.js';
 
// ─── Facade ───────────────────────────────────────────────────────────────────
 
/**
 * European Parliament API Client.
 *
 * Provides type-safe, high-performance access to the European Parliament Open
 * Data Portal with comprehensive caching, rate limiting, and GDPR-compliant
 * audit logging.
 *
 * **Architecture Note:** This class is a thin facade; all logic lives in the
 * bounded-context sub-clients under `src/clients/ep/`.  The facade owns the
 * shared LRU cache and rate-limiter and passes them to every sub-client so
 * they all operate within the same resource budget.
 *
 * **Performance Targets:**
 * - P50 latency: <100ms (cached responses)
 * - P95 latency: <200ms (cached responses)
 * - P99 latency: <2000ms (uncached API calls)
 *
 * **Features:**
 * - LRU caching with 15-minute TTL (500 entry max)
 * - Token bucket rate limiting (100 requests/minute)
 * - GDPR Article 30 compliant audit logging
 * - Automatic JSON-LD to internal format transformation
 * - Type-safe API with branded types
 *
 * **ISMS Policy Compliance:**
 * - SC-002: Secure coding with input validation and error handling
 * - PE-001: Performance monitoring and optimization (<200ms P95)
 * - AU-002: Comprehensive audit logging for GDPR compliance
 * - DP-001: Data protection and privacy by design
 *
 * @example
 * ```typescript
 * const client = new EuropeanParliamentClient();
 * const meps = await client.getMEPs({ country: 'SE', limit: 20 });
 * console.log(`Found ${meps.total} Swedish MEPs`);
 * ```
 *
 * @security
 * - All personal data access is audit logged per GDPR Article 30
 * - Rate limiting prevents API abuse and DoS attacks
 * - No credentials stored (EP API is public)
 * - Cache entries sanitized to prevent injection attacks
 * - TLS 1.3 enforced for all API communications
 *
 * @class
 * @public
 * @see https://data.europarl.europa.eu/api/v2/ - EP Open Data Portal API
 * @see https://github.com/Hack23/ISMS-PUBLIC/blob/main/Secure_Development_Policy.md
 * @see https://gdpr-info.eu/art-30-gdpr/ - GDPR Article 30
 */
export class EuropeanParliamentClient {
  // ─── Sub-clients (package-private for testability) ──────────────────────
  private readonly mepClient: MEPClient;
  private readonly plenaryClient: PlenaryClient;
  private readonly votingClient: VotingClient;
  private readonly committeeClient: CommitteeClient;
  private readonly documentClient: DocumentClient;
  private readonly legislativeClient: LegislativeClient;
  private readonly questionClient: QuestionClient;
  private readonly vocabularyClient: VocabularyClient;
 
  /**
   * Creates a new European Parliament API client.
   *
   * Builds shared LRU cache and rate-limiter, then wires all sub-clients
   * to use them so the whole facade operates within one resource budget.
   *
   * @param config - Optional client configuration
   */
  constructor(config: EPClientConfig = {}) {
    const shared = EuropeanParliamentClient.buildShared(config);
    this.mepClient = new MEPClient({}, shared);
    this.plenaryClient = new PlenaryClient({}, shared);
    this.votingClient = new VotingClient({}, shared);
    this.committeeClient = new CommitteeClient({}, shared);
    this.documentClient = new DocumentClient({}, shared);
    this.legislativeClient = new LegislativeClient({}, shared);
    this.questionClient = new QuestionClient({}, shared);
    this.vocabularyClient = new VocabularyClient({}, shared);
  }
 
  /**
   * Builds the shared resources object that is passed to all sub-clients.
   * Extracted to keep the constructor complexity within ESLint limits.
   *
   * @param config - Client configuration options
   * @returns Shared resources for all EP sub-clients
   * @private
   */
  private static buildShared(config: EPClientConfig): EPSharedResources {
    const sharedCache = new LRUCache<string, Record<string, unknown>>({
      max: config.maxCacheSize ?? DEFAULT_MAX_CACHE_SIZE,
      ttl: config.cacheTTL ?? DEFAULT_CACHE_TTL_MS,
      allowStale: false,
      updateAgeOnGet: true,
    });
    const sharedRateLimiter =
      config.rateLimiter ??
      new RateLimiter({
        tokensPerInterval: DEFAULT_RATE_LIMIT_TOKENS,
        interval: DEFAULT_RATE_LIMIT_INTERVAL,
      });
    const rawBaseURL = config.baseURL ?? DEFAULT_EP_API_BASE_URL;
    // Validate baseURL to prevent SSRF (same rules as EP_API_URL env var)
    validateApiUrl(rawBaseURL, 'config.baseURL');
    // Ensure baseURL always ends with '/' so relative endpoints resolve correctly
    const baseURL = rawBaseURL.endsWith('/') ? rawBaseURL : `${rawBaseURL}/`;
    return {
      cache: sharedCache,
      rateLimiter: sharedRateLimiter,
      baseURL,
      timeoutMs: config.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
      enableRetry: config.enableRetry ?? DEFAULT_RETRY_ENABLED,
      maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,
      maxResponseBytes: config.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES,
      cacheCounters: { hits: 0, misses: 0 },
    };
  }
 
  // ─── Cache management ────────────────────────────────────────────────────
 
  /**
   * Clears all entries from the LRU cache.
   *
   * @example
   * ```typescript
   * client.clearCache();
   * const freshData = await client.getMEPs({ country: 'SE' });
   * ```
   * @public
   */
  clearCache(): void {
    // Delegating to any sub-client clears the shared cache
    this.mepClient.clearCache();
  }
 
  /**
   * Returns cache statistics for monitoring and debugging.
   *
   * @returns `{ size, maxSize, hitRate, hits, misses }`
   * @public
   */
  getCacheStats(): { size: number; maxSize: number; hitRate: number; hits: number; misses: number } {
    return this.mepClient.getCacheStats();
  }
 
  // ─── MEP endpoints ────────────────────────────────────────────────────────
 
  /**
   * Retrieves Members of the European Parliament with filtering and pagination.
   *
   * @param params - country, group, committee, active, limit, offset
   * @returns Paginated MEP list
   * @security Personal data access logged per GDPR Article 30
   * @performance Cached: <100ms P50, <200ms P95. Uncached: <2s P99
   * @see https://data.europarl.europa.eu/api/v2/meps
   */
  async getMEPs(params: {
    country?: string;
    group?: string;
    committee?: string;
    active?: boolean;
    limit?: number;
    offset?: number;
  }): Promise<PaginatedResponse<MEP>> {
    return this.mepClient.getMEPs(params);
  }
 
  /**
   * Retrieves detailed information about a specific MEP.
   *
   * Supports numeric ID ("124936"), person URI ("person/124936"),
   * or MEP-prefixed ID ("MEP-124936").
   *
   * @param id - MEP identifier in any supported format
   * @returns Detailed MEP information
   * @security Personal data access logged per GDPR Article 30
   * @performance Cached: <100ms P50, <200ms P95. Uncached: <2s P99
   * @see https://data.europarl.europa.eu/api/v2/meps/{id}
   */
  async getMEPDetails(id: string): Promise<MEPDetails> {
    return this.mepClient.getMEPDetails(id);
  }
 
  /**
   * Returns all currently active MEPs for today's date.
   * **EP API Endpoint:** `GET /meps/show-current`
   */
  async getCurrentMEPs(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<MEP>> {
    return this.mepClient.getCurrentMEPs(params);
  }
 
  /**
   * Returns all incoming MEPs for the current parliamentary term.
   * **EP API Endpoint:** `GET /meps/show-incoming`
   */
  async getIncomingMEPs(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<MEP>> {
    return this.mepClient.getIncomingMEPs(params);
  }
 
  /**
   * Returns all outgoing MEPs for the current parliamentary term.
   * **EP API Endpoint:** `GET /meps/show-outgoing`
   */
  async getOutgoingMEPs(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<MEP>> {
    return this.mepClient.getOutgoingMEPs(params);
  }
 
  /**
   * Returns homonym MEPs for the current parliamentary term.
   * **EP API Endpoint:** `GET /meps/show-homonyms`
   */
  async getHomonymMEPs(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<MEP>> {
    return this.mepClient.getHomonymMEPs(params);
  }
 
  /**
   * Returns MEP declarations of financial interests.
   * **EP API Endpoint:** `GET /meps-declarations`
   * @gdpr Declarations contain personal financial data – access is audit-logged
   */
  async getMEPDeclarations(params: {
    year?: number;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<MEPDeclaration>> {
    return this.mepClient.getMEPDeclarations(params);
  }
 
  /**
   * Returns a single MEP declaration by document ID.
   * **EP API Endpoint:** `GET /meps-declarations/{doc-id}`
   * @gdpr Declarations contain personal financial data – access is audit-logged
   */
  async getMEPDeclarationById(docId: string): Promise<MEPDeclaration> {
    return this.mepClient.getMEPDeclarationById(docId);
  }
 
  // ─── Plenary / meeting endpoints ─────────────────────────────────────────
 
  /**
   * Retrieves plenary sessions with date and location filtering.
   *
   * @param params - dateFrom, dateTo, location, limit, offset
   * @returns Paginated plenary session list
   * @performance Cached: <100ms P50, <200ms P95. Uncached: <2s P99
   * @see https://data.europarl.europa.eu/api/v2/meetings
   */
  async getPlenarySessions(params: {
    dateFrom?: string;
    dateTo?: string;
    location?: string;
    limit?: number;
    offset?: number;
  }): Promise<PaginatedResponse<PlenarySession>> {
    return this.plenaryClient.getPlenarySessions(params);
  }
 
  /**
   * Returns activities linked to a specific meeting (plenary sitting).
   * **EP API Endpoint:** `GET /meetings/{sitting-id}/activities`
   */
  async getMeetingActivities(
    sittingId: string,
    params: { limit?: number; offset?: number } = {}
  ): Promise<PaginatedResponse<MeetingActivity>> {
    return this.plenaryClient.getMeetingActivities(sittingId, params);
  }
 
  /**
   * Returns decisions made in a specific meeting (plenary sitting).
   * **EP API Endpoint:** `GET /meetings/{sitting-id}/decisions`
   */
  async getMeetingDecisions(
    sittingId: string,
    params: { limit?: number; offset?: number } = {}
  ): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.plenaryClient.getMeetingDecisions(sittingId, params);
  }
 
  /**
   * Returns foreseen activities linked to a specific meeting.
   * **EP API Endpoint:** `GET /meetings/{sitting-id}/foreseen-activities`
   */
  async getMeetingForeseenActivities(
    sittingId: string,
    params: { limit?: number; offset?: number } = {}
  ): Promise<PaginatedResponse<MeetingActivity>> {
    return this.plenaryClient.getMeetingForeseenActivities(sittingId, params);
  }
 
  /**
   * Returns plenary session documents for a specific meeting.
   * **EP API Endpoint:** `GET /meetings/{sitting-id}/plenary-session-documents`
   */
  async getMeetingPlenarySessionDocuments(
    sittingId: string,
    params: { limit?: number; offset?: number } = {}
  ): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.plenaryClient.getMeetingPlenarySessionDocuments(sittingId, params);
  }
 
  /**
   * Returns plenary session document items for a specific meeting.
   * **EP API Endpoint:** `GET /meetings/{sitting-id}/plenary-session-document-items`
   */
  async getMeetingPlenarySessionDocumentItems(
    sittingId: string,
    params: { limit?: number; offset?: number } = {}
  ): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.plenaryClient.getMeetingPlenarySessionDocumentItems(sittingId, params);
  }
 
  /**
   * Returns a single EP meeting by ID.
   * **EP API Endpoint:** `GET /meetings/{event-id}`
   */
  async getMeetingById(eventId: string): Promise<PlenarySession> {
    return this.plenaryClient.getMeetingById(eventId);
  }
 
  /**
   * Returns EP events (hearings, conferences, etc.).
   * **EP API Endpoint:** `GET /events`
   */
  async getEvents(params: {
    dateFrom?: string;
    dateTo?: string;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<EPEvent>> {
    return this.plenaryClient.getEvents(params);
  }
 
  /**
   * Returns a single EP event by ID.
   * **EP API Endpoint:** `GET /events/{event-id}`
   */
  async getEventById(eventId: string): Promise<EPEvent> {
    return this.plenaryClient.getEventById(eventId);
  }
 
  // ─── Voting / speech endpoints ────────────────────────────────────────────
 
  /**
   * Retrieves voting records with filtering by session, topic, and date.
   *
   * @param params - sessionId, topic, dateFrom, dateTo, limit, offset
   * @param params.mepId - **Deprecated / ignored.** The EP API returns only aggregate
   *   vote counts; per-MEP vote positions are not available from this endpoint.
   * @returns Paginated voting records list
   * @security Audit logged per GDPR Article 30
   * @performance Cached: <100ms P50, <200ms P95. Uncached: <2s P99
   * @see https://data.europarl.europa.eu/api/v2/meetings/{sitting-id}/vote-results
   */
  async getVotingRecords(params: {
    sessionId?: string;
    /** @deprecated Ignored; this endpoint only returns aggregate vote counts, not per-MEP positions. */
    mepId?: string;
    topic?: string;
    dateFrom?: string;
    dateTo?: string;
    limit?: number;
    offset?: number;
  }): Promise<PaginatedResponse<VotingRecord>> {
    return this.votingClient.getVotingRecords(params);
  }
 
  /**
   * Returns plenary speeches.
   * **EP API Endpoint:** `GET /speeches`
   */
  async getSpeeches(params: {
    dateFrom?: string;
    dateTo?: string;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<Speech>> {
    return this.votingClient.getSpeeches(params);
  }
 
  /**
   * Returns a single speech by ID.
   * **EP API Endpoint:** `GET /speeches/{speech-id}`
   */
  async getSpeechById(speechId: string): Promise<Speech> {
    return this.votingClient.getSpeechById(speechId);
  }
 
  // ─── Committee endpoints ──────────────────────────────────────────────────
 
  /**
   * Retrieves committee (corporate body) information by ID or abbreviation.
   *
   * @param params - id or abbreviation
   * @returns Committee information
   * @security Audit logged per GDPR Article 30
   * @performance Cached: <100ms P50, <200ms P95. Uncached: <2s P99
   * @see https://data.europarl.europa.eu/api/v2/corporate-bodies
   */
  async getCommitteeInfo(params: {
    id?: string;
    abbreviation?: string;
  }): Promise<Committee> {
    return this.committeeClient.getCommitteeInfo(params);
  }
 
  /**
   * Returns the list of all current EP Corporate Bodies for today's date.
   * **EP API Endpoint:** `GET /corporate-bodies/show-current`
   */
  async getCurrentCorporateBodies(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<Committee>> {
    return this.committeeClient.getCurrentCorporateBodies(params);
  }
 
  // ─── Document endpoints ───────────────────────────────────────────────────
 
  /**
   * Searches legislative documents by keyword, type, date, and committee.
   *
   * @param params - keyword, documentType, dateFrom, dateTo, committee, limit, offset
   * @returns Paginated legislative documents list
   * @security Audit logged per GDPR Article 30
   * @performance Cached: <100ms P50, <200ms P95. Uncached: <2s P99
   * @see https://data.europarl.europa.eu/api/v2/documents
   */
  async searchDocuments(params: {
    keyword: string;
    documentType?: string;
    dateFrom?: string;
    dateTo?: string;
    committee?: string;
    limit?: number;
    offset?: number;
  }): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.documentClient.searchDocuments(params);
  }
 
  /**
   * Returns plenary documents.
   * **EP API Endpoint:** `GET /plenary-documents`
   */
  async getPlenaryDocuments(params: {
    year?: number;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.documentClient.getPlenaryDocuments(params);
  }
 
  /**
   * Returns committee documents.
   * **EP API Endpoint:** `GET /committee-documents`
   */
  async getCommitteeDocuments(params: {
    year?: number;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.documentClient.getCommitteeDocuments(params);
  }
 
  /**
   * Returns plenary session documents.
   * **EP API Endpoint:** `GET /plenary-session-documents`
   */
  async getPlenarySessionDocuments(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.documentClient.getPlenarySessionDocuments(params);
  }
 
  /**
   * Returns all Plenary Session Document Items.
   * **EP API Endpoint:** `GET /plenary-session-documents-items`
   */
  async getPlenarySessionDocumentItems(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.documentClient.getPlenarySessionDocumentItems(params);
  }
 
  /**
   * Returns all External Documents.
   * **EP API Endpoint:** `GET /external-documents`
   */
  async getExternalDocuments(params: {
    year?: number;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<LegislativeDocument>> {
    return this.documentClient.getExternalDocuments(params);
  }
 
  /**
   * Returns a single document by ID.
   * **EP API Endpoint:** `GET /documents/{doc-id}`
   */
  async getDocumentById(docId: string): Promise<LegislativeDocument> {
    return this.documentClient.getDocumentById(docId);
  }
 
  /**
   * Returns a single plenary document by ID.
   * **EP API Endpoint:** `GET /plenary-documents/{doc-id}`
   */
  async getPlenaryDocumentById(docId: string): Promise<LegislativeDocument> {
    return this.documentClient.getPlenaryDocumentById(docId);
  }
 
  /**
   * Returns a single plenary session document by ID.
   * **EP API Endpoint:** `GET /plenary-session-documents/{doc-id}`
   */
  async getPlenarySessionDocumentById(docId: string): Promise<LegislativeDocument> {
    return this.documentClient.getPlenarySessionDocumentById(docId);
  }
 
  /**
   * Returns a single committee document by ID.
   * **EP API Endpoint:** `GET /committee-documents/{doc-id}`
   */
  async getCommitteeDocumentById(docId: string): Promise<LegislativeDocument> {
    return this.documentClient.getCommitteeDocumentById(docId);
  }
 
  /**
   * Returns a single external document by ID.
   * **EP API Endpoint:** `GET /external-documents/{doc-id}`
   */
  async getExternalDocumentById(docId: string): Promise<LegislativeDocument> {
    return this.documentClient.getExternalDocumentById(docId);
  }
 
  // ─── Legislative endpoints ────────────────────────────────────────────────
 
  /**
   * Returns legislative procedures.
   * **EP API Endpoint:** `GET /procedures`
   */
  async getProcedures(params: {
    year?: number;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<Procedure>> {
    return this.legislativeClient.getProcedures(params);
  }
 
  /**
   * Returns a single procedure by ID.
   * **EP API Endpoint:** `GET /procedures/{process-id}`
   * @throws {APIError} When the procedure is not found (404)
   */
  async getProcedureById(processId: string): Promise<Procedure> {
    return this.legislativeClient.getProcedureById(processId);
  }
 
  /**
   * Returns events linked to a procedure.
   * **EP API Endpoint:** `GET /procedures/{process-id}/events`
   */
  async getProcedureEvents(
    processId: string,
    params: { limit?: number; offset?: number } = {}
  ): Promise<PaginatedResponse<EPEvent>> {
    return this.legislativeClient.getProcedureEvents(processId, params);
  }
 
  /**
   * Returns adopted texts.
   * **EP API Endpoint:** `GET /adopted-texts`
   */
  async getAdoptedTexts(params: {
    year?: number;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<AdoptedText>> {
    return this.legislativeClient.getAdoptedTexts(params);
  }
 
  /**
   * Returns a single adopted text by document ID.
   * **EP API Endpoint:** `GET /adopted-texts/{doc-id}`
   */
  async getAdoptedTextById(docId: string): Promise<AdoptedText> {
    return this.legislativeClient.getAdoptedTextById(docId);
  }
 
  // ─── Parliamentary question endpoints ────────────────────────────────────
 
  /**
   * Retrieves parliamentary questions with filtering by type, author, and status.
   *
   * @param params - type, author, topic, status, dateFrom, dateTo, limit, offset
   * @returns Paginated parliamentary questions list
   * @security Audit logged per GDPR Article 30
   * @performance Cached: <100ms P50, <200ms P95. Uncached: <2s P99
   * @see https://data.europarl.europa.eu/api/v2/parliamentary-questions
   */
  async getParliamentaryQuestions(params: {
    type?: 'WRITTEN' | 'ORAL';
    author?: string;
    topic?: string;
    status?: 'PENDING' | 'ANSWERED';
    dateFrom?: string;
    dateTo?: string;
    limit?: number;
    offset?: number;
  }): Promise<PaginatedResponse<ParliamentaryQuestion>> {
    return this.questionClient.getParliamentaryQuestions(params);
  }
 
  /**
   * Returns a single parliamentary question by document ID.
   * **EP API Endpoint:** `GET /parliamentary-questions/{doc-id}`
   */
  async getParliamentaryQuestionById(
    docId: string
  ): Promise<ParliamentaryQuestion> {
    return this.questionClient.getParliamentaryQuestionById(docId);
  }
 
  // ─── Vocabulary endpoints ─────────────────────────────────────────────────
 
  /**
   * Returns EP controlled vocabularies.
   * **EP API Endpoint:** `GET /controlled-vocabularies`
   */
  async getControlledVocabularies(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<Record<string, unknown>>> {
    return this.vocabularyClient.getControlledVocabularies(params);
  }
 
  /**
   * Returns a single EP Controlled Vocabulary by ID.
   * **EP API Endpoint:** `GET /controlled-vocabularies/{voc-id}`
   */
  async getControlledVocabularyById(
    vocId: string
  ): Promise<Record<string, unknown>> {
    return this.vocabularyClient.getControlledVocabularyById(vocId);
  }
}
 
// ─── Singleton ────────────────────────────────────────────────────────────────
 
/**
 * Singleton instance of European Parliament API client for global use.
 *
 * Pre-configured with defaults (15 min cache, 100 req/min rate limit).
 * Recommended for most use cases to share cache and rate limiter across
 * the application.
 *
 * **Environment Variables:**
 * - `EP_API_URL`: Override base API URL
 * - `EP_REQUEST_TIMEOUT_MS`: Override request timeout in milliseconds (default: 10000)
 *
 * @example
 * ```typescript
 * import { epClient } from './clients/europeanParliamentClient.js';
 * const meps = await epClient.getMEPs({ country: 'SE' });
 * ```
 *
 * @public
 * @see {@link EuropeanParliamentClient}
 */
export const epClient = new EuropeanParliamentClient({
  baseURL: ((): string => {
    const raw = process.env['EP_API_URL'];
    Iif (typeof raw === 'string') {
      const trimmed = raw.trim();
      if (trimmed.length > 0) {
        const withSlash = trimmed.endsWith('/') ? trimmed : `${trimmed}/`;
        return validateApiUrl(withSlash);
      }
    }
    return DEFAULT_EP_API_BASE_URL;
  })(),
  timeoutMs: ((): number => {
    const raw = process.env['EP_REQUEST_TIMEOUT_MS'];
    Iif (typeof raw === 'string' && raw.trim().length > 0) {
      const parsed = Number.parseInt(raw, 10);
      if (Number.isFinite(parsed) && parsed > 0) return parsed;
    }
    return DEFAULT_REQUEST_TIMEOUT_MS;
  })(),
});