@scalemule/nextjs 0.0.1 → 0.0.2
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/client.d.mts +8 -8
- package/dist/client.d.ts +8 -8
- package/dist/client.js +65 -59
- package/dist/client.mjs +65 -59
- package/dist/{index-BkacIKdu.d.mts → index-9v0SaLgg.d.mts} +8 -1
- package/dist/{index-BkacIKdu.d.ts → index-9v0SaLgg.d.ts} +8 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +455 -514
- package/dist/index.mjs +455 -515
- package/dist/server/auth.js +116 -78
- package/dist/server/auth.mjs +116 -78
- package/dist/server/index.d.mts +14 -11
- package/dist/server/index.d.ts +14 -11
- package/dist/server/index.js +209 -175
- package/dist/server/index.mjs +209 -175
- package/dist/server/webhook-handler.d.mts +2 -2
- package/dist/server/webhook-handler.d.ts +2 -2
- package/dist/testing.d.mts +10 -10
- package/dist/testing.d.ts +10 -10
- package/dist/testing.js +13 -7
- package/dist/testing.mjs +13 -7
- package/dist/{webhook-handler-BPNqhuwL.d.ts → webhook-handler-DCSwldKC.d.mts} +66 -66
- package/dist/{webhook-handler-C-5_Ey1T.d.mts → webhook-handler-Ymeice_x.d.ts} +66 -66
- package/package.json +1 -1
package/dist/client.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { j as StorageAdapter } from './index-9v0SaLgg.mjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* ScaleMule API Client
|
|
@@ -116,27 +116,27 @@ declare class ScaleMuleClient {
|
|
|
116
116
|
/**
|
|
117
117
|
* Make an HTTP request to the ScaleMule API
|
|
118
118
|
*/
|
|
119
|
-
request<T>(path: string, options?: RequestOptions): Promise<
|
|
119
|
+
request<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
120
120
|
/**
|
|
121
121
|
* GET request
|
|
122
122
|
*/
|
|
123
|
-
get<T>(path: string, options?: RequestOptions): Promise<
|
|
123
|
+
get<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
124
124
|
/**
|
|
125
125
|
* POST request with JSON body
|
|
126
126
|
*/
|
|
127
|
-
post<T>(path: string, body?: unknown, options?: RequestOptions): Promise<
|
|
127
|
+
post<T>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
128
128
|
/**
|
|
129
129
|
* PUT request with JSON body
|
|
130
130
|
*/
|
|
131
|
-
put<T>(path: string, body?: unknown, options?: RequestOptions): Promise<
|
|
131
|
+
put<T>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
132
132
|
/**
|
|
133
133
|
* PATCH request with JSON body
|
|
134
134
|
*/
|
|
135
|
-
patch<T>(path: string, body?: unknown, options?: RequestOptions): Promise<
|
|
135
|
+
patch<T>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
136
136
|
/**
|
|
137
137
|
* DELETE request
|
|
138
138
|
*/
|
|
139
|
-
delete<T>(path: string, options?: RequestOptions): Promise<
|
|
139
|
+
delete<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
140
140
|
/**
|
|
141
141
|
* Upload a file using multipart/form-data
|
|
142
142
|
*
|
|
@@ -145,7 +145,7 @@ declare class ScaleMuleClient {
|
|
|
145
145
|
*/
|
|
146
146
|
upload<T>(path: string, file: File, additionalFields?: Record<string, string>, options?: RequestOptions & {
|
|
147
147
|
onProgress?: (progress: number) => void;
|
|
148
|
-
}): Promise<
|
|
148
|
+
}): Promise<T>;
|
|
149
149
|
/**
|
|
150
150
|
* Upload with progress using XMLHttpRequest (with retry)
|
|
151
151
|
*/
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { j as StorageAdapter } from './index-9v0SaLgg.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* ScaleMule API Client
|
|
@@ -116,27 +116,27 @@ declare class ScaleMuleClient {
|
|
|
116
116
|
/**
|
|
117
117
|
* Make an HTTP request to the ScaleMule API
|
|
118
118
|
*/
|
|
119
|
-
request<T>(path: string, options?: RequestOptions): Promise<
|
|
119
|
+
request<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
120
120
|
/**
|
|
121
121
|
* GET request
|
|
122
122
|
*/
|
|
123
|
-
get<T>(path: string, options?: RequestOptions): Promise<
|
|
123
|
+
get<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
124
124
|
/**
|
|
125
125
|
* POST request with JSON body
|
|
126
126
|
*/
|
|
127
|
-
post<T>(path: string, body?: unknown, options?: RequestOptions): Promise<
|
|
127
|
+
post<T>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
128
128
|
/**
|
|
129
129
|
* PUT request with JSON body
|
|
130
130
|
*/
|
|
131
|
-
put<T>(path: string, body?: unknown, options?: RequestOptions): Promise<
|
|
131
|
+
put<T>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
132
132
|
/**
|
|
133
133
|
* PATCH request with JSON body
|
|
134
134
|
*/
|
|
135
|
-
patch<T>(path: string, body?: unknown, options?: RequestOptions): Promise<
|
|
135
|
+
patch<T>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
136
136
|
/**
|
|
137
137
|
* DELETE request
|
|
138
138
|
*/
|
|
139
|
-
delete<T>(path: string, options?: RequestOptions): Promise<
|
|
139
|
+
delete<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
140
140
|
/**
|
|
141
141
|
* Upload a file using multipart/form-data
|
|
142
142
|
*
|
|
@@ -145,7 +145,7 @@ declare class ScaleMuleClient {
|
|
|
145
145
|
*/
|
|
146
146
|
upload<T>(path: string, file: File, additionalFields?: Record<string, string>, options?: RequestOptions & {
|
|
147
147
|
onProgress?: (progress: number) => void;
|
|
148
|
-
}): Promise<
|
|
148
|
+
}): Promise<T>;
|
|
149
149
|
/**
|
|
150
150
|
* Upload with progress using XMLHttpRequest (with retry)
|
|
151
151
|
*/
|
package/dist/client.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
// src/types/index.ts
|
|
4
|
+
var ScaleMuleApiError = class extends Error {
|
|
5
|
+
constructor(error) {
|
|
6
|
+
super(error.message);
|
|
7
|
+
this.name = "ScaleMuleApiError";
|
|
8
|
+
this.code = error.code;
|
|
9
|
+
this.field = error.field;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
3
13
|
// src/client.ts
|
|
4
14
|
var GATEWAY_URLS = {
|
|
5
15
|
dev: "https://api-dev.scalemule.com",
|
|
@@ -82,14 +92,14 @@ var RateLimitQueue = class {
|
|
|
82
92
|
try {
|
|
83
93
|
this.requestsInWindow++;
|
|
84
94
|
const result = await request.execute();
|
|
85
|
-
|
|
95
|
+
request.resolve(result);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (error instanceof ScaleMuleApiError && error.code === "RATE_LIMITED") {
|
|
86
98
|
this.queue.unshift(request);
|
|
87
99
|
this.rateLimitedUntil = Date.now() + 6e4;
|
|
88
100
|
} else {
|
|
89
|
-
request.
|
|
101
|
+
request.reject(error);
|
|
90
102
|
}
|
|
91
|
-
} catch (error) {
|
|
92
|
-
request.reject(error);
|
|
93
103
|
}
|
|
94
104
|
}
|
|
95
105
|
this.processing = false;
|
|
@@ -432,11 +442,12 @@ var ScaleMuleClient = class {
|
|
|
432
442
|
signal: controller.signal
|
|
433
443
|
});
|
|
434
444
|
clearTimeout(timeoutId);
|
|
435
|
-
const
|
|
445
|
+
const text = await response.text();
|
|
446
|
+
const responseData = text ? JSON.parse(text) : null;
|
|
436
447
|
if (!response.ok) {
|
|
437
|
-
const error =
|
|
448
|
+
const error = responseData?.error || {
|
|
438
449
|
code: `HTTP_${response.status}`,
|
|
439
|
-
message:
|
|
450
|
+
message: responseData?.message || response.statusText
|
|
440
451
|
};
|
|
441
452
|
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
442
453
|
lastError = error;
|
|
@@ -450,11 +461,15 @@ var ScaleMuleClient = class {
|
|
|
450
461
|
if (this.debug) {
|
|
451
462
|
console.error("[ScaleMule] Request failed:", error);
|
|
452
463
|
}
|
|
453
|
-
|
|
464
|
+
throw new ScaleMuleApiError(error);
|
|
454
465
|
}
|
|
466
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
455
467
|
return data;
|
|
456
468
|
} catch (err) {
|
|
457
469
|
clearTimeout(timeoutId);
|
|
470
|
+
if (err instanceof ScaleMuleApiError) {
|
|
471
|
+
throw err;
|
|
472
|
+
}
|
|
458
473
|
const error = {
|
|
459
474
|
code: err instanceof Error && err.name === "AbortError" ? "TIMEOUT" : "NETWORK_ERROR",
|
|
460
475
|
message: err instanceof Error ? err.message : "Network request failed"
|
|
@@ -471,10 +486,10 @@ var ScaleMuleClient = class {
|
|
|
471
486
|
if (this.debug) {
|
|
472
487
|
console.error("[ScaleMule] Network error:", err);
|
|
473
488
|
}
|
|
474
|
-
|
|
489
|
+
throw new ScaleMuleApiError(error);
|
|
475
490
|
}
|
|
476
491
|
}
|
|
477
|
-
|
|
492
|
+
throw new ScaleMuleApiError(lastError || { code: "UNKNOWN", message: "Request failed" });
|
|
478
493
|
}
|
|
479
494
|
/**
|
|
480
495
|
* GET request
|
|
@@ -570,11 +585,12 @@ var ScaleMuleClient = class {
|
|
|
570
585
|
headers,
|
|
571
586
|
body: retryFormData
|
|
572
587
|
});
|
|
573
|
-
const
|
|
588
|
+
const uploadText = await response.text();
|
|
589
|
+
const responseData = uploadText ? JSON.parse(uploadText) : null;
|
|
574
590
|
if (!response.ok) {
|
|
575
|
-
const error =
|
|
591
|
+
const error = responseData?.error || {
|
|
576
592
|
code: `HTTP_${response.status}`,
|
|
577
|
-
message:
|
|
593
|
+
message: responseData?.message || response.statusText
|
|
578
594
|
};
|
|
579
595
|
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
580
596
|
lastError = error;
|
|
@@ -585,10 +601,14 @@ var ScaleMuleClient = class {
|
|
|
585
601
|
await sleep(delay);
|
|
586
602
|
continue;
|
|
587
603
|
}
|
|
588
|
-
|
|
604
|
+
throw new ScaleMuleApiError(error);
|
|
589
605
|
}
|
|
606
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
590
607
|
return data;
|
|
591
608
|
} catch (err) {
|
|
609
|
+
if (err instanceof ScaleMuleApiError) {
|
|
610
|
+
throw err;
|
|
611
|
+
}
|
|
592
612
|
lastError = {
|
|
593
613
|
code: "UPLOAD_ERROR",
|
|
594
614
|
message: err instanceof Error ? err.message : "Upload failed"
|
|
@@ -603,10 +623,7 @@ var ScaleMuleClient = class {
|
|
|
603
623
|
}
|
|
604
624
|
}
|
|
605
625
|
}
|
|
606
|
-
|
|
607
|
-
success: false,
|
|
608
|
-
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
609
|
-
};
|
|
626
|
+
throw new ScaleMuleApiError(lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" });
|
|
610
627
|
}
|
|
611
628
|
/**
|
|
612
629
|
* Upload with progress using XMLHttpRequest (with retry)
|
|
@@ -614,35 +631,35 @@ var ScaleMuleClient = class {
|
|
|
614
631
|
async uploadWithProgress(url, formData, onProgress, maxRetries = 2) {
|
|
615
632
|
let lastError = null;
|
|
616
633
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
634
|
+
try {
|
|
635
|
+
return await this.singleUploadWithProgress(url, formData, onProgress);
|
|
636
|
+
} catch (err) {
|
|
637
|
+
if (!(err instanceof ScaleMuleApiError)) {
|
|
638
|
+
throw err;
|
|
639
|
+
}
|
|
640
|
+
const errorCode = err.code;
|
|
641
|
+
const isNetworkError = errorCode === "UPLOAD_ERROR" || errorCode === "NETWORK_ERROR";
|
|
642
|
+
const isRetryableHttp = errorCode.startsWith("HTTP_") && RETRYABLE_STATUS_CODES.has(parseInt(errorCode.replace("HTTP_", ""), 10));
|
|
643
|
+
if (attempt < maxRetries && (isNetworkError || isRetryableHttp)) {
|
|
644
|
+
lastError = { code: err.code, message: err.message };
|
|
645
|
+
const delay = getBackoffDelay(attempt);
|
|
646
|
+
if (this.debug) {
|
|
647
|
+
console.log(`[ScaleMule] Upload retry ${attempt + 1}/${maxRetries} after ${delay}ms`);
|
|
648
|
+
}
|
|
649
|
+
await sleep(delay);
|
|
650
|
+
onProgress(0);
|
|
651
|
+
continue;
|
|
629
652
|
}
|
|
630
|
-
|
|
631
|
-
onProgress(0);
|
|
632
|
-
continue;
|
|
653
|
+
throw err;
|
|
633
654
|
}
|
|
634
|
-
return result;
|
|
635
655
|
}
|
|
636
|
-
|
|
637
|
-
success: false,
|
|
638
|
-
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
639
|
-
};
|
|
656
|
+
throw new ScaleMuleApiError(lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" });
|
|
640
657
|
}
|
|
641
658
|
/**
|
|
642
659
|
* Single upload attempt with progress using XMLHttpRequest
|
|
643
660
|
*/
|
|
644
661
|
singleUploadWithProgress(url, formData, onProgress) {
|
|
645
|
-
return new Promise((resolve) => {
|
|
662
|
+
return new Promise((resolve, reject) => {
|
|
646
663
|
const xhr = new XMLHttpRequest();
|
|
647
664
|
xhr.upload.addEventListener("progress", (event) => {
|
|
648
665
|
if (event.lengthComputable) {
|
|
@@ -654,34 +671,23 @@ var ScaleMuleClient = class {
|
|
|
654
671
|
try {
|
|
655
672
|
const data = JSON.parse(xhr.responseText);
|
|
656
673
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
657
|
-
|
|
674
|
+
const unwrapped = data?.data !== void 0 ? data.data : data;
|
|
675
|
+
resolve(unwrapped);
|
|
658
676
|
} else {
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
message: data.message || "Upload failed"
|
|
664
|
-
}
|
|
665
|
-
});
|
|
677
|
+
reject(new ScaleMuleApiError(data.error || {
|
|
678
|
+
code: `HTTP_${xhr.status}`,
|
|
679
|
+
message: data.message || "Upload failed"
|
|
680
|
+
}));
|
|
666
681
|
}
|
|
667
682
|
} catch {
|
|
668
|
-
|
|
669
|
-
success: false,
|
|
670
|
-
error: { code: "PARSE_ERROR", message: "Failed to parse response" }
|
|
671
|
-
});
|
|
683
|
+
reject(new ScaleMuleApiError({ code: "PARSE_ERROR", message: "Failed to parse response" }));
|
|
672
684
|
}
|
|
673
685
|
});
|
|
674
686
|
xhr.addEventListener("error", () => {
|
|
675
|
-
|
|
676
|
-
success: false,
|
|
677
|
-
error: { code: "UPLOAD_ERROR", message: "Upload failed" }
|
|
678
|
-
});
|
|
687
|
+
reject(new ScaleMuleApiError({ code: "UPLOAD_ERROR", message: "Upload failed" }));
|
|
679
688
|
});
|
|
680
689
|
xhr.addEventListener("abort", () => {
|
|
681
|
-
|
|
682
|
-
success: false,
|
|
683
|
-
error: { code: "UPLOAD_ABORTED", message: "Upload cancelled" }
|
|
684
|
-
});
|
|
690
|
+
reject(new ScaleMuleApiError({ code: "UPLOAD_ABORTED", message: "Upload cancelled" }));
|
|
685
691
|
});
|
|
686
692
|
xhr.open("POST", url);
|
|
687
693
|
xhr.setRequestHeader("x-api-key", this.apiKey);
|
package/dist/client.mjs
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
// src/types/index.ts
|
|
2
|
+
var ScaleMuleApiError = class extends Error {
|
|
3
|
+
constructor(error) {
|
|
4
|
+
super(error.message);
|
|
5
|
+
this.name = "ScaleMuleApiError";
|
|
6
|
+
this.code = error.code;
|
|
7
|
+
this.field = error.field;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
1
11
|
// src/client.ts
|
|
2
12
|
var GATEWAY_URLS = {
|
|
3
13
|
dev: "https://api-dev.scalemule.com",
|
|
@@ -80,14 +90,14 @@ var RateLimitQueue = class {
|
|
|
80
90
|
try {
|
|
81
91
|
this.requestsInWindow++;
|
|
82
92
|
const result = await request.execute();
|
|
83
|
-
|
|
93
|
+
request.resolve(result);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof ScaleMuleApiError && error.code === "RATE_LIMITED") {
|
|
84
96
|
this.queue.unshift(request);
|
|
85
97
|
this.rateLimitedUntil = Date.now() + 6e4;
|
|
86
98
|
} else {
|
|
87
|
-
request.
|
|
99
|
+
request.reject(error);
|
|
88
100
|
}
|
|
89
|
-
} catch (error) {
|
|
90
|
-
request.reject(error);
|
|
91
101
|
}
|
|
92
102
|
}
|
|
93
103
|
this.processing = false;
|
|
@@ -430,11 +440,12 @@ var ScaleMuleClient = class {
|
|
|
430
440
|
signal: controller.signal
|
|
431
441
|
});
|
|
432
442
|
clearTimeout(timeoutId);
|
|
433
|
-
const
|
|
443
|
+
const text = await response.text();
|
|
444
|
+
const responseData = text ? JSON.parse(text) : null;
|
|
434
445
|
if (!response.ok) {
|
|
435
|
-
const error =
|
|
446
|
+
const error = responseData?.error || {
|
|
436
447
|
code: `HTTP_${response.status}`,
|
|
437
|
-
message:
|
|
448
|
+
message: responseData?.message || response.statusText
|
|
438
449
|
};
|
|
439
450
|
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
440
451
|
lastError = error;
|
|
@@ -448,11 +459,15 @@ var ScaleMuleClient = class {
|
|
|
448
459
|
if (this.debug) {
|
|
449
460
|
console.error("[ScaleMule] Request failed:", error);
|
|
450
461
|
}
|
|
451
|
-
|
|
462
|
+
throw new ScaleMuleApiError(error);
|
|
452
463
|
}
|
|
464
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
453
465
|
return data;
|
|
454
466
|
} catch (err) {
|
|
455
467
|
clearTimeout(timeoutId);
|
|
468
|
+
if (err instanceof ScaleMuleApiError) {
|
|
469
|
+
throw err;
|
|
470
|
+
}
|
|
456
471
|
const error = {
|
|
457
472
|
code: err instanceof Error && err.name === "AbortError" ? "TIMEOUT" : "NETWORK_ERROR",
|
|
458
473
|
message: err instanceof Error ? err.message : "Network request failed"
|
|
@@ -469,10 +484,10 @@ var ScaleMuleClient = class {
|
|
|
469
484
|
if (this.debug) {
|
|
470
485
|
console.error("[ScaleMule] Network error:", err);
|
|
471
486
|
}
|
|
472
|
-
|
|
487
|
+
throw new ScaleMuleApiError(error);
|
|
473
488
|
}
|
|
474
489
|
}
|
|
475
|
-
|
|
490
|
+
throw new ScaleMuleApiError(lastError || { code: "UNKNOWN", message: "Request failed" });
|
|
476
491
|
}
|
|
477
492
|
/**
|
|
478
493
|
* GET request
|
|
@@ -568,11 +583,12 @@ var ScaleMuleClient = class {
|
|
|
568
583
|
headers,
|
|
569
584
|
body: retryFormData
|
|
570
585
|
});
|
|
571
|
-
const
|
|
586
|
+
const uploadText = await response.text();
|
|
587
|
+
const responseData = uploadText ? JSON.parse(uploadText) : null;
|
|
572
588
|
if (!response.ok) {
|
|
573
|
-
const error =
|
|
589
|
+
const error = responseData?.error || {
|
|
574
590
|
code: `HTTP_${response.status}`,
|
|
575
|
-
message:
|
|
591
|
+
message: responseData?.message || response.statusText
|
|
576
592
|
};
|
|
577
593
|
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
578
594
|
lastError = error;
|
|
@@ -583,10 +599,14 @@ var ScaleMuleClient = class {
|
|
|
583
599
|
await sleep(delay);
|
|
584
600
|
continue;
|
|
585
601
|
}
|
|
586
|
-
|
|
602
|
+
throw new ScaleMuleApiError(error);
|
|
587
603
|
}
|
|
604
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
588
605
|
return data;
|
|
589
606
|
} catch (err) {
|
|
607
|
+
if (err instanceof ScaleMuleApiError) {
|
|
608
|
+
throw err;
|
|
609
|
+
}
|
|
590
610
|
lastError = {
|
|
591
611
|
code: "UPLOAD_ERROR",
|
|
592
612
|
message: err instanceof Error ? err.message : "Upload failed"
|
|
@@ -601,10 +621,7 @@ var ScaleMuleClient = class {
|
|
|
601
621
|
}
|
|
602
622
|
}
|
|
603
623
|
}
|
|
604
|
-
|
|
605
|
-
success: false,
|
|
606
|
-
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
607
|
-
};
|
|
624
|
+
throw new ScaleMuleApiError(lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" });
|
|
608
625
|
}
|
|
609
626
|
/**
|
|
610
627
|
* Upload with progress using XMLHttpRequest (with retry)
|
|
@@ -612,35 +629,35 @@ var ScaleMuleClient = class {
|
|
|
612
629
|
async uploadWithProgress(url, formData, onProgress, maxRetries = 2) {
|
|
613
630
|
let lastError = null;
|
|
614
631
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
632
|
+
try {
|
|
633
|
+
return await this.singleUploadWithProgress(url, formData, onProgress);
|
|
634
|
+
} catch (err) {
|
|
635
|
+
if (!(err instanceof ScaleMuleApiError)) {
|
|
636
|
+
throw err;
|
|
637
|
+
}
|
|
638
|
+
const errorCode = err.code;
|
|
639
|
+
const isNetworkError = errorCode === "UPLOAD_ERROR" || errorCode === "NETWORK_ERROR";
|
|
640
|
+
const isRetryableHttp = errorCode.startsWith("HTTP_") && RETRYABLE_STATUS_CODES.has(parseInt(errorCode.replace("HTTP_", ""), 10));
|
|
641
|
+
if (attempt < maxRetries && (isNetworkError || isRetryableHttp)) {
|
|
642
|
+
lastError = { code: err.code, message: err.message };
|
|
643
|
+
const delay = getBackoffDelay(attempt);
|
|
644
|
+
if (this.debug) {
|
|
645
|
+
console.log(`[ScaleMule] Upload retry ${attempt + 1}/${maxRetries} after ${delay}ms`);
|
|
646
|
+
}
|
|
647
|
+
await sleep(delay);
|
|
648
|
+
onProgress(0);
|
|
649
|
+
continue;
|
|
627
650
|
}
|
|
628
|
-
|
|
629
|
-
onProgress(0);
|
|
630
|
-
continue;
|
|
651
|
+
throw err;
|
|
631
652
|
}
|
|
632
|
-
return result;
|
|
633
653
|
}
|
|
634
|
-
|
|
635
|
-
success: false,
|
|
636
|
-
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
637
|
-
};
|
|
654
|
+
throw new ScaleMuleApiError(lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" });
|
|
638
655
|
}
|
|
639
656
|
/**
|
|
640
657
|
* Single upload attempt with progress using XMLHttpRequest
|
|
641
658
|
*/
|
|
642
659
|
singleUploadWithProgress(url, formData, onProgress) {
|
|
643
|
-
return new Promise((resolve) => {
|
|
660
|
+
return new Promise((resolve, reject) => {
|
|
644
661
|
const xhr = new XMLHttpRequest();
|
|
645
662
|
xhr.upload.addEventListener("progress", (event) => {
|
|
646
663
|
if (event.lengthComputable) {
|
|
@@ -652,34 +669,23 @@ var ScaleMuleClient = class {
|
|
|
652
669
|
try {
|
|
653
670
|
const data = JSON.parse(xhr.responseText);
|
|
654
671
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
655
|
-
|
|
672
|
+
const unwrapped = data?.data !== void 0 ? data.data : data;
|
|
673
|
+
resolve(unwrapped);
|
|
656
674
|
} else {
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
message: data.message || "Upload failed"
|
|
662
|
-
}
|
|
663
|
-
});
|
|
675
|
+
reject(new ScaleMuleApiError(data.error || {
|
|
676
|
+
code: `HTTP_${xhr.status}`,
|
|
677
|
+
message: data.message || "Upload failed"
|
|
678
|
+
}));
|
|
664
679
|
}
|
|
665
680
|
} catch {
|
|
666
|
-
|
|
667
|
-
success: false,
|
|
668
|
-
error: { code: "PARSE_ERROR", message: "Failed to parse response" }
|
|
669
|
-
});
|
|
681
|
+
reject(new ScaleMuleApiError({ code: "PARSE_ERROR", message: "Failed to parse response" }));
|
|
670
682
|
}
|
|
671
683
|
});
|
|
672
684
|
xhr.addEventListener("error", () => {
|
|
673
|
-
|
|
674
|
-
success: false,
|
|
675
|
-
error: { code: "UPLOAD_ERROR", message: "Upload failed" }
|
|
676
|
-
});
|
|
685
|
+
reject(new ScaleMuleApiError({ code: "UPLOAD_ERROR", message: "Upload failed" }));
|
|
677
686
|
});
|
|
678
687
|
xhr.addEventListener("abort", () => {
|
|
679
|
-
|
|
680
|
-
success: false,
|
|
681
|
-
error: { code: "UPLOAD_ABORTED", message: "Upload cancelled" }
|
|
682
|
-
});
|
|
688
|
+
reject(new ScaleMuleApiError({ code: "UPLOAD_ABORTED", message: "Upload cancelled" }));
|
|
683
689
|
});
|
|
684
690
|
xhr.open("POST", url);
|
|
685
691
|
xhr.setRequestHeader("x-api-key", this.apiKey);
|
|
@@ -72,6 +72,13 @@ interface StorageAdapter {
|
|
|
72
72
|
setItem(key: string, value: string): void | Promise<void>;
|
|
73
73
|
removeItem(key: string): void | Promise<void>;
|
|
74
74
|
}
|
|
75
|
+
declare class ScaleMuleApiError extends Error {
|
|
76
|
+
code: string;
|
|
77
|
+
status?: number;
|
|
78
|
+
field?: string;
|
|
79
|
+
details?: unknown;
|
|
80
|
+
constructor(error: ApiError);
|
|
81
|
+
}
|
|
75
82
|
interface ApiResponse<T> {
|
|
76
83
|
success: boolean;
|
|
77
84
|
data?: T;
|
|
@@ -804,4 +811,4 @@ interface UseBillingReturn {
|
|
|
804
811
|
}) => Promise<string | null>;
|
|
805
812
|
}
|
|
806
813
|
|
|
807
|
-
export type
|
|
814
|
+
export { type ClientContext as $, type ApiError as A, type MFASMSSetupResponse as B, type ChangePasswordRequest as C, type DeviceFingerprint as D, type MFAVerifyRequest as E, type ForgotPasswordRequest as F, type MFAChallengeResponse as G, type MFAStatus as H, type PhoneSendCodeRequest as I, type PhoneVerifyRequest as J, type PhoneLoginRequest as K, type LoginResponse as L, type MFAMethod as M, type StorageFile as N, type OAuthProvider as O, type Profile as P, type UploadOptions as Q, type RegisterRequest as R, type ScaleMuleConfig as S, type ListFilesResponse as T, type User as U, type VerifyEmailRequest as V, type UploadResponse as W, type SignedUploadUrl as X, type SignedUploadRequest as Y, type SignedUploadResponse as Z, type SignedUploadCompleteRequest as _, type UseAuthReturn as a, type UpdateProfileRequest as a0, type ConnectedAccount as a1, type AccountBalance as a2, type BillingPayment as a3, type BillingRefund as a4, type BillingPayout as a5, type PayoutSchedule as a6, type BillingTransaction as a7, type TransactionSummary as a8, type AnalyticsEvent as a9, type PageViewData as aa, type UTMParams as ab, type DeviceInfo as ac, type EnhancedAnalyticsEvent as ad, type TrackEventResponse as ae, type BatchTrackRequest as af, type UseBillingReturn as b, type ListFilesParams as c, type UseContentReturn as d, type UseUserReturn as e, type UseAnalyticsOptions as f, type UseAnalyticsReturn as g, ScaleMuleApiError as h, type ScaleMuleEnvironment as i, type StorageAdapter as j, type ApiResponse as k, type LoginRequest as l, type LoginResponseWithMFA as m, type LoginDeviceInfo as n, type LoginRiskInfo as o, type RefreshResponse as p, type ResetPasswordRequest as q, type ChangeEmailRequest as r, type Session as s, type OAuthConfig as t, type OAuthStartResponse as u, type OAuthCallbackRequest as v, type OAuthCallbackResponse as w, type LinkedAccount as x, type MFASetupRequest as y, type MFATOTPSetupResponse as z };
|
|
@@ -72,6 +72,13 @@ interface StorageAdapter {
|
|
|
72
72
|
setItem(key: string, value: string): void | Promise<void>;
|
|
73
73
|
removeItem(key: string): void | Promise<void>;
|
|
74
74
|
}
|
|
75
|
+
declare class ScaleMuleApiError extends Error {
|
|
76
|
+
code: string;
|
|
77
|
+
status?: number;
|
|
78
|
+
field?: string;
|
|
79
|
+
details?: unknown;
|
|
80
|
+
constructor(error: ApiError);
|
|
81
|
+
}
|
|
75
82
|
interface ApiResponse<T> {
|
|
76
83
|
success: boolean;
|
|
77
84
|
data?: T;
|
|
@@ -804,4 +811,4 @@ interface UseBillingReturn {
|
|
|
804
811
|
}) => Promise<string | null>;
|
|
805
812
|
}
|
|
806
813
|
|
|
807
|
-
export type
|
|
814
|
+
export { type ClientContext as $, type ApiError as A, type MFASMSSetupResponse as B, type ChangePasswordRequest as C, type DeviceFingerprint as D, type MFAVerifyRequest as E, type ForgotPasswordRequest as F, type MFAChallengeResponse as G, type MFAStatus as H, type PhoneSendCodeRequest as I, type PhoneVerifyRequest as J, type PhoneLoginRequest as K, type LoginResponse as L, type MFAMethod as M, type StorageFile as N, type OAuthProvider as O, type Profile as P, type UploadOptions as Q, type RegisterRequest as R, type ScaleMuleConfig as S, type ListFilesResponse as T, type User as U, type VerifyEmailRequest as V, type UploadResponse as W, type SignedUploadUrl as X, type SignedUploadRequest as Y, type SignedUploadResponse as Z, type SignedUploadCompleteRequest as _, type UseAuthReturn as a, type UpdateProfileRequest as a0, type ConnectedAccount as a1, type AccountBalance as a2, type BillingPayment as a3, type BillingRefund as a4, type BillingPayout as a5, type PayoutSchedule as a6, type BillingTransaction as a7, type TransactionSummary as a8, type AnalyticsEvent as a9, type PageViewData as aa, type UTMParams as ab, type DeviceInfo as ac, type EnhancedAnalyticsEvent as ad, type TrackEventResponse as ae, type BatchTrackRequest as af, type UseBillingReturn as b, type ListFilesParams as c, type UseContentReturn as d, type UseUserReturn as e, type UseAnalyticsOptions as f, type UseAnalyticsReturn as g, ScaleMuleApiError as h, type ScaleMuleEnvironment as i, type StorageAdapter as j, type ApiResponse as k, type LoginRequest as l, type LoginResponseWithMFA as m, type LoginDeviceInfo as n, type LoginRiskInfo as o, type RefreshResponse as p, type ResetPasswordRequest as q, type ChangeEmailRequest as r, type Session as s, type OAuthConfig as t, type OAuthStartResponse as u, type OAuthCallbackRequest as v, type OAuthCallbackResponse as w, type LinkedAccount as x, type MFASetupRequest as y, type MFATOTPSetupResponse as z };
|
package/dist/index.d.mts
CHANGED
|
@@ -2,8 +2,8 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
3
|
import { ScaleMuleClient } from './client.mjs';
|
|
4
4
|
export { ClientConfig, RequestOptions, createClient } from './client.mjs';
|
|
5
|
-
import { S as ScaleMuleConfig, U as User, L as LoginResponse, A as ApiError, a as UseAuthReturn, b as UseBillingReturn, c as ListFilesParams, d as UseContentReturn, e as UseUserReturn, f as UseAnalyticsOptions, g as UseAnalyticsReturn } from './index-
|
|
6
|
-
export {
|
|
5
|
+
import { S as ScaleMuleConfig, U as User, L as LoginResponse, A as ApiError, a as UseAuthReturn, b as UseBillingReturn, c as ListFilesParams, d as UseContentReturn, e as UseUserReturn, f as UseAnalyticsOptions, g as UseAnalyticsReturn } from './index-9v0SaLgg.mjs';
|
|
6
|
+
export { a2 as AccountBalance, a9 as AnalyticsEvent, k as ApiResponse, af as BatchTrackRequest, a3 as BillingPayment, a5 as BillingPayout, a4 as BillingRefund, a7 as BillingTransaction, r as ChangeEmailRequest, C as ChangePasswordRequest, $ as ClientContext, a1 as ConnectedAccount, D as DeviceFingerprint, ac as DeviceInfo, ad as EnhancedAnalyticsEvent, F as ForgotPasswordRequest, x as LinkedAccount, T as ListFilesResponse, n as LoginDeviceInfo, l as LoginRequest, m as LoginResponseWithMFA, o as LoginRiskInfo, G as MFAChallengeResponse, M as MFAMethod, B as MFASMSSetupResponse, y as MFASetupRequest, H as MFAStatus, z as MFATOTPSetupResponse, E as MFAVerifyRequest, v as OAuthCallbackRequest, w as OAuthCallbackResponse, t as OAuthConfig, O as OAuthProvider, u as OAuthStartResponse, aa as PageViewData, a6 as PayoutSchedule, K as PhoneLoginRequest, I as PhoneSendCodeRequest, J as PhoneVerifyRequest, P as Profile, p as RefreshResponse, R as RegisterRequest, q as ResetPasswordRequest, h as ScaleMuleApiError, i as ScaleMuleEnvironment, s as Session, _ as SignedUploadCompleteRequest, Y as SignedUploadRequest, Z as SignedUploadResponse, X as SignedUploadUrl, j as StorageAdapter, N as StorageFile, ae as TrackEventResponse, a8 as TransactionSummary, ab as UTMParams, a0 as UpdateProfileRequest, Q as UploadOptions, W as UploadResponse, V as VerifyEmailRequest } from './index-9v0SaLgg.mjs';
|
|
7
7
|
|
|
8
8
|
interface ScaleMuleContextValue {
|
|
9
9
|
/** The API client instance */
|
package/dist/index.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
3
|
import { ScaleMuleClient } from './client.js';
|
|
4
4
|
export { ClientConfig, RequestOptions, createClient } from './client.js';
|
|
5
|
-
import { S as ScaleMuleConfig, U as User, L as LoginResponse, A as ApiError, a as UseAuthReturn, b as UseBillingReturn, c as ListFilesParams, d as UseContentReturn, e as UseUserReturn, f as UseAnalyticsOptions, g as UseAnalyticsReturn } from './index-
|
|
6
|
-
export {
|
|
5
|
+
import { S as ScaleMuleConfig, U as User, L as LoginResponse, A as ApiError, a as UseAuthReturn, b as UseBillingReturn, c as ListFilesParams, d as UseContentReturn, e as UseUserReturn, f as UseAnalyticsOptions, g as UseAnalyticsReturn } from './index-9v0SaLgg.js';
|
|
6
|
+
export { a2 as AccountBalance, a9 as AnalyticsEvent, k as ApiResponse, af as BatchTrackRequest, a3 as BillingPayment, a5 as BillingPayout, a4 as BillingRefund, a7 as BillingTransaction, r as ChangeEmailRequest, C as ChangePasswordRequest, $ as ClientContext, a1 as ConnectedAccount, D as DeviceFingerprint, ac as DeviceInfo, ad as EnhancedAnalyticsEvent, F as ForgotPasswordRequest, x as LinkedAccount, T as ListFilesResponse, n as LoginDeviceInfo, l as LoginRequest, m as LoginResponseWithMFA, o as LoginRiskInfo, G as MFAChallengeResponse, M as MFAMethod, B as MFASMSSetupResponse, y as MFASetupRequest, H as MFAStatus, z as MFATOTPSetupResponse, E as MFAVerifyRequest, v as OAuthCallbackRequest, w as OAuthCallbackResponse, t as OAuthConfig, O as OAuthProvider, u as OAuthStartResponse, aa as PageViewData, a6 as PayoutSchedule, K as PhoneLoginRequest, I as PhoneSendCodeRequest, J as PhoneVerifyRequest, P as Profile, p as RefreshResponse, R as RegisterRequest, q as ResetPasswordRequest, h as ScaleMuleApiError, i as ScaleMuleEnvironment, s as Session, _ as SignedUploadCompleteRequest, Y as SignedUploadRequest, Z as SignedUploadResponse, X as SignedUploadUrl, j as StorageAdapter, N as StorageFile, ae as TrackEventResponse, a8 as TransactionSummary, ab as UTMParams, a0 as UpdateProfileRequest, Q as UploadOptions, W as UploadResponse, V as VerifyEmailRequest } from './index-9v0SaLgg.js';
|
|
7
7
|
|
|
8
8
|
interface ScaleMuleContextValue {
|
|
9
9
|
/** The API client instance */
|