@autorender/sdk-core 0.1.5 → 0.1.6

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 CHANGED
@@ -27,6 +27,7 @@ module.exports = __toCommonJS(src_exports);
27
27
 
28
28
  // src/constants/defaults.ts
29
29
  var DEFAULT_BASE_URL = "https://autorenderv3.vercel.app/api/public";
30
+ var DEFAULT_ASSET_DELIVERY_BASE_URL = "https://dev.autorender.io";
30
31
  var DEFAULT_LABELS = {
31
32
  title: "Upload files",
32
33
  description: "Drag & drop files or click to browse.",
@@ -354,6 +355,49 @@ function calculateBatchProgress(items) {
354
355
  const total = items.reduce((acc, item) => acc + item.progress, 0);
355
356
  return Math.round(total / items.length);
356
357
  }
358
+ var S3_URL_PATTERN = /^s3:\/\/([^/]+)\/(.+)$/i;
359
+ function sanitizeBaseUrl(baseUrl) {
360
+ if (!baseUrl) return void 0;
361
+ const trimmed = baseUrl.trim();
362
+ if (!trimmed) return void 0;
363
+ return trimmed.replace(/\/+$/, "");
364
+ }
365
+ function extractWorkspaceFromUrl(url) {
366
+ if (!url) return void 0;
367
+ const s3Match = S3_URL_PATTERN.exec(url);
368
+ if (s3Match) {
369
+ return s3Match[1];
370
+ }
371
+ try {
372
+ const parsed = new URL(url);
373
+ const firstSegment = parsed.pathname.replace(/^\/+/, "").split("/")[0];
374
+ return firstSegment || void 0;
375
+ } catch {
376
+ return void 0;
377
+ }
378
+ }
379
+ function extractPathFromUrl(url) {
380
+ if (!url) return void 0;
381
+ const s3Match = S3_URL_PATTERN.exec(url);
382
+ if (s3Match) {
383
+ return s3Match[2];
384
+ }
385
+ try {
386
+ const parsed = new URL(url);
387
+ const [, ...rest] = parsed.pathname.replace(/^\/+/, "").split("/");
388
+ return rest.length ? rest.join("/") : void 0;
389
+ } catch {
390
+ return void 0;
391
+ }
392
+ }
393
+ function buildAssetDeliveryUrl(baseUrl, workspace, path, fallback) {
394
+ const sanitizedBase = sanitizeBaseUrl(baseUrl);
395
+ if (!sanitizedBase || !workspace || !path) {
396
+ return fallback;
397
+ }
398
+ const normalizedPath = path.split("/").filter(Boolean).map((segment) => encodeURIComponent(segment)).join("/");
399
+ return `${sanitizedBase}/${workspace}/${normalizedPath}`.replace(/([^:]\/)\/+/g, "$1");
400
+ }
357
401
 
358
402
  // src/core/uploader-controller.ts
359
403
  var DEFAULT_PARALLEL_UPLOADS = 4;
@@ -429,7 +473,7 @@ var UploaderController = class extends EventTarget {
429
473
  return baseItem;
430
474
  });
431
475
  this.items = [...this.items, ...newItems];
432
- this.dispatch("filesadded", { detail: { items: newItems } });
476
+ this.dispatch("filesadded", { items: newItems });
433
477
  const duplicates = newItems.filter((item) => item.status === "completed");
434
478
  if (duplicates.length) {
435
479
  this.markDuplicatesCompleted(duplicates);
@@ -466,19 +510,18 @@ var UploaderController = class extends EventTarget {
466
510
  try {
467
511
  await this.client.validateApiKey(false, this.abortController?.signal ?? void 0);
468
512
  await this.processUploads();
513
+ const files = this.buildSuccessFiles();
469
514
  this.dispatch("complete", {
470
- detail: {
471
- items: this.items,
472
- files: this.items.filter((item) => item.status === "completed").map((item) => item.response).filter(Boolean)
473
- }
515
+ items: this.items,
516
+ files
474
517
  });
475
518
  } catch (error) {
476
519
  if (this.isAbortError(error)) {
477
- this.dispatch("error", { detail: error });
520
+ this.dispatch("error", error);
478
521
  return;
479
522
  }
480
523
  this.annotateItemsWithError(error);
481
- this.dispatch("error", { detail: error });
524
+ this.dispatch("error", error);
482
525
  throw error;
483
526
  } finally {
484
527
  this.isUploading = false;
@@ -523,14 +566,12 @@ var UploaderController = class extends EventTarget {
523
566
  item.status = "completed";
524
567
  item.progress = 100;
525
568
  item.error = annotateError ? "duplicate" : void 0;
526
- this.dispatch("fileprogress", { detail: { item } });
569
+ this.dispatch("fileprogress", { item });
527
570
  });
528
571
  if (duplicates.length > 0) {
529
572
  this.dispatch("progress", {
530
- detail: {
531
- progress: calculateBatchProgress(this.items),
532
- items: this.items
533
- }
573
+ progress: calculateBatchProgress(this.items),
574
+ items: this.items
534
575
  });
535
576
  this.dispatch("statechange");
536
577
  }
@@ -559,14 +600,12 @@ var UploaderController = class extends EventTarget {
559
600
  item.status = "error";
560
601
  item.progress = item.progress ?? 0;
561
602
  item.error = isAuth ? "invalid-api-key" : fallbackMessage;
562
- this.dispatch("fileprogress", { detail: { item } });
603
+ this.dispatch("fileprogress", { item });
563
604
  });
564
605
  if (mutated) {
565
606
  this.dispatch("progress", {
566
- detail: {
567
- progress: calculateBatchProgress(this.items),
568
- items: this.items
569
- }
607
+ progress: calculateBatchProgress(this.items),
608
+ items: this.items
570
609
  });
571
610
  this.dispatch("statechange");
572
611
  }
@@ -596,7 +635,7 @@ var UploaderController = class extends EventTarget {
596
635
  const err = error instanceof Error ? error : new Error(String(error));
597
636
  item.status = "error";
598
637
  item.error = err.message;
599
- this.dispatch("fileprogress", { detail: { item } });
638
+ this.dispatch("fileprogress", { item });
600
639
  this.dispatch("statechange");
601
640
  }
602
641
  }
