@tmlmobilidade/external 20260604.2309.43 → 20260604.2324.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  /* * */
2
2
  import { Logger } from '@tmlmobilidade/logger';
3
3
  import { asyncSingletonProxy } from '@tmlmobilidade/utils';
4
- import { curlFetcher } from './curl-fetcher.js';
5
4
  /* * */
6
5
  export class MLAuthClient {
7
6
  //
@@ -56,7 +55,7 @@ export class MLAuthClient {
56
55
  password: process.env.ML_PASSWORD,
57
56
  username: process.env.ML_USERNAME,
58
57
  }).toString();
59
- const data = await curlFetcher(process.env.ML_AUTH_URL, {
58
+ const response = await fetch(process.env.ML_AUTH_URL, {
60
59
  body: requestBody,
61
60
  headers: {
62
61
  'Authorization': `Basic ${Buffer.from(`${process.env.ML_CLIENT_ID}:${process.env.ML_CLIENT_SECRET}`).toString('base64')}`,
@@ -64,6 +63,10 @@ export class MLAuthClient {
64
63
  },
65
64
  method: 'POST',
66
65
  });
66
+ if (!response.ok) {
67
+ throw new Error(`[MLAuthClient] Token request failed (${response.status}): ${response.statusText}`);
68
+ }
69
+ const data = await response.json();
67
70
  //
68
71
  // With the response data, set the token and calculate the expiration time
69
72
  // based on the current time and the expires_in value from the response.
@@ -1,6 +1,5 @@
1
1
  /* * */
2
2
  import { mlAuthClient } from './auth.js';
3
- import { curlFetcher } from './curl-fetcher.js';
4
3
  import { DESTINATION_MAP } from './types.js';
5
4
  /* * */
6
5
  const BASE_URL = process.env.ML_API_URL;
@@ -10,11 +9,15 @@ async function fetcher(endpoint) {
10
9
  throw new Error('Missing ML_API_URL environment variable.');
11
10
  }
12
11
  const apiToken = await mlAuthClient.getToken();
13
- return await curlFetcher(`${BASE_URL}${endpoint}`, {
12
+ const response = await fetch(`${BASE_URL}${endpoint}`, {
14
13
  headers: {
15
14
  Authorization: `Bearer ${apiToken}`,
16
15
  },
17
16
  });
17
+ if (!response.ok) {
18
+ throw new Error(`Request failed (${response.status}): ${response.statusText}`);
19
+ }
20
+ return response.json();
18
21
  }
19
22
  /* * */
20
23
  const endpoints = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/external",
3
- "version": "20260604.2309.43",
3
+ "version": "20260604.2324.46",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"
@@ -1,5 +0,0 @@
1
- export declare function curlFetcher<T>(url: string, options?: {
2
- body?: unknown;
3
- headers?: Record<string, string>;
4
- method?: string;
5
- }): Promise<T>;
@@ -1,37 +0,0 @@
1
- // ! THIS IS A TEMPORARY FIX FOR THE ML API CERTIFICATE ISSUE
2
- import { exec } from 'child_process';
3
- export async function curlFetcher(url, options) {
4
- const method = options?.method ?? 'GET';
5
- const headers = options?.headers ?? {};
6
- const curlArgs = [
7
- 'curl',
8
- '-sSLf',
9
- '-X', method,
10
- ...Object.entries(headers).map(([key, value]) => `-H "${key}: ${value}"`),
11
- ];
12
- if (options?.body !== undefined) {
13
- const bodyStr = typeof options.body === 'string' ? options.body : JSON.stringify(options.body);
14
- curlArgs.push('--data-raw', `"${bodyStr}"`);
15
- }
16
- curlArgs.push(`"${url}"`);
17
- const curlCommand = curlArgs.join(' ');
18
- return await new Promise((resolve, reject) => {
19
- exec(curlCommand, (error, stdout, stderr) => {
20
- if (error) {
21
- console.error(`curl error: ${error.message}`);
22
- reject(new Error(`curl error: ${error.message}`));
23
- return;
24
- }
25
- if (stderr) {
26
- console.error(`curl stderr: ${stderr}`);
27
- }
28
- try {
29
- const data = JSON.parse(stdout);
30
- resolve(data);
31
- }
32
- catch (parseError) {
33
- reject(new Error(`Failed to parse curl response: ${parseError.message}`));
34
- }
35
- });
36
- });
37
- }