All files / src/utils allMepFetcher.ts

100% Statements 20/20
100% Branches 6/6
100% Functions 3/3
100% Lines 18/18

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                      1x                                                 1x 1x                     2x 2x   2x 5x 5x 5x 3x                                         4x 4x 4x   4x 7x 7x 7x 3x      
/**
 * Retrieves the complete MEP listing with a caller-configured page size.
 *
 * @module utils/allMepFetcher
 */
 
interface MEPPage {
  data: unknown[];
  hasMore: boolean;
}
 
const CURRENT_MEP_PAGE_LIMIT = 100;
 
export interface MEPPageClient {
  getMEPs(params: {
    active: boolean;
    limit: number;
    offset: number;
  }): Promise<MEPPage>;
}
 
export interface CurrentMEPPageClient {
  getCurrentMEPs(params: {
    limit: number;
    offset: number;
  }): Promise<MEPPage>;
}
 
/**
 * Fetches one MEP listing page for an incremental detail-cache refresh.
 *
 * @param client - Client used to retrieve the MEP page.
 * @param batchSize - Number of MEPs requested for this refresh.
 * @returns MEP records from the first listing page.
 */
export async function fetchMEPBatch(client: MEPPageClient, batchSize: number): Promise<unknown[]> {
  const page = await client.getMEPs({ active: false, limit: batchSize, offset: 0 });
  return page.data;
}
 
/**
 * Fetches every MEP page, preserving the configured page size for each request.
 *
 * @param client - Client used to retrieve MEP pages.
 * @param batchSize - Number of MEPs requested per page.
 * @returns All MEP records returned by the paginated endpoint.
 */
export async function fetchAllMEPs(client: MEPPageClient, batchSize: number): Promise<unknown[]> {
  const result: unknown[] = [];
  let offset = 0;
 
  for (;;) {
    const page = await client.getMEPs({ active: false, limit: batchSize, offset });
    result.push(...page.data);
    if (!page.hasMore) return result;
    offset += batchSize;
  }
}
 
/**
 * Fetches every currently active MEP via the `/meps/show-current` endpoint.
 *
 * Unlike {@link fetchAllMEPs} (which pages `/meps?status=all` and surfaces the
 * oldest historical members first), this returns the present-day roster with
 * `active: true`, country of representation, and political-group enrichment —
 * the data that OSINT tooling (e.g. `get_meps`, defaulting to `active: true`)
 * actually consumes.
 *
 * @param client - Client used to retrieve current MEP pages.
 * @param batchSize - Number of MEPs requested per page.
 * @returns All currently active MEP records.
 */
export async function fetchAllCurrentMEPs(
  client: CurrentMEPPageClient,
  batchSize: number,
): Promise<unknown[]> {
  const result: unknown[] = [];
  let offset = 0;
  const pageLimit = Math.min(batchSize, CURRENT_MEP_PAGE_LIMIT);
 
  for (;;) {
    const page = await client.getCurrentMEPs({ limit: pageLimit, offset });
    result.push(...page.data);
    if (!page.hasMore && page.data.length < pageLimit) return result;
    offset += pageLimit;
  }
}