Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion specs/swagger-appwrite-0.13.0.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Spec/Swagger2.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public function getMethods($service)
'required' => $parameter['required'] ?? false,
'default' => $parameter['default'] ?? null,
'example' => $parameter['x-example'] ?? null,
'isUploadID' => $parameter['x-upload-id'] ?? false,
'array' => [
'type' => $parameter['items']['type'] ?? '',
],
Expand Down Expand Up @@ -233,6 +234,7 @@ public function getMethods($service)
$param['required'] = (in_array($key, $bodyRequired));
$param['default'] = $value['default'] ?? null;
$param['example'] = $value['x-example'] ?? null;
$param['isUploadID'] = $value['x-upload-id'] ?? false;
$param['array'] = [
'type' => $value['items']['type'] ?? '',
];
Expand Down
73 changes: 47 additions & 26 deletions templates/web/src/sdk.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class {{ spec.title | caseUcfirst }} {
* @returns {% if method.type == 'webAuth' %}{void|string}{% elseif method.type == 'location' %}{URL}{% else %}{Promise}{% endif %}

*/
{{ method.name | caseCamel }}: {% if method.type != 'location' and method.type != 'webAuth'%}async {% if spec.definitions[method.responseModel].additionalProperties %}<{{method.responseModel | caseUcfirst}} extends Models.{{method.responseModel | caseUcfirst}}>{% endif %}{% for property in spec.definitions[method.responseModel].properties %}{% if spec.definitions[property.sub_schema].additionalProperties %}<{{property.sub_schema | caseUcfirst}} extends Models.{{property.sub_schema | caseUcfirst}}>{% endif %}{% endfor %}{% endif %}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %}?{% endif %}: {{ parameter.type | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, onProgress = (progress: number) => {}{% endif %}): {% if method.type == 'webAuth' %}void | URL{% elseif method.type == 'location' %}URL{% else %}Promise<{% if method.responseModel and method.responseModel != 'any' %}{% if not spec.definitions[method.responseModel].additionalProperties %}Models.{% endif %}{{method.responseModel | caseUcfirst}}{% for property in spec.definitions[method.responseModel].properties %}{% if spec.definitions[property.sub_schema].additionalProperties %}<{{property.sub_schema | caseUcfirst}}>{% endif %}{% endfor %}{% else %}{}{% endif %}>{% endif %} => {
{{ method.name | caseCamel }}: {% if method.type != 'location' and method.type != 'webAuth'%}async {% if spec.definitions[method.responseModel].additionalProperties %}<{{method.responseModel | caseUcfirst}} extends Models.{{method.responseModel | caseUcfirst}}>{% endif %}{% for property in spec.definitions[method.responseModel].properties %}{% if spec.definitions[property.sub_schema].additionalProperties %}<{{property.sub_schema | caseUcfirst}} extends Models.{{property.sub_schema | caseUcfirst}}>{% endif %}{% endfor %}{% endif %}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %}?{% endif %}: {{ parameter.type | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, onProgress = (progress: any) => {}{% endif %}): {% if method.type == 'webAuth' %}void | URL{% elseif method.type == 'location' %}URL{% else %}Promise<{% if method.responseModel and method.responseModel != 'any' %}{% if not spec.definitions[method.responseModel].additionalProperties %}Models.{% endif %}{{method.responseModel | caseUcfirst}}{% for property in spec.definitions[method.responseModel].properties %}{% if spec.definitions[property.sub_schema].additionalProperties %}<{{property.sub_schema | caseUcfirst}}>{% endif %}{% endfor %}{% else %}{}{% endif %}>{% endif %} => {
{% for parameter in method.parameters.all %}
{% if parameter.required %}
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') {
Expand Down Expand Up @@ -508,51 +508,72 @@ class {{ spec.title | caseUcfirst }} {

if (size <= {{ spec.title | caseUcfirst }}.CHUNK_SIZE) {
return await this.call('{{ method.method | caseLower }}', uri, {

{% for parameter in method.parameters.header %}
'{{ parameter.name | caseCamel | escapeKeyword }}': this.${{ parameter.name | caseCamel | escapeKeyword }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}': '{{ header }}',
{% endfor %}
}, payload);
} else {
let id = undefined;
let response = undefined;

const totalCounters = Math.ceil(size / {{ spec.title | caseUcfirst }}.CHUNK_SIZE);
for (let counter = 0; counter < totalCounters; counter++) {
const start = (counter * {{ spec.title | caseUcfirst }}.CHUNK_SIZE);
const end = Math.min((((counter * {{ spec.title | caseUcfirst }}.CHUNK_SIZE) + {{ spec.title | caseUcfirst }}.CHUNK_SIZE) - 1), size);
const headers: { [header: string]: string } = {
}
let id = undefined;
let response = undefined;

const headers: { [header: string]: string } = {
{% for parameter in method.parameters.header %}
'{{ parameter.name | caseCamel | escapeKeyword }}': this.${{ parameter.name | caseCamel | escapeKeyword }},
'{{ parameter.name | caseCamel | escapeKeyword }}': this.${{ parameter.name | caseCamel | escapeKeyword }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}': '{{ header }}',
'content-range': 'bytes ' + start + '-' + end + '/' + size
'{{ key }}': '{{ header }}',
{% endfor %}
}

let counter = 0;
const totalCounters = Math.ceil(size / {{ spec.title | caseUcfirst }}.CHUNK_SIZE);
{% for parameter in method.parameters.all %}
{% if parameter.isUploadID %}
if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
try {
response = await this.call('GET', new URL(this.config.endpoint + path + '/' + {{ parameter.name }}), headers);
counter = response.chunksUploaded;
} catch(e) {
}
}
{% endif %}
{% endfor %}

if (id) {
headers['x-{{spec.title | caseLower }}-id'] = id;
}
for (counter; counter < totalCounters; counter++) {
const start = (counter * {{ spec.title | caseUcfirst }}.CHUNK_SIZE);
const end = Math.min((((counter * {{ spec.title | caseUcfirst }}.CHUNK_SIZE) + {{ spec.title | caseUcfirst }}.CHUNK_SIZE) - 1), size);

const stream = {{ parameter.name | caseCamel | escapeKeyword }}.slice(start, end + 1);
payload['{{ parameter.name }}'] = new File([stream], {{ parameter.name | caseCamel | escapeKeyword }}.name);
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size

response = await this.call('{{ method.method | caseLower }}', uri, headers, payload);
if (id) {
headers['x-{{spec.title | caseLower }}-id'] = id;
}

if (!id) {
id = response['$id'];
}
const stream = {{ parameter.name | caseCamel | escapeKeyword }}.slice(start, end + 1);
payload['{{ parameter.name }}'] = new File([stream], {{ parameter.name | caseCamel | escapeKeyword }}.name);

if (onProgress) {
onProgress(Math.min((counter + 1) * {{ spec.title | caseUcfirst }}.CHUNK_SIZE, size) / size * 100);
}
response = await this.call('{{ method.method | caseLower }}', uri, headers, payload);

if (!id) {
id = response['$id'];
}

return response;
if (onProgress) {
onProgress({
$id: response.$id,
progress: Math.min((counter + 1) * Appwrite.CHUNK_SIZE, size) / size * 100,
sizeUpploaded: end+1,
chunksTotal: response.chunksTotal,
chunksUploaded: response.chunksUploaded
});
}
}

return response;
{% endif %}
{% endfor %}
{% else %}
Expand Down