Skip to content

Commit 185a360

Browse files
committed
feat: filter readonly attributes
1 parent 0628cdb commit 185a360

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/SDK/Language/Web.php

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

180+
public function getReadOnlyProperties(array $parameter, string $responseModel, array $spec = []): array
181+
{
182+
// fwrite(STDOUT, json_encode($parameter) . "\n");
183+
// fwrite(STDOUT, json_encode($spec['definitions'][$responseModel]) . "\n");
184+
$properties = [];
185+
foreach ($spec['definitions'][$responseModel]['properties'] as $property) {
186+
if (isset($property['readOnly']) && $property['readOnly']) {
187+
$properties[] = $property['name'];
188+
}
189+
}
190+
191+
return $properties;
192+
}
193+
180194
public function getTypeName(array $parameter, array $method = []): string
181195
{
182196
if (isset($parameter['enumName'])) {
@@ -224,7 +238,7 @@ public function getTypeName(array $parameter, array $method = []): string
224238
if ($method['method'] === 'post') {
225239
return "Document extends Models.DefaultDocument ? Partial<Models.Document> & Record<string, any> : Partial<Models.Document> & Omit<Document, keyof Models.Document>";
226240
}
227-
if ($method['method'] === 'patch') {
241+
if ($method['method'] === 'patch' || $method['method'] === 'put') {
228242
return "Document extends Models.DefaultDocument ? Partial<Models.Document> & Record<string, any> : Partial<Models.Document> & Partial<Omit<Document, keyof Models.Document>>";
229243
}
230244
}
@@ -336,6 +350,9 @@ public function getFilters(): array
336350
new TwigFilter('getPropertyType', function ($value, $method = []) {
337351
return $this->getTypeName($value, $method);
338352
}),
353+
new TwigFilter('getReadOnlyProperties', function ($value, $responseModel, $spec = []) {
354+
return $this->getReadOnlyProperties($value, $responseModel, $spec);
355+
}),
339356
new TwigFilter('getSubSchema', function (array $property, array $spec) {
340357
return $this->getSubSchema($property, $spec);
341358
}),

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ export class {{ service.name | caseUcfirst }} {
100100
}
101101
{%~ endif %}
102102
{%~ endfor %}
103+
104+
{%~ for parameter in method.parameters.all %}
105+
{%~ if parameter.type == 'object' %}
106+
{%~ for attribute in parameter | getReadOnlyProperties(method.responseModel, spec) %}
107+
delete {{ parameter.name | caseCamel | escapeKeyword }}?.{{ attribute }};
108+
{%~ endfor %}
109+
{%~ endif %}
110+
{%~ endfor %}
111+
103112
const apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};
104113
const payload: Payload = {};
105114
{%~ for parameter in method.parameters.query %}

0 commit comments

Comments
 (0)