Skip to content

Commit 45c7be1

Browse files
authored
Merge pull request #1148 from appwrite/filter-readonly
feat: filter readonly attributes
2 parents 51dac6e + 7e1eaa3 commit 45c7be1

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

src/SDK/Language/ReactNative.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function getFiles(): array
128128
* @param array $nestedTypes
129129
* @return string
130130
*/
131-
public function getTypeName(array $parameter, array $spec = []): string
131+
public function getTypeName(array $parameter, array $method = []): string
132132
{
133133
if (isset($parameter['enumName'])) {
134134
return \ucfirst($parameter['enumName']);
@@ -151,6 +151,22 @@ public function getTypeName(array $parameter, array $spec = []): string
151151
return 'string[]';
152152
case self::TYPE_FILE:
153153
return '{name: string, type: string, size: number, uri: string}';
154+
case self::TYPE_OBJECT:
155+
if (empty($method)) {
156+
return $parameter['type'];
157+
}
158+
switch ($method['responseModel']) {
159+
case 'user':
160+
return "Partial<Preferences>";
161+
case 'document':
162+
if ($method['method'] === 'post') {
163+
return "Document extends Models.DefaultDocument ? Partial<Models.Document> & Record<string, any> : Partial<Models.Document> & Omit<Document, keyof Models.Document>";
164+
}
165+
if ($method['method'] === 'patch' || $method['method'] === 'put') {
166+
return "Document extends Models.DefaultDocument ? Partial<Models.Document> & Record<string, any> : Partial<Models.Document> & Partial<Omit<Document, keyof Models.Document>>";
167+
}
168+
}
169+
break;
154170
}
155171

156172
return $parameter['type'];

src/SDK/Language/Web.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ public function getParamExample(array $param): string
177177
return $output;
178178
}
179179

180+
public function getReadOnlyProperties(array $parameter, string $responseModel, array $spec = []): array
181+
{
182+
$properties = [];
183+
184+
if (
185+
!isset($spec['definitions'][$responseModel]['properties']) ||
186+
!is_array($spec['definitions'][$responseModel]['properties'])
187+
) {
188+
return $properties;
189+
}
190+
191+
foreach ($spec['definitions'][$responseModel]['properties'] as $property) {
192+
if (isset($property['readOnly']) && $property['readOnly']) {
193+
$properties[] = $property['name'];
194+
}
195+
}
196+
197+
return $properties;
198+
}
199+
180200
public function getTypeName(array $parameter, array $method = []): string
181201
{
182202
if (isset($parameter['enumName'])) {
@@ -224,7 +244,7 @@ public function getTypeName(array $parameter, array $method = []): string
224244
if ($method['method'] === 'post') {
225245
return "Document extends Models.DefaultDocument ? Partial<Models.Document> & Record<string, any> : Partial<Models.Document> & Omit<Document, keyof Models.Document>";
226246
}
227-
if ($method['method'] === 'patch') {
247+
if ($method['method'] === 'patch' || $method['method'] === 'put') {
228248
return "Document extends Models.DefaultDocument ? Partial<Models.Document> & Record<string, any> : Partial<Models.Document> & Partial<Omit<Document, keyof Models.Document>>";
229249
}
230250
}
@@ -336,6 +356,9 @@ public function getFilters(): array
336356
new TwigFilter('getPropertyType', function ($value, $method = []) {
337357
return $this->getTypeName($value, $method);
338358
}),
359+
new TwigFilter('getReadOnlyProperties', function ($value, $responseModel, $spec = []) {
360+
return $this->getReadOnlyProperties($value, $responseModel, $spec);
361+
}),
339362
new TwigFilter('getSubSchema', function (array $property, array $spec) {
340363
return $this->getSubSchema($property, $spec);
341364
}),

templates/node/src/services/template.ts.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ export class {{ service.name | caseUcfirst }} {
108108
}
109109
{%~ endif %}
110110
{%~ endfor %}
111+
{%~ for parameter in method.parameters.all %}
112+
{%~ if parameter.type == 'object' %}
113+
{%~ for attribute in parameter | getReadOnlyProperties(method.responseModel, spec) %}
114+
delete {{ parameter.name | caseCamel | escapeKeyword }}?.{{ attribute }};
115+
{%~ endfor %}
116+
{%~ endif %}
117+
{%~ endfor %}
118+
111119
const apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};
112120
const payload: Payload = {};
113121
{%~ for parameter in method.parameters.query %}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ export class {{ service.name | caseUcfirst }} extends Service {
112112
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"');
113113
}
114114

115+
{% endif %}
116+
{% endfor %}
117+
{% for parameter in method.parameters.all %}
118+
{% if parameter.type == 'object' %}
119+
{% for attribute in parameter | getReadOnlyProperties(method.responseModel, spec) %}
120+
delete {{ parameter.name | caseCamel | escapeKeyword }}?.{{ attribute }};
121+
{% endfor %}
115122
{% endif %}
116123
{% endfor %}
117124
const apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};

templates/web/src/services/template.ts.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ export class {{ service.name | caseUcfirst }} {
109109
}
110110
{%~ endif %}
111111
{%~ endfor %}
112+
{%~ for parameter in method.parameters.all %}
113+
{%~ if parameter.type == 'object' %}
114+
{%~ for attribute in parameter | getReadOnlyProperties(method.responseModel, spec) %}
115+
delete {{ parameter.name | caseCamel | escapeKeyword }}?.{{ attribute }};
116+
{%~ endfor %}
117+
{%~ endif %}
118+
{%~ endfor %}
119+
112120
const apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};
113121
const payload: Payload = {};
114122
{%~ for parameter in method.parameters.query %}

0 commit comments

Comments
 (0)