@autorender/sdk-core 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +72 -148
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -39
- package/dist/index.d.ts +12 -39
- package/dist/index.js +72 -148
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -26,7 +26,16 @@ __export(src_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(src_exports);
|
|
27
27
|
|
|
28
28
|
// src/constants/defaults.ts
|
|
29
|
-
|
|
29
|
+
function getBaseUrl(environment = "prod") {
|
|
30
|
+
switch (environment) {
|
|
31
|
+
case "dev":
|
|
32
|
+
return "https://upload-dev.autorender.io";
|
|
33
|
+
case "prod":
|
|
34
|
+
default:
|
|
35
|
+
return "https://upload.autorender.io";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
var DEFAULT_BASE_URL = getBaseUrl("prod");
|
|
30
39
|
var DEFAULT_ASSET_DELIVERY_BASE_URL = "https://dev.autorender.io";
|
|
31
40
|
var DEFAULT_LABELS = {
|
|
32
41
|
title: "Upload files",
|
|
@@ -165,83 +174,13 @@ var DEFAULT_THEME = {
|
|
|
165
174
|
// src/client/api-client.ts
|
|
166
175
|
var AutorenderApiClient = class {
|
|
167
176
|
constructor(opts) {
|
|
168
|
-
this.authValidated = false;
|
|
169
177
|
if (!opts.apiKey) {
|
|
170
178
|
throw new Error("apiKey is required");
|
|
171
179
|
}
|
|
172
180
|
this.apiKey = opts.apiKey;
|
|
173
|
-
this.baseUrl =
|
|
174
|
-
}
|
|
175
|
-
async request(path, { method = "POST", body, signal } = {}) {
|
|
176
|
-
const url = `${this.baseUrl}${path}`;
|
|
177
|
-
const response = await fetch(url, {
|
|
178
|
-
method,
|
|
179
|
-
headers: {
|
|
180
|
-
"Content-Type": "application/json",
|
|
181
|
-
"x-api-key": this.apiKey
|
|
182
|
-
},
|
|
183
|
-
body: body ? JSON.stringify(body) : void 0,
|
|
184
|
-
signal
|
|
185
|
-
});
|
|
186
|
-
if (!response.ok) {
|
|
187
|
-
let errorMessage = `Request failed with status ${response.status}`;
|
|
188
|
-
let errorPayload;
|
|
189
|
-
try {
|
|
190
|
-
const data = await response.json();
|
|
191
|
-
errorPayload = data;
|
|
192
|
-
if (data?.error) {
|
|
193
|
-
errorMessage = data.error;
|
|
194
|
-
} else if (data?.message) {
|
|
195
|
-
errorMessage = data.message;
|
|
196
|
-
}
|
|
197
|
-
} catch {
|
|
198
|
-
}
|
|
199
|
-
const error = new Error(errorMessage);
|
|
200
|
-
error.status = response.status;
|
|
201
|
-
error.payload = errorPayload;
|
|
202
|
-
throw error;
|
|
203
|
-
}
|
|
204
|
-
if (response.status === 204) {
|
|
205
|
-
return void 0;
|
|
206
|
-
}
|
|
207
|
-
return await response.json();
|
|
208
|
-
}
|
|
209
|
-
async validateApiKey(force = false, signal) {
|
|
210
|
-
if (this.authValidated && !force) return;
|
|
211
|
-
await this.request("/auth/validate", { method: "GET", signal });
|
|
212
|
-
this.authValidated = true;
|
|
213
|
-
}
|
|
214
|
-
// Legacy method - kept for backward compatibility but not used by new upload flow
|
|
215
|
-
completeUpload(payload, signal) {
|
|
216
|
-
return this.request("/assets/complete", {
|
|
217
|
-
method: "POST",
|
|
218
|
-
body: payload,
|
|
219
|
-
signal
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
createFolder(payload, signal) {
|
|
223
|
-
return this.request(
|
|
224
|
-
"/assets/folder",
|
|
225
|
-
{
|
|
226
|
-
method: "POST",
|
|
227
|
-
body: payload,
|
|
228
|
-
signal
|
|
229
|
-
}
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
// New upload flow methods
|
|
233
|
-
uploadInit(payload, signal) {
|
|
234
|
-
return this.request("/assets/upload", {
|
|
235
|
-
method: "POST",
|
|
236
|
-
body: payload,
|
|
237
|
-
signal
|
|
238
|
-
});
|
|
181
|
+
this.baseUrl = (opts.baseUrl || getBaseUrl(opts.environment || "prod")).replace(/\/+$/, "");
|
|
239
182
|
}
|
|
240
|
-
|
|
241
|
-
* Upload file to App Runner /ingest endpoint
|
|
242
|
-
* This is called directly to App Runner, not through our API
|
|
243
|
-
*/
|
|
244
|
-
async ingest(file, uploadToken, uploadUrl, onProgress, signal) {
|
|
183
|
+
uploadFile(params, onProgress, signal) {
|
|
245
184
|
return new Promise((resolve, reject) => {
|
|
246
185
|
const xhr = new XMLHttpRequest();
|
|
247
186
|
if (signal) {
|
|
@@ -260,33 +199,35 @@ var AutorenderApiClient = class {
|
|
|
260
199
|
reject(new Error(`Upload failed: ${xhr.statusText}`));
|
|
261
200
|
};
|
|
262
201
|
xhr.onload = () => {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
reject(new Error("Failed to parse response"));
|
|
269
|
-
}
|
|
270
|
-
} else {
|
|
271
|
-
try {
|
|
272
|
-
const error = JSON.parse(xhr.responseText);
|
|
273
|
-
reject(new Error(error.message || `Upload failed: ${xhr.statusText}`));
|
|
274
|
-
} catch {
|
|
275
|
-
reject(new Error(`Upload failed: ${xhr.statusText}`));
|
|
202
|
+
try {
|
|
203
|
+
const response = JSON.parse(xhr.responseText);
|
|
204
|
+
if (!response.success) {
|
|
205
|
+
reject(new Error(response.message || `Upload failed: ${xhr.statusText}`));
|
|
206
|
+
return;
|
|
276
207
|
}
|
|
208
|
+
resolve(response);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
reject(new Error("Failed to parse response"));
|
|
277
211
|
}
|
|
278
212
|
};
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
213
|
+
const endpoint = `${this.baseUrl}/file/upload`;
|
|
214
|
+
const formData = new FormData();
|
|
215
|
+
formData.append("file", params.file);
|
|
216
|
+
formData.append("fileName", params.file.name);
|
|
217
|
+
formData.append("fileSize", String(params.file.size));
|
|
218
|
+
formData.append("mimeType", params.file.type || "application/octet-stream");
|
|
219
|
+
if (params.folderPath) {
|
|
220
|
+
formData.append("folderPath", params.folderPath);
|
|
221
|
+
}
|
|
222
|
+
if (params.relativePath) {
|
|
223
|
+
formData.append("relativePath", params.relativePath);
|
|
224
|
+
}
|
|
225
|
+
if (params.settings) {
|
|
226
|
+
formData.append("settings", JSON.stringify(params.settings));
|
|
227
|
+
}
|
|
228
|
+
xhr.open("POST", endpoint);
|
|
229
|
+
xhr.setRequestHeader("x-api-key", this.apiKey);
|
|
230
|
+
xhr.send(formData);
|
|
290
231
|
});
|
|
291
232
|
}
|
|
292
233
|
};
|
|
@@ -450,7 +391,7 @@ var UploaderController = class extends EventTarget {
|
|
|
450
391
|
const folderPath = getRelativeFolderPath(
|
|
451
392
|
this.options.folderPath,
|
|
452
393
|
relativePath
|
|
453
|
-
);
|
|
394
|
+
) ?? "";
|
|
454
395
|
const previewUrl = file.type.startsWith("image/") ? URL.createObjectURL(file) : void 0;
|
|
455
396
|
const baseItem = {
|
|
456
397
|
id: generateId(),
|
|
@@ -508,7 +449,6 @@ var UploaderController = class extends EventTarget {
|
|
|
508
449
|
this.abortController = new AbortController();
|
|
509
450
|
this.dispatch("statechange");
|
|
510
451
|
try {
|
|
511
|
-
await this.client.validateApiKey(false, this.abortController?.signal ?? void 0);
|
|
512
452
|
await this.processUploads();
|
|
513
453
|
const files = this.buildSuccessFiles();
|
|
514
454
|
this.dispatch("complete", {
|
|
@@ -647,29 +587,19 @@ var UploaderController = class extends EventTarget {
|
|
|
647
587
|
item.progress = 0;
|
|
648
588
|
this.dispatch("statechange");
|
|
649
589
|
const folderPath = item.folderPath ? this.combinePaths(this.options.folderPath, item.folderPath) : this.normalizePath(this.options.folderPath);
|
|
650
|
-
const
|
|
590
|
+
const appRunnerResponse = await this.client.uploadFile(
|
|
651
591
|
{
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
path: folderPath,
|
|
592
|
+
file: item.file,
|
|
593
|
+
folderPath: folderPath ?? void 0,
|
|
594
|
+
relativePath: item.relativePath,
|
|
656
595
|
settings: uploadSettings ? {
|
|
657
596
|
pretransformations: uploadSettings.pretransformations,
|
|
658
597
|
tags: uploadSettings.tags,
|
|
659
598
|
is_unique_suffix_name: uploadSettings.is_unique_suffix_name ?? false
|
|
660
599
|
} : void 0
|
|
661
600
|
},
|
|
662
|
-
this.abortController?.signal
|
|
663
|
-
);
|
|
664
|
-
item.progress = 10;
|
|
665
|
-
this.dispatch("fileprogress", { item });
|
|
666
|
-
this.dispatch("statechange");
|
|
667
|
-
const appRunnerResponse = await this.client.ingest(
|
|
668
|
-
item.file,
|
|
669
|
-
initResponse.uploadToken,
|
|
670
|
-
initResponse.uploadUrl,
|
|
671
601
|
(progress) => {
|
|
672
|
-
item.progress =
|
|
602
|
+
item.progress = progress;
|
|
673
603
|
this.dispatch("fileprogress", { item });
|
|
674
604
|
this.dispatch("progress", {
|
|
675
605
|
progress: calculateBatchProgress(this.items),
|
|
@@ -678,36 +608,27 @@ var UploaderController = class extends EventTarget {
|
|
|
678
608
|
},
|
|
679
609
|
this.abortController?.signal
|
|
680
610
|
);
|
|
681
|
-
if (!appRunnerResponse.
|
|
682
|
-
|
|
611
|
+
if (!appRunnerResponse.success) {
|
|
612
|
+
const error = new Error(appRunnerResponse.message ?? "Upload failed");
|
|
613
|
+
error.code = appRunnerResponse.errorCode;
|
|
614
|
+
throw error;
|
|
683
615
|
}
|
|
684
|
-
item.progress = 90;
|
|
685
|
-
this.dispatch("fileprogress", { item });
|
|
686
|
-
this.dispatch("statechange");
|
|
687
|
-
const completeResponse = await this.client.completeUploadNew(
|
|
688
|
-
{
|
|
689
|
-
uploadId: initResponse.uploadId,
|
|
690
|
-
appRunnerResponse
|
|
691
|
-
},
|
|
692
|
-
this.abortController?.signal
|
|
693
|
-
);
|
|
694
616
|
item.status = "completed";
|
|
695
617
|
item.progress = 100;
|
|
696
618
|
item.response = {
|
|
697
|
-
file_no:
|
|
698
|
-
name:
|
|
699
|
-
url:
|
|
700
|
-
file_size:
|
|
619
|
+
file_no: appRunnerResponse.file_no,
|
|
620
|
+
name: appRunnerResponse.name,
|
|
621
|
+
url: appRunnerResponse.url,
|
|
622
|
+
file_size: appRunnerResponse.file_size,
|
|
701
623
|
folder_no: void 0,
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
workspace_no: completeResponse.workspace_no
|
|
624
|
+
path: appRunnerResponse.path,
|
|
625
|
+
width: appRunnerResponse.width,
|
|
626
|
+
height: appRunnerResponse.height,
|
|
627
|
+
format: appRunnerResponse.format,
|
|
628
|
+
workspace_no: appRunnerResponse.workspace_no
|
|
708
629
|
};
|
|
709
|
-
item.uploadedAt = new Date(
|
|
710
|
-
if (
|
|
630
|
+
item.uploadedAt = new Date(appRunnerResponse.created_at);
|
|
631
|
+
if (appRunnerResponse.isDuplicate) {
|
|
711
632
|
item.error = "duplicate";
|
|
712
633
|
}
|
|
713
634
|
this.dispatch("fileprogress", { item });
|
|
@@ -1814,7 +1735,9 @@ button,
|
|
|
1814
1735
|
);
|
|
1815
1736
|
|
|
1816
1737
|
// src/widget/uploader-element.ts
|
|
1817
|
-
var
|
|
1738
|
+
var HTMLElementShim = typeof globalThis === "undefined" || typeof globalThis.HTMLElement === "undefined" ? class HTMLElementFallback {
|
|
1739
|
+
} : globalThis.HTMLElement;
|
|
1740
|
+
var AutorenderUploaderElement = class extends HTMLElementShim {
|
|
1818
1741
|
constructor() {
|
|
1819
1742
|
super();
|
|
1820
1743
|
this.controller = null;
|
|
@@ -2532,12 +2455,6 @@ var AutorenderUploaderElement = class extends HTMLElement {
|
|
|
2532
2455
|
progressBorder.appendChild(progressFill);
|
|
2533
2456
|
const metaRow = document.createElement("div");
|
|
2534
2457
|
metaRow.className = "ar-file-meta";
|
|
2535
|
-
if (item.status === "completed" && item.error === "duplicate") {
|
|
2536
|
-
const duplicateBadge = document.createElement("span");
|
|
2537
|
-
duplicateBadge.className = "ar-status is-warning";
|
|
2538
|
-
duplicateBadge.appendChild(document.createTextNode(this.labels.duplicateFileError));
|
|
2539
|
-
metaRow.appendChild(duplicateBadge);
|
|
2540
|
-
}
|
|
2541
2458
|
li.appendChild(progressBorder);
|
|
2542
2459
|
if (metaRow.childNodes.length > 0) {
|
|
2543
2460
|
li.appendChild(metaRow);
|
|
@@ -2701,8 +2618,14 @@ var SOURCE_ALIAS_MAP = {
|
|
|
2701
2618
|
"google-drive": "google-drive"
|
|
2702
2619
|
};
|
|
2703
2620
|
function ensureCustomElement() {
|
|
2704
|
-
if (
|
|
2705
|
-
|
|
2621
|
+
if (typeof globalThis === "undefined" || typeof globalThis.customElements === "undefined") {
|
|
2622
|
+
return;
|
|
2623
|
+
}
|
|
2624
|
+
if (!globalThis.customElements.get(AutorenderUploaderElement.tagName)) {
|
|
2625
|
+
globalThis.customElements.define(
|
|
2626
|
+
AutorenderUploaderElement.tagName,
|
|
2627
|
+
AutorenderUploaderElement
|
|
2628
|
+
);
|
|
2706
2629
|
}
|
|
2707
2630
|
}
|
|
2708
2631
|
function resolveTarget(target) {
|
|
@@ -2827,7 +2750,8 @@ function createUploader(options) {
|
|
|
2827
2750
|
target.innerHTML = "";
|
|
2828
2751
|
target.appendChild(widget);
|
|
2829
2752
|
const client = new AutorenderApiClient({
|
|
2830
|
-
apiKey: options.apiKey
|
|
2753
|
+
apiKey: options.apiKey,
|
|
2754
|
+
environment: options.environment
|
|
2831
2755
|
});
|
|
2832
2756
|
const controllerOptions = normalizeOptions(options, true);
|
|
2833
2757
|
const controller = new UploaderController(
|