All files / src/services api.ts

60.73% Statements 99/163
56.79% Branches 46/81
90.9% Functions 10/11
60.86% Lines 98/161

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                                                                                                                            10x   10x     10x         10x         10x 1x     9x   1x 1x                       1x   1x     1x         1x         1x       1x 1x   1x                             8x     8x     8x     8x                                         8x   8x 8x     8x   8x 6x     6x     6x                                       6x                       8x                                                                   2x   2x     2x 2x               2x     2x                 2x               2x 2x     2x 2x 2x                                                     2x   2x   2x             2x                 2x     2x         50475x                     2x   2x     2x   2x                         2x   2x   2x 2x   2x         2x     2x     2x 302x     302x 302x 302x   302x     302x 302x 302x     302x 302x 302x     302x 302x     302x 1208x       302x     302x                             2x                         3x   3x     2x   2x   1x 1x                     2x   2x 2x 2x     2x 2x 2x     2x 2x 2x     2x                                         2x  
import { BASE_URL } from '../config.js';
 
/**
 * Service for interacting with the tkconv API
 */
export class ApiService {
  /**
   * Fetches JSON data from the API
   * @param path The API path to fetch
   * @param options Additional fetch options
   * @returns Parsed JSON data
   * @throws Error if the request fails or returns HTML
   */
  async fetchJson<T>(path: string, options: RequestInit = {}): Promise<T> {
    try {
      // Ensure the path starts with a slash
      const normalizedPath = path.startsWith('/') ? path : `/${path}`;
 
      // Set default headers if not provided
      const headers = {
        'User-Agent': 'Mozilla/5.0 (compatible; OpenTK-MCP/1.0)',
        ...options.headers
      };
 
      const res = await fetch(`${BASE_URL}${normalizedPath}`, {
        ...options,
        headers
      });
 
      Iif (!res.ok) {
        throw new Error(`API error: ${res.status} ${res.statusText}`);
      }
 
      const text = await res.text();
 
      // Check if the response is HTML
      Iif (text.trim().startsWith('<!DOCTYPE')) {
        console.error(`The API returned HTML instead of JSON for path: ${normalizedPath}`);
        return {} as T;
      }
 
      // Parse JSON
      try {
        return JSON.parse(text) as T;
      } catch (error) {
        console.error(`Failed to parse JSON for path: ${normalizedPath}`, error);
        return {} as T;
      }
    } catch (error) {
      console.error(`Error fetching JSON: ${(error as Error).message}`);
      throw error;
    }
  }
 
  /**
   * Fetches HTML content from the API
   * @param path The API path to fetch
   * @param options Additional fetch options
   * @returns HTML content as string
   * @throws Error if the request fails
   */
  async fetchHtml(path: string, options: RequestInit = {}): Promise<string> {
    try {
      // Ensure the path starts with a slash
      const normalizedPath = path.startsWith('/') ? path : `/${path}`;
 
      // Set default headers if not provided
      const headers = {
        'User-Agent': 'Mozilla/5.0 (compatible; OpenTK-MCP/1.0)',
        ...options.headers
      };
 
      const res = await fetch(`${BASE_URL}${normalizedPath}`, {
        ...options,
        headers
      });
 
      if (!res.ok) {
        throw new Error(`API error: ${res.status} ${res.statusText}`);
      }
 
      return await res.text();
    } catch (error) {
      console.error(`Error fetching HTML: ${(error as Error).message}`);
      throw error;
    }
  }
 
  /**
   * Fetches binary data from the API
   * @param path The API path to fetch
   * @param options Additional fetch options
   * @returns Binary data as ArrayBuffer and content type
   * @throws Error if the request fails
   */
  async fetchBinary(path: string, options: RequestInit = {}): Promise<{ data: ArrayBuffer, contentType: string }> {
    try {
      // Ensure the path starts with a slash
      const normalizedPath = path.startsWith('/') ? path : `/${path}`;
 
      // Set default headers if not provided
      const headers = {
        'User-Agent': 'Mozilla/5.0 (compatible; OpenTK-MCP/1.0)',
        ...options.headers
      };
 
      const res = await fetch(`${BASE_URL}${normalizedPath}`, {
        ...options,
        headers
      });
 
      Iif (!res.ok) {
        throw new Error(`API error: ${res.status} ${res.statusText}`);
      }
 
      const data = await res.arrayBuffer();
      const contentType = res.headers.get('content-type') || 'application/octet-stream';
 
      return { data, contentType };
    } catch (error) {
      console.error(`Error fetching binary data: ${(error as Error).message}`);
      throw error;
    }
  }
 
