@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.
- package/dist/src/http.d.ts +1 -1
- package/dist/src/http.js +48 -28
- package/package.json +1 -1
package/dist/src/http.d.ts
CHANGED
|
@@ -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'
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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:
|
|
51
|
-
error:
|
|
65
|
+
data: data.data,
|
|
66
|
+
error: null,
|
|
52
67
|
statusCode: response.status,
|
|
53
68
|
});
|
|
54
69
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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