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: 1 addition & 1 deletion src/SDK/Language/Deno.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getTypeName($type)
return 'string';
break;
case self::TYPE_FILE:
return 'File | Blob';
return 'string';
break;
case self::TYPE_BOOLEAN:
return 'boolean';
Expand Down
7 changes: 5 additions & 2 deletions templates/deno/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export interface Payload {
}

export class Client {
static CHUNK_SIZE = 5*1024*1024; // 5MB
static DENO_READ_CHUNK_SIZE = 16384; // 16kb; refference: https://github.com/denoland/deno/discussions/9906

endpoint: string = 'https://appwrite.io/v1';
headers: Payload = {
'content-type': '',
Expand Down Expand Up @@ -93,14 +96,14 @@ export class Client {
const contentType = response.headers.get('content-type');

if (contentType && contentType.includes('application/json')) {
if(response.status >= 400) {
if (response.status >= 400) {
let res = await response.json();
throw new {{ spec.title | caseUcfirst}}Exception(res.message, res.status, res);
}

return response.json();
} else {
if(response.status >= 400) {
if (response.status >= 400) {
let res = await response.text();
throw new {{ spec.title | caseUcfirst}}Exception(res, response.status, null);
}
Expand Down
80 changes: 78 additions & 2 deletions templates/deno/src/services/service.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
{% endfor %}
{% endspaceless %}
{% endmacro %}
import { basename } from "https://deno.land/std@0.122.0/path/mod.ts";
import { Service } from '../service.ts';
import { Payload } from '../client.ts';
import { Payload, Client } from '../client.ts';
import { AppwriteException } from '../exception.ts';
import type { Models } from '../models.d.ts'

Expand All @@ -47,7 +48,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
* @throws {AppwriteException}
* @returns {Promise}
*/
async {{ method.name | caseCamel }}{% if generics %}<{{generics}}>{% 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 %}): Promise<{% if method.type == 'webAuth' %}Response{% elseif method.type == 'location' %}Response{% else %}{% if method.responseModel and method.responseModel != 'any' %}{% if not spec.definitions[method.responseModel].additionalProperties %}Models.{% endif %}{{method.responseModel | caseUcfirst}}{% if generics_return %}<{{generics_return}}>{% endif %}{% else %}Response{% endif %}{% endif %}> {
async {{ method.name | caseCamel }}{% if generics %}<{{generics}}>{% 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 %}): Promise<{% if method.type == 'webAuth' %}Response{% elseif method.type == 'location' %}Response{% else %}{% if method.responseModel and method.responseModel != 'any' %}{% if not spec.definitions[method.responseModel].additionalProperties %}Models.{% endif %}{{method.responseModel | caseUcfirst}}{% if generics_return %}<{{generics_return}}>{% endif %}{% else %}Response{% endif %}{% endif %}> {
{% for parameter in method.parameters.all %}
{% if parameter.required %}
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') {
Expand All @@ -69,8 +70,82 @@ export class {{ service.name | caseUcfirst }} extends Service {
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') {
payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }};
}
{% endfor %}
{% if 'multipart/form-data' in method.consumes %}
{% for parameter in method.parameters.all %}
{% if parameter.type == 'file' %}
const {size: size} = await Deno.stat({{ parameter.name | caseCamel | escapeKeyword }});

if (size <= Client.CHUNK_SIZE) {
payload['{{ parameter.name }}'] = new File([await Deno.readFile({{ parameter.name | caseCamel | escapeKeyword }})], basename({{ parameter.name | caseCamel | escapeKeyword }}));

return await this.client.call('{{ method.method | caseLower }}', path, {
{% for parameter in method.parameters.header %}
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}': '{{ header }}',
{% endfor %}
}, payload);
} else {
const stream = await Deno.open({{ parameter.name | caseCamel | escapeKeyword }}, { read: true });

let id = undefined;
let response = undefined;

const totalCounters = Math.ceil(size / Client.CHUNK_SIZE);

for (let counter = 0; counter < totalCounters; counter++) {
const start = (counter * Client.CHUNK_SIZE);
const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size);
const headers: { [header: string]: string } = {
{% for parameter in method.parameters.header %}
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}': '{{ header }}',
{% endfor %}
'content-range': 'bytes ' + start + '-' + end + '/' + size
};

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

const totalBuffer = new Uint8Array(Client.CHUNK_SIZE);

for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) {
const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE);
const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start);
const numberOfBytesRead = await Deno.read(stream.rid, buf);

if (!numberOfBytesRead) {
break;
}

for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) {
totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex];
}
}

payload['{{ parameter.name }}'] = new File([totalBuffer], basename({{ parameter.name | caseCamel | escapeKeyword }}));

response = await this.client.call('{{ method.method | caseLower }}', path, headers, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});

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

if (onProgress !== null) {
onProgress(Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100);
}
}

return response;
}
{% endif %}
{% endfor %}
{% else %}
return await this.client.call('{{ method.method | caseLower }}', path, {
{% for parameter in method.parameters.header %}
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
Expand All @@ -79,6 +154,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
'{{ key }}': '{{ header }}',
{% endfor %}
}, payload);
{% endif %}
}
{% endfor %}
}
1 change: 1 addition & 0 deletions tests/SDKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class SDKTest extends TestCase
...FOO_RESPONSES,
...BAR_RESPONSES,
...GENERAL_RESPONSES,
'POST:/v1/mock/tests/general/upload:passed', // for large file upload
...EXCEPTION_RESPONSES,
],
],
Expand Down
6 changes: 4 additions & 2 deletions tests/languages/deno/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ async function start() {
// @ts-ignore
console.log(response.result)

let fileArray = new File([await Deno.readFile(`./tests/resources/file.png`)], "file.png")
response = await general.upload('string', 123, ['string in array'], fileArray)
response = await general.upload('string', 123, ['string in array'], './tests/resources/file.png')
// @ts-ignore
console.log(response.result)

response = await general.upload('string', 123, ['string in array'], './tests/resources/large_file.mp4')
// @ts-ignore
console.log(response.result)

try {
response = await general.error400();
Expand Down