@tmlmobilidade/utils 20251012.2109.36 → 20251015.1047.23

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.
@@ -37,7 +37,7 @@ export type WithPagination<T> = T & {
37
37
  * );
38
38
  * ```
39
39
  */
40
- export declare function fetchData<T>(url: string, method?: 'DELETE' | 'GET' | 'POST' | 'PUT', body?: unknown, headers?: Record<string, string>, options?: Omit<RequestInit, 'body' | 'headers' | 'method'>): Promise<HttpResponse<T>>;
40
+ export declare function fetchData<T>(url: string, method?: 'DELETE' | 'GET' | 'POST' | 'PUT', body?: unknown, headers?: Record<string, string>, options?: Omit<RequestInit, 'body' | 'headers' | 'method'>, retries?: number): Promise<HttpResponse<T>>;
41
41
  /**
42
42
  * Sends a multipart form data request to a URL.
43
43
  * @param url - The URL to send the request to
package/dist/src/http.js CHANGED
@@ -32,39 +32,59 @@ export class HttpResponse {
32
32
  * );
33
33
  * ```
34
34
  */
35
- export async function fetchData(url, method = 'GET', body, headers = {}, options = {}) {
36
- try {
37
- const response = await fetch(url, {
38
- body: body ? JSON.stringify(body) : undefined,
39
- credentials: 'include',
40
- headers: {
41
- ...(method === 'GET' || method === 'DELETE' || 'Content-Type' in headers ? {} : { 'Content-Type': 'application/json' }),
42
- ...headers,
43
- },
44
- method,
45
- ...options,
46
- });
47
- const data = await response.json();
48
- if (!response.ok || data.error) {
35
+ export async function fetchData(url, method = 'GET', body, headers = {}, options = {}, retries = 3) {
36
+ let attempt = 0;
37
+ let lastError;
38
+ while (attempt <= retries) {
39
+ try {
40
+ const response = await fetch(url, {
41
+ body: body ? JSON.stringify(body) : undefined,
42
+ credentials: 'include',
43
+ headers: {
44
+ ...(method === 'GET' || method === 'DELETE' || 'Content-Type' in headers
45
+ ? {}
46
+ : { 'Content-Type': 'application/json' }),
47
+ ...headers,
48
+ },
49
+ method,
50
+ ...options,
51
+ });
52
+ const data = (await response.json());
53
+ if (!response.ok || data.error) {
54
+ // Don't retry for 4xx (client) errors
55
+ if (response.status >= 400 && response.status < 500) {
56
+ return new HttpResponse({
57
+ data: null,
58
+ error: data.error,
59
+ statusCode: response.status,
60
+ });
61
+ }
62
+ throw new Error(`HTTP ${response.status} - ${data.error ?? 'Unknown error'}`);
63
+ }
49
64
  return new HttpResponse({
50
- data: null,
51
- error: data.error,
65
+ data: data.data,
66
+ error: null,
52
67
  statusCode: response.status,
53
68
  });
54
69
  }
55
- return new HttpResponse({
56
- data: data.data,
57
- error: null,
58
- statusCode: response.status,
59
- });
60
- }
61
- catch (error) {
62
- return new HttpResponse({
63
- data: null,
64
- error: error instanceof Error ? error.message : 'Network error',
65
- statusCode: 500,
66
- });
70
+ catch (error) {
71
+ lastError = error;
72
+ attempt++;
73
+ // Stop retrying if we've reached the limit
74
+ if (attempt > retries)
75
+ break;
76
+ // Wait before retrying (exponential backoff: 0.5s, 1s, 2s, etc.)
77
+ const delay = 500 * Math.pow(2, attempt - 1);
78
+ await new Promise(resolve => setTimeout(resolve, delay));
79
+ }
67
80
  }
81
+ return new HttpResponse({
82
+ data: null,
83
+ error: lastError instanceof Error
84
+ ? `${lastError.message} - ${lastError.cause ?? ''}`
85
+ : 'Network error',
86
+ statusCode: 500,
87
+ });
68
88
  }
69
89
  /**
70
90
  * Sends a multipart form data request to a URL.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/utils",
3
- "version": "20251012.2109.36",
3
+ "version": "20251015.1047.23",
4
4
  "author": "João de Vasconcelos & Jusi Monteiro",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "homepage": "https://github.com/tmlmobilidade/services#readme",