Skip to content

Commit 93ffb24

Browse files
Merge pull request #1076 from appwrite/PLA-2728
feat (breaking): methods to generate file URLs
2 parents d1599fc + 9973cd6 commit 93ffb24

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/SDK/Language/ReactNative.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function getReturn(array $method, array $spec): string
215215
if ($method['type'] === 'webAuth') {
216216
return 'void | URL';
217217
} elseif ($method['type'] === 'location') {
218-
return 'URL';
218+
return 'Promise<ArrayBuffer>';
219219
}
220220

221221
if (array_key_exists('responseModel', $method) && !empty($method['responseModel']) && $method['responseModel'] !== 'any') {

templates/react-native/src/client.ts.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class Client {
378378
}
379379
}
380380

381-
async call(method: string, url: URL, headers: Headers = {}, params: Payload = {}): Promise<any> {
381+
async call(method: string, url: URL, headers: Headers = {}, params: Payload = {}, responseType = 'json'): Promise<any> {
382382
method = method.toUpperCase();
383383

384384
headers = Object.assign({}, this.headers, headers);
@@ -434,6 +434,8 @@ class Client {
434434

435435
if (response.headers.get('content-type')?.includes('application/json')) {
436436
data = await response.json();
437+
} else if (responseType === 'arrayBuffer') {
438+
data = await response.arrayBuffer();
437439
} else {
438440
data = {
439441
message: await response.text()

templates/react-native/src/services/template.ts.twig

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
3939
* @param {{ '{' }}{{ parameter | getPropertyType(method) | raw }}{{ '}' }} {{ parameter.name | caseCamel | escapeKeyword }}
4040
{% endfor %}
4141
* @throws {{ '{' }}{{ spec.title | caseUcfirst}}Exception}
42-
* @returns {% if method.type == 'webAuth' %}{void|string}{% elseif method.type == 'location' %}{URL}{% else %}{Promise}{% endif %}
42+
* @returns {% if method.type == 'webAuth' %}{void|string}{% elseif method.type == 'location' %}{ArrayBuffer}{% else %}{Promise}{% endif %}
4343

4444
*/
4545
{% if method.type == 'upload'%}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 }} {
@@ -83,8 +83,6 @@ export class {{ service.name | caseUcfirst }} extends Service {
8383
{% endif %}
8484
{% if method.type == 'webAuth' %}
8585
return uri;
86-
{% elseif method.type == 'location' %}
87-
return uri;
8886
{% else %}
8987
{% if 'multipart/form-data' in method.consumes %}
9088
{% for parameter in method.parameters.all %}
@@ -170,9 +168,46 @@ export class {{ service.name | caseUcfirst }} extends Service {
170168
{% for key, header in method.headers %}
171169
'{{ key }}': '{{ header }}',
172170
{% endfor %}
173-
}, payload);
171+
}, payload{% if method.type == 'location' %}, 'arrayBuffer'{% endif %});
172+
{% endif %}
174173
{% endif %}
174+
}
175+
{% endfor %}
176+
{# Extra methods for just getting the URL of 'location' type methods #}
177+
{% for method in service.methods %}
178+
{% if method.type == 'location' %}
179+
180+
/**
181+
{% if method.description %}
182+
{{ method.description|comment2 }}
175183
{% endif %}
184+
*
185+
{% for parameter in method.parameters.all %}
186+
* @param {{ '{' }}{{ parameter | getPropertyType(method) | raw }}{{ '}' }} {{ parameter.name | caseCamel | escapeKeyword }}
187+
{% endfor %}
188+
* @throws {{ '{' }}{{ spec.title | caseUcfirst}}Exception}
189+
* @returns {{ '{' }}URL{{ '}' }}
190+
*/
191+
{{ method.name | caseCamel }}URL({% 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 %}): URL {
192+
const apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};
193+
const payload: Payload = {};
194+
195+
{% for parameter in method.parameters.query %}
196+
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') {
197+
payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }};
198+
}
199+
200+
{% endfor %}
201+
{% for parameter in method.parameters.body %}
202+
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} !== 'undefined') {
203+
payload['{{ parameter.name }}'] = {{ parameter.name | caseCamel | escapeKeyword }};
204+
}
205+
206+
{% endfor %}
207+
const uri = new URL(this.client.config.endpoint + apiPath);
208+
209+
return uri;
176210
}
211+
{% endif %}
177212
{% endfor %}
178213
};

0 commit comments

Comments
 (0)