@byteship/react 0.1.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/dist/index.d.ts +99 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +91 -0
- package/package.json +37 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ByteshipClient, ByteshipUploadManyOptions, ByteshipUploadManyResult, ByteshipUploadOptions } from "@byteship/js";
|
|
2
|
+
export type UploadState = {
|
|
3
|
+
error?: undefined;
|
|
4
|
+
fileId?: undefined;
|
|
5
|
+
fileIds?: undefined;
|
|
6
|
+
progress?: undefined;
|
|
7
|
+
status: "idle";
|
|
8
|
+
} | {
|
|
9
|
+
error?: undefined;
|
|
10
|
+
fileId?: undefined;
|
|
11
|
+
fileIds?: undefined;
|
|
12
|
+
progress: number;
|
|
13
|
+
status: "uploading";
|
|
14
|
+
} | {
|
|
15
|
+
error?: undefined;
|
|
16
|
+
fileId: string;
|
|
17
|
+
fileIds?: undefined;
|
|
18
|
+
progress?: undefined;
|
|
19
|
+
status: "ready";
|
|
20
|
+
} | {
|
|
21
|
+
error?: undefined;
|
|
22
|
+
fileId?: undefined;
|
|
23
|
+
fileIds: string[];
|
|
24
|
+
progress?: undefined;
|
|
25
|
+
status: "ready";
|
|
26
|
+
} | {
|
|
27
|
+
error: Error;
|
|
28
|
+
fileId?: undefined;
|
|
29
|
+
fileIds?: undefined;
|
|
30
|
+
progress?: undefined;
|
|
31
|
+
status: "error";
|
|
32
|
+
};
|
|
33
|
+
export declare function useByteshipUpload(client: ByteshipClient, defaultOptions?: ByteshipUploadOptions): {
|
|
34
|
+
upload: (file: File, options?: ByteshipUploadOptions) => Promise<{
|
|
35
|
+
byteSize: number;
|
|
36
|
+
etag?: string | null | undefined;
|
|
37
|
+
id: string;
|
|
38
|
+
status: "uploading" | "ready" | "pending" | "failed" | "deleted";
|
|
39
|
+
}>;
|
|
40
|
+
uploadMany: (files: Iterable<File>, options?: ByteshipUploadManyOptions) => Promise<ByteshipUploadManyResult[]>;
|
|
41
|
+
error?: undefined;
|
|
42
|
+
fileId?: undefined;
|
|
43
|
+
fileIds?: undefined;
|
|
44
|
+
progress?: undefined;
|
|
45
|
+
status: "idle";
|
|
46
|
+
} | {
|
|
47
|
+
upload: (file: File, options?: ByteshipUploadOptions) => Promise<{
|
|
48
|
+
byteSize: number;
|
|
49
|
+
etag?: string | null | undefined;
|
|
50
|
+
id: string;
|
|
51
|
+
status: "uploading" | "ready" | "pending" | "failed" | "deleted";
|
|
52
|
+
}>;
|
|
53
|
+
uploadMany: (files: Iterable<File>, options?: ByteshipUploadManyOptions) => Promise<ByteshipUploadManyResult[]>;
|
|
54
|
+
error?: undefined;
|
|
55
|
+
fileId?: undefined;
|
|
56
|
+
fileIds?: undefined;
|
|
57
|
+
progress: number;
|
|
58
|
+
status: "uploading";
|
|
59
|
+
} | {
|
|
60
|
+
upload: (file: File, options?: ByteshipUploadOptions) => Promise<{
|
|
61
|
+
byteSize: number;
|
|
62
|
+
etag?: string | null | undefined;
|
|
63
|
+
id: string;
|
|
64
|
+
status: "uploading" | "ready" | "pending" | "failed" | "deleted";
|
|
65
|
+
}>;
|
|
66
|
+
uploadMany: (files: Iterable<File>, options?: ByteshipUploadManyOptions) => Promise<ByteshipUploadManyResult[]>;
|
|
67
|
+
error?: undefined;
|
|
68
|
+
fileId: string;
|
|
69
|
+
fileIds?: undefined;
|
|
70
|
+
progress?: undefined;
|
|
71
|
+
status: "ready";
|
|
72
|
+
} | {
|
|
73
|
+
upload: (file: File, options?: ByteshipUploadOptions) => Promise<{
|
|
74
|
+
byteSize: number;
|
|
75
|
+
etag?: string | null | undefined;
|
|
76
|
+
id: string;
|
|
77
|
+
status: "uploading" | "ready" | "pending" | "failed" | "deleted";
|
|
78
|
+
}>;
|
|
79
|
+
uploadMany: (files: Iterable<File>, options?: ByteshipUploadManyOptions) => Promise<ByteshipUploadManyResult[]>;
|
|
80
|
+
error?: undefined;
|
|
81
|
+
fileId?: undefined;
|
|
82
|
+
fileIds: string[];
|
|
83
|
+
progress?: undefined;
|
|
84
|
+
status: "ready";
|
|
85
|
+
} | {
|
|
86
|
+
upload: (file: File, options?: ByteshipUploadOptions) => Promise<{
|
|
87
|
+
byteSize: number;
|
|
88
|
+
etag?: string | null | undefined;
|
|
89
|
+
id: string;
|
|
90
|
+
status: "uploading" | "ready" | "pending" | "failed" | "deleted";
|
|
91
|
+
}>;
|
|
92
|
+
uploadMany: (files: Iterable<File>, options?: ByteshipUploadManyOptions) => Promise<ByteshipUploadManyResult[]>;
|
|
93
|
+
error: Error;
|
|
94
|
+
fileId?: undefined;
|
|
95
|
+
fileIds?: undefined;
|
|
96
|
+
progress?: undefined;
|
|
97
|
+
status: "error";
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,yBAAyB,EACzB,wBAAwB,EACxB,qBAAqB,EAEtB,MAAM,cAAc,CAAA;AAGrB,MAAM,MAAM,WAAW,GACnB;IACE,KAAK,CAAC,EAAE,SAAS,CAAA;IACjB,MAAM,CAAC,EAAE,SAAS,CAAA;IAClB,OAAO,CAAC,EAAE,SAAS,CAAA;IACnB,QAAQ,CAAC,EAAE,SAAS,CAAA;IACpB,MAAM,EAAE,MAAM,CAAA;CACf,GACD;IACE,KAAK,CAAC,EAAE,SAAS,CAAA;IACjB,MAAM,CAAC,EAAE,SAAS,CAAA;IAClB,OAAO,CAAC,EAAE,SAAS,CAAA;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,WAAW,CAAA;CACpB,GACD;IACE,KAAK,CAAC,EAAE,SAAS,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,SAAS,CAAA;I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eACN,SAAS;YACZ,OAAO;;mBAiBJ,IAAI,YAAW,qBAAqB;;;;;;wBAqCxC,QAAQ,CAAC,IAAI,CAAC,YACZ,yBAAyB,KACjC,OAAO,CAAC,wBAAwB,EAAE,CAAC;WArD7B,KAAK;aACH,SAAS;cACR,SAAS;eACR,SAAS;YACZ,OAAO;EAuHpB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
2
|
+
export function useByteshipUpload(client, defaultOptions = {}) {
|
|
3
|
+
const [state, setState] = useState({ status: "idle" });
|
|
4
|
+
const upload = useCallback(async (file, options = {}) => {
|
|
5
|
+
setState({ progress: 0, status: "uploading" });
|
|
6
|
+
try {
|
|
7
|
+
const uploadOptions = withProgressHandler(defaultOptions, options, (progress) => {
|
|
8
|
+
setState({
|
|
9
|
+
progress: progress.percent,
|
|
10
|
+
status: "uploading",
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
const uploaded = await client.upload(file, {
|
|
14
|
+
...uploadOptions,
|
|
15
|
+
});
|
|
16
|
+
setState({
|
|
17
|
+
fileId: uploaded.id,
|
|
18
|
+
status: "ready",
|
|
19
|
+
});
|
|
20
|
+
return uploaded;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
const normalized = error instanceof Error ? error : new Error("Upload failed");
|
|
24
|
+
setState({
|
|
25
|
+
error: normalized,
|
|
26
|
+
status: "error",
|
|
27
|
+
});
|
|
28
|
+
throw normalized;
|
|
29
|
+
}
|
|
30
|
+
}, [client, defaultOptions]);
|
|
31
|
+
const uploadMany = useCallback(async (files, options = {}) => {
|
|
32
|
+
setState({ progress: 0, status: "uploading" });
|
|
33
|
+
try {
|
|
34
|
+
const fileList = Array.from(files);
|
|
35
|
+
const loadedByIndex = new Map();
|
|
36
|
+
const totalBytes = fileList.reduce((total, file) => total + file.size, 0);
|
|
37
|
+
const results = await client.uploadMany(fileList, {
|
|
38
|
+
...defaultOptions,
|
|
39
|
+
...options,
|
|
40
|
+
onFileProgress: (progress) => {
|
|
41
|
+
defaultOptions.onProgress?.(progress);
|
|
42
|
+
options.onFileProgress?.(progress);
|
|
43
|
+
loadedByIndex.set(progress.index, progress.loaded);
|
|
44
|
+
const loaded = Array.from(loadedByIndex.values()).reduce((total, value) => total + value, 0);
|
|
45
|
+
setState({
|
|
46
|
+
progress: totalBytes > 0 ? Math.min(100, (loaded / totalBytes) * 100) : 0,
|
|
47
|
+
status: "uploading",
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
const failures = results.filter((result) => result.status === "rejected");
|
|
52
|
+
if (failures.length > 0) {
|
|
53
|
+
const error = new Error(`${failures.length} upload${failures.length === 1 ? "" : "s"} failed`);
|
|
54
|
+
setState({
|
|
55
|
+
error,
|
|
56
|
+
status: "error",
|
|
57
|
+
});
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
setState({
|
|
61
|
+
fileIds: results.flatMap((result) => result.result ? [result.result.id] : []),
|
|
62
|
+
status: "ready",
|
|
63
|
+
});
|
|
64
|
+
return results;
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
const normalized = error instanceof Error ? error : new Error("Uploads failed");
|
|
68
|
+
setState({
|
|
69
|
+
error: normalized,
|
|
70
|
+
status: "error",
|
|
71
|
+
});
|
|
72
|
+
throw normalized;
|
|
73
|
+
}
|
|
74
|
+
}, [client, defaultOptions]);
|
|
75
|
+
return {
|
|
76
|
+
...state,
|
|
77
|
+
upload,
|
|
78
|
+
uploadMany,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function withProgressHandler(defaultOptions, options, onProgress) {
|
|
82
|
+
return {
|
|
83
|
+
...defaultOptions,
|
|
84
|
+
...options,
|
|
85
|
+
onProgress: (progress) => {
|
|
86
|
+
defaultOptions.onProgress?.(progress);
|
|
87
|
+
options.onProgress?.(progress);
|
|
88
|
+
onProgress(progress);
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@byteship/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React upload hooks for Byteship.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "bun run --cwd ../sdk-js build && tsc -p tsconfig.build.json",
|
|
15
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@byteship/js": "^0.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/react": "latest",
|
|
32
|
+
"react": "latest"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": ">=18"
|
|
36
|
+
}
|
|
37
|
+
}
|