Skip to content

Commit

Permalink
feat(nodejs) upload with token and videoId
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lando committed Mar 13, 2023
1 parent 7844017 commit faed55e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion templates/nodejs/doc/api/api_doc.md.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ All URIs are relative to *{{basePath}}*
| Name | Type | Required | Description |
| ------------- | ------------- | ------------- | ------------- |{{/-last}}{{/allParams}}
{{#allParams}} | **{{paramName}}** | {{#isPrimitiveType}}**{{#isFile}}string \| Readable \| Buffer{{/isFile}}{{^isFile}}{{dataType}}{{/isFile}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isContainer}}**{{dataType}}**{{/isContainer}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}{{^isContainer}}[**{{dataType}}**](../model/{{baseType}}.md){{/isContainer}}{{/isFile}}{{/isPrimitiveType}}| {{#required}}**yes**{{/required}}{{^required}}no{{/required}}| {{description}} |
{{/allParams}}{{#vendorExtensions.x-client-chunk-upload}}| **progressListener** | **(event: UploadProgressEvent) => void \| undefined** | no | Optional upload progress listener |{{/vendorExtensions.x-client-chunk-upload}}
{{/allParams}}{{#vendorExtensions.x-client-chunk-upload}}| **progressListener** | **(event: UploadProgressEvent) => void \| undefined** | no | Optional upload progress listener |{{/vendorExtensions.x-client-chunk-upload}}{{#vendorExtensions.x-client-copy-from-response}}
| **{{paramName}}** | **string \| undefined** | no | Optional {{paramName}} |{{/vendorExtensions.x-client-copy-from-response}}

### Return type

Expand Down
36 changes: 32 additions & 4 deletions templates/nodejs/src/api/api.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,15 @@ export default class {{classname}} {
* @param {{paramName}} {{description}}
{{/allParams}}
*/
public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{#isFile}}{{#vendorExtensions.x-client-chunk-upload}}string{{/vendorExtensions.x-client-chunk-upload}}{{^vendorExtensions.x-client-chunk-upload}}string | Readable | Buffer{{/vendorExtensions.x-client-chunk-upload}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}}{{#vendorExtensions.x-optional-object}} = {}{{/vendorExtensions.x-optional-object}}{{^-last}}, {{/-last}}{{/allParams}}{{#vendorExtensions.x-client-chunk-upload}}, progressListener?: (event: UploadProgressEvent) => void{{/vendorExtensions.x-client-chunk-upload}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}} {{^returnType}}void{{/returnType}}> {
public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{#isFile}}{{#vendorExtensions.x-client-chunk-upload}}string{{/vendorExtensions.x-client-chunk-upload}}{{^vendorExtensions.x-client-chunk-upload}}string | Readable | Buffer{{/vendorExtensions.x-client-chunk-upload}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}}{{#vendorExtensions.x-optional-object}} = {}{{/vendorExtensions.x-optional-object}}{{^-last}}, {{/-last}}{{/allParams}}{{#vendorExtensions.x-client-chunk-upload}}, progressListener?: (event: UploadProgressEvent) => void, {{/vendorExtensions.x-client-chunk-upload}}{{#vendorExtensions.x-client-copy-from-response}}{{paramName}}?: string, {{/vendorExtensions.x-client-copy-from-response}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}} {{^returnType}}void{{/returnType}}> {
{{/vendorExtensions.x-group-parameters}}
const queryParams: QueryOptions = {};
queryParams.headers = {};
{{#vendorExtensions.x-client-chunk-upload}}
const toRelay: { [key: string]: string | undefined } = {
{{#vendorExtensions.x-client-copy-from-response}}{{paramName}}, {{/vendorExtensions.x-client-copy-from-response}}
};
{{/vendorExtensions.x-client-chunk-upload}}
{{#allParams}}
{{#required}}
{{^isFile}}
Expand Down Expand Up @@ -301,8 +306,20 @@ export default class {{classname}} {

{{#hasFormParams}}
const formData = new FormData();
{{/hasFormParams}}

{{/hasFormParams}}{{#vendorExtensions.x-client-copy-from-response}}
Object.keys(toRelay).forEach((key) => {
if (toRelay[key] !== undefined) {
formData.append(key, toRelay[key]);
}
});
{{/vendorExtensions.x-client-copy-from-response}}
{{#formParams}}
{{#vendorExtensions.x-client-copy-from-response}}
if ({{paramName}}) {
formData.append('{{paramName}}', {{paramName}});
}
{{/vendorExtensions.x-client-copy-from-response}}
{{#isArray}}
if ({{paramName}}) {
{{#isCollectionFormatMulti}}
Expand All @@ -316,7 +333,6 @@ export default class {{classname}} {
}
{{/isArray}}
{{^isArray}}

{{^isFile}}
if (typeof {{paramName}} !== undefined) {
formData.append('{{baseName}}', {{paramName}});
Expand Down Expand Up @@ -362,7 +378,7 @@ export default class {{classname}} {
) as {{{returnType}}});
}
let uploadChunkSize = chunkSize;
let lastBody;
let lastBody: {{{returnType}}} | undefined = undefined;
let stream;
let chunkNumber = 0;

Expand All @@ -383,6 +399,12 @@ export default class {{classname}} {
stream = createReadStream(file, { start: offset, end: uploadChunkSize + offset - 1});
chunkFormData.append(filename, stream, filename);

Object.keys(toRelay).forEach((key) => {
if (toRelay[key] !== undefined) {
chunkFormData.append(key, toRelay[key]);
}
});

queryParams.body = chunkFormData;
queryParams.headers['Content-Range'] = `part ${part}/${partsCount}`;
part++;
Expand All @@ -407,6 +429,12 @@ export default class {{classname}} {
"{{{returnType}}}", "{{returnFormat}}"
) as {{{returnType}}});

Object.keys(toRelay).forEach((key) => {
if ((lastBody as unknown as any)[key] !== undefined) {
toRelay[key] = (lastBody as unknown as any)[key];
}
});

stream.close();
}

Expand Down

0 comments on commit faed55e

Please sign in to comment.