@karpeleslab/klbfw 0.2.20 → 0.2.22
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 +42 -0
- package/index.d.ts +72 -3
- package/package.json +1 -1
- package/upload.js +3 -3
package/README.md
CHANGED
|
@@ -50,6 +50,48 @@ npm run test:integration
|
|
|
50
50
|
- **Standardized Naming**: Consistent use of camelCase with backward compatibility for legacy APIs
|
|
51
51
|
- **Enhanced Error Handling**: More robust error handling and reporting
|
|
52
52
|
|
|
53
|
+
## Migrating from upload.append() to uploadFile()
|
|
54
|
+
|
|
55
|
+
The new `uploadFile()` function provides a simpler Promise-based API for file uploads. Here are the key differences when migrating:
|
|
56
|
+
|
|
57
|
+
### Return Value
|
|
58
|
+
|
|
59
|
+
**Legacy `upload.append()`** resolves with an upload object containing the result in `.final`:
|
|
60
|
+
```javascript
|
|
61
|
+
upload.append('Misc/Debug:testUpload', file)
|
|
62
|
+
.then(result => {
|
|
63
|
+
console.log(result.final); // The completion response data
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**New `uploadFile()`** resolves with the full REST response:
|
|
68
|
+
```javascript
|
|
69
|
+
uploadFile('Misc/Debug:testUpload', buffer)
|
|
70
|
+
.then(response => {
|
|
71
|
+
console.log(response.data); // The completion response data
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Migration Example
|
|
76
|
+
|
|
77
|
+
Before:
|
|
78
|
+
```javascript
|
|
79
|
+
upload.append('Misc/Debug:testUpload', file, params, context)
|
|
80
|
+
.then(up => {
|
|
81
|
+
const data = up.final;
|
|
82
|
+
// use data
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
After:
|
|
87
|
+
```javascript
|
|
88
|
+
uploadFile('Misc/Debug:testUpload', file, 'POST', params, context)
|
|
89
|
+
.then(response => {
|
|
90
|
+
const data = response.data;
|
|
91
|
+
// use data
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
53
95
|
# API
|
|
54
96
|
|
|
55
97
|
## REST API Methods
|
package/index.d.ts
CHANGED
|
@@ -35,7 +35,38 @@ declare function restGet(name: string, params?: Record<string, any>): Promise<an
|
|
|
35
35
|
declare function restSSE(name: string, method: 'GET', params?: Record<string, any>, context?: Record<string, any>): EventSource;
|
|
36
36
|
|
|
37
37
|
// Upload module types
|
|
38
|
-
|
|
38
|
+
|
|
39
|
+
/** File input types supported by uploadFile */
|
|
40
|
+
type UploadFileInput =
|
|
41
|
+
| ArrayBuffer
|
|
42
|
+
| Uint8Array
|
|
43
|
+
| File
|
|
44
|
+
| string
|
|
45
|
+
| { name?: string; size?: number; type?: string; content: ArrayBuffer | Uint8Array | string; lastModified?: number }
|
|
46
|
+
| NodeJS.ReadableStream;
|
|
47
|
+
|
|
48
|
+
/** Options for uploadFile */
|
|
49
|
+
interface UploadFileOptions {
|
|
50
|
+
/** Progress callback (0-1) */
|
|
51
|
+
onProgress?: (progress: number) => void;
|
|
52
|
+
/** Error callback - resolve to retry, reject to fail */
|
|
53
|
+
onError?: (error: Error, context: { phase: string; blockNum?: number; attempt: number }) => Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Options for uploadManyFiles */
|
|
57
|
+
interface UploadManyFilesOptions extends UploadFileOptions {
|
|
58
|
+
/** Progress callback with file-level details */
|
|
59
|
+
onProgress?: (progress: { fileIndex: number; fileCount: number; fileProgress: number; totalProgress: number }) => void;
|
|
60
|
+
/** Called when each file completes */
|
|
61
|
+
onFileComplete?: (info: { fileIndex: number; fileCount: number; result: any }) => void;
|
|
62
|
+
/** Error callback - context includes fileIndex */
|
|
63
|
+
onError?: (error: Error, context: { fileIndex: number; phase: string; blockNum?: number; attempt: number }) => Promise<void>;
|
|
64
|
+
/** Maximum concurrent uploads (1-10, default 3) */
|
|
65
|
+
concurrency?: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** @deprecated Use uploadFile() instead */
|
|
69
|
+
interface UploadLegacyOptions {
|
|
39
70
|
progress?: (progress: number) => void;
|
|
40
71
|
endpoint?: string;
|
|
41
72
|
headers?: Record<string, string>;
|
|
@@ -44,7 +75,40 @@ interface UploadOptions {
|
|
|
44
75
|
params?: Record<string, any>;
|
|
45
76
|
}
|
|
46
77
|
|
|
47
|
-
|
|
78
|
+
/** @deprecated Use uploadFile() instead */
|
|
79
|
+
declare const upload: {
|
|
80
|
+
init(path: string, params?: Record<string, any>, notify?: (status: any) => void): Promise<any> | ((files: any) => Promise<any>);
|
|
81
|
+
append(path: string, file: File | object, params?: Record<string, any>, context?: Record<string, any>): Promise<any>;
|
|
82
|
+
run(): void;
|
|
83
|
+
getStatus(): { queue: any[]; running: any[]; failed: any[] };
|
|
84
|
+
resume(): void;
|
|
85
|
+
cancelItem(uploadId: number): void;
|
|
86
|
+
deleteItem(uploadId: number): void;
|
|
87
|
+
pauseItem(uploadId: number): void;
|
|
88
|
+
resumeItem(uploadId: number): void;
|
|
89
|
+
retryItem(uploadId: number): void;
|
|
90
|
+
onprogress?: (status: { queue: any[]; running: any[]; failed: any[] }) => void;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** Upload a single file. Resolves with the full REST response. */
|
|
94
|
+
declare function uploadFile(
|
|
95
|
+
api: string,
|
|
96
|
+
buffer: UploadFileInput,
|
|
97
|
+
method?: string,
|
|
98
|
+
params?: Record<string, any>,
|
|
99
|
+
context?: Record<string, any>,
|
|
100
|
+
options?: UploadFileOptions
|
|
101
|
+
): Promise<any>;
|
|
102
|
+
|
|
103
|
+
/** Upload multiple files with concurrency control */
|
|
104
|
+
declare function uploadManyFiles(
|
|
105
|
+
api: string,
|
|
106
|
+
files: UploadFileInput[],
|
|
107
|
+
method?: string,
|
|
108
|
+
params?: Record<string, any>,
|
|
109
|
+
context?: Record<string, any>,
|
|
110
|
+
options?: UploadManyFilesOptions
|
|
111
|
+
): Promise<any[]>;
|
|
48
112
|
|
|
49
113
|
// Utility types
|
|
50
114
|
declare function getI18N(key: string, args?: Record<string, any>): string;
|
|
@@ -78,6 +142,11 @@ export {
|
|
|
78
142
|
restGet,
|
|
79
143
|
restSSE,
|
|
80
144
|
upload,
|
|
145
|
+
uploadFile,
|
|
146
|
+
uploadManyFiles,
|
|
81
147
|
getI18N,
|
|
82
|
-
trimPrefix
|
|
148
|
+
trimPrefix,
|
|
149
|
+
UploadFileInput,
|
|
150
|
+
UploadFileOptions,
|
|
151
|
+
UploadManyFilesOptions
|
|
83
152
|
};
|
package/package.json
CHANGED
package/upload.js
CHANGED
|
@@ -37,7 +37,7 @@ const { env, utils, awsReq, readChunkFromStream, readFileSlice } = require('./up
|
|
|
37
37
|
* @param {Function} [options.onError] - Error callback(error, context). Can return a Promise
|
|
38
38
|
* that, if resolved, will cause the failed operation to be retried. Context contains
|
|
39
39
|
* { phase, blockNum, attempt } for block uploads or { phase, attempt } for other operations.
|
|
40
|
-
* @returns {Promise<Object>} - Resolves with the
|
|
40
|
+
* @returns {Promise<Object>} - Resolves with the full REST response
|
|
41
41
|
*
|
|
42
42
|
* @example
|
|
43
43
|
* // Upload a buffer with filename
|
|
@@ -290,7 +290,7 @@ async function doPutUpload(file, uploadInfo, context, options) {
|
|
|
290
290
|
attempt++;
|
|
291
291
|
try {
|
|
292
292
|
const completeResponse = await rest.rest(uploadInfo.Complete, 'POST', {}, context);
|
|
293
|
-
return completeResponse
|
|
293
|
+
return completeResponse;
|
|
294
294
|
} catch (error) {
|
|
295
295
|
if (onError) {
|
|
296
296
|
await onError(error, { phase: 'complete', attempt });
|
|
@@ -542,7 +542,7 @@ async function doAwsUpload(file, uploadInfo, context, options) {
|
|
|
542
542
|
{},
|
|
543
543
|
context
|
|
544
544
|
);
|
|
545
|
-
return finalResponse
|
|
545
|
+
return finalResponse;
|
|
546
546
|
} catch (error) {
|
|
547
547
|
if (onError) {
|
|
548
548
|
await onError(error, { phase: 'handleComplete', attempt: handleAttempt });
|