  /**
   * Searches for documents in the tkconv API
   * @param query The search query
   * @param options Additional options like twomonths flag
   * @returns Search results
   * @throws Error if the request fails or returns HTML
   */
  async search<T>(query: string, options: { twomonths?: boolean, soorten?: string } = {}): Promise<T> {
    try {
      // Don't sanitize quotes as they're important for exact phrase searches
      // Only sanitize backslashes which could cause issues
      const sanitizedQuery = query.replace(/[\\]/g, ' ').trim();
 
      // Create a simple string for the form data (the API expects application/x-www-form-urlencoded)
      const formData = `q=${encodeURIComponent(sanitizedQuery)}&twomonths=${options.twomonths ? "true" : "false"}&soorten=${options.soorten || ""}`;
 
 
      const res = await fetch(`${BASE_URL}/search`, {
        method: "POST",
        headers: {
          'Accept': '*/*',
          'Content-Type': 'application/x-www-form-urlencoded',
          'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
          'Referer': `${BASE_URL}/search.html?q=${encodeURIComponent(sanitizedQuery)}&twomonths=${options.twomonths ? "true" : "false"}&soorten=${options.soorten || "alles"}`,
          'Origin': BASE_URL,
          'Host': 'berthub.eu',
          'Connection': 'keep-alive',
          'Sec-Fetch-Dest': 'empty',
          'Sec-Fetch-Mode': 'cors',
          'Sec-Fetch-Site': 'same-origin',
          'sec-ch-ua': '"Chromium";v="135", "Not-A.Brand";v="8"',
          'sec-ch-ua-mobile': '?0',
          'sec-ch-ua-platform': '"macOS"'
        },
        body: formData,
        // We would add timeout here, but it's not supported in the RequestInit type
      });
 
      if (!res.ok) {
        // If we get a 500 error, try to use a simplified query instead
        if (res.status === 500) {
          console.error(`Search API returned 500 for query: ${sanitizedQuery}`);
 
          // Try to simplify the query by taking just the first term
          const simplifiedQuery = sanitizedQuery.split(/\s+/)[0];
 
          if (simplifiedQuery && simplifiedQuery !== sanitizedQuery) {
            console.log(`Retrying with simplified query: ${simplifiedQuery}`);
 
            // Create a simple string for the form data with the simplified query
            const simplifiedForm = `q=${encodeURIComponent(simplifiedQuery)}&twomonths=${options.twomonths ? "true" : "false"}&soorten=${options.soorten || ""}`;
 
 
            const retryRes = await fetch(`${BASE_URL}/search`, {
              method: "POST",
              headers: {
                'Accept': '*/*',
                'Content-Type': 'application/x-www-form-urlencoded',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
                'Referer': `${BASE_URL}/search.html?q=${encodeURIComponent(simplifiedQuery)}&twomonths=${options.twomonths ? "true" : "false"}&soorten=${options.soorten || "alles"}`,
                'Origin': BASE_URL,
                'Host': 'berthub.eu',
                'Connection': 'keep-alive',
                'Sec-Fetch-Dest': 'empty',
                'Sec-Fetch-Mode': 'cors',
                'Sec-Fetch-Site': 'same-origin',
                'sec-ch-ua': '"Chromium";v="135", "Not-A.Brand";v="8"',
                'sec-ch-ua-mobile': '?0',
                'sec-ch-ua-platform': '"macOS"'
              },
              body: simplifiedForm
            });
 
            Iif (retryRes.ok) {
              const retryText = await retryRes.text();
              try {
                const retryData = JSON.parse(retryText);
                return retryData as T;
              } catch (e) {
                // If parsing fails, fall back to empty results
              }
            }
          }
 
          // If retry failed or wasn't attempted, return empty results with a message
          return {
            results: [],
            error: `The search query '${sanitizedQuery}' caused an error in the search API. Try simplifying your query.`
          } as T;
        }
        throw new Error(`API error: ${res.status} ${res.statusText}`);
      }
 
      // Check if the response is HTML
      const text = await res.text();
      Iif (text.trim().startsWith('<!DOCTYPE')) {
        console.error(`The search API returned HTML instead of JSON for query: ${sanitizedQuery}`);
        return { results: [] } as T;
      }
 
      // Parse JSON
      try {
        return JSON.parse(text) as T;
      } catch (error) {
        console.error(`Failed to parse JSON for query: ${sanitizedQuery}`, error);
        return { results: [] } as T;
      }
    } catch (error) {
      console.error(`Unexpected error in search: ${(error as Error).message}`);
      throw error;
    }
  }
 
