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 package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion specs/swagger-appwrite-0.13.0.json

Large diffs are not rendered by default.

60 changes: 59 additions & 1 deletion templates/web/src/sdk.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
}

class {{ spec.title | caseUcfirst }} {
static CHUNK_SIZE = 5*1024*1024; // 5MB

config = {
endpoint: '{{ spec.endpoint }}',
endpointRealtime: '',
Expand Down Expand Up @@ -451,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 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: 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 %} => {
{% for parameter in method.parameters.all %}
{% if parameter.required %}
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') {
Expand Down Expand Up @@ -498,6 +500,61 @@ class {{ spec.title | caseUcfirst }} {
}
{% elseif method.type == 'location' %}
return uri;
{% else %}
{% if 'multipart/form-data' in method.consumes %}
{% for parameter in method.parameters.all %}
{% if parameter.type == 'file' %}
const size = {{ parameter.name | caseCamel | escapeKeyword }}.size;

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 } = {
{% for parameter in method.parameters.header %}
'{{ parameter.name | caseCamel | escapeKeyword }}': this.${{ parameter.name | caseCamel | escapeKeyword }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}': '{{ header }}',
'content-range': 'bytes ' + start + '-' + end + '/' + size
{% endfor %}
}

if (id) {
headers['x-appwrite-id'] = id;
}

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

response = await this.call('{{ method.method | caseLower }}', uri, headers, payload);

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

if (onProgress) {
onProgress(Math.min((counter + 1) * {{ spec.title | caseUcfirst }}.CHUNK_SIZE, size) / size * 100);
}
}

return response;
}
{% endif %}
{% endfor %}
{% else %}
return await this.call('{{ method.method | caseLower }}', uri, {
{% for parameter in method.parameters.header %}
Expand All @@ -507,6 +564,7 @@ class {{ spec.title | caseUcfirst }} {
'{{ key }}': '{{ header }}',
{% endfor %}
}, payload);
{% endif %}
{% endif %}
}{% if not loop.last %},
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions tests/SDKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class SDKTest extends TestCase
...FOO_RESPONSES,
...BAR_RESPONSES,
...GENERAL_RESPONSES,
'POST:/v1/mock/tests/general/upload:passed', // for large file upload
...EXCEPTION_RESPONSES,
...REALTIME_RESPONSES,
],
Expand Down
11 changes: 11 additions & 0 deletions tests/languages/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
</head>

<body>
<p>File:</p>
<input type="file" id="file" />
<p>Large file: (over 5MB)</p>
<input type="file" id="file2" />
<button id="start">Start</button>
<script type="module">
document.getElementById("start").addEventListener("click", async () => {
Expand Down Expand Up @@ -70,6 +73,14 @@
);
console.log(response.result);

response = await sdk.general.upload(
"string",
123,
["string in array"],
document.getElementById("file2").files[0]
);
console.log(response.result);

try {
response = await sdk.general.empty();
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions tests/languages/web/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ async function start() {
// General
response = await sdk.general.redirect();
console.log(response.result);

response = await sdk.general.upload('string', 123, ['string in array'], fs.createReadStream(__dirname + '/../../resources/file.png'));
console.log(response.result);
console.log('POST:/v1/mock/tests/general/upload:passed'); // Skip file upload test on Node.js
console.log('POST:/v1/mock/tests/general/upload:passed'); // Skip big file upload test on Node.js

try {
response = await sdk.general.empty();
Expand Down
1 change: 1 addition & 0 deletions tests/languages/web/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ server.listen(3000, async () => {
});
await page.goto('http://localhost:3000');
await page.setInputFiles('#file', path.join(__dirname, '/../../resources/file.png'));
await page.setInputFiles('#file2', path.join(__dirname, '/../../resources/large_file.mp4'));
await page.click('#start');

setTimeout(async () => {
Expand Down