All files / src/clients/ep legislativeClient.ts

97.72% Statements 43/44
86.11% Branches 31/36
100% Functions 12/12
97.36% Lines 37/38

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                                                                          295x           14x       8x       4x                                 12x 12x 12x         12x   12x 11x 12x 12x                                 4x 2x   2x       2x 2x     2x                           5x 2x   3x 5x   5x         3x 5x 5x                           5x 5x 5x         5x   5x 4x 6x 5x               4x 2x       2x      
/**
 * @fileoverview Legislative sub-client for European Parliament API
 *
 * Handles legislative procedures, procedure events, and adopted texts.
 *
 * @module clients/ep/legislativeClient
 */
 
import type {
  Procedure,
  AdoptedText,
  EPEvent,
  PaginatedResponse,
} from '../../types/europeanParliament.js';
import {
  transformProcedure as _transformProcedure,
  transformAdoptedText as _transformAdoptedText,
  transformEvent as _transformEvent,
} from './transformers.js';
import {
  BaseEPClient,
  APIError,
  type EPClientConfig,
  type EPSharedResources,
  type JSONLDResponse,
} from './baseClient.js';
 
// ─── Legislative Client ───────────────────────────────────────────────────────
 
/**
 * Sub-client for legislative procedures and adopted-texts EP API endpoints.
 *
 * @extends BaseEPClient
 * @public
 */
export class LegislativeClient extends BaseEPClient {
  constructor(config: EPClientConfig = {}, shared?: EPSharedResources) {
    super(config, shared);
  }
 
  // ─── Transform helpers ────────────────────────────────────────────────────
 
  private transformProcedure(apiData: Record<string, unknown>): Procedure {
    return _transformProcedure(apiData);
  }
 
  private transformAdoptedText(apiData: Record<string, unknown>): AdoptedText {
    return _transformAdoptedText(apiData);
  }
 
  private transformEvent(apiData: Record<string, unknown>): EPEvent {
    return _transformEvent(apiData);
  }
 
  // ─── Public methods ───────────────────────────────────────────────────────
 
  /**
   * Returns legislative procedures.
   * **EP API Endpoint:** `GET /procedures`
   *
   * @param params - year, limit, offset
   * @returns Paginated list of procedures
   */
  async getProcedures(params: {
    year?: number;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<Procedure>> {
    const limit = params.limit ?? 50;
    const offset = params.offset ?? 0;
    const apiParams: Record<string, unknown> = {
      format: 'application/ld+json',
      offset,
      limit,
    };
    if (params.year !== undefined) apiParams['year'] = params.year;
 
    const response = await this.get<JSONLDResponse>('procedures', apiParams);
    const items = Array.isArray(response.data) ? response.data : [];
    const procedures = items.map((item) => this.transformProcedure(item));
    return { data: procedures, total: procedures.length + offset, limit, offset, hasMore: procedures.length === limit };
  }
 
  /**
   * Returns a single procedure by ID.
   *
   * The EP API wraps single-item responses in a JSON-LD `data` array,
   * so this method extracts `data[0]` before transforming.
   *
   * **EP API Endpoint:** `GET /procedures/{process-id}`
   *
   * @param processId - Procedure **process-id** in `"YYYY-NNNN"` format (e.g. `"2024-0006"`).
   *   This is different from the human-readable `Procedure.id` (`"COD/YYYY/NNNN"`) or
   *   `Procedure.reference` (`"YYYY/NNNN(COD)"`) fields.
   * @throws {APIError} When the procedure is not found (404)
   */
  async getProcedureById(processId: string): Promise<Procedure> {
    if (processId.trim() === '') {
      throw new APIError('Procedure process-id is required', 400);
    }
    const response = await this.get<Record<string, unknown>>(
      `procedures/${processId}`,
      { format: 'application/ld+json' }
    );
    const dataArray = response['data'];
    Iif (Array.isArray(dataArray) && dataArray.length > 0) {
      return this.transformProcedure(dataArray[0] as Record<string, unknown>);
    }
    return this.transformProcedure(response);
  }
 
  /**
   * Returns events linked to a procedure.
   * **EP API Endpoint:** `GET /procedures/{process-id}/events`
   *
   * @param processId - Procedure process ID
   * @param params - limit, offset
   */
  async getProcedureEvents(
    processId: string,
    params: { limit?: number; offset?: number } = {}
  ): Promise<PaginatedResponse<EPEvent>> {
    if (processId.trim() === '') {
      throw new APIError('Procedure process-id is required', 400);
    }
    const limit = params.limit ?? 50;
    const offset = params.offset ?? 0;
 
    const response = await this.get<JSONLDResponse>(
      `procedures/${processId}/events`,
      { format: 'application/ld+json', offset, limit }
    );
 
    const items = Array.isArray(response.data) ? response.data : [];
    const events = items.map((item) => this.transformEvent(item));
    return { data: events, total: events.length + offset, limit, offset, hasMore: events.length === limit };
  }
 
  /**
   * Returns adopted texts.
   * **EP API Endpoint:** `GET /adopted-texts`
   *
   * @param params - year, limit, offset
   */
  async getAdoptedTexts(params: {
    year?: number;
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<AdoptedText>> {
    const limit = params.limit ?? 50;
    const offset = params.offset ?? 0;
    const apiParams: Record<string, unknown> = {
      format: 'application/ld+json',
      offset,
      limit,
    };
    if (params.year !== undefined) apiParams['year'] = params.year;
 
    const response = await this.get<JSONLDResponse>('adopted-texts', apiParams);
    const items = Array.isArray(response.data) ? response.data : [];
    const texts = items.map((item) => this.transformAdoptedText(item));
    return { data: texts, total: texts.length + offset, limit, offset, hasMore: texts.length === limit };
  }
 
  /**
   * Returns a single adopted text by document ID.
   * **EP API Endpoint:** `GET /adopted-texts/{doc-id}`
   */
  async getAdoptedTextById(docId: string): Promise<AdoptedText> {
    if (docId.trim() === '') throw new APIError('Document ID is required', 400);
    const response = await this.get<Record<string, unknown>>(
      `adopted-texts/${docId}`,
      { format: 'application/ld+json' }
    );
    return this.transformAdoptedText(response);
  }
}