@scalemule/nextjs 0.0.1 → 0.0.3
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 +78 -60
- package/dist/client.mjs +78 -60
- 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 +468 -515
- package/dist/index.mjs +468 -516
- package/dist/server/auth.js +124 -78
- package/dist/server/auth.mjs +124 -78
- package/dist/server/index.d.mts +14 -11
- package/dist/server/index.d.ts +14 -11
- package/dist/server/index.js +217 -175
- package/dist/server/index.mjs +217 -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,16 @@ var ScaleMuleClient = class {
|
|
|
432
442
|
signal: controller.signal
|
|
433
443
|
});
|
|
434
444
|
clearTimeout(timeoutId);
|
|
435
|
-
const
|
|
445
|
+
const text = await response.text();
|
|
446
|
+
let responseData = null;
|
|
447
|
+
try {
|
|
448
|
+
responseData = text ? JSON.parse(text) : null;
|
|
449
|
+
} catch {
|
|
450
|
+
}
|
|
436
451
|
if (!response.ok) {
|
|
437
|
-
const error =
|
|
452
|
+
const error = responseData?.error || {
|
|
438
453
|
code: `HTTP_${response.status}`,
|
|
439
|
-
message:
|
|
454
|
+
message: responseData?.message || text || response.statusText
|
|
440
455
|
};
|
|
441
456
|
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
442
457
|
lastError = error;
|
|
@@ -450,11 +465,15 @@ var ScaleMuleClient = class {
|
|
|
450
465
|
if (this.debug) {
|
|
451
466
|
console.error("[ScaleMule] Request failed:", error);
|
|
452
467
|
}
|
|
453
|
-
|
|
468
|
+
throw new ScaleMuleApiError(error);
|
|
454
469
|
}
|
|
470
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
455
471
|
return data;
|
|
456
472
|
} catch (err) {
|
|
457
473
|
clearTimeout(timeoutId);
|
|
474
|
+
if (err instanceof ScaleMuleApiError) {
|
|
475
|
+
throw err;
|
|
476
|
+
}
|
|
458
477
|
const error = {
|
|
459
478
|
code: err instanceof Error && err.name === "AbortError" ? "TIMEOUT" : "NETWORK_ERROR",
|
|
460
479
|
message: err instanceof Error ? err.message : "Network request failed"
|
|
@@ -471,10 +490,10 @@ var ScaleMuleClient = class {
|
|
|
471
490
|
if (this.debug) {
|
|
472
491
|
console.error("[ScaleMule] Network error:", err);
|
|
473
492
|
}
|
|
474
|
-
|
|
493
|
+
throw new ScaleMuleApiError(error);
|
|
475
494
|
}
|
|
476
495
|
}
|
|
477
|
-
|
|
496
|
+
throw new ScaleMuleApiError(lastError || { code: "UNKNOWN", message: "Request failed" });
|
|
478
497
|
}
|
|
479
498
|
/**
|
|
480
499
|
* GET request
|
|
@@ -570,11 +589,16 @@ var ScaleMuleClient = class {
|
|
|
570
589
|
headers,
|
|
571
590
|
body: retryFormData
|
|
572
591
|
});
|
|
573
|
-
const
|
|
592
|
+
const uploadText = await response.text();
|
|
593
|
+
let responseData = null;
|
|
594
|
+
try {
|
|
595
|
+
responseData = uploadText ? JSON.parse(uploadText) : null;
|
|
596
|
+
} catch {
|
|
597
|
+
}
|
|
574
598
|
if (!response.ok) {
|
|
575
|
-
const error =
|
|
599
|
+
const error = responseData?.error || {
|
|
576
600
|
code: `HTTP_${response.status}`,
|
|
577
|
-
message:
|
|
601
|
+
message: responseData?.message || uploadText || response.statusText
|
|
578
602
|
};
|
|
579
603
|
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
580
604
|
lastError = error;
|
|
@@ -585,10 +609,14 @@ var ScaleMuleClient = class {
|
|
|
585
609
|
await sleep(delay);
|
|
586
610
|
continue;
|
|
587
611
|
}
|
|
588
|
-
|
|
612
|
+
throw new ScaleMuleApiError(error);
|
|
589
613
|
}
|
|
614
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
590
615
|
return data;
|
|
591
616
|
} catch (err) {
|
|
617
|
+
if (err instanceof ScaleMuleApiError) {
|
|
618
|
+
throw err;
|
|
619
|
+
}
|
|
592
620
|
lastError = {
|
|
593
621
|
code: "UPLOAD_ERROR",
|
|
594
622
|
message: err instanceof Error ? err.message : "Upload failed"
|
|
@@ -603,10 +631,7 @@ var ScaleMuleClient = class {
|
|
|
603
631
|
}
|
|
604
632
|
}
|
|
605
633
|
}
|
|
606
|
-
|
|
607
|
-
success: false,
|
|
608
|
-
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
609
|
-
};
|
|
634
|
+
throw new ScaleMuleApiError(lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" });
|
|
610
635
|
}
|
|
611
636
|
/**
|
|
612
637
|
* Upload with progress using XMLHttpRequest (with retry)
|
|
@@ -614,35 +639,35 @@ var ScaleMuleClient = class {
|
|
|
614
639
|
async uploadWithProgress(url, formData, onProgress, maxRetries = 2) {
|
|
615
640
|
let lastError = null;
|
|
616
641
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
const isNetworkError = errorCode === "UPLOAD_ERROR" || errorCode === "NETWORK_ERROR";
|
|
623
|
-
const isRetryableHttp = errorCode.startsWith("HTTP_") && RETRYABLE_STATUS_CODES.has(parseInt(errorCode.replace("HTTP_", ""), 10));
|
|
624
|
-
if (attempt < maxRetries && (isNetworkError || isRetryableHttp)) {
|
|
625
|
-
lastError = result.error || null;
|
|
626
|
-
const delay = getBackoffDelay(attempt);
|
|
627
|
-
if (this.debug) {
|
|
628
|
-
console.log(`[ScaleMule] Upload retry ${attempt + 1}/${maxRetries} after ${delay}ms`);
|
|
642
|
+
try {
|
|
643
|
+
return await this.singleUploadWithProgress(url, formData, onProgress);
|
|
644
|
+
} catch (err) {
|
|
645
|
+
if (!(err instanceof ScaleMuleApiError)) {
|
|
646
|
+
throw err;
|
|
629
647
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
648
|
+
const errorCode = err.code;
|
|
649
|
+
const isNetworkError = errorCode === "UPLOAD_ERROR" || errorCode === "NETWORK_ERROR";
|
|
650
|
+
const isRetryableHttp = errorCode.startsWith("HTTP_") && RETRYABLE_STATUS_CODES.has(parseInt(errorCode.replace("HTTP_", ""), 10));
|
|
651
|
+
if (attempt < maxRetries && (isNetworkError || isRetryableHttp)) {
|
|
652
|
+
lastError = { code: err.code, message: err.message };
|
|
653
|
+
const delay = getBackoffDelay(attempt);
|
|
654
|
+
if (this.debug) {
|
|
655
|
+
console.log(`[ScaleMule] Upload retry ${attempt + 1}/${maxRetries} after ${delay}ms`);
|
|
656
|
+
}
|
|
657
|
+
await sleep(delay);
|
|
658
|
+
onProgress(0);
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
throw err;
|
|
633
662
|
}
|
|
634
|
-
return result;
|
|
635
663
|
}
|
|
636
|
-
|
|
637
|
-
success: false,
|
|
638
|
-
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
639
|
-
};
|
|
664
|
+
throw new ScaleMuleApiError(lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" });
|
|
640
665
|
}
|
|
641
666
|
/**
|
|
642
667
|
* Single upload attempt with progress using XMLHttpRequest
|
|
643
668
|
*/
|
|
644
669
|
singleUploadWithProgress(url, formData, onProgress) {
|
|
645
|
-
return new Promise((resolve) => {
|
|
670
|
+
return new Promise((resolve, reject) => {
|
|
646
671
|
const xhr = new XMLHttpRequest();
|
|
647
672
|
xhr.upload.addEventListener("progress", (event) => {
|
|
648
673
|
if (event.lengthComputable) {
|
|
@@ -652,36 +677,29 @@ var ScaleMuleClient = class {
|
|
|
652
677
|
});
|
|
653
678
|
xhr.addEventListener("load", () => {
|
|
654
679
|
try {
|
|
655
|
-
|
|
680
|
+
let data = null;
|
|
681
|
+
try {
|
|
682
|
+
data = xhr.responseText ? JSON.parse(xhr.responseText) : null;
|
|
683
|
+
} catch {
|
|
684
|
+
}
|
|
656
685
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
657
|
-
|
|
686
|
+
const unwrapped = data?.data !== void 0 ? data.data : data;
|
|
687
|
+
resolve(unwrapped);
|
|
658
688
|
} else {
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
message: data.message || "Upload failed"
|
|
664
|
-
}
|
|
665
|
-
});
|
|
689
|
+
reject(new ScaleMuleApiError(data?.error || {
|
|
690
|
+
code: `HTTP_${xhr.status}`,
|
|
691
|
+
message: data?.message || xhr.responseText || "Upload failed"
|
|
692
|
+
}));
|
|
666
693
|
}
|
|
667
694
|
} catch {
|
|
668
|
-
|
|
669
|
-
success: false,
|
|
670
|
-
error: { code: "PARSE_ERROR", message: "Failed to parse response" }
|
|
671
|
-
});
|
|
695
|
+
reject(new ScaleMuleApiError({ code: "PARSE_ERROR", message: "Failed to parse response" }));
|
|
672
696
|
}
|
|
673
697
|
});
|
|
674
698
|
xhr.addEventListener("error", () => {
|
|
675
|
-
|
|
676
|
-
success: false,
|
|
677
|
-
error: { code: "UPLOAD_ERROR", message: "Upload failed" }
|
|
678
|
-
});
|
|
699
|
+
reject(new ScaleMuleApiError({ code: "UPLOAD_ERROR", message: "Upload failed" }));
|
|
679
700
|
});
|
|
680
701
|
xhr.addEventListener("abort", () => {
|
|
681
|
-
|
|
682
|
-
success: false,
|
|
683
|
-
error: { code: "UPLOAD_ABORTED", message: "Upload cancelled" }
|
|
684
|
-
});
|
|
702
|
+
reject(new ScaleMuleApiError({ code: "UPLOAD_ABORTED", message: "Upload cancelled" }));
|
|
685
703
|
});
|
|
686
704
|
xhr.open("POST", url);
|
|
687
705
|
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,16 @@ var ScaleMuleClient = class {
|
|
|
430
440
|
signal: controller.signal
|
|
431
441
|
});
|
|
432
442
|
clearTimeout(timeoutId);
|
|
433
|
-
const
|
|
443
|
+
const text = await response.text();
|
|
444
|
+
let responseData = null;
|
|
445
|
+
try {
|
|
446
|
+
responseData = text ? JSON.parse(text) : null;
|
|
447
|
+
} catch {
|
|
448
|
+
}
|
|
434
449
|
if (!response.ok) {
|
|
435
|
-
const error =
|
|
450
|
+
const error = responseData?.error || {
|
|
436
451
|
code: `HTTP_${response.status}`,
|
|
437
|
-
message:
|
|
452
|
+
message: responseData?.message || text || response.statusText
|
|
438
453
|
};
|
|
439
454
|
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
440
455
|
lastError = error;
|
|
@@ -448,11 +463,15 @@ var ScaleMuleClient = class {
|
|
|
448
463
|
if (this.debug) {
|
|
449
464
|
console.error("[ScaleMule] Request failed:", error);
|
|
450
465
|
}
|
|
451
|
-
|
|
466
|
+
throw new ScaleMuleApiError(error);
|
|
452
467
|
}
|
|
468
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
453
469
|
return data;
|
|
454
470
|
} catch (err) {
|
|
455
471
|
clearTimeout(timeoutId);
|
|
472
|
+
if (err instanceof ScaleMuleApiError) {
|
|
473
|
+
throw err;
|
|
474
|
+
}
|
|
456
475
|
const error = {
|
|
457
476
|
code: err instanceof Error && err.name === "AbortError" ? "TIMEOUT" : "NETWORK_ERROR",
|
|
458
477
|
message: err instanceof Error ? err.message : "Network request failed"
|
|
@@ -469,10 +488,10 @@ var ScaleMuleClient = class {
|
|
|
469
488
|
if (this.debug) {
|
|
470
489
|
console.error("[ScaleMule] Network error:", err);
|
|
471
490
|
}
|
|
472
|
-
|
|
491
|
+
throw new ScaleMuleApiError(error);
|
|
473
492
|
}
|
|
474
493
|
}
|
|
475
|
-
|
|
494
|
+
throw new ScaleMuleApiError(lastError || { code: "UNKNOWN", message: "Request failed" });
|
|
476
495
|
}
|
|
477
496
|
/**
|
|
478
497
|
* GET request
|
|
@@ -568,11 +587,16 @@ var ScaleMuleClient = class {
|
|
|
568
587
|
headers,
|
|
569
588
|
body: retryFormData
|
|
570
589
|
});
|
|
571
|
-
const
|
|
590
|
+
const uploadText = await response.text();
|
|
591
|
+
let responseData = null;
|
|
592
|
+
try {
|
|
593
|
+
responseData = uploadText ? JSON.parse(uploadText) : null;
|
|
594
|
+
} catch {
|
|
595
|
+
}
|
|
572
596
|
if (!response.ok) {
|
|
573
|
-
const error =
|
|
597
|
+
const error = responseData?.error || {
|
|
574
598
|
code: `HTTP_${response.status}`,
|
|
575
|
-
message:
|
|
599
|
+
message: responseData?.message || uploadText || response.statusText
|
|
576
600
|
};
|
|
577
601
|
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(response.status)) {
|
|
578
602
|
lastError = error;
|
|
@@ -583,10 +607,14 @@ var ScaleMuleClient = class {
|
|
|
583
607
|
await sleep(delay);
|
|
584
608
|
continue;
|
|
585
609
|
}
|
|
586
|
-
|
|
610
|
+
throw new ScaleMuleApiError(error);
|
|
587
611
|
}
|
|
612
|
+
const data = responseData?.data !== void 0 ? responseData.data : responseData;
|
|
588
613
|
return data;
|
|
589
614
|
} catch (err) {
|
|
615
|
+
if (err instanceof ScaleMuleApiError) {
|
|
616
|
+
throw err;
|
|
617
|
+
}
|
|
590
618
|
lastError = {
|
|
591
619
|
code: "UPLOAD_ERROR",
|
|
592
620
|
message: err instanceof Error ? err.message : "Upload failed"
|
|
@@ -601,10 +629,7 @@ var ScaleMuleClient = class {
|
|
|
601
629
|
}
|
|
602
630
|
}
|
|
603
631
|
}
|
|
604
|
-
|
|
605
|
-
success: false,
|
|
606
|
-
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
607
|
-
};
|
|
632
|
+
throw new ScaleMuleApiError(lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" });
|
|
608
633
|
}
|
|
609
634
|
/**
|
|
610
635
|
* Upload with progress using XMLHttpRequest (with retry)
|
|
@@ -612,35 +637,35 @@ var ScaleMuleClient = class {
|
|
|
612
637
|
async uploadWithProgress(url, formData, onProgress, maxRetries = 2) {
|
|
613
638
|
let lastError = null;
|
|
614
639
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
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`);
|
|
640
|
+
try {
|
|
641
|
+
return await this.singleUploadWithProgress(url, formData, onProgress);
|
|
642
|
+
} catch (err) {
|
|
643
|
+
if (!(err instanceof ScaleMuleApiError)) {
|
|
644
|
+
throw err;
|
|
627
645
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
646
|
+
const errorCode = err.code;
|
|
647
|
+
const isNetworkError = errorCode === "UPLOAD_ERROR" || errorCode === "NETWORK_ERROR";
|
|
648
|
+
const isRetryableHttp = errorCode.startsWith("HTTP_") && RETRYABLE_STATUS_CODES.has(parseInt(errorCode.replace("HTTP_", ""), 10));
|
|
649
|
+
if (attempt < maxRetries && (isNetworkError || isRetryableHttp)) {
|
|
650
|
+
lastError = { code: err.code, message: err.message };
|
|
651
|
+
const delay = getBackoffDelay(attempt);
|
|
652
|
+
if (this.debug) {
|
|
653
|
+
console.log(`[ScaleMule] Upload retry ${attempt + 1}/${maxRetries} after ${delay}ms`);
|
|
654
|
+
}
|
|
655
|
+
await sleep(delay);
|
|
656
|
+
onProgress(0);
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
throw err;
|
|
631
660
|
}
|
|
632
|
-
return result;
|
|
633
661
|
}
|
|
634
|
-
|
|
635
|
-
success: false,
|
|
636
|
-
error: lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" }
|
|
637
|
-
};
|
|
662
|
+
throw new ScaleMuleApiError(lastError || { code: "UPLOAD_ERROR", message: "Upload failed after retries" });
|
|
638
663
|
}
|
|
639
664
|
/**
|
|
640
665
|
* Single upload attempt with progress using XMLHttpRequest
|
|
641
666
|
*/
|
|
642
667
|
singleUploadWithProgress(url, formData, onProgress) {
|
|
643
|
-
return new Promise((resolve) => {
|
|
668
|
+
return new Promise((resolve, reject) => {
|
|
644
669
|
const xhr = new XMLHttpRequest();
|
|
645
670
|
xhr.upload.addEventListener("progress", (event) => {
|
|
646
671
|
if (event.lengthComputable) {
|
|
@@ -650,36 +675,29 @@ var ScaleMuleClient = class {
|
|
|
650
675
|
});
|
|
651
676
|
xhr.addEventListener("load", () => {
|
|
652
677
|
try {
|
|
653
|
-
|
|
678
|
+
let data = null;
|
|
679
|
+
try {
|
|
680
|
+
data = xhr.responseText ? JSON.parse(xhr.responseText) : null;
|
|
681
|
+
} catch {
|
|
682
|
+
}
|
|
654
683
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
655
|
-
|
|
684
|
+
const unwrapped = data?.data !== void 0 ? data.data : data;
|
|
685
|
+
resolve(unwrapped);
|
|
656
686
|
} else {
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
message: data.message || "Upload failed"
|
|
662
|
-
}
|
|
663
|
-
});
|
|
687
|
+
reject(new ScaleMuleApiError(data?.error || {
|
|
688
|
+
code: `HTTP_${xhr.status}`,
|
|
689
|
+
message: data?.message || xhr.responseText || "Upload failed"
|
|
690
|
+
}));
|
|
664
691
|
}
|
|
665
692
|
} catch {
|
|
666
|
-
|
|
667
|
-
success: false,
|
|
668
|
-
error: { code: "PARSE_ERROR", message: "Failed to parse response" }
|
|
669
|
-
});
|
|
693
|
+
reject(new ScaleMuleApiError({ code: "PARSE_ERROR", message: "Failed to parse response" }));
|
|
670
694
|
}
|
|
671
695
|
});
|
|
672
696
|
xhr.addEventListener("error", () => {
|
|
673
|
-
|
|
674
|
-
success: false,
|
|
675
|
-
error: { code: "UPLOAD_ERROR", message: "Upload failed" }
|
|
676
|
-
});
|
|
697
|
+
reject(new ScaleMuleApiError({ code: "UPLOAD_ERROR", message: "Upload failed" }));
|
|
677
698
|
});
|
|
678
699
|
xhr.addEventListener("abort", () => {
|
|
679
|
-
|
|
680
|
-
success: false,
|
|
681
|
-
error: { code: "UPLOAD_ABORTED", message: "Upload cancelled" }
|
|
682
|
-
});
|
|
700
|
+
reject(new ScaleMuleApiError({ code: "UPLOAD_ABORTED", message: "Upload cancelled" }));
|
|
683
701
|
});
|
|
684
702
|
xhr.open("POST", url);
|
|
685
703
|
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 */
|