@zodic/shared 0.0.424 → 0.0.425

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.
Files changed (2) hide show
  1. package/app/api/index.ts +45 -16
  2. package/package.json +1 -1
package/app/api/index.ts CHANGED
@@ -182,25 +182,54 @@ export const Api = (env: BackendBindings) => ({
182
182
  body['init_strength'] = initStrength;
183
183
  }
184
184
 
185
- try {
186
- const response = await fetch(endpoint, {
187
- method: 'POST',
188
- headers,
189
- body: JSON.stringify(body),
190
- });
185
+ const maxRetries = 3;
186
+ const baseDelayMs = 2000;
187
+
188
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
189
+ try {
190
+ const response = await fetch(endpoint, {
191
+ method: 'POST',
192
+ headers,
193
+ body: JSON.stringify(body),
194
+ });
191
195
 
192
- if (!response.ok) {
193
- const error = await response.json();
194
- console.error('❌ Error from Leonardo API:', error);
195
- throw new Error(`Leonardo API Error: ${response.status}`);
196
- }
196
+ if (!response.ok) {
197
+ const error = await response.json().catch(() => ({}));
198
+ const isRetryable = response.status >= 500 || response.status === 429;
197
199
 
198
- const data = (await response.json()) as LeonardoGenerateImageResponse;
199
- return data;
200
- } catch (err: any) {
201
- console.error('❌ Error calling Leonardo API:', err.message);
202
- throw err;
200
+ if (isRetryable && attempt < maxRetries) {
201
+ const delayMs = baseDelayMs * Math.pow(2, attempt - 1);
202
+ console.warn(
203
+ `Leonardo API ${response.status} (attempt ${attempt}/${maxRetries}), retrying in ${delayMs}ms`,
204
+ error
205
+ );
206
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
207
+ continue;
208
+ }
209
+
210
+ console.error('❌ Error from Leonardo API:', error);
211
+ throw new Error(`Leonardo API Error: ${response.status}`);
212
+ }
213
+
214
+ const data = (await response.json()) as LeonardoGenerateImageResponse;
215
+ return data;
216
+ } catch (err: any) {
217
+ const isNetworkError = !err.message?.startsWith('Leonardo API Error');
218
+ if (isNetworkError && attempt < maxRetries) {
219
+ const delayMs = baseDelayMs * Math.pow(2, attempt - 1);
220
+ console.warn(
221
+ `Leonardo API network error (attempt ${attempt}/${maxRetries}), retrying in ${delayMs}ms:`,
222
+ err.message
223
+ );
224
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
225
+ continue;
226
+ }
227
+ console.error('❌ Error calling Leonardo API:', err.message);
228
+ throw err;
229
+ }
203
230
  }
231
+
232
+ throw new Error('Leonardo API: max retries exhausted');
204
233
  },
205
234
 
206
235
  generatePresignedUrl: async (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zodic/shared",
3
- "version": "0.0.424",
3
+ "version": "0.0.425",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "publishConfig": {