@@ -623,7 +662,7 @@ var UploaderController = class extends EventTarget {
623
662
  this.abortController?.signal
624
663
  );
625
664
  item.progress = 10;
626
- this.dispatch("fileprogress", { detail: { item } });
665
+ this.dispatch("fileprogress", { item });
627
666
  this.dispatch("statechange");
628
667
  const appRunnerResponse = await this.client.ingest(
629
668
  item.file,
@@ -631,12 +670,10 @@ var UploaderController = class extends EventTarget {
631
670
  initResponse.uploadUrl,
632
671
  (progress) => {
633
672
  item.progress = 10 + Math.round(progress * 0.8);
634
- this.dispatch("fileprogress", { detail: { item } });
673
+ this.dispatch("fileprogress", { item });
635
674
  this.dispatch("progress", {
636
- detail: {
637
- progress: calculateBatchProgress(this.items),
638
- items: this.items
639
- }
675
+ progress: calculateBatchProgress(this.items),
676
+ items: this.items
640
677
  });
641
678
  },
642
679
  this.abortController?.signal
@@ -645,7 +682,7 @@ var UploaderController = class extends EventTarget {
645
682
  throw new Error(appRunnerResponse.message || "Upload failed");
646
683
  }
647
684
  item.progress = 90;
648
- this.dispatch("fileprogress", { detail: { item } });
685
+ this.dispatch("fileprogress", { item });
649
686
  this.dispatch("statechange");
650
687
  const completeResponse = await this.client.completeUploadNew(
651
688
  {
@@ -666,18 +703,17 @@ var UploaderController = class extends EventTarget {
666
703
  path: completeResponse.path,
667
704
  width: completeResponse.width,
668
705
  height: completeResponse.height,
669
- format: completeResponse.format
706
+ format: completeResponse.format,
707
+ workspace_no: completeResponse.workspace_no
670
708
  };
671
709
  item.uploadedAt = new Date(completeResponse.created_at);
672
710
  if (completeResponse.isDuplicate) {
673
711
  item.error = "duplicate";
674
712
  }
675
- this.dispatch("fileprogress", { detail: { item } });
713
+ this.dispatch("fileprogress", { item });
676
714
  this.dispatch("progress", {
677
- detail: {
678
- progress: calculateBatchProgress(this.items),
679
- items: this.items
680
- }
715
+ progress: calculateBatchProgress(this.items),
716
+ items: this.items
681
717
  });
682
718
  this.dispatch("statechange");
683
719
  }
@@ -710,15 +746,39 @@ var UploaderController = class extends EventTarget {
710
746
  );
711
747
  break;
712
748
  case "complete":
713
- this.options.onSuccess?.({
714
- files: detail?.files ?? [],
715
- items: this.items
716
- });
749
+ this.options.onSuccess?.(detail?.files ?? []);
717
750
  break;
718
751
  default:
719
752
  break;
720
753
  }
721
754
  }
755
+ resolveAssetDeliveryBaseUrl() {
756
+ return sanitizeBaseUrl(this.options.assetDeliveryBaseUrl) ?? DEFAULT_ASSET_DELIVERY_BASE_URL;
757
+ }
758
+ buildSuccessFiles() {
759
+ const baseUrl = this.resolveAssetDeliveryBaseUrl();
760
+ return this.items.filter((item) => item.status === "completed" && item.response).map((item) => {
761
+ const response = item.response;
762
+ const workspace = response.workspace_no ?? extractWorkspaceFromUrl(response.url);
763
+ const assetPath = response.path ?? extractPathFromUrl(response.url);
764
+ const resolvedUrl = buildAssetDeliveryUrl(baseUrl, workspace, assetPath, response.url) ?? response.url;
765
+ return {
766
+ id: item.id,
767
+ previewUrl: item.previewUrl,
768
+ file_no: response.file_no,
769
+ name: response.name,
770
+ url: resolvedUrl,
771
+ input_file_size: item.file?.size ?? response.file_size ?? 0,
772
+ output_file_size: response.file_size ?? 0,
773
+ path: response.path ?? assetPath,
774
+ width: response.width,
775
+ height: response.height,
776
+ format: response.format,
777
+ workspace_no: workspace,
778
+ uploadedAt: item.uploadedAt?.toISOString()
779
+ };
780
+ });
781
+ }
722
782
  async runWithConcurrency(tasks, limit) {
723
783
  const executing = /* @__PURE__ */ new Set();
724
784
  for (const task of tasks) {
@@ -2751,6 +2811,11 @@ function normalizeOptions(options, applyDefaults = false) {
2751
2811
  } else if (applyDefaults) {
2752
2812
  normalized.showGridFileName = true;
2753
2813
  }
2814
+ if (options.assetDeliveryBaseUrl !== void 0) {
2815
+ normalized.assetDeliveryBaseUrl = options.assetDeliveryBaseUrl;
2816
+ } else if (applyDefaults) {
2817
+ normalized.assetDeliveryBaseUrl = DEFAULT_ASSET_DELIVERY_BASE_URL;
2818
+ }
2754
2819
  return normalized;
2755
2820
  }
2756
2821
  function createUploader(options) {