Skip to content

Commit 0e79594

Browse files
committed
feat: support onProgress
1 parent 982fe0f commit 0e79594

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

templates/node/src/client.ts.twig

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ type Payload = {
55
[key: string]: any;
66
}
77

8+
type UploadProgress = {
9+
$id: string;
10+
progress: number;
11+
sizeUploaded: number;
12+
chunksTotal: number;
13+
chunksUploaded: number;
14+
}
15+
816
type Headers = {
917
[key: string]: string;
1018
}
@@ -164,7 +172,7 @@ class Client {
164172
return { uri: url.toString(), options };
165173
}
166174

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) => {}) {
168176
const file = Object.values(originalPayload).find((value) => value instanceof File);
169177

170178
if (file.size <= Client.CHUNK_SIZE) {
@@ -187,6 +195,16 @@ class Client {
187195

188196
response = await this.call(method, url, headers, payload);
189197

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+
190208
if (response && response.$id) {
191209
headers['x-{{spec.title | caseLower }}-id'] = response.$id;
192210
}
@@ -254,5 +272,5 @@ class Client {
254272

255273
export { Client, {{spec.title | caseUcfirst}}Exception };
256274
export { Query } from './query';
257-
export type { Models, Payload };
275+
export type { Models, Payload, UploadProgress };
258276
export type { QueryTypes, QueryTypesList } from './query';

templates/node/src/services/template.ts.twig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class {{ service.name | caseUcfirst }} {
3838
* @throws {{ '{' }}{{ spec.title | caseUcfirst}}Exception}
3939
* @returns {{ '{' }}{{ method | getReturn(spec) | raw }}{{ '}' }}
4040
*/
41-
async {{ method.name | caseCamel }}{{ method.responseModel | getGenerics(spec) | raw }}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required or parameter.nullable %}?{% endif %}: {{ parameter | getPropertyType(method) | raw }}{% if not loop.last %}, {% endif %}{% endfor %}): {{ method | getReturn(spec) | raw }} {
41+
async {{ method.name | caseCamel }}{{ method.responseModel | getGenerics(spec) | raw }}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required or parameter.nullable %}?{% endif %}: {{ parameter | getPropertyType(method) | raw }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, onProgress = (progress: UploadProgress) => {}{% endif %}): {{ method | getReturn(spec) | raw }} {
4242
{%~ for parameter in method.parameters.all %}
4343
{%~ if parameter.required %}
4444
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') {
@@ -81,7 +81,8 @@ export class {{ service.name | caseUcfirst }} {
8181
'{{ method.method | caseLower }}',
8282
uri,
8383
apiHeaders,
84-
payload
84+
payload,
85+
onProgress
8586
);
8687
{%~ else %}
8788
return await this.client.call(

0 commit comments

Comments
 (0)