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 | 305x 129x 6x 9x 19x 19x 19x 19x 19x 19x 19x 19x 5x 5x 4x 35x 5x 2x 2x 11x 5x 5x 5x 5x 5x 5x 5x 5x 45x 5x 5x 5x 5x 10x 10x 10x 10x 19x 19x 19x 19x 56x 15x 19x 19x 4x 4x 6x 6x 6x 6x 3x 3x 1x 6x 6x 6x 6x 6x 6x 11x 11x 11x 11x 5x 5x 5x 6x 5x 13x 11x 3x 3x 3x 2x 4x 3x 3x 3x 3x 2x 5x 3x 4x 4x 4x 3x 6x 4x 7x 7x 7x 7x 7x 6x 7x 7x 7x 4x 2x 2x 2x 2x 2x | /**
* @fileoverview MEP sub-client for European Parliament API
*
* Handles all Member of European Parliament API calls:
* current/incoming/outgoing/homonym MEP lists, MEP details,
* and MEP financial declarations.
*
* **GDPR:** Personal data access (declarations) is audit-logged per Article 30.
*
* @module clients/ep/mepClient
*/
import { auditLogger } from '../../utils/auditLogger.js';
import type {
MEP,
MEPDetails,
MEPDeclaration,
PaginatedResponse,
} from '../../types/europeanParliament.js';
import {
transformMEP as _transformMEP,
transformMEPDetails as _transformMEPDetails,
transformMEPDeclaration as _transformMEPDeclaration,
} from './transformers.js';
import {
BaseEPClient,
APIError,
type EPClientConfig,
type EPSharedResources,
type JSONLDResponse,
} from './baseClient.js';
// ─── MEP Client ───────────────────────────────────────────────────────────────
/**
* Sub-client for MEP-related European Parliament API endpoints.
*
* Handles all MEP data fetching: active lists, individual profiles,
* incoming/outgoing/homonym lists, and financial declarations.
*
* @extends BaseEPClient
* @public
*/
export class MEPClient extends BaseEPClient {
constructor(config: EPClientConfig = {}, shared?: EPSharedResources) {
super(config, shared);
}
// ─── Transform helpers ────────────────────────────────────────────────────
private transformMEP(apiData: Record<string, unknown>): MEP {
return _transformMEP(apiData);
}
private transformMEPDetails(apiData: Record<string, unknown>): MEPDetails {
return _transformMEPDetails(apiData);
}
private transformMEPDeclaration(apiData: Record<string, unknown>): MEPDeclaration {
return _transformMEPDeclaration(apiData);
}
// ─── Private helpers ─────────────────────────────────────────────────────
/**
* Maps getMEPs params to EP API query parameters.
* @private
*/
private buildMEPParams(params: {
country?: string;
group?: string;
committee?: string;
active?: boolean;
limit?: number;
offset?: number;
}): Record<string, unknown> {
const apiParams: Record<string, unknown> = {};
if (params.limit !== undefined) apiParams['limit'] = params.limit;
Iif (params.offset !== undefined) apiParams['offset'] = params.offset;
if (params.country !== undefined) apiParams['country-code'] = params.country;
Iif (params.group !== undefined) apiParams['political-group'] = params.group;
Iif (params.committee !== undefined) apiParams['committee'] = params.committee;
Iif (params.active !== undefined) apiParams['status'] = params.active ? 'current' : 'all';
return apiParams;
}
/** Apply optional client-side country and group filters to an MEP array. */
private filterMEPs(
meps: MEP[],
country: string | undefined,
group: string | undefined,
): MEP[] {
let result = meps;
if (country !== undefined) {
const upper = country.toUpperCase();
result = result.filter((m) => m.country.toUpperCase() === upper);
}
if (group !== undefined) {
const normalizedGroup = group.trim().toLowerCase();
result = result.filter(
(m) => m.politicalGroup.trim().toLowerCase() === normalizedGroup,
);
}
return result;
}
/** Fetch all current MEPs in paginated batches (max 100 per request). */
private async fetchAllCurrentMEPs(): Promise<MEP[]> {
const batchSize = 100;
const allMeps: MEP[] = [];
let fetchOffset = 0;
let hasMore = true;
while (hasMore) {
const response = await this.get<JSONLDResponse>('meps/show-current', {
format: 'application/ld+json',
offset: fetchOffset,
limit: batchSize,
});
const items = Array.isArray(response.data) ? response.data : [];
const batch = items.map((item) => ({ ...this.transformMEP(item), active: true }));
allMeps.push(...batch);
hasMore = batch.length === batchSize;
fetchOffset += batchSize;
}
return allMeps;
}
/** Build paginated result from filtered MEPs. */
private paginateFiltered(
meps: MEP[], limit: number, offset: number, filtered: boolean
): PaginatedResponse<MEP> {
const total = filtered ? meps.length : meps.length + offset;
const paged = filtered ? meps.slice(offset, offset + limit) : meps;
const hasMore = filtered ? offset + paged.length < meps.length : paged.length === limit;
return { data: paged, total, limit, offset, hasMore };
}
// ─── Public methods ───────────────────────────────────────────────────────
/**
* Retrieves Members of the European Parliament with filtering and pagination.
*
* @param params - Country, group, committee, active status, limit, offset
* @returns Paginated MEP list
* @security Personal data access logged per GDPR Article 30
*/
async getMEPs(params: {
country?: string;
group?: string;
committee?: string;
active?: boolean;
limit?: number;
offset?: number;
}): Promise<PaginatedResponse<MEP>> {
const action = 'get_meps';
try {
const apiParams = this.buildMEPParams(params);
const response = await this.get<JSONLDResponse>('meps', apiParams);
const meps = response.data.map((item) => this.transformMEP(item));
const result: PaginatedResponse<MEP> = {
data: meps,
total: (params.offset ?? 0) + meps.length,
limit: params.limit ?? 50,
offset: params.offset ?? 0,
hasMore: meps.length >= (params.limit ?? 50),
};
auditLogger.logDataAccess(action, params, result.data.length);
return result;
} catch (error) {
auditLogger.logError(
action,
params,
error instanceof Error ? error.message : 'Unknown error'
);
throw error;
}
}
/**
* 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
*/
async getMEPDetails(id: string): Promise<MEPDetails> {
const action = 'get_mep_details';
const params = { id };
let normalizedId = id;
if (id.startsWith('MEP-')) {
normalizedId = id.substring(4);
} else if (id.startsWith('person/')) {
normalizedId = id.substring(7);
}
try {
const response = await this.get<JSONLDResponse>(`meps/${normalizedId}`, {});
Eif (response.data.length > 0) {
const mepDetails = this.transformMEPDetails(response.data[0] ?? {});
auditLogger.logDataAccess(action, params, 1);
return mepDetails;
}
throw new APIError(`MEP with ID ${id} not found`, 404);
} catch (error) {
auditLogger.logError(
action,
params,
error instanceof Error ? error.message : 'Unknown error'
);
throw error;
}
}
/**
* Returns all currently active MEPs for today's date.
*
* Unlike `getMEPs()`, this uses `GET /meps/show-current` which returns
* `api:country-of-representation` and `api:political-group` in responses.
* Optional `country` and `group` filters are applied client-side after fetch.
*
* **EP API Endpoint:** `GET /meps/show-current`
*
* @param params - Optional filters and pagination
* @param params.country - ISO 3166-1 alpha-2 country code for client-side filtering
* @param params.group - Political group identifier for client-side filtering
* @param params.limit - Maximum results to return (default 50)
* @param params.offset - Pagination offset (default 0)
*/
async getCurrentMEPs(params: {
country?: string;
group?: string;
limit?: number;
offset?: number;
} = {}): Promise<PaginatedResponse<MEP>> {
const limit = params.limit ?? 50;
const offset = params.offset ?? 0;
const needsFiltering = params.country !== undefined || params.group !== undefined;
if (needsFiltering) {
// Fetch all MEPs in batches of 100 for client-side filtering
const allMeps = await this.fetchAllCurrentMEPs();
const filtered = this.filterMEPs(allMeps, params.country, params.group);
return this.paginateFiltered(filtered, limit, offset, true);
}
// No filtering — single request with caller's pagination
const response = await this.get<JSONLDResponse>('meps/show-current', {
format: 'application/ld+json',
offset,
limit,
});
const items = Array.isArray(response.data) ? response.data : [];
// show-current endpoint only returns MEPs with active mandates,
// so mark every returned MEP as active even when the API response
// omits the explicit `active` flag.
const allMeps = items.map((item) => ({ ...this.transformMEP(item), active: true }));
return this.paginateFiltered(allMeps, limit, offset, false);
}
/**
* 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>> {
const limit = params.limit ?? 50;
const offset = params.offset ?? 0;
const response = await this.get<JSONLDResponse>('meps/show-incoming', {
format: 'application/ld+json',
offset,
limit,
});
const items = Array.isArray(response.data) ? response.data : [];
const meps = items.map((item) => this.transformMEP(item));
return { data: meps, total: meps.length + offset, limit, offset, hasMore: meps.length === limit };
}
/**
* 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>> {
const limit = params.limit ?? 50;
const offset = params.offset ?? 0;
const response = await this.get<JSONLDResponse>('meps/show-outgoing', {
format: 'application/ld+json',
offset,
limit,
});
const items = Array.isArray(response.data) ? response.data : [];
const meps = items.map((item) => this.transformMEP(item));
return { data: meps, total: meps.length + offset, limit, offset, hasMore: meps.length === limit };
}
/**
* 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>> {
const limit = params.limit ?? 50;
const offset = params.offset ?? 0;
const response = await this.get<JSONLDResponse>('meps/show-homonyms', {
format: 'application/ld+json',
offset,
limit,
});
const items = Array.isArray(response.data) ? response.data : [];
const meps = items.map((item) => this.transformMEP(item));
return { data: meps, total: meps.length + offset, limit, offset, hasMore: meps.length === limit };
}
/**
* 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>> {
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>('meps-declarations', apiParams);
const items = Array.isArray(response.data) ? response.data : [];
const declarations = items.map((item) => this.transformMEPDeclaration(item));
auditLogger.logDataAccess('getMEPDeclarations', apiParams, declarations.length);
return { data: declarations, total: declarations.length + offset, limit, offset, hasMore: declarations.length === limit };
}
/**
* Retrieves recently updated MEPs via the feed endpoint.
* **EP API Endpoint:** `GET /meps/feed`
*/
async getMEPsFeed(params: {
timeframe?: string;
startDate?: string;
} = {}): Promise<JSONLDResponse> {
return this.get<JSONLDResponse>('meps/feed', {
format: 'application/ld+json',
...(params.timeframe !== undefined ? { timeframe: params.timeframe } : {}),
...(params.startDate !== undefined ? { 'start-date': params.startDate } : {}),
});
}
/**
* Retrieves recently updated MEP declarations via the feed endpoint.
* **EP API Endpoint:** `GET /meps-declarations/feed`
*/
async getMEPDeclarationsFeed(params: {
timeframe?: string;
startDate?: string;
workType?: string;
} = {}): Promise<JSONLDResponse> {
return this.get<JSONLDResponse>('meps-declarations/feed', {
format: 'application/ld+json',
...(params.timeframe !== undefined ? { timeframe: params.timeframe } : {}),
...(params.startDate !== undefined ? { 'start-date': params.startDate } : {}),
...(params.workType !== undefined ? { 'work-type': params.workType } : {}),
});
}
/**
* 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> {
if (docId.trim() === '') {
throw new APIError('Document ID is required', 400);
}
const response = await this.get<Record<string, unknown>>(
`meps-declarations/${docId}`,
{ format: 'application/ld+json' }
);
const declaration = this.transformMEPDeclaration(response);
auditLogger.logDataAccess('getMEPDeclarationById', { docId }, 1);
return declaration;
}
}
|