diff --git a/templates/web/src/services/template.ts.twig b/templates/web/src/services/template.ts.twig index 34e47ecf4..cd7ddb88a 100644 --- a/templates/web/src/services/template.ts.twig +++ b/templates/web/src/services/template.ts.twig @@ -11,159 +11,159 @@ export class {{ service.name | caseUcfirst }} extends Service { } {% for method in service.methods %} - /** - * {{ method.title }} - * + /** + * {{ method.title }} + * {% if method.description %} -{{ method.description|comment3 }} +{{ method.description|comment2 }} {% endif %} - * + * {% for parameter in method.parameters.all %} - * @param {{ '{' }}{{ parameter | getPropertyType(method) | raw }}{{ '}' }} {{ parameter.name | caseCamel | escapeKeyword }} + * @param {{ '{' }}{{ parameter | getPropertyType(method) | raw }}{{ '}' }} {{ parameter.name | caseCamel | escapeKeyword }} {% endfor %} - * @throws {{ '{' }}{{ spec.title | caseUcfirst}}Exception} - * @returns {% if method.type == 'webAuth' %}{void|string}{% elseif method.type == 'location' %}{URL}{% else %}{Promise}{% endif %} + * @throws {{ '{' }}{{ spec.title | caseUcfirst}}Exception} + * @returns {% if method.type == 'webAuth' %}{void|string}{% elseif method.type == 'location' %}{URL}{% else %}{Promise}{% endif %} - */ - {% if method.type != 'location' and method.type != 'webAuth'%}async {% endif %}{{ 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 }} { + */ + {% if method.type != 'location' and method.type != 'webAuth'%}async {% endif %}{{ 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 }} { {% for parameter in method.parameters.all %} {% if parameter.required %} - if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') { - throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"'); - } + if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') { + throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"'); + } {% endif %} {% endfor %} - let path = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %}; - let payload: Payload = {}; + let path = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %}; + let payload: Payload = {}; {% for parameter in method.parameters.query %} - if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') { - payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }}; - } + if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') { + payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }}; + } {% endfor %} {% for parameter in method.parameters.body %} - if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') { - payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }}; - } + if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') { + payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }}; + } {% endfor %} - const uri = new URL(this.client.config.endpoint + path); + const uri = new URL(this.client.config.endpoint + path); {% if method.type == 'location' or method.type == 'webAuth' %} {% if method.auth|length > 0 %} {% for node in method.auth %} {% for key,header in node|keys %} - payload['{{header|caseLower}}'] = this.client.config.{{header|caseLower}}; + payload['{{header|caseLower}}'] = this.client.config.{{header|caseLower}}; {% endfor %} {% endfor %} {% endif %} - for (const [key, value] of Object.entries(Service.flatten(payload))) { - uri.searchParams.append(key, value); - } + for (const [key, value] of Object.entries(Service.flatten(payload))) { + uri.searchParams.append(key, value); + } {% endif %} {% if method.type == 'webAuth' %} - if (typeof window !== 'undefined' && window?.location) { - window.location.href = uri.toString(); - } else { - return uri; - } -{% elseif method.type == 'location' %} + if (typeof window !== 'undefined' && window?.location) { + window.location.href = uri.toString(); + } else { return uri; + } +{% elseif method.type == 'location' %} + return uri; {% else %} {% if 'multipart/form-data' in method.consumes %} {% for parameter in method.parameters.all %} {% if parameter.type == 'file' %} - if(!({{ parameter.name | caseCamel | escapeKeyword }} instanceof File)) { - throw new {{spec.title | caseUcfirst}}Exception('Parameter "{{ parameter.name | caseCamel | escapeKeyword }}" has to be a File.'); - } + if(!({{ parameter.name | caseCamel | escapeKeyword }} instanceof File)) { + throw new {{spec.title | caseUcfirst}}Exception('Parameter "{{ parameter.name | caseCamel | escapeKeyword }}" has to be a File.'); + } - const size = {{ parameter.name | caseCamel | escapeKeyword }}.size; + const size = {{ parameter.name | caseCamel | escapeKeyword }}.size; - if (size <= Service.CHUNK_SIZE) { - return await this.client.call('{{ method.method | caseLower }}', uri, { + if (size <= Service.CHUNK_SIZE) { + return await this.client.call('{{ method.method | caseLower }}', uri, { {% for parameter in method.parameters.header %} - '{{ parameter.name | caseCamel | escapeKeyword }}': this.client.${{ parameter.name | caseCamel | escapeKeyword }}, + '{{ parameter.name | caseCamel | escapeKeyword }}': this.client.${{ parameter.name | caseCamel | escapeKeyword }}, {% endfor %} {% for key, header in method.headers %} - '{{ key }}': '{{ header }}', + '{{ key }}': '{{ header }}', {% endfor %} - }, payload); - } - let id = undefined; - let response = undefined; + }, payload); + } + let id = undefined; + let response = undefined; - const headers: { [header: string]: string } = { + const headers: { [header: string]: string } = { {% for parameter in method.parameters.header %} - '{{ parameter.name | caseCamel | escapeKeyword }}': this.client.${{ parameter.name | caseCamel | escapeKeyword }}, + '{{ parameter.name | caseCamel | escapeKeyword }}': this.client.${{ parameter.name | caseCamel | escapeKeyword }}, {% endfor %} {% for key, header in method.headers %} - '{{ key }}': '{{ header }}', + '{{ key }}': '{{ header }}', {% endfor %} - } + } - let counter = 0; - const totalCounters = Math.ceil(size / Service.CHUNK_SIZE); + let counter = 0; + const totalCounters = Math.ceil(size / Service.CHUNK_SIZE); {% for parameter in method.parameters.all %} {% if parameter.isUploadID %} - if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') { - try { - response = await this.client.call('GET', new URL(this.client.config.endpoint + path + '/' + {{ parameter.name }}), headers); - counter = response.chunksUploaded; - } catch(e) { - } + if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') { + try { + response = await this.client.call('GET', new URL(this.client.config.endpoint + path + '/' + {{ parameter.name }}), headers); + counter = response.chunksUploaded; + } catch(e) { } + } {% endif %} {% endfor %} - for (counter; counter < totalCounters; counter++) { - const start = (counter * Service.CHUNK_SIZE); - const end = Math.min((((counter * Service.CHUNK_SIZE) + Service.CHUNK_SIZE) - 1), size); + for (counter; counter < totalCounters; counter++) { + const start = (counter * Service.CHUNK_SIZE); + const end = Math.min((((counter * Service.CHUNK_SIZE) + Service.CHUNK_SIZE) - 1), size); - headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size + headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size - if (id) { - headers['x-{{spec.title | caseLower }}-id'] = id; - } + if (id) { + headers['x-{{spec.title | caseLower }}-id'] = id; + } - const stream = {{ parameter.name | caseCamel | escapeKeyword }}.slice(start, end + 1); - payload['{{ parameter.name }}'] = new File([stream], {{ parameter.name | caseCamel | escapeKeyword }}.name); + const stream = {{ parameter.name | caseCamel | escapeKeyword }}.slice(start, end + 1); + payload['{{ parameter.name }}'] = new File([stream], {{ parameter.name | caseCamel | escapeKeyword }}.name); - response = await this.client.call('{{ method.method | caseLower }}', uri, headers, payload); + response = await this.client.call('{{ method.method | caseLower }}', uri, headers, payload); - if (!id) { - id = response['$id']; - } + if (!id) { + id = response['$id']; + } - if (onProgress) { - onProgress({ - $id: response.$id, - progress: Math.min((counter + 1) * Service.CHUNK_SIZE - 1, size) / size * 100, - sizeUploaded: end, - chunksTotal: response.chunksTotal, - chunksUploaded: response.chunksUploaded - }); - } + if (onProgress) { + onProgress({ + $id: response.$id, + progress: Math.min((counter + 1) * Service.CHUNK_SIZE - 1, size) / size * 100, + sizeUploaded: end, + chunksTotal: response.chunksTotal, + chunksUploaded: response.chunksUploaded + }); } + } - return response; + return response; {% endif %} {% endfor %} {% else %} - return await this.client.call('{{ method.method | caseLower }}', uri, { + return await this.client.call('{{ method.method | caseLower }}', uri, { {% for parameter in method.parameters.header %} - '{{ parameter.name | caseCamel | escapeKeyword }}': this.client.${{ parameter.name | caseCamel | escapeKeyword }}, + '{{ parameter.name | caseCamel | escapeKeyword }}': this.client.${{ parameter.name | caseCamel | escapeKeyword }}, {% endfor %} {% for key, header in method.headers %} - '{{ key }}': '{{ header }}', + '{{ key }}': '{{ header }}', {% endfor %} - }, payload); + }, payload); {% endif %} {% endif %} - } + } {% endfor %} };