wavespeed 0.0.3 → 0.0.4

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/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export interface PredictionUrls {
16
16
  */
17
17
  export interface RequestOptions extends RequestInit {
18
18
  timeout?: number;
19
+ maxRetries?: number;
19
20
  }
20
21
  /**
21
22
  * Prediction model representing an image generation job
@@ -68,6 +69,14 @@ export declare class WaveSpeed {
68
69
  * @param options Fetch options
69
70
  */
70
71
  fetchWithTimeout(path: string, options?: RequestOptions): Promise<Response>;
72
+ /**
73
+ * Calculate backoff time with exponential backoff and jitter
74
+ * @param retryCount Current retry attempt number
75
+ * @param initialBackoff Initial backoff time in ms
76
+ * @returns Backoff time in ms
77
+ * @private
78
+ */
79
+ _getBackoffTime(retryCount: number, initialBackoff: number): number;
71
80
  /**
72
81
  * Generate an image and wait for the result
73
82
  *
package/dist/index.js CHANGED
@@ -107,23 +107,78 @@ class WaveSpeed {
107
107
  'Authorization': `Bearer ${this.apiKey}`,
108
108
  'Content-Type': 'application/json'
109
109
  };
110
- // Use AbortController for timeout (supported in modern browsers)
111
- const controller = new AbortController();
112
- const id = setTimeout(() => controller.abort(), timeout);
113
- try {
114
- // Use browser's built-in URL API
115
- const url = new URL(path, this.baseUrl).toString();
116
- // Use the global fetch API available in browsers
117
- const response = await fetch(url, {
118
- ...fetchOptions,
119
- signal: controller.signal
120
- });
121
- return response;
122
- }
123
- finally {
124
- clearTimeout(id);
110
+ // Default retry options
111
+ const maxRetries = options.maxRetries || 3;
112
+ const initialBackoff = 1000; // 1 second
113
+ let retryCount = 0;
114
+ // Function to determine if a response should be retried
115
+ const shouldRetry = (response) => {
116
+ // Retry on rate limit (429) for all requests
117
+ // For GET requests, also retry on server errors (5xx)
118
+ const method = (fetchOptions.method || 'GET').toUpperCase();
119
+ return response.status === 429 || (method === 'GET' && response.status >= 500);
120
+ };
121
+ while (true) {
122
+ // Use AbortController for timeout (supported in modern browsers)
123
+ const controller = new AbortController();
124
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
125
+ try {
126
+ // Construct the full URL by joining baseUrl and path
127
+ const url = new URL(path.startsWith('/') ? path.substring(1) : path, this.baseUrl).toString();
128
+ const response = await fetch(url, {
129
+ ...fetchOptions,
130
+ signal: controller.signal
131
+ });
132
+ // If the response is successful or we've used all retries, return it
133
+ if (response.ok || !shouldRetry(response) || retryCount >= maxRetries) {
134
+ return response;
135
+ }
136
+ // Otherwise, increment retry count and wait before retrying
137
+ retryCount++;
138
+ const backoffTime = this._getBackoffTime(retryCount, initialBackoff);
139
+ // Log retry information if console is available
140
+ if (typeof console !== 'undefined') {
141
+ console.warn(`Request failed with status ${response.status}. Retrying (${retryCount}/${maxRetries}) in ${Math.round(backoffTime)}ms...`);
142
+ }
143
+ // Wait for backoff time before retrying
144
+ await new Promise(resolve => setTimeout(resolve, backoffTime));
145
+ }
146
+ catch (error) {
147
+ // If the error is due to timeout or network issues and we have retries left
148
+ if (error instanceof Error &&
149
+ (error.name === 'AbortError' || error.name === 'TypeError') &&
150
+ retryCount < maxRetries) {
151
+ retryCount++;
152
+ const backoffTime = this._getBackoffTime(retryCount, initialBackoff);
153
+ // Log retry information if console is available
154
+ if (typeof console !== 'undefined') {
155
+ console.warn(`Request failed with error: ${error.message}. Retrying (${retryCount}/${maxRetries}) in ${Math.round(backoffTime)}ms...`);
156
+ }
157
+ // Wait for backoff time before retrying
158
+ await new Promise(resolve => setTimeout(resolve, backoffTime));
159
+ }
160
+ else {
161
+ // If we're out of retries or it's a non-retryable error, throw it
162
+ throw error;
163
+ }
164
+ }
165
+ finally {
166
+ clearTimeout(timeoutId);
167
+ }
125
168
  }
126
169
  }
170
+ /**
171
+ * Calculate backoff time with exponential backoff and jitter
172
+ * @param retryCount Current retry attempt number
173
+ * @param initialBackoff Initial backoff time in ms
174
+ * @returns Backoff time in ms
175
+ * @private
176
+ */
177
+ _getBackoffTime(retryCount, initialBackoff) {
178
+ const backoff = initialBackoff * Math.pow(2, retryCount);
179
+ // Add jitter (random value between 0 and backoff/2)
180
+ return backoff + Math.random() * (backoff / 2);
181
+ }
127
182
  /**
128
183
  * Generate an image and wait for the result
129
184
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wavespeed",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "WaveSpeed Client SDK for Wavespeed API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",