All files / src/clients/ep vocabularyClient.ts

100% Statements 10/10
90% Branches 9/10
100% Functions 3/3
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 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                                                      295x                               4x 4x   4x           3x 4x                         4x 2x   2x       2x      
/**
 * @fileoverview Vocabulary sub-client for European Parliament API
 *
 * Handles EP controlled-vocabularies endpoints (list and single-item lookup).
 *
 * @module clients/ep/vocabularyClient
 */
 
import type { PaginatedResponse } from '../../types/europeanParliament.js';
import {
  BaseEPClient,
  APIError,
  type EPClientConfig,
  type EPSharedResources,
  type JSONLDResponse,
} from './baseClient.js';
 
// ─── Vocabulary Client ────────────────────────────────────────────────────────
 
/**
 * Sub-client for EP controlled-vocabularies endpoints.
 *
 * @extends BaseEPClient
 * @public
 */
export class VocabularyClient extends BaseEPClient {
  constructor(config: EPClientConfig = {}, shared?: EPSharedResources) {
    super(config, shared);
  }
 
  // ─── Public methods ───────────────────────────────────────────────────────
 
  /**
   * Returns EP controlled vocabularies.
   * **EP API Endpoint:** `GET /controlled-vocabularies`
   *
   * @param params - limit, offset
   * @returns Raw API response with vocabulary items
   */
  async getControlledVocabularies(params: {
    limit?: number;
    offset?: number;
  } = {}): Promise<PaginatedResponse<Record<string, unknown>>> {
    const limit = params.limit ?? 50;
    const offset = params.offset ?? 0;
 
    const response = await this.get<JSONLDResponse>('controlled-vocabularies', {
      format: 'application/ld+json',
      offset,
      limit,
    });
 
    const items = Array.isArray(response.data) ? response.data : [];
    return { data: items, total: items.length + offset, limit, offset, hasMore: items.length === limit };
  }
 
  /**
   * Returns a single EP Controlled Vocabulary by ID.
   * **EP API Endpoint:** `GET /controlled-vocabularies/{voc-id}`
   *
   * @param vocId - Vocabulary identifier
   * @returns Single vocabulary entry
   */
  async getControlledVocabularyById(
    vocId: string
  ): Promise<Record<string, unknown>> {
    if (vocId.trim() === '') {
      throw new APIError('Vocabulary ID is required', 400);
    }
    const response = await this.get<Record<string, unknown>>(
      `controlled-vocabularies/${vocId}`,
      { format: 'application/ld+json' }
    );
    return response;
  }
}