@dimah-s3/react 0.7.3 → 0.8.0
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/README.md +2 -3
- package/dist/create-s3-client.d.ts +15 -14
- package/dist/create-s3-client.d.ts.map +1 -1
- package/dist/helpers/format-dimah-error.d.ts.map +1 -1
- package/dist/hooks/index.d.ts +1 -2
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/use-delete.d.ts +3 -3
- package/dist/hooks/use-delete.d.ts.map +1 -1
- package/dist/hooks/use-download.d.ts +48 -10
- package/dist/hooks/use-download.d.ts.map +1 -1
- package/dist/hooks/use-file-upload.d.ts +3 -2
- package/dist/hooks/use-file-upload.d.ts.map +1 -1
- package/dist/hooks/use-multi-file-upload.d.ts +3 -2
- package/dist/hooks/use-multi-file-upload.d.ts.map +1 -1
- package/dist/hooks/use-multi-upload.d.ts +4 -3
- package/dist/hooks/use-multi-upload.d.ts.map +1 -1
- package/dist/hooks/use-upload.d.ts +4 -3
- package/dist/hooks/use-upload.d.ts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +191 -165
- package/dist/index.js.map +1 -1
- package/dist/s3-provider.d.ts +5 -10
- package/dist/s3-provider.d.ts.map +1 -1
- package/dist/translations/types.d.ts +5 -5
- package/dist/translations/types.d.ts.map +1 -1
- package/dist/types/error.d.ts +10 -12
- package/dist/types/error.d.ts.map +1 -1
- package/dist/types/multi-upload.d.ts +3 -2
- package/dist/types/multi-upload.d.ts.map +1 -1
- package/dist/upload/upload-file.d.ts.map +1 -1
- package/dist/upload/upload-files.d.ts +3 -3
- package/dist/upload/upload-files.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/helpers/format-dimah-error.test.d.ts +0 -2
- package/dist/helpers/format-dimah-error.test.d.ts.map +0 -1
- package/dist/helpers/helpers.test.d.ts +0 -2
- package/dist/helpers/helpers.test.d.ts.map +0 -1
- package/dist/hooks/use-fetch-download.d.ts +0 -33
- package/dist/hooks/use-fetch-download.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { DimahS3Error, createS3Client as createS3Client$1, formatFileSize, S3_ERROR_CODES, validateFile, parseFileName } from '@dimah-s3/core';
|
|
1
|
+
import { DimahS3Error, isDimahS3Error, createS3Client as createS3Client$1, formatFileSize, S3_ERROR_CODES, validateFile, parseFileName, defaultObjectKey } from '@dimah-s3/core';
|
|
2
|
+
export { DimahS3Error, S3_ERROR_CODES, buildObjectKey, defaultObjectKey, formatFileSize, isAPIError, isDimahS3Error, isS3ErrorCode, sanitizeFileName, validateFile } from '@dimah-s3/core';
|
|
2
3
|
import { createContext, useContext, useCallback, useRef, useEffect, useState, useLayoutEffect } from 'react';
|
|
3
4
|
import { TranslationProvider, useTranslations } from '@fuma-translate/react';
|
|
4
5
|
import { jsx } from 'react/jsx-runtime';
|
|
@@ -9,35 +10,40 @@ import { createStore } from 'zustand/vanilla';
|
|
|
9
10
|
import { useDropzone } from 'react-dropzone';
|
|
10
11
|
|
|
11
12
|
var S3UploadError = class extends DimahS3Error {
|
|
12
|
-
code;
|
|
13
13
|
phase;
|
|
14
14
|
constructor(message, code, status = 400, phase) {
|
|
15
|
-
super(message,
|
|
15
|
+
super(status, { message, code });
|
|
16
16
|
this.name = "S3UploadError";
|
|
17
|
-
this.code = code;
|
|
18
17
|
this.phase = phase;
|
|
19
18
|
}
|
|
20
|
-
/** Alias for {@link DimahS3Error.status}. */
|
|
21
|
-
get statusCode() {
|
|
22
|
-
return this.status;
|
|
23
|
-
}
|
|
24
19
|
};
|
|
25
20
|
function toUploadError(err, phase) {
|
|
26
21
|
if (err instanceof S3UploadError) {
|
|
27
22
|
if (phase != null && err.phase == null) {
|
|
28
|
-
return new S3UploadError(
|
|
23
|
+
return new S3UploadError(
|
|
24
|
+
err.message,
|
|
25
|
+
err.code ?? "UPLOAD_ERROR",
|
|
26
|
+
err.statusCode,
|
|
27
|
+
phase
|
|
28
|
+
);
|
|
29
29
|
}
|
|
30
30
|
return err;
|
|
31
31
|
}
|
|
32
32
|
if (err?.name === "AbortError") {
|
|
33
33
|
throw err;
|
|
34
34
|
}
|
|
35
|
-
if (err
|
|
36
|
-
return new S3UploadError(err.message, "API_ERROR", err.status, phase);
|
|
37
|
-
}
|
|
35
|
+
if (isDimahS3Error(err)) return err;
|
|
38
36
|
const message = err instanceof Error ? err.message : "Upload failed";
|
|
39
37
|
return new S3UploadError(message, "UPLOAD_ERROR", 500, phase);
|
|
40
38
|
}
|
|
39
|
+
function toHookError(err, fallback = "Request failed") {
|
|
40
|
+
if (isDimahS3Error(err)) return err;
|
|
41
|
+
const message = err instanceof Error ? err.message : fallback;
|
|
42
|
+
return new DimahS3Error("BAD_REQUEST", { message });
|
|
43
|
+
}
|
|
44
|
+
function hookBlockedError(message) {
|
|
45
|
+
return new DimahS3Error("BAD_REQUEST", { message });
|
|
46
|
+
}
|
|
41
47
|
|
|
42
48
|
// src/api.ts
|
|
43
49
|
function defineApi(input) {
|
|
@@ -62,13 +68,13 @@ function useApi() {
|
|
|
62
68
|
}
|
|
63
69
|
function createS3Client(options) {
|
|
64
70
|
const api = createS3Client$1(options);
|
|
65
|
-
function
|
|
71
|
+
function Provider(props) {
|
|
66
72
|
return /* @__PURE__ */ jsx(S3Provider, { api, ...props });
|
|
67
73
|
}
|
|
68
74
|
function useApi2() {
|
|
69
75
|
return useApi();
|
|
70
76
|
}
|
|
71
|
-
return
|
|
77
|
+
return Object.assign(api, { Provider, useApi: useApi2 });
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
// src/store/local-storage-store.ts
|
|
@@ -443,10 +449,10 @@ async function uploadFile(api, file, objectKey, config = {}, callbacks = {}, sig
|
|
|
443
449
|
const partSize = requestOptions?.partSize ?? config.partSize ?? DEFAULT_PART_SIZE;
|
|
444
450
|
const contentType = requestOptions?.contentType ?? file.type;
|
|
445
451
|
try {
|
|
446
|
-
let eTag;
|
|
447
452
|
if (useMultipart) {
|
|
448
453
|
callbacks.onPhaseChange?.("uploading");
|
|
449
|
-
|
|
454
|
+
let key = objectKey;
|
|
455
|
+
const eTag = await uploadMultipart(
|
|
450
456
|
api,
|
|
451
457
|
file,
|
|
452
458
|
objectKey,
|
|
@@ -458,51 +464,53 @@ async function uploadFile(api, file, objectKey, config = {}, callbacks = {}, sig
|
|
|
458
464
|
config.retry,
|
|
459
465
|
config.uploadStore,
|
|
460
466
|
callbacks.onPartUpload,
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
async () => {
|
|
466
|
-
callbacks.onPhaseChange?.("presigning");
|
|
467
|
-
const presign = await api.upload({
|
|
468
|
-
key: objectKey,
|
|
469
|
-
contentType,
|
|
470
|
-
fileSize: file.size,
|
|
471
|
-
fileName: requestOptions?.fileName !== null ? requestOptions?.fileName ?? file.name : void 0,
|
|
472
|
-
metadata: requestOptions?.metadata,
|
|
473
|
-
bucket: requestOptions?.bucket,
|
|
474
|
-
acl: requestOptions?.acl
|
|
475
|
-
});
|
|
476
|
-
callbacks.onPhaseChange?.("uploading");
|
|
477
|
-
if (presign.method === "PUT") {
|
|
478
|
-
await uploadPut(
|
|
479
|
-
file,
|
|
480
|
-
presign.url,
|
|
481
|
-
presign.headers ?? {},
|
|
482
|
-
callbacks.onProgress,
|
|
483
|
-
signal
|
|
484
|
-
);
|
|
485
|
-
} else {
|
|
486
|
-
await uploadSimple(
|
|
487
|
-
file,
|
|
488
|
-
presign.url,
|
|
489
|
-
presign.fields ?? {},
|
|
490
|
-
callbacks.onProgress,
|
|
491
|
-
signal
|
|
492
|
-
);
|
|
493
|
-
}
|
|
494
|
-
},
|
|
495
|
-
config.retry,
|
|
496
|
-
signal
|
|
467
|
+
(uploadId, serverKey) => {
|
|
468
|
+
key = serverKey;
|
|
469
|
+
callbacks.onMultipartInit?.(uploadId, serverKey);
|
|
470
|
+
}
|
|
497
471
|
);
|
|
498
|
-
|
|
499
|
-
const confirmed = await api.confirm({
|
|
500
|
-
key: objectKey,
|
|
501
|
-
bucket: requestOptions?.bucket
|
|
502
|
-
});
|
|
503
|
-
eTag = confirmed.eTag;
|
|
472
|
+
return { key, eTag };
|
|
504
473
|
}
|
|
505
|
-
return
|
|
474
|
+
return await withRetry(
|
|
475
|
+
async () => {
|
|
476
|
+
callbacks.onPhaseChange?.("presigning");
|
|
477
|
+
const presign = await api.upload({
|
|
478
|
+
key: objectKey,
|
|
479
|
+
contentType,
|
|
480
|
+
fileSize: file.size,
|
|
481
|
+
fileName: requestOptions?.fileName !== null ? requestOptions?.fileName ?? file.name : void 0,
|
|
482
|
+
metadata: requestOptions?.metadata,
|
|
483
|
+
bucket: requestOptions?.bucket,
|
|
484
|
+
acl: requestOptions?.acl
|
|
485
|
+
});
|
|
486
|
+
callbacks.onPhaseChange?.("uploading");
|
|
487
|
+
if (presign.method === "PUT") {
|
|
488
|
+
await uploadPut(
|
|
489
|
+
file,
|
|
490
|
+
presign.url,
|
|
491
|
+
presign.headers ?? {},
|
|
492
|
+
callbacks.onProgress,
|
|
493
|
+
signal
|
|
494
|
+
);
|
|
495
|
+
} else {
|
|
496
|
+
await uploadSimple(
|
|
497
|
+
file,
|
|
498
|
+
presign.url,
|
|
499
|
+
presign.fields ?? {},
|
|
500
|
+
callbacks.onProgress,
|
|
501
|
+
signal
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
callbacks.onPhaseChange?.("finalizing");
|
|
505
|
+
const confirmed = await api.confirm({
|
|
506
|
+
key: presign.key,
|
|
507
|
+
bucket: presign.bucket
|
|
508
|
+
});
|
|
509
|
+
return { key: presign.key, eTag: confirmed.eTag };
|
|
510
|
+
},
|
|
511
|
+
config.retry,
|
|
512
|
+
signal
|
|
513
|
+
);
|
|
506
514
|
} catch (err) {
|
|
507
515
|
throw toUploadError(err, "uploading");
|
|
508
516
|
}
|
|
@@ -564,13 +572,13 @@ async function uploadFiles(api, items, config = {}, callbacks = {}, signal, getR
|
|
|
564
572
|
} catch (err) {
|
|
565
573
|
if (err.name === "AbortError") {
|
|
566
574
|
item.status = "error";
|
|
567
|
-
item.error = "Upload cancelled";
|
|
575
|
+
item.error = toHookError(err, "Upload cancelled");
|
|
568
576
|
return;
|
|
569
577
|
}
|
|
570
|
-
const
|
|
578
|
+
const error = toHookError(err, "Upload failed");
|
|
571
579
|
item.status = "error";
|
|
572
|
-
item.error =
|
|
573
|
-
callbacks.onFileError?.(item.id,
|
|
580
|
+
item.error = error;
|
|
581
|
+
callbacks.onFileError?.(item.id, error);
|
|
574
582
|
reportTotalProgress();
|
|
575
583
|
}
|
|
576
584
|
}
|
|
@@ -662,46 +670,44 @@ function useFormatDimahError() {
|
|
|
662
670
|
const t = useTranslations();
|
|
663
671
|
return useCallback(
|
|
664
672
|
(err) => {
|
|
665
|
-
if (!(err
|
|
673
|
+
if (!isDimahS3Error(err) || err.code === void 0) {
|
|
666
674
|
return err instanceof Error ? err.message : t("Unknown error", { note: "fallback" });
|
|
667
675
|
}
|
|
668
676
|
switch (err.code) {
|
|
669
|
-
case S3_ERROR_CODES.NOT_FOUND:
|
|
677
|
+
case S3_ERROR_CODES.NOT_FOUND.code:
|
|
670
678
|
return t("Not found", { note: "API error" });
|
|
671
|
-
case S3_ERROR_CODES.
|
|
672
|
-
return t("
|
|
673
|
-
case S3_ERROR_CODES.FORBIDDEN:
|
|
679
|
+
case S3_ERROR_CODES.UNAUTHORIZED.code:
|
|
680
|
+
return t("Unauthorized", { note: "API error" });
|
|
681
|
+
case S3_ERROR_CODES.FORBIDDEN.code:
|
|
674
682
|
return t("Access denied", { note: "API error" });
|
|
675
|
-
case S3_ERROR_CODES.
|
|
683
|
+
case S3_ERROR_CODES.CONFLICT.code:
|
|
684
|
+
return t("Conflict", { note: "API error" });
|
|
685
|
+
case S3_ERROR_CODES.INTERNAL_ERROR.code:
|
|
676
686
|
return t("Something went wrong", { note: "API error" });
|
|
677
|
-
case S3_ERROR_CODES.OBJECT_NOT_FOUND:
|
|
687
|
+
case S3_ERROR_CODES.OBJECT_NOT_FOUND.code:
|
|
678
688
|
return t("File not found", { note: "API error" });
|
|
679
|
-
case S3_ERROR_CODES.
|
|
680
|
-
return t("
|
|
689
|
+
case S3_ERROR_CODES.FEATURE_DISABLED.code:
|
|
690
|
+
return t("{feature} is disabled", {
|
|
681
691
|
note: "API error",
|
|
682
|
-
variables: {
|
|
692
|
+
variables: {
|
|
693
|
+
feature: param(err.params, "feature", "This feature")
|
|
694
|
+
}
|
|
683
695
|
});
|
|
684
|
-
case S3_ERROR_CODES.
|
|
685
|
-
return t("
|
|
696
|
+
case S3_ERROR_CODES.INVALID_KEY.code:
|
|
697
|
+
return t("Object key is invalid", { note: "API error" });
|
|
698
|
+
case S3_ERROR_CODES.INVALID_BUCKET.code:
|
|
699
|
+
return t("Bucket is not allowed", { note: "API error" });
|
|
700
|
+
case S3_ERROR_CODES.S3_NETWORK_ERROR.code:
|
|
701
|
+
return t("Could not reach storage ({code})", {
|
|
686
702
|
note: "API error",
|
|
687
|
-
variables: {
|
|
688
|
-
});
|
|
689
|
-
case S3_ERROR_CODES.KEY_REQUIRED:
|
|
690
|
-
return t("Object key is required", { note: "API error" });
|
|
691
|
-
case S3_ERROR_CODES.UPLOAD_ID_REQUIRED:
|
|
692
|
-
return t("Upload ID is required", { note: "API error" });
|
|
693
|
-
case S3_ERROR_CODES.PART_NUMBER_INVALID:
|
|
694
|
-
return t("Part number must be a positive integer", {
|
|
695
|
-
note: "API error"
|
|
696
|
-
});
|
|
697
|
-
case S3_ERROR_CODES.PARTS_REQUIRED:
|
|
698
|
-
return t("At least one upload part is required", {
|
|
699
|
-
note: "API error"
|
|
703
|
+
variables: { code: param(err.params, "code", "UNKNOWN") }
|
|
700
704
|
});
|
|
701
|
-
case S3_ERROR_CODES.FILE_SIZE_REQUIRED_UPLOAD:
|
|
705
|
+
case S3_ERROR_CODES.FILE_SIZE_REQUIRED_UPLOAD.code:
|
|
702
706
|
return t("File size is required", { note: "upload" });
|
|
703
|
-
case S3_ERROR_CODES.FILE_SIZE_REQUIRED_MULTIPART:
|
|
707
|
+
case S3_ERROR_CODES.FILE_SIZE_REQUIRED_MULTIPART.code:
|
|
704
708
|
return t("File size is required", { note: "multipart" });
|
|
709
|
+
case S3_ERROR_CODES.VALIDATION_ERROR.code:
|
|
710
|
+
return t("Invalid request", { note: "API error" });
|
|
705
711
|
default:
|
|
706
712
|
return err.message;
|
|
707
713
|
}
|
|
@@ -885,7 +891,7 @@ function useFileUpload(options) {
|
|
|
885
891
|
const message = formatValidateFileError(validationError);
|
|
886
892
|
patchHookState(store, (draft) => {
|
|
887
893
|
draft.phase = "error";
|
|
888
|
-
draft.error = message;
|
|
894
|
+
draft.error = new DimahS3Error("BAD_REQUEST", { message });
|
|
889
895
|
});
|
|
890
896
|
opts.onError?.(file, new Error(message), "validating");
|
|
891
897
|
return;
|
|
@@ -895,7 +901,9 @@ function useFileUpload(options) {
|
|
|
895
901
|
if (!allowed) {
|
|
896
902
|
patchHookState(store, (draft) => {
|
|
897
903
|
draft.phase = "error";
|
|
898
|
-
draft.error =
|
|
904
|
+
draft.error = hookBlockedError(
|
|
905
|
+
"Upload blocked by beforeUpload hook"
|
|
906
|
+
);
|
|
899
907
|
});
|
|
900
908
|
opts.onError?.(file, new Error("blocked"), "validating");
|
|
901
909
|
return;
|
|
@@ -974,7 +982,7 @@ function useFileUpload(options) {
|
|
|
974
982
|
const message = err instanceof Error ? err.message : "Upload failed";
|
|
975
983
|
patchHookState(store, (draft) => {
|
|
976
984
|
draft.phase = "error";
|
|
977
|
-
draft.error = message;
|
|
985
|
+
draft.error = toHookError(err, message);
|
|
978
986
|
});
|
|
979
987
|
opts.onError?.(file, err, "uploading");
|
|
980
988
|
} finally {
|
|
@@ -1083,7 +1091,7 @@ function useMultiFileUpload(options) {
|
|
|
1083
1091
|
const msg = `Too many files. Maximum is ${opts.maxFiles}.`;
|
|
1084
1092
|
patchHookState(store, (draft) => {
|
|
1085
1093
|
draft.phase = "error";
|
|
1086
|
-
draft.error = msg;
|
|
1094
|
+
draft.error = new DimahS3Error("BAD_REQUEST", { message: msg });
|
|
1087
1095
|
});
|
|
1088
1096
|
opts.onError?.(new Error(msg));
|
|
1089
1097
|
return;
|
|
@@ -1098,7 +1106,7 @@ function useMultiFileUpload(options) {
|
|
|
1098
1106
|
const msg = `${file.name}: ${detail}`;
|
|
1099
1107
|
patchHookState(store, (draft) => {
|
|
1100
1108
|
draft.phase = "error";
|
|
1101
|
-
draft.error = msg;
|
|
1109
|
+
draft.error = new DimahS3Error("BAD_REQUEST", { message: msg });
|
|
1102
1110
|
});
|
|
1103
1111
|
opts.onError?.(new Error(msg));
|
|
1104
1112
|
return;
|
|
@@ -1109,7 +1117,9 @@ function useMultiFileUpload(options) {
|
|
|
1109
1117
|
if (!allowed) {
|
|
1110
1118
|
patchHookState(store, (draft) => {
|
|
1111
1119
|
draft.phase = "error";
|
|
1112
|
-
draft.error =
|
|
1120
|
+
draft.error = hookBlockedError(
|
|
1121
|
+
"Upload blocked by beforeUpload hook"
|
|
1122
|
+
);
|
|
1113
1123
|
});
|
|
1114
1124
|
opts.onError?.(new Error("blocked"));
|
|
1115
1125
|
return;
|
|
@@ -1248,7 +1258,9 @@ function useMultiFileUpload(options) {
|
|
|
1248
1258
|
const successResults = results.filter((r) => r.result !== null).map((r) => r.result);
|
|
1249
1259
|
patchHookState(store, (draft) => {
|
|
1250
1260
|
draft.phase = hasErrors ? "error" : "success";
|
|
1251
|
-
draft.error = hasErrors ?
|
|
1261
|
+
draft.error = hasErrors ? new DimahS3Error("BAD_REQUEST", {
|
|
1262
|
+
message: `${results.filter((r) => r.status === "error").length} file(s) failed`
|
|
1263
|
+
}) : null;
|
|
1252
1264
|
if (!hasErrors) {
|
|
1253
1265
|
draft.totalProgress = {
|
|
1254
1266
|
loaded: draft.totalProgress.total,
|
|
@@ -1271,10 +1283,9 @@ function useMultiFileUpload(options) {
|
|
|
1271
1283
|
clearToIdle();
|
|
1272
1284
|
return;
|
|
1273
1285
|
}
|
|
1274
|
-
const message = err instanceof Error ? err.message : "Upload failed";
|
|
1275
1286
|
patchHookState(store, (draft) => {
|
|
1276
1287
|
draft.phase = "error";
|
|
1277
|
-
draft.error =
|
|
1288
|
+
draft.error = toHookError(err, "Upload failed");
|
|
1278
1289
|
});
|
|
1279
1290
|
opts.onError?.(err);
|
|
1280
1291
|
} finally {
|
|
@@ -1389,7 +1400,11 @@ function useUpload(options) {
|
|
|
1389
1400
|
...uploadOpts
|
|
1390
1401
|
} = options;
|
|
1391
1402
|
const single = useFileUpload(uploadOpts);
|
|
1392
|
-
const resolveKey = (file) =>
|
|
1403
|
+
const resolveKey = (file) => {
|
|
1404
|
+
if (typeof objectKey === "function") return objectKey(file);
|
|
1405
|
+
if (objectKey) return objectKey;
|
|
1406
|
+
return defaultObjectKey(file);
|
|
1407
|
+
};
|
|
1393
1408
|
const handleFiles = (files) => {
|
|
1394
1409
|
const list = files == null ? [] : Array.from(files);
|
|
1395
1410
|
const file = list[0];
|
|
@@ -1437,8 +1452,6 @@ function useUpload(options) {
|
|
|
1437
1452
|
fileRejections: intake.fileRejections
|
|
1438
1453
|
};
|
|
1439
1454
|
}
|
|
1440
|
-
|
|
1441
|
-
// src/hooks/use-multi-upload.ts
|
|
1442
1455
|
function useMultiUpload(options) {
|
|
1443
1456
|
const {
|
|
1444
1457
|
objectKey,
|
|
@@ -1450,7 +1463,11 @@ function useMultiUpload(options) {
|
|
|
1450
1463
|
...multiOpts
|
|
1451
1464
|
} = options;
|
|
1452
1465
|
const multi = useMultiFileUpload(multiOpts);
|
|
1453
|
-
const resolveKey = (file) =>
|
|
1466
|
+
const resolveKey = (file) => {
|
|
1467
|
+
if (typeof objectKey === "function") return objectKey(file);
|
|
1468
|
+
if (objectKey) return objectKey;
|
|
1469
|
+
return defaultObjectKey(file);
|
|
1470
|
+
};
|
|
1454
1471
|
const handleFiles = (files) => {
|
|
1455
1472
|
if (files == null) return;
|
|
1456
1473
|
const list = Array.from(files);
|
|
@@ -1489,31 +1506,43 @@ function useMultiUpload(options) {
|
|
|
1489
1506
|
fileRejections: intake.fileRejections
|
|
1490
1507
|
};
|
|
1491
1508
|
}
|
|
1509
|
+
var INITIAL_PROGRESS3 = { loaded: 0, total: 0, percent: 0 };
|
|
1492
1510
|
var INITIAL_STATE3 = {
|
|
1493
1511
|
phase: "idle",
|
|
1494
1512
|
error: null,
|
|
1495
1513
|
url: null,
|
|
1496
|
-
expiresIn: null
|
|
1514
|
+
expiresIn: null,
|
|
1515
|
+
progress: INITIAL_PROGRESS3,
|
|
1516
|
+
fileName: null,
|
|
1517
|
+
fileSize: null
|
|
1497
1518
|
};
|
|
1498
|
-
function
|
|
1519
|
+
function missingApiMessage(hook) {
|
|
1520
|
+
return `[dimah-s3] No S3Api found. Pass \`api\` to ${hook} or wrap with <S3Provider>.`;
|
|
1521
|
+
}
|
|
1522
|
+
function useDownload(options = {}) {
|
|
1499
1523
|
const store = useHookStoreInstance(INITIAL_STATE3);
|
|
1500
1524
|
const state = useHookStoreShallow(store, (s) => ({
|
|
1501
1525
|
phase: s.phase,
|
|
1502
1526
|
error: s.error,
|
|
1503
1527
|
url: s.url,
|
|
1504
|
-
expiresIn: s.expiresIn
|
|
1528
|
+
expiresIn: s.expiresIn,
|
|
1529
|
+
progress: s.progress,
|
|
1530
|
+
fileName: s.fileName,
|
|
1531
|
+
fileSize: s.fileSize
|
|
1505
1532
|
}));
|
|
1506
1533
|
const contextApi = useContext(S3Context);
|
|
1507
1534
|
const optsRef = useLiveRef(options);
|
|
1508
1535
|
const apiRef = useLiveRef(contextApi);
|
|
1536
|
+
const abortRef = useRef(null);
|
|
1537
|
+
const resettingRef = useRef(false);
|
|
1538
|
+
const speedUpdaterRef = useRef(
|
|
1539
|
+
createThrottledSpeedUpdater(createSpeedTracker())
|
|
1540
|
+
);
|
|
1509
1541
|
const presign = useCallback(
|
|
1510
1542
|
async (key, downloadName) => {
|
|
1511
1543
|
const opts = optsRef.current;
|
|
1512
1544
|
const api = opts.api ?? apiRef.current;
|
|
1513
|
-
if (!api)
|
|
1514
|
-
throw new Error(
|
|
1515
|
-
"[dimah-s3] No S3Api found. Pass `api` to useDownload or wrap with <S3Provider>."
|
|
1516
|
-
);
|
|
1545
|
+
if (!api) throw new Error(missingApiMessage("useDownload"));
|
|
1517
1546
|
patchHookState(store, (draft) => {
|
|
1518
1547
|
draft.phase = "presigning";
|
|
1519
1548
|
draft.error = null;
|
|
@@ -1533,10 +1562,9 @@ function useDownload(options) {
|
|
|
1533
1562
|
});
|
|
1534
1563
|
return { url: result.url, expiresIn: result.expiresIn };
|
|
1535
1564
|
} catch (err) {
|
|
1536
|
-
const message = err instanceof Error ? err.message : "Download failed";
|
|
1537
1565
|
patchHookState(store, (draft) => {
|
|
1538
1566
|
draft.phase = "error";
|
|
1539
|
-
draft.error =
|
|
1567
|
+
draft.error = toHookError(err, "Download failed");
|
|
1540
1568
|
draft.url = null;
|
|
1541
1569
|
draft.expiresIn = null;
|
|
1542
1570
|
});
|
|
@@ -1546,7 +1574,7 @@ function useDownload(options) {
|
|
|
1546
1574
|
},
|
|
1547
1575
|
[apiRef, optsRef, store]
|
|
1548
1576
|
);
|
|
1549
|
-
const
|
|
1577
|
+
const downloadNavigate = useCallback(
|
|
1550
1578
|
async (key, downloadName) => {
|
|
1551
1579
|
const opts = optsRef.current;
|
|
1552
1580
|
if (opts.beforeDownload) {
|
|
@@ -1554,7 +1582,9 @@ function useDownload(options) {
|
|
|
1554
1582
|
if (!allowed) {
|
|
1555
1583
|
patchHookState(store, (draft) => {
|
|
1556
1584
|
draft.phase = "error";
|
|
1557
|
-
draft.error =
|
|
1585
|
+
draft.error = hookBlockedError(
|
|
1586
|
+
"Download blocked by beforeDownload hook"
|
|
1587
|
+
);
|
|
1558
1588
|
draft.url = null;
|
|
1559
1589
|
draft.expiresIn = null;
|
|
1560
1590
|
});
|
|
@@ -1569,52 +1599,20 @@ function useDownload(options) {
|
|
|
1569
1599
|
},
|
|
1570
1600
|
[optsRef, presign, store]
|
|
1571
1601
|
);
|
|
1572
|
-
const
|
|
1573
|
-
() => replaceHookState(store, INITIAL_STATE3),
|
|
1574
|
-
[store]
|
|
1575
|
-
);
|
|
1576
|
-
return { ...state, download, presign, reset };
|
|
1577
|
-
}
|
|
1578
|
-
var INITIAL_PROGRESS3 = { loaded: 0, total: 0, percent: 0 };
|
|
1579
|
-
var INITIAL_STATE4 = {
|
|
1580
|
-
phase: "idle",
|
|
1581
|
-
progress: INITIAL_PROGRESS3,
|
|
1582
|
-
error: null,
|
|
1583
|
-
fileName: null,
|
|
1584
|
-
fileSize: null
|
|
1585
|
-
};
|
|
1586
|
-
function useFetchDownload(options) {
|
|
1587
|
-
const store = useHookStoreInstance(INITIAL_STATE4);
|
|
1588
|
-
const state = useHookStoreShallow(store, (s) => ({
|
|
1589
|
-
phase: s.phase,
|
|
1590
|
-
progress: s.progress,
|
|
1591
|
-
error: s.error,
|
|
1592
|
-
fileName: s.fileName,
|
|
1593
|
-
fileSize: s.fileSize
|
|
1594
|
-
}));
|
|
1595
|
-
const contextApi = useContext(S3Context);
|
|
1596
|
-
const optsRef = useLiveRef(options);
|
|
1597
|
-
const apiRef = useLiveRef(contextApi);
|
|
1598
|
-
const abortRef = useRef(null);
|
|
1599
|
-
const resettingRef = useRef(false);
|
|
1600
|
-
const speedUpdaterRef = useRef(
|
|
1601
|
-
createThrottledSpeedUpdater(createSpeedTracker())
|
|
1602
|
-
);
|
|
1603
|
-
const download = useCallback(
|
|
1602
|
+
const downloadFetch = useCallback(
|
|
1604
1603
|
async (key, downloadName) => {
|
|
1605
1604
|
const fallback = key.split("/").pop() ?? key;
|
|
1606
1605
|
const opts = optsRef.current;
|
|
1607
1606
|
const api = opts.api ?? apiRef.current;
|
|
1608
|
-
if (!api)
|
|
1609
|
-
throw new Error(
|
|
1610
|
-
"[dimah-s3] No S3Api found. Pass `api` to useFetchDownload or wrap with <S3Provider>."
|
|
1611
|
-
);
|
|
1607
|
+
if (!api) throw new Error(missingApiMessage("useDownload"));
|
|
1612
1608
|
if (opts.beforeDownload) {
|
|
1613
1609
|
const allowed = await opts.beforeDownload(key);
|
|
1614
1610
|
if (!allowed) {
|
|
1615
1611
|
patchHookState(store, (draft) => {
|
|
1616
1612
|
draft.phase = "error";
|
|
1617
|
-
draft.error =
|
|
1613
|
+
draft.error = hookBlockedError(
|
|
1614
|
+
"Download blocked by beforeDownload hook"
|
|
1615
|
+
);
|
|
1618
1616
|
});
|
|
1619
1617
|
opts.onError?.(key, new Error("blocked"), "presigning");
|
|
1620
1618
|
return;
|
|
@@ -1692,13 +1690,12 @@ function useFetchDownload(options) {
|
|
|
1692
1690
|
if (err.name === "AbortError") {
|
|
1693
1691
|
if (!resettingRef.current) opts.onCancel?.(key);
|
|
1694
1692
|
resettingRef.current = false;
|
|
1695
|
-
replaceHookState(store,
|
|
1693
|
+
replaceHookState(store, INITIAL_STATE3);
|
|
1696
1694
|
return;
|
|
1697
1695
|
}
|
|
1698
|
-
const message = err instanceof Error ? err.message : "Download failed";
|
|
1699
1696
|
patchHookState(store, (draft) => {
|
|
1700
1697
|
draft.phase = "error";
|
|
1701
|
-
draft.error =
|
|
1698
|
+
draft.error = toHookError(err, "Download failed");
|
|
1702
1699
|
});
|
|
1703
1700
|
opts.onError?.(key, err, "downloading");
|
|
1704
1701
|
} finally {
|
|
@@ -1707,24 +1704,54 @@ function useFetchDownload(options) {
|
|
|
1707
1704
|
},
|
|
1708
1705
|
[apiRef, optsRef, store]
|
|
1709
1706
|
);
|
|
1707
|
+
const download = useCallback(
|
|
1708
|
+
async (key, downloadName) => {
|
|
1709
|
+
if (optsRef.current.mode === "fetch") {
|
|
1710
|
+
await downloadFetch(key, downloadName);
|
|
1711
|
+
return;
|
|
1712
|
+
}
|
|
1713
|
+
await downloadNavigate(key, downloadName);
|
|
1714
|
+
},
|
|
1715
|
+
[downloadFetch, downloadNavigate, optsRef]
|
|
1716
|
+
);
|
|
1710
1717
|
const cancel = useCallback(() => {
|
|
1711
1718
|
abortRef.current?.abort();
|
|
1712
|
-
replaceHookState(store,
|
|
1719
|
+
replaceHookState(store, INITIAL_STATE3);
|
|
1713
1720
|
}, [store]);
|
|
1714
1721
|
const reset = useCallback(() => {
|
|
1715
1722
|
resettingRef.current = true;
|
|
1716
1723
|
abortRef.current?.abort();
|
|
1717
|
-
replaceHookState(store,
|
|
1724
|
+
replaceHookState(store, INITIAL_STATE3);
|
|
1718
1725
|
}, [store]);
|
|
1719
|
-
|
|
1726
|
+
if (options.mode === "fetch") {
|
|
1727
|
+
return {
|
|
1728
|
+
phase: state.phase,
|
|
1729
|
+
progress: state.progress,
|
|
1730
|
+
error: state.error,
|
|
1731
|
+
fileName: state.fileName,
|
|
1732
|
+
fileSize: state.fileSize,
|
|
1733
|
+
download,
|
|
1734
|
+
cancel,
|
|
1735
|
+
reset
|
|
1736
|
+
};
|
|
1737
|
+
}
|
|
1738
|
+
return {
|
|
1739
|
+
phase: state.phase,
|
|
1740
|
+
error: state.error,
|
|
1741
|
+
url: state.url,
|
|
1742
|
+
expiresIn: state.expiresIn,
|
|
1743
|
+
download,
|
|
1744
|
+
presign,
|
|
1745
|
+
reset
|
|
1746
|
+
};
|
|
1720
1747
|
}
|
|
1721
|
-
var
|
|
1748
|
+
var INITIAL_STATE4 = {
|
|
1722
1749
|
phase: "idle",
|
|
1723
1750
|
error: null,
|
|
1724
1751
|
pendingKey: null
|
|
1725
1752
|
};
|
|
1726
1753
|
function useDelete(options) {
|
|
1727
|
-
const store = useHookStoreInstance(
|
|
1754
|
+
const store = useHookStoreInstance(INITIAL_STATE4);
|
|
1728
1755
|
const state = useHookStoreShallow(store, (s) => ({
|
|
1729
1756
|
phase: s.phase,
|
|
1730
1757
|
error: s.error,
|
|
@@ -1759,7 +1786,7 @@ function useDelete(options) {
|
|
|
1759
1786
|
if (!allowed) {
|
|
1760
1787
|
patchHookState(store, (draft) => {
|
|
1761
1788
|
draft.phase = "error";
|
|
1762
|
-
draft.error = "Delete blocked by beforeDelete hook";
|
|
1789
|
+
draft.error = hookBlockedError("Delete blocked by beforeDelete hook");
|
|
1763
1790
|
draft.pendingKey = null;
|
|
1764
1791
|
});
|
|
1765
1792
|
opts.onError?.(key, new Error("blocked"), "confirming");
|
|
@@ -1782,21 +1809,20 @@ function useDelete(options) {
|
|
|
1782
1809
|
});
|
|
1783
1810
|
await opts.onSuccess?.(key);
|
|
1784
1811
|
} catch (err) {
|
|
1785
|
-
const message = err instanceof Error ? err.message : "Delete failed";
|
|
1786
1812
|
patchHookState(store, (draft) => {
|
|
1787
1813
|
draft.phase = "error";
|
|
1788
|
-
draft.error =
|
|
1814
|
+
draft.error = toHookError(err, "Delete failed");
|
|
1789
1815
|
});
|
|
1790
1816
|
opts.onError?.(key, err, "deleting");
|
|
1791
1817
|
}
|
|
1792
1818
|
}, [apiRef, optsRef, store]);
|
|
1793
1819
|
const cancelDelete = useCallback(() => {
|
|
1794
1820
|
pendingKeyRef.current = null;
|
|
1795
|
-
replaceHookState(store,
|
|
1821
|
+
replaceHookState(store, INITIAL_STATE4);
|
|
1796
1822
|
}, [store]);
|
|
1797
1823
|
const reset = useCallback(() => {
|
|
1798
1824
|
pendingKeyRef.current = null;
|
|
1799
|
-
replaceHookState(store,
|
|
1825
|
+
replaceHookState(store, INITIAL_STATE4);
|
|
1800
1826
|
}, [store]);
|
|
1801
1827
|
return {
|
|
1802
1828
|
phase: state.phase,
|
|
@@ -1809,6 +1835,6 @@ function useDelete(options) {
|
|
|
1809
1835
|
};
|
|
1810
1836
|
}
|
|
1811
1837
|
|
|
1812
|
-
export {
|
|
1838
|
+
export { S3Provider, S3UploadError, createImagePreviewUrl, createLocalStorageStore, createMemoryStore, createS3Client, createSpeedTracker, defineApi, formatAcceptLabels, formatEta, formatSpeed, formatUploadProgress, hookBlockedError, revokePreviewUrl, toDropzoneAccept, toHookError, toUploadError, uploadFile, uploadFiles, useApi, useDelete, useDownload, useFileIntake, useFileUpload, useFormatDimahError, useFormatValidateFileError, useMultiFileUpload, useMultiUpload, useUpload };
|
|
1813
1839
|
//# sourceMappingURL=index.js.map
|
|
1814
1840
|
//# sourceMappingURL=index.js.map
|