@@ -5,6 +5,14 @@ type Payload = {
5
5
[key: string]: any;
6
6
}
7
7
8
+ type UploadProgress = {
9
+ $id: string;
10
+ progress: number;
11
+ sizeUploaded: number;
12
+ chunksTotal: number;
13
+ chunksUploaded: number;
14
+ }
15
+
8
16
type Headers = {
9
17
[key: string]: string;
10
18
}
@@ -164,7 +172,7 @@ class Client {
164
172
return { uri: url.toString(), options };
165
173
}
166
174
167
- async chunkedUpload(method: string, url: URL, headers: Headers = {}, originalPayload: Payload = {}) {
175
+ async chunkedUpload(method: string, url: URL, headers: Headers = {}, originalPayload: Payload = {}, onProgress: (progress: UploadProgress) => {} ) {
168
176
const file = Object.values(originalPayload).find((value) => value instanceof File);
169
177
170
178
if (file.size < = Client.CHUNK_SIZE) {
@@ -187,6 +195,16 @@ class Client {
187
195
188
196
response = await this.call(method, url, headers, payload);
189
197
198
+ if (onProgress && typeof onProgress === 'function') {
199
+ onProgress({
200
+ $id: response.$id,
201
+ progress: Math.round((end / file.size) * 100),
202
+ sizeUploaded: end,
203
+ chunksTotal: Math.ceil(file.size / Client.CHUNK_SIZE),
204
+ chunksUploaded: Math.ceil(end / Client.CHUNK_SIZE)
205
+ });
206
+ }
207
+
190
208
if (response && response.$id) {
191
209
headers['x-{{spec .title | caseLower }}-id'] = response.$id;
192
210
}
@@ -254,5 +272,5 @@ class Client {
254
272
255
273
export { Client, {{spec .title | caseUcfirst }}Exception };
256
274
export { Query } from './query';
257
- export type { Models, Payload };
275
+ export type { Models, Payload, UploadProgress };
258
276
export type { QueryTypes, QueryTypesList } from './query';
0 commit comments