wavespeed 0.0.2 → 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
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ /**
3
+ * Input parameters for image generation
4
+ */
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.WaveSpeed = exports.Prediction = void 0;
4
- const url_1 = require("url");
5
7
  /**
6
8
  * Prediction model representing an image generation job
7
9
  */
@@ -73,15 +75,23 @@ class WaveSpeed {
73
75
  */
74
76
  constructor(apiKey, options = {}) {
75
77
  this.baseUrl = 'https://api.wavespeed.ai/api/v2/';
76
- this.apiKey = apiKey || process.env.WAVESPEED_API_KEY || '';
78
+ // Browser-friendly environment variable handling
79
+ const getEnvVar = (name) => {
80
+ // Try to get from process.env for Node.js environments
81
+ if (typeof process !== 'undefined' && process.env && process.env[name]) {
82
+ return process.env[name];
83
+ }
84
+ return undefined;
85
+ };
86
+ this.apiKey = apiKey || getEnvVar('WAVESPEED_API_KEY') || '';
77
87
  if (!this.apiKey) {
78
88
  throw new Error('API key is required. Provide it as a parameter or set the WAVESPEED_API_KEY environment variable.');
79
89
  }
80
90
  if (options.baseUrl) {
81
91
  this.baseUrl = options.baseUrl;
82
92
  }
83
- this.pollInterval = options.pollInterval || Number(process.env.WAVESPEED_POLL_INTERVAL) || 1;
84
- this.timeout = options.timeout || Number(process.env.WAVESPEED_TIMEOUT) || 60;
93
+ this.pollInterval = options.pollInterval || Number(getEnvVar('WAVESPEED_POLL_INTERVAL')) || 1;
94
+ this.timeout = options.timeout || Number(getEnvVar('WAVESPEED_TIMEOUT')) || 60;
85
95
  }
86
96
  /**
87
97
  * Fetch with timeout support
@@ -97,21 +107,78 @@ class WaveSpeed {
97
107
  'Authorization': `Bearer ${this.apiKey}`,
98
108
  'Content-Type': 'application/json'
99
109
  };
100
- const controller = new AbortController();
101
- const id = setTimeout(() => controller.abort(), timeout);
102
- try {
103
- // Construct the full URL by joining baseUrl and path
104
- const url = new url_1.URL(path, this.baseUrl).toString();
105
- const response = await fetch(url, {
106
- ...fetchOptions,
107
- signal: controller.signal
108
- });
109
- return response;
110
- }
111
- finally {
112
- 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
+ }
113
168
  }
114
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
+ }
115
182
  /**
116
183
  * Generate an image and wait for the result
117
184
  *
@@ -147,3 +214,7 @@ class WaveSpeed {
147
214
  exports.WaveSpeed = WaveSpeed;
148
215
  // Export default and named exports for different import styles
149
216
  exports.default = WaveSpeed;
217
+ // Add browser global for UMD-style usage
218
+ if (typeof window !== 'undefined') {
219
+ window.WaveSpeed = WaveSpeed;
220
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wavespeed",
3
- "version": "0.0.2",
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",
@@ -34,4 +34,4 @@
34
34
  "files": [
35
35
  "dist"
36
36
  ]
37
- }
37
+ }