zudello-integration-sdk 1.0.33 → 1.0.34

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zudello-integration-sdk",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "description": "Zudello Integrations SDK",
5
5
  "main": "./src/index.js",
6
6
  "repository": {
@@ -11,13 +11,20 @@ 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({ keepAlive: true })
19
+ httpsAgent: new https.Agent({
20
+ keepAlive: true,
21
+ timeout: 60000, // 60 seconds timeout
22
+ maxSockets: 100,
23
+ maxFreeSockets: 10,
24
+ }),
25
+ timeout: 30000, // 30 seconds timeout for the request itself
20
26
  })
27
+
21
28
  this.axiosInstance.interceptors.request.use((config) => {
22
29
  if (this.bearerToken) {
23
30
  config.headers.Authorization = `Bearer ${this.bearerToken}`
@@ -27,6 +34,35 @@ class ApiInstance {
27
34
  }, (error) => {
28
35
  return Promise.reject(error)
29
36
  })
37
+
38
+ // Add response interceptor for global error handling
39
+ this.axiosInstance.interceptors.response.use(
40
+ response => response,
41
+ error => {
42
+ // Check if the error is a network error or ECONNRESET
43
+ if (error.code === 'ECONNRESET' || error.code === 'ECONNABORTED' || error.message === 'socket hang up') {
44
+ error.shouldRetry = true
45
+ }
46
+
47
+ return Promise.reject(error)
48
+ }
49
+ )
50
+ }
51
+
52
+ /**
53
+ * Determines if a request should be retried based on the error
54
+ * @param {Error} error The error that occurred
55
+ * @returns {boolean} Whether to retry the request
56
+ */
57
+ shouldRetry(error) {
58
+ // Retry on network errors and specific HTTP status codes
59
+ return (
60
+ error.shouldRetry || // Set by our interceptor for network errors
61
+ this.retryStatuses.includes(error?.response?.status) ||
62
+ (error.code === 'ECONNRESET') ||
63
+ (error.message === 'socket hang up') ||
64
+ (error.code === 'ECONNABORTED')
65
+ )
30
66
  }
31
67
 
32
68
  /**
@@ -75,7 +111,7 @@ class ApiInstance {
75
111
  callback(CircularJSON.stringify(error, null, 2))
76
112
  callback(CircularJSON.stringify(error.response, null, 2))
77
113
 
78
- if (this.retryStatuses.includes(error?.response?.status) && retryCount === 0) {
114
+ if (this.shouldRetry(error) && retryCount === 0) {
79
115
  await this.sleep(20000)
80
116
  return await this.get(url, params, headers, returnHeaders, callback, retryCount + 1)
81
117
  }
@@ -135,7 +171,7 @@ class ApiInstance {
135
171
  callback(CircularJSON.stringify(error, null, 2))
136
172
  callback(CircularJSON.stringify(error.response, null, 2))
137
173
 
138
- if (this.retryStatuses.includes(error?.response?.status) && retryCount === 0) {
174
+ if (this.shouldRetry(error) && retryCount === 0) {
139
175
  await this.sleep(20000)
140
176
  return await this.post(url, data, headers, params, callback, retryCount + 1)
141
177
  }
@@ -180,7 +216,7 @@ class ApiInstance {
180
216
 
181
217
  callback(error?.response)
182
218
 
183
- if (this.retryStatuses.includes(error?.response?.status) && retryCount === 0) {
219
+ if (this.shouldRetry(error) && retryCount === 0) {
184
220
  await this.sleep(20000)
185
221
  return await this.put(url, data, headers, params, callback, retryCount + 1)
186
222
  }
@@ -216,7 +252,7 @@ class ApiInstance {
216
252
 
217
253
  callback(error?.response)
218
254
 
219
- if (this.retryStatuses.includes(error?.response?.status) && retryCount === 0) {
255
+ if (this.shouldRetry(error) && retryCount === 0) {
220
256
  await this.sleep(20000)
221
257
  return await this.delete(url, callback, retryCount + 1)
222
258
  }