  /**
   * Resolves an external reference to a URL
   * @param extId The external ID to resolve
   * @returns The resolved URL or empty string if not found
   */
  async resolveExternal(extId: string): Promise<string> {
    try {
      // Sanitize the external ID
      const sanitizedExtId = extId.replace(/[\s]+/g, ' ').trim();
 
      // First try the direct approach with the /op/ endpoint
      try {
        const res = await fetch(`${BASE_URL}/op/${encodeURIComponent(sanitizedExtId)}`, {
          redirect: "manual",
          headers: {
            'User-Agent': 'Mozilla/5.0 (compatible; OpenTK-MCP/1.0)'
          }
        });
 
        // Even if the response is not OK, we still want to check for a location header
        const location = res.headers.get("location") || "";
 
        // If we didn't get a location header but the response was OK, try to extract from body
        Iif (!location && res.ok) {
          const text = await res.text();
          // Look for a redirect URL in the response body (simple heuristic)
          const match = text.match(/window\.location\s*=\s*['"]([^'"]+)['"]/);
          Iif (match && match[1]) {
            return match[1];
          }
        }
 
        Iif (location) {
          return location;
        }
      } catch (directError) {
        console.log(`Direct external reference resolution failed: ${(directError as Error).message}`);
      }
 
      // If the direct approach failed, try to get the document page and extract the link
      try {
        const html = await this.fetchHtml(`/document.html?nummer=${encodeURIComponent(sanitizedExtId)}`);
 
        // Look for the link to the Tweede Kamer site
        const tkLinkMatch = html.match(/href="(https:\/\/www\.tweedekamer\.nl\/kamerstukken\/[^"]+)"/);
        if (tkLinkMatch && tkLinkMatch[1]) {
          return tkLinkMatch[1];
        }
 
        // If we couldn't find a TK link, look for any external link
        const anyLinkMatch = html.match(/href="(https?:\/\/[^"]+)"/);
        Iif (anyLinkMatch && anyLinkMatch[1]) {
          return anyLinkMatch[1];
        }
      } catch (htmlError) {
        console.log(`HTML approach for external reference failed: ${(htmlError as Error).message}`);
      }
 
