Skip to content

Commit e808b53

Browse files
Merge pull request #261 from appwrite/feat-web-response-models
feat(web): response models
2 parents 335284c + 5fcd4fb commit e808b53

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

templates/web/src/sdk.ts.twig

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}{{property.sub_schema | caseUcfirst}}[]{% else %}{{property.sub_schema | caseUcfirst}}{% endif %}{% else %}{{property.type | typeName}}{% endif %}{% endmacro %}
12
import 'isomorphic-form-data';
23
import { fetch } from 'cross-fetch';
34

5+
namespace Models {
6+
{% for definition in spec.definitions %}
7+
/**
8+
* {{ definition.description }}
9+
*/
10+
export type {{ definition.name | caseUcfirst }}<{% for property in definition.properties %}{% if spec.definitions[property.sub_schema].additionalProperties %}{{property.sub_schema | caseUcfirst}} extends Models.{{property.sub_schema | caseUcfirst}}{% endif %}{% endfor %}> = {
11+
{% for property in definition.properties %}
12+
/**
13+
* {{ property.description }}
14+
*/
15+
{{ property.name | escapeKeyword }}{% if not property.required %}?{% endif %}: {{_self.sub_schema(property)}};
16+
{% endfor %}
17+
}
18+
{% endfor %}
19+
}
20+
421
type Payload = {
522
[key: string]: any;
623
}
@@ -434,7 +451,7 @@ class {{ spec.title | caseUcfirst }} {
434451
* @returns {% if method.type == 'webAuth' %}{void|string}{% elseif method.type == 'location' %}{URL}{% else %}{Promise}{% endif %}
435452

436453
*/
437-
{{ method.name | caseCamel }}: {% if method.type != 'location' and method.type != 'webAuth'%}async <T extends unknown>{% 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<T>{% endif %} => {
454+
{{ 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 %} => {
438455
{% for parameter in method.parameters.all %}
439456
{% if parameter.required %}
440457
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') {
@@ -501,4 +518,4 @@ class {{ spec.title | caseUcfirst }} {
501518
};
502519

503520
export { {{spec.title | caseUcfirst}} }
504-
export type { {{spec.title | caseUcfirst}}Exception }
521+
export type { {{spec.title | caseUcfirst}}Exception, Models }

tests/SDKTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class SDKTest extends TestCase
233233
...REALTIME_RESPONSES,
234234
],
235235
],
236+
236237
'deno' => [
237238
'class' => 'Appwrite\SDK\Language\Deno',
238239
'build' => [

0 commit comments

Comments
 (0)