zudello-integration-sdk 1.0.33 → 1.0.35
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/package.json +1 -1
- package/src/utils/apiInstance.js +40 -6
package/package.json
CHANGED
package/src/utils/apiInstance.js
CHANGED
|
@@ -11,13 +11,18 @@ class ApiInstance {
|
|
|
11
11
|
*/
|
|
12
12
|
constructor () {
|
|
13
13
|
this.bearerToken = null
|
|
14
|
-
this.retryStatuses = [502, 504]
|
|
14
|
+
this.retryStatuses = [502, 504, 408, 429, 500]
|
|
15
15
|
|
|
16
16
|
this.responseHandler = new ResponseHandler()
|
|
17
17
|
|
|
18
18
|
this.axiosInstance = axios.create({
|
|
19
|
-
httpsAgent: new https.Agent({
|
|
19
|
+
httpsAgent: new https.Agent({
|
|
20
|
+
keepAlive: true,
|
|
21
|
+
maxSockets: 100,
|
|
22
|
+
maxFreeSockets: 10,
|
|
23
|
+
})
|
|
20
24
|
})
|
|
25
|
+
|
|
21
26
|
this.axiosInstance.interceptors.request.use((config) => {
|
|
22
27
|
if (this.bearerToken) {
|
|
23
28
|
config.headers.Authorization = `Bearer ${this.bearerToken}`
|
|
@@ -27,6 +32,35 @@ class ApiInstance {
|
|
|
27
32
|
}, (error) => {
|
|
28
33
|
return Promise.reject(error)
|
|
29
34
|
})
|
|
35
|
+
|
|
36
|
+
// Add response interceptor for global error handling
|
|
37
|
+
this.axiosInstance.interceptors.response.use(
|
|
38
|
+
response => response,
|
|
39
|
+
error => {
|
|
40
|
+
// Check if the error is a network error or ECONNRESET
|
|
41
|
+
if (error.code === 'ECONNRESET' || error.code === 'ECONNABORTED' || error.message === 'socket hang up') {
|
|
42
|
+
error.shouldRetry = true
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return Promise.reject(error)
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Determines if a request should be retried based on the error
|
|
52
|
+
* @param {Error} error The error that occurred
|
|
53
|
+
* @returns {boolean} Whether to retry the request
|
|
54
|
+
*/
|
|
55
|
+
shouldRetry(error) {
|
|
56
|
+
// Retry on network errors and specific HTTP status codes
|
|
57
|
+
return (
|
|
58
|
+
error.shouldRetry || // Set by our interceptor for network errors
|
|
59
|
+
this.retryStatuses.includes(error?.response?.status) ||
|
|
60
|
+
(error.code === 'ECONNRESET') ||
|
|
61
|
+
(error.message === 'socket hang up') ||
|
|
62
|
+
(error.code === 'ECONNABORTED')
|
|
63
|
+
)
|
|
30
64
|
}
|
|
31
65
|
|
|
32
66
|
/**
|
|
@@ -75,7 +109,7 @@ class ApiInstance {
|
|
|
75
109
|
callback(CircularJSON.stringify(error, null, 2))
|
|
76
110
|
callback(CircularJSON.stringify(error.response, null, 2))
|
|
77
111
|
|
|
78
|
-
if (this.
|
|
112
|
+
if (this.shouldRetry(error) && retryCount === 0) {
|
|
79
113
|
await this.sleep(20000)
|
|
80
114
|
return await this.get(url, params, headers, returnHeaders, callback, retryCount + 1)
|
|
81
115
|
}
|
|
@@ -135,7 +169,7 @@ class ApiInstance {
|
|
|
135
169
|
callback(CircularJSON.stringify(error, null, 2))
|
|
136
170
|
callback(CircularJSON.stringify(error.response, null, 2))
|
|
137
171
|
|
|
138
|
-
if (this.
|
|
172
|
+
if (this.shouldRetry(error) && retryCount === 0) {
|
|
139
173
|
await this.sleep(20000)
|
|
140
174
|
return await this.post(url, data, headers, params, callback, retryCount + 1)
|
|
141
175
|
}
|
|
@@ -180,7 +214,7 @@ class ApiInstance {
|
|
|
180
214
|
|
|
181
215
|
callback(error?.response)
|
|
182
216
|
|
|
183
|
-
if (this.
|
|
217
|
+
if (this.shouldRetry(error) && retryCount === 0) {
|
|
184
218
|
await this.sleep(20000)
|
|
185
219
|
return await this.put(url, data, headers, params, callback, retryCount + 1)
|
|
186
220
|
}
|
|
@@ -216,7 +250,7 @@ class ApiInstance {
|
|
|
216
250
|
|
|
217
251
|
callback(error?.response)
|
|
218
252
|
|
|
219
|
-
if (this.
|
|
253
|
+
if (this.shouldRetry(error) && retryCount === 0) {
|
|
220
254
|
await this.sleep(20000)
|
|
221
255
|
return await this.delete(url, callback, retryCount + 1)
|
|
222
256
|
}
|