@scalemule/nextjs 0.0.1
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/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/client.d.mts +163 -0
- package/dist/client.d.ts +163 -0
- package/dist/client.js +700 -0
- package/dist/client.mjs +697 -0
- package/dist/index-BkacIKdu.d.mts +807 -0
- package/dist/index-BkacIKdu.d.ts +807 -0
- package/dist/index.d.mts +418 -0
- package/dist/index.d.ts +418 -0
- package/dist/index.js +3103 -0
- package/dist/index.mjs +3084 -0
- package/dist/server/auth.d.mts +38 -0
- package/dist/server/auth.d.ts +38 -0
- package/dist/server/auth.js +1088 -0
- package/dist/server/auth.mjs +1083 -0
- package/dist/server/index.d.mts +868 -0
- package/dist/server/index.d.ts +868 -0
- package/dist/server/index.js +2028 -0
- package/dist/server/index.mjs +1972 -0
- package/dist/server/webhook-handler.d.mts +2 -0
- package/dist/server/webhook-handler.d.ts +2 -0
- package/dist/server/webhook-handler.js +56 -0
- package/dist/server/webhook-handler.mjs +54 -0
- package/dist/testing.d.mts +109 -0
- package/dist/testing.d.ts +109 -0
- package/dist/testing.js +134 -0
- package/dist/testing.mjs +128 -0
- package/dist/webhook-handler-BPNqhuwL.d.ts +728 -0
- package/dist/webhook-handler-C-5_Ey1T.d.mts +728 -0
- package/package.json +99 -0
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
var GATEWAY_URLS = {
|
|
3
|
+
dev: "https://api-dev.scalemule.com",
|
|
4
|
+
prod: "https://api.scalemule.com"
|
|
5
|
+
};
|
|
6
|
+
var SESSION_STORAGE_KEY = "scalemule_session";
|
|
7
|
+
var USER_ID_STORAGE_KEY = "scalemule_user_id";
|
|
8
|
+
var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
9
|
+
function sleep(ms) {
|
|
10
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
|
+
}
|
|
12
|
+
function getBackoffDelay(attempt, baseDelay = 1e3) {
|
|
13
|
+
const exponentialDelay = baseDelay * Math.pow(2, attempt);
|
|
14
|
+
const jitter = Math.random() * 0.3 * exponentialDelay;
|
|
15
|
+
return Math.min(exponentialDelay + jitter, 3e4);
|
|
16
|
+
}
|
|
17
|
+
function sanitizeFilename(filename) {
|
|
18
|
+
let sanitized = filename.replace(/[\x00-\x1f\x7f]/g, "");
|
|
19
|
+
sanitized = sanitized.replace(/["\\/\n\r]/g, "_").normalize("NFC").replace(/[\u200b-\u200f\ufeff\u2028\u2029]/g, "");
|
|
20
|
+
if (!sanitized || sanitized.trim() === "") {
|
|
21
|
+
sanitized = "unnamed";
|
|
22
|
+
}
|
|
23
|
+
if (sanitized.length > 200) {
|
|
24
|
+
const ext = sanitized.split(".").pop();
|
|
25
|
+
const base = sanitized.substring(0, 190);
|
|
26
|
+
sanitized = ext ? `${base}.${ext}` : base;
|
|
27
|
+
}
|
|
28
|
+
return sanitized.trim();
|
|
29
|
+
}
|
|
30
|
+
var RateLimitQueue = class {
|
|
31
|
+
constructor() {
|
|
32
|
+
this.queue = [];
|
|
33
|
+
this.processing = false;
|
|
34
|
+
this.rateLimitedUntil = 0;
|
|
35
|
+
this.requestsInWindow = 0;
|
|
36
|
+
this.windowStart = Date.now();
|
|
37
|
+
this.maxRequestsPerWindow = 100;
|
|
38
|
+
this.windowDurationMs = 6e4;
|
|
39
|
+
}
|
|
40
|
+
// 1 minute
|
|
41
|
+
/**
|
|
42
|
+
* Add request to queue
|
|
43
|
+
*/
|
|
44
|
+
enqueue(execute, priority = 0) {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
this.queue.push({
|
|
47
|
+
execute,
|
|
48
|
+
resolve,
|
|
49
|
+
reject,
|
|
50
|
+
priority
|
|
51
|
+
});
|
|
52
|
+
this.queue.sort((a, b) => b.priority - a.priority);
|
|
53
|
+
this.processQueue();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Process queued requests
|
|
58
|
+
*/
|
|
59
|
+
async processQueue() {
|
|
60
|
+
if (this.processing || this.queue.length === 0) return;
|
|
61
|
+
this.processing = true;
|
|
62
|
+
while (this.queue.length > 0) {
|
|
63
|
+
const now = Date.now();
|
|
64
|
+
if (now < this.rateLimitedUntil) {
|
|
65
|
+
const waitTime = this.rateLimitedUntil - now;
|
|
66
|
+
await sleep(waitTime);
|
|
67
|
+
}
|
|
68
|
+
if (now - this.windowStart >= this.windowDurationMs) {
|
|
69
|
+
this.windowStart = now;
|
|
70
|
+
this.requestsInWindow = 0;
|
|
71
|
+
}
|
|
72
|
+
if (this.requestsInWindow >= this.maxRequestsPerWindow) {
|
|
73
|
+
const waitTime = this.windowDurationMs - (now - this.windowStart);
|
|
74
|
+
await sleep(waitTime);
|
|
75
|
+
this.windowStart = Date.now();
|
|
76
|
+
this.requestsInWindow = 0;
|
|
77
|
+
}
|
|
78
|
+
const request = this.queue.shift();
|
|
79
|
+
if (!request) continue;
|
|
80
|
+
try {
|
|
81
|
+
this.requestsInWindow++;
|
|
82
|
+
const result = await request.execute();
|
|
83
|
+
if (!result.success && result.error?.code === "RATE_LIMITED") {
|
|
84
|
+
this.queue.unshift(request);
|
|
85
|
+
this.rateLimitedUntil = Date.now() + 6e4;
|
|
86
|
+
} else {
|
|
87
|
+
request.resolve(result);
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
request.reject(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
this.processing = false;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Update rate limit from response headers
|
|
97
|
+
*/
|
|
98
|
+
updateFromHeaders(headers) {
|
|
99
|
+
const retryAfter = headers.get("Retry-After");
|
|
100
|
+
if (retryAfter) {
|
|
101
|
+
const seconds = parseInt(retryAfter, 10);
|
|
102
|
+
if (!isNaN(seconds)) {
|
|
103
|
+
this.rateLimitedUntil = Date.now() + seconds * 1e3;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const remaining = headers.get("X-RateLimit-Remaining");
|
|
107
|
+
if (remaining) {
|
|
108
|
+
const count = parseInt(remaining, 10);
|
|
109
|
+
if (!isNaN(count) && count === 0) {
|
|
110
|
+
const reset = headers.get("X-RateLimit-Reset");
|
|
111
|
+
if (reset) {
|
|
112
|
+
const resetTime = parseInt(reset, 10) * 1e3;
|
|
113
|
+
if (!isNaN(resetTime)) {
|
|
114
|
+
this.rateLimitedUntil = resetTime;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get queue length
|
|
122
|
+
*/
|
|
123
|
+
get length() {
|
|
124
|
+
return this.queue.length;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Check if rate limited
|
|
128
|
+
*/
|
|
129
|
+
get isRateLimited() {
|
|
130
|
+
return Date.now() < this.rateLimitedUntil;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var OfflineQueue = class {
|
|
134
|
+
constructor(storage) {
|
|
135
|
+
this.queue = [];
|
|
136
|
+
this.storageKey = "scalemule_offline_queue";
|
|
137
|
+
this.isOnline = true;
|
|
138
|
+
this.onOnline = null;
|
|
139
|
+
this.storage = storage;
|
|
140
|
+
this.loadFromStorage();
|
|
141
|
+
this.setupOnlineListener();
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Setup online/offline event listeners
|
|
145
|
+
*/
|
|
146
|
+
setupOnlineListener() {
|
|
147
|
+
if (typeof window === "undefined") return;
|
|
148
|
+
this.isOnline = navigator.onLine;
|
|
149
|
+
window.addEventListener("online", () => {
|
|
150
|
+
this.isOnline = true;
|
|
151
|
+
if (this.onOnline) this.onOnline();
|
|
152
|
+
});
|
|
153
|
+
window.addEventListener("offline", () => {
|
|
154
|
+
this.isOnline = false;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Load queue from storage
|
|
159
|
+
*/
|
|
160
|
+
async loadFromStorage() {
|
|
161
|
+
try {
|
|
162
|
+
const data = await this.storage.getItem(this.storageKey);
|
|
163
|
+
if (data) {
|
|
164
|
+
this.queue = JSON.parse(data);
|
|
165
|
+
}
|
|
166
|
+
} catch {
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Save queue to storage
|
|
171
|
+
*/
|
|
172
|
+
async saveToStorage() {
|
|
173
|
+
try {
|
|
174
|
+
await this.storage.setItem(this.storageKey, JSON.stringify(this.queue));
|
|
175
|
+
} catch {
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Add request to offline queue
|
|
180
|
+
*/
|
|
181
|
+
async add(method, path, body) {
|
|
182
|
+
const item = {
|
|
183
|
+
id: `${Date.now()}-${Math.random().toString(36).slice(2)}`,
|
|
184
|
+
method,
|
|
185
|
+
path,
|
|
186
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
187
|
+
timestamp: Date.now()
|
|
188
|
+
};
|
|
189
|
+
this.queue.push(item);
|
|
190
|
+
await this.saveToStorage();
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get all queued requests
|
|
194
|
+
*/
|
|
195
|
+
getAll() {
|
|
196
|
+
return [...this.queue];
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Remove a request from queue
|
|
200
|
+
*/
|
|
201
|
+
async remove(id) {
|
|
202
|
+
this.queue = this.queue.filter((item) => item.id !== id);
|
|
203
|
+
await this.saveToStorage();
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Clear all queued requests
|
|
207
|
+
*/
|
|
208
|
+
async clear() {
|
|
209
|
+
this.queue = [];
|
|
210
|
+
await this.saveToStorage();
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Set callback for when coming back online
|
|
214
|
+
*/
|
|
215
|
+
setOnlineCallback(callback) {
|
|
216
|
+
this.onOnline = callback;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Check if currently online
|
|
220
|
+
*/
|
|
221
|
+
get online() {
|
|
222
|
+
return this.isOnline;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Get queue length
|
|
226
|
+
*/
|
|
227
|
+
get length() {
|
|
228
|
+
return this.queue.length;
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
function resolveGatewayUrl(config) {
|
|
232
|
+
if (config.gatewayUrl) {
|
|
233
|
+
return config.gatewayUrl;
|
|
234
|
+
}
|
|
235
|
+
const env = config.environment || "prod";
|
|
236
|
+
return GATEWAY_URLS[env];
|
|
237
|
+
}
|
|
238
|
+
function createDefaultStorage() {
|
|
239
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
240
|
+
return {
|
|
241
|
+
getItem: (key) => localStorage.getItem(key),
|
|
242
|
+
setItem: (key, value) => localStorage.setItem(key, value),
|
|
243
|
+
removeItem: (key) => localStorage.removeItem(key)
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
const memoryStorage = /* @__PURE__ */ new Map();
|
|
247
|
+
return {
|
|
248
|
+
getItem: (key) => memoryStorage.get(key) ?? null,
|
|
249
|
+
setItem: (key, value) => {
|
|
250
|
+
memoryStorage.set(key, value);
|
|
251
|
+
},
|
|
252
|
+
removeItem: (key) => {
|
|
253
|
+
memoryStorage.delete(key);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
var ScaleMuleClient = class {
|
|
258
|
+
constructor(config) {
|
|
259
|
+
this.applicationId = null;
|
|
260
|
+
this.sessionToken = null;
|
|
261
|
+
this.userId = null;
|
|
262
|
+
this.rateLimitQueue = null;
|
|
263
|
+
this.offlineQueue = null;
|
|
264
|
+
this.apiKey = config.apiKey;
|
|
265
|
+
this.applicationId = config.applicationId || null;
|
|
266
|
+
this.gatewayUrl = resolveGatewayUrl(config);
|
|
267
|
+
this.debug = config.debug || false;
|
|
268
|
+
this.storage = config.storage || createDefaultStorage();
|
|
269
|
+
this.enableRateLimitQueue = config.enableRateLimitQueue || false;
|
|
270
|
+
this.enableOfflineQueue = config.enableOfflineQueue || false;
|
|
271
|
+
if (this.enableRateLimitQueue) {
|
|
272
|
+
this.rateLimitQueue = new RateLimitQueue();
|
|
273
|
+
}
|
|
274
|
+
if (this.enableOfflineQueue) {
|
|
275
|
+
this.offlineQueue = new OfflineQueue(this.storage);
|
|
276
|
+
this.offlineQueue.setOnlineCallback(() => this.syncOfflineQueue());
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Sync offline queue when coming back online
|
|
281
|
+
*/
|
|
282
|
+
async syncOfflineQueue() {
|
|
283
|
+
if (!this.offlineQueue) return;
|
|
284
|
+
const items = this.offlineQueue.getAll();
|
|
285
|
+
if (this.debug && items.length > 0) {
|
|
286
|
+
console.log(`[ScaleMule] Syncing ${items.length} offline requests`);
|
|
287
|
+
}
|
|
288
|
+
for (const item of items) {
|
|
289
|
+
try {
|
|
290
|
+
await this.request(item.path, {
|
|
291
|
+
method: item.method,
|
|
292
|
+
body: item.body,
|
|
293
|
+
skipRetry: true
|
|
294
|
+
});
|
|
295
|
+
await this.offlineQueue.remove(item.id);
|
|
296
|
+
} catch (err) {
|
|
297
|
+
if (this.debug) {
|
|
298
|
+
console.error("[ScaleMule] Failed to sync offline request:", err);
|
|
299
|
+
}
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Check if client is online
|
|
306
|
+
*/
|
|
307
|
+
isOnline() {
|
|
308
|
+
if (this.offlineQueue) {
|
|
309
|
+
return this.offlineQueue.online;
|
|
310
|
+
}
|
|
311
|
+
return typeof navigator === "undefined" || navigator.onLine;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Get number of pending offline requests
|
|
315
|
+
*/
|
|
316
|
+
getOfflineQueueLength() {
|
|
317
|
+
return this.offlineQueue?.length || 0;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Get number of pending rate-limited requests
|
|
321
|
+
*/
|
|
322
|
+
getRateLimitQueueLength() {
|
|
323
|
+
return this.rateLimitQueue?.length || 0;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Check if currently rate limited
|
|
327
|
+
*/
|
|
328
|
+
isRateLimited() {
|
|
329
|
+
return this.rateLimitQueue?.isRateLimited || false;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Get the gateway URL
|
|
333
|
+
*/
|
|
334
|
+
getGatewayUrl() {
|
|
335
|
+
return this.gatewayUrl;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Get the application ID (required for realtime features)
|
|
339
|
+
*/
|
|
340
|
+
getApplicationId() {
|
|
341
|
+
return this.applicationId;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Initialize client by loading persisted session
|
|
345
|
+
*/
|
|
346
|
+
async initialize() {
|
|
347
|
+
const token = await this.storage.getItem(SESSION_STORAGE_KEY);
|
|
348
|
+
const userId = await this.storage.getItem(USER_ID_STORAGE_KEY);
|
|
349
|
+
if (token) this.sessionToken = token;
|
|
350
|
+
if (userId) this.userId = userId;
|
|
351
|
+
if (this.debug) {
|
|
352
|
+
console.log("[ScaleMule] Initialized with session:", !!token);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Set session after login
|
|
357
|
+
*/
|
|
358
|
+
async setSession(token, userId) {
|
|
359
|
+
this.sessionToken = token;
|
|
360
|
+
this.userId = userId;
|
|
361
|
+
await this.storage.setItem(SESSION_STORAGE_KEY, token);
|
|
362
|
+
await this.storage.setItem(USER_ID_STORAGE_KEY, userId);
|
|
363
|
+
if (this.debug) {
|
|
364
|
+
console.log("[ScaleMule] Session set for user:", userId);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Clear session on logout
|
|
369
|
+
*/
|
|
370
|
+
async clearSession() {
|
|
371
|
+
this.sessionToken = null;
|
|
372
|
+
this.userId = null;
|
|
373
|
+
await this.storage.removeItem(SESSION_STORAGE_KEY);
|
|
374
|
+
await this.storage.removeItem(USER_ID_STORAGE_KEY);
|
|
375
|
+
if (this.debug) {
|
|
376
|
+
console.log("[ScaleMule] Session cleared");
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Get current session token
|
|
381
|
+
*/
|
|
382
|
+
getSessionToken() {
|
|
383
|
+
return this.sessionToken;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Get current user ID
|
|
387
|
+
*/
|
|
388
|
+
getUserId() {
|
|
389
|
+
return this.userId;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Check if client has an active session
|
|
393
|
+
*/
|
|
394
|
+
isAuthenticated() {
|
|
395
|
+
return this.sessionToken !== null && this.userId !== null;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Build headers for a request
|
|
399
|
+
*/
|
|
400
|
+
buildHeaders(options) {
|
|
401
|
+
const headers = new Headers(options?.headers);
|
|
402
|
+
headers.set("x-api-key", this.apiKey);
|
|
403
|
+
if (!options?.skipAuth && this.sessionToken) {
|
|
404
|
+
headers.set("Authorization", `Bearer ${this.sessionToken}`);
|
|
405
|
+
}
|
|
406
|
+
if (!headers.has("Content-Type") && options?.body && typeof options.body === "string") {
|
|
407
|
+
headers.set("Content-Type", "application/json");
|
|
408
|
+
}
|
|
409
|
+
return headers;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Make an HTTP request to the ScaleMule API
|
|
413
|
+
*/
|
|
414
|
+
async request(path, options = {}) {
|
|
415
|
+
const url = `${this.gatewayUrl}${path}`;
|
|
416
|
+
const headers = this.buildHeaders(options);
|
|
417
|
+
const maxRetries = options.skipRetry ? 0 : options.retries ?? 2;
|
|
418
|
+
const timeout = options.timeout || 3e4;
|
|
419
|
+
if (this.debug) {
|
|
420
|
+
console.log(`[ScaleMule] ${options.method || "GET"} ${path}`);
|
|
421
|
+
}
|
|
422
|
+
let lastError = null;
|
|
423
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
424
|
+
const controller = new AbortController();
|
|
425
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
426
|
+
try {
|
|
427
|
+
const response = await fetch(url, {
|
|
428
|
+
...options,
|
|
429
|
+
headers,
|
|
430
|
+
signal: controller.signal
|
|
431
|
+
});
|
|
432
|
+
clearTimeout(timeoutId);
|
|
433
|
+
const data = await response.json();
|
|
434
|
+
if (!response.ok) {
|
|
435
|
+
const error = data.error || {
|
|
436
|
+
code: `HTTP_${response.status}`,
|
|
437
|
+
message: data.message || response.statusText
|
|
438
|
+
};
|
|
439
|
+
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
440
|
+
lastError = error;
|
|
441
|
+
const delay = getBackoffDelay(attempt);
|
|
442
|
+
if (this.debug) {
|
|
443
|
+
console.log(`[ScaleMule] Retrying in ${delay}ms (attempt ${attempt + 1}/${maxRetries})`);
|
|
444
|
+
}
|
|
445
|
+
await sleep(delay);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (this.debug) {
|
|
449
|
+
console.error("[ScaleMule] Request failed:", error);
|
|
450
|
+
}
|
|
451
|
+
return { success: false, error };
|
|
452
|
+
}
|
|
453
|
+
return data;
|
|
454
|
+
} catch (err) {
|
|
455
|
+
clearTimeout(timeoutId);
|
|
456
|
+
const error = {
|
|
457
|
+
code: err instanceof Error && err.name === "AbortError" ? "TIMEOUT" : "NETWORK_ERROR",
|
|
458
|
+
message: err instanceof Error ? err.message : "Network request failed"
|
|
459
|
+
};
|
|
460
|
+
if (attempt < maxRetries) {
|
|
461
|
+
lastError = error;
|
|
462
|
+
const delay = getBackoffDelay(attempt);
|
|
463
|
+
if (this.debug) {
|
|
464
|
+
console.log(`[ScaleMule] Retrying after error in ${delay}ms (attempt ${attempt + 1}/${maxRetries})`);
|
|
465
|
+
}
|
|
466
|
+
await sleep(delay);
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
if (this.debug) {
|
|
470
|
+
console.error("[ScaleMule] Network error:", err);
|
|
471
|
+
}
|
|
472
|
+
return { success: false, error };
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return { success: false, error: lastError || { code: "UNKNOWN", message: "Request failed" } };
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* GET request
|
|
479
|
+
*/
|
|
480
|
+
async get(path, options) {
|
|
481
|
+
return this.request(path, { ...options, method: "GET" });
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* POST request with JSON body
|
|
485
|
+
*/
|
|
486
|
+
async post(path, body, options) {
|
|
487
|
+
return this.request(path, {
|
|
488
|
+
...options,
|
|
489
|
+
method: "POST",
|
|
490
|
+
body: body ? JSON.stringify(body) : void 0
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* PUT request with JSON body
|
|
495
|
+
*/
|
|
496
|
+
async put(path, body, options) {
|
|
497
|
+
return this.request(path, {
|
|
498
|
+
...options,
|
|
499
|
+
method: "PUT",
|
|
500
|
+
body: body ? JSON.stringify(body) : void 0
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* PATCH request with JSON body
|
|
505
|
+
*/
|
|
506
|
+
async patch(path, body, options) {
|
|
507
|
+
return this.request(path, {
|
|
508
|
+
...options,
|
|
509
|
+
method: "PATCH",
|
|
510
|
+
body: body ? JSON.stringify(body) : void 0
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* DELETE request
|
|
515
|
+
*/
|
|
516
|
+
async delete(path, options) {
|
|
517
|
+
return this.request(path, { ...options, method: "DELETE" });
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Upload a file using multipart/form-data
|
|
521
|
+
*
|
|
522
|
+
* Automatically includes Authorization: Bearer header for user identity.
|
|
523
|
+
* Supports progress callback via XMLHttpRequest when onProgress is provided.
|
|
524
|
+
*/
|
|
525
|
+
async upload(path, file, additionalFields, options) {
|
|
526
|
+
const sanitizedName = sanitizeFilename(file.name);
|
|
527
|
+
const sanitizedFile = sanitizedName !== file.name ? new File([file], sanitizedName, { type: file.type }) : file;
|
|
528
|
+
const formData = new FormData();
|
|
529
|
+
formData.append("file", sanitizedFile);
|
|
530
|
+
if (this.userId) {
|
|
531
|
+
formData.append("sm_user_id", this.userId);
|
|
532
|
+
}
|
|
533
|
+
if (additionalFields) {
|
|
534
|
+
for (const [key, value] of Object.entries(additionalFields)) {
|
|
535
|
+
formData.append(key, value);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
const url = `${this.gatewayUrl}${path}`;
|
|
539
|
+
if (this.debug) {
|
|
540
|
+
console.log(`[ScaleMule] UPLOAD ${path}`);
|
|
541
|
+
}
|
|
542
|
+
if (options?.onProgress && typeof XMLHttpRequest !== "undefined") {
|
|
543
|
+
return this.uploadWithProgress(url, formData, options.onProgress);
|
|
544
|
+
}
|
|
545
|
+
const maxRetries = options?.retries ?? 2;
|
|
546
|
+
const headers = new Headers();
|
|
547
|
+
headers.set("x-api-key", this.apiKey);
|
|
548
|
+
if (this.sessionToken) {
|
|
549
|
+
headers.set("Authorization", `Bearer ${this.sessionToken}`);
|
|
550
|
+
}
|
|
551
|
+
let lastError = null;
|
|
552
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
553
|
+
try {
|
|
554
|
+
const retryFormData = attempt === 0 ? formData : new FormData();
|
|
555
|
+
if (attempt > 0) {
|
|
556
|
+
retryFormData.append("file", sanitizedFile);
|
|
557
|
+
if (this.userId) {
|
|
558
|
+
retryFormData.append("sm_user_id", this.userId);
|
|
559
|
+
}
|
|
560
|
+
if (additionalFields) {
|
|
561
|
+
for (const [key, value] of Object.entries(additionalFields)) {
|
|
562
|
+
retryFormData.append(key, value);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
const response = await fetch(url, {
|
|
567
|
+
method: "POST",
|
|
568
|
+
headers,
|
|
569
|
+
body: retryFormData
|
|
570
|
+
});
|
|
571
|
+
const data = await response.json();
|
|
572
|
+
if (!response.ok) {
|
|
573
|
+
const error = data.error || {
|
|
574
|
+
code: `HTTP_${response.status}`,
|
|
575
|
+
message: data.message || response.statusText
|
|
576
|
+
};
|
|
577
|
+
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
578
|
+
lastError = error;
|
|
579
|
+
const delay = getBackoffDelay(attempt);
|
|
580
|
+
if (this.debug) {
|
|
581
|
+
console.log(`[ScaleMule] Upload retry ${attempt + 1}/${maxRetries} after ${delay}ms`);
|
|
582
|
+
}
|
|
583
|
+
await sleep(delay);
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
return { success: false, error };
|
|
587
|
+
}
|
|
588
|
+
return data;
|
|
589
|
+
} catch (err) {
|
|
590
|
+
lastError = {
|
|
591
|
+
code: "UPLOAD_ERROR",
|
|
592
|
+
message: err instanceof Error ? err.message : "Upload failed"
|
|
593
|
+
};
|
|
594
|
+
if (attempt < maxRetries) {
|
|
595
|
+
const delay = getBackoffDelay(attempt);
|
|
596
|
+
if (this.debug) {
|
|
597
|
+
console.log(`[ScaleMule] Upload retry ${attempt + 1}/${maxRetries} after ${delay}ms (network error)`);
|
|
598
|
+
}
|
|
599
|
+
await sleep(delay);
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return {
|
|
605
|
+
success: false,
|
|
606
|
+
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Upload with progress using XMLHttpRequest (with retry)
|
|
611
|
+
*/
|
|
612
|
+
async uploadWithProgress(url, formData, onProgress, maxRetries = 2) {
|
|
613
|
+
let lastError = null;
|
|
614
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
615
|
+
const result = await this.singleUploadWithProgress(url, formData, onProgress);
|
|
616
|
+
if (result.success) {
|
|
617
|
+
return result;
|
|
618
|
+
}
|
|
619
|
+
const errorCode = result.error?.code || "";
|
|
620
|
+
const isNetworkError = errorCode === "UPLOAD_ERROR" || errorCode === "NETWORK_ERROR";
|
|
621
|
+
const isRetryableHttp = errorCode.startsWith("HTTP_") && RETRYABLE_STATUS_CODES.has(parseInt(errorCode.replace("HTTP_", ""), 10));
|
|
622
|
+
if (attempt < maxRetries && (isNetworkError || isRetryableHttp)) {
|
|
623
|
+
lastError = result.error || null;
|
|
624
|
+
const delay = getBackoffDelay(attempt);
|
|
625
|
+
if (this.debug) {
|
|
626
|
+
console.log(`[ScaleMule] Upload retry ${attempt + 1}/${maxRetries} after ${delay}ms`);
|
|
627
|
+
}
|
|
628
|
+
await sleep(delay);
|
|
629
|
+
onProgress(0);
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
return result;
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
success: false,
|
|
636
|
+
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Single upload attempt with progress using XMLHttpRequest
|
|
641
|
+
*/
|
|
642
|
+
singleUploadWithProgress(url, formData, onProgress) {
|
|
643
|
+
return new Promise((resolve) => {
|
|
644
|
+
const xhr = new XMLHttpRequest();
|
|
645
|
+
xhr.upload.addEventListener("progress", (event) => {
|
|
646
|
+
if (event.lengthComputable) {
|
|
647
|
+
const progress = Math.round(event.loaded / event.total * 100);
|
|
648
|
+
onProgress(progress);
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
xhr.addEventListener("load", () => {
|
|
652
|
+
try {
|
|
653
|
+
const data = JSON.parse(xhr.responseText);
|
|
654
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
655
|
+
resolve(data);
|
|
656
|
+
} else {
|
|
657
|
+
resolve({
|
|
658
|
+
success: false,
|
|
659
|
+
error: data.error || {
|
|
660
|
+
code: `HTTP_${xhr.status}`,
|
|
661
|
+
message: data.message || "Upload failed"
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
} catch {
|
|
666
|
+
resolve({
|
|
667
|
+
success: false,
|
|
668
|
+
error: { code: "PARSE_ERROR", message: "Failed to parse response" }
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
xhr.addEventListener("error", () => {
|
|
673
|
+
resolve({
|
|
674
|
+
success: false,
|
|
675
|
+
error: { code: "UPLOAD_ERROR", message: "Upload failed" }
|
|
676
|
+
});
|
|
677
|
+
});
|
|
678
|
+
xhr.addEventListener("abort", () => {
|
|
679
|
+
resolve({
|
|
680
|
+
success: false,
|
|
681
|
+
error: { code: "UPLOAD_ABORTED", message: "Upload cancelled" }
|
|
682
|
+
});
|
|
683
|
+
});
|
|
684
|
+
xhr.open("POST", url);
|
|
685
|
+
xhr.setRequestHeader("x-api-key", this.apiKey);
|
|
686
|
+
if (this.sessionToken) {
|
|
687
|
+
xhr.setRequestHeader("Authorization", `Bearer ${this.sessionToken}`);
|
|
688
|
+
}
|
|
689
|
+
xhr.send(formData);
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
function createClient(config) {
|
|
694
|
+
return new ScaleMuleClient(config);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
export { ScaleMuleClient, createClient };
|