      // If all else fails, construct a link to the document page
      return `${BASE_URL}/document.html?nummer=${encodeURIComponent(sanitizedExtId)}`;
    } catch (error) {
      console.error(`Error resolving external reference: ${(error as Error).message}`);
      // Return a fallback URL to the document page
      return `${BASE_URL}/document.html?nummer=${encodeURIComponent(extId)}`;
    }
  }
 
  /**
   * Fetches a sitemap of URLs for a specific time period
   * @param path The sitemap path (e.g., sitemap-2025.txt)
   * @returns Array of URLs
   */
  async fetchSitemap(path: string): Promise<string[]> {
    try {
      // Sanitize the path
      const sanitizedPath = path.replace(/[\s]+/g, ' ').trim();
 
      const res = await fetch(`${BASE_URL}/${sanitizedPath}`, {
        headers: {
          'User-Agent': 'Mozilla/5.0 (compatible; OpenTK-MCP/1.0)'
        },
        // We would add timeout here, but it's not supported in the RequestInit type
      });
 
      Iif (!res.ok) {
        // For 404 errors, return an empty array instead of failing
        Iif (res.status === 404) {
          console.error(`Sitemap not found: ${sanitizedPath}`);
          return [];
        }
        throw new Error(`API error: ${res.status} ${res.statusText}`);
      }
 
      const text = await res.text();
 
      // Check if the response is HTML (which would be an error for a sitemap)
      Iif (text.trim().startsWith('<!DOCTYPE')) {
        console.error(`The API returned HTML instead of a sitemap for path: ${sanitizedPath}`);
        return [];
      }
 
      return text.trim().split(/\r?\n/).filter(line => line.trim() !== '');
    } catch (error) {
      console.error(`Error fetching sitemap: ${(error as Error).message}`);
      throw error;
    }
  }
  /**
   * Fetches a list of all current Members of Parliament
   * @returns Array of MP data
   */
  async getPersons(): Promise<any[]> {
    try {
      // Fetch the HTML page that contains the MP list
      const html = await this.fetchHtml("/kamerleden.html");
 
      // Extract MP data from the HTML
      const persons = this.extractPersonsFromHtml(html);
 
      return persons;
    } catch (error) {
      console.error(`Error fetching persons: ${(error as Error).message}`);
      return [];
    }
  }
 
  /**
   * Extracts MP data from the HTML of the kamerleden.html page
   * @param html The HTML content of the kamerleden.html page
   * @returns Array of MP data
   */
  private extractPersonsFromHtml(html: string): any[] {
    const persons: any[] = [];
 
    try {
      // Extract the table rows containing MP data
      const tableRegex = /<table[^>]*>[\s\S]*?<tbody>([\s\S]*?)<\/tbody>/gi;
      const tableMatch = tableRegex.exec(html);
 
      Iif (!tableMatch || !tableMatch[1]) {
        console.error("Could not find MP table in HTML");
        return [];
      }
 
      const tableContent = tableMatch[1];
 
      // Extract each row (MP) from the table
      const rowRegex = /<tr[^>]*>([\s\S]*?)<\/tr>/gi;
      let rowMatch;
 
      while ((rowMatch = rowRegex.exec(tableContent)) !== null) {
        const rowContent = rowMatch[1];
 
        // Extract the MP ID from the link
        const idRegex = /persoon\.html\?nummer=(\d+)/i;
        const idMatch = rowContent.match(idRegex);
        const id = idMatch ? parseInt(idMatch[1]) : null;
 
        Iif (!id) continue;
 
        // Extract the MP name
        const nameRegex = /<a[^>]*>([\s\S]*?)<\/a>/i;
        const nameMatch = rowContent.match(nameRegex);
        const fullName = nameMatch ? nameMatch[1].trim() : "";
 
        // Split the name into first and last name (simple approach)
        const nameParts = fullName.split(" ");
        const firstName = nameParts.length > 1 ? nameParts[0] : "";
        const lastName = nameParts.length > 1 ? nameParts.slice(1).join(" ") : fullName;
 
        // Extract the party
        const partyRegex = /<td[^>]*>([\s\S]*?)<\/td>/gi;
        const cells = [];
        let cellMatch;
 
        while ((cellMatch = partyRegex.exec(rowContent)) !== null) {
          cells.push(cellMatch[1].trim());
        }
 
        // Party is typically in the second or third cell
        const party = cells.length > 2 ? cells[2] : (cells.length > 1 ? cells[1] : "");
 
        // Create the person object
        persons.push({
          Id: id,
          Persoonsnummer: id,
          Voornaam: firstName,
          Achternaam: lastName,
          Fullname: fullName,
          Fractie: party,
          FractieAfkorting: party,
          Functie: "Tweede Kamerlid", // Assuming all are MPs
          Links: {
            self: `/persoon.html?nummer=${id}`
          }
        });
      }
 
      return persons;
    } catch (error) {
      console.error(`Error extracting persons from HTML: ${(error as Error).message}`);
      return [];
    }
  }
 
  /**
   * Fetches details for a specific Member of Parliament
   * @param id The ID of the MP to fetch
   * @returns MP data or null if not found
   */
  async getPerson(id: number): Promise<any | null> {
    try {
      // Fetch the HTML page for the specific MP
      const html = await this.fetchHtml(`/persoon.html?nummer=${id}`);
 
      // Extract the MP data from the HTML
      const person = this.extractPersonFromHtml(html, id);
 
      return person;
    } catch (error) {
      console.error(`Error fetching person with ID ${id}: ${(error as Error).message}`);
      return null;
    }
  }
 
  /**
   * Extracts MP data from the HTML of the persoon.html page
   * @param html The HTML content of the persoon.html page
   * @param id The ID of the MP
   * @returns MP data or null if not found
   */
  private extractPersonFromHtml(html: string, id: number): any | null {
    try {
      // Extract the MP name from the title
      const titleRegex = /<title>([\s\S]*?)<\/title>/i;
      const titleMatch = html.match(titleRegex);
      const fullName = titleMatch ? titleMatch[1].trim() : "";
 
      // Split the name into first and last name (simple approach)
      const nameParts = fullName.split(" ");
      const firstName = nameParts.length > 1 ? nameParts[0] : "";
      const lastName = nameParts.length > 1 ? nameParts.slice(1).join(" ") : fullName;
 
      // Extract the party
      const partyRegex = /<h4>([\s\S]*?)<\/h4>/i;
      const partyMatch = html.match(partyRegex);
      const party = partyMatch ? partyMatch[1].trim() : "";
 
      // Create the person object
      return {
        Id: id,
        Persoonsnummer: id,
        Voornaam: firstName,
        Achternaam: lastName,
        Fullname: fullName,
        Fractie: party,
        FractieAfkorting: party,
        Functie: "Tweede Kamerlid", // Assuming all are MPs
        Links: {
          self: `/persoon.html?nummer=${id}`
        }
      };
    } catch (error) {
      console.error(`Error extracting person from HTML: ${(error as Error).message}`);
      return null;
    }
  }
}
 
// Export a singleton instance
export const apiService = new ApiService();