Skip to content

Commit

Permalink
Collapse nested response fields
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jul 8, 2022
1 parent 7cf0773 commit 00b09bb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 39 deletions.
11 changes: 10 additions & 1 deletion camel/Output/OutputEndpointData.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ class OutputEndpointData extends BaseDTO
public array $responseFields = [];

/**
* The same as bodyParameters, but organized in a hierarchy.
* So, top-level items first, with a __fields property containing their children, and so on.
* Useful so we can easily render and nest on the frontend.
* @var array<string, array>
*/
public array $nestedBodyParameters = [];

/**
* @var array<string, array>
*/
public array $nestedResponseFields = [];

public ?string $boundUri;

public function __construct(array $parameters = [])
Expand All @@ -100,6 +108,7 @@ public function __construct(array $parameters = [])
$this->cleanQueryParameters = Extractor::cleanParams($this->queryParameters);
$this->cleanUrlParameters = Extractor::cleanParams($this->urlParameters);
$this->nestedBodyParameters = Extractor::nestArrayAndObjectFields($this->bodyParameters, $this->cleanBodyParameters);
$this->nestedResponseFields = Extractor::nestArrayAndObjectFields($this->responseFields);

$this->boundUri = u::getUrlWithBoundParameters($this->uri, $this->cleanUrlParameters);

Expand Down Expand Up @@ -189,4 +198,4 @@ public static function getFileParameters(array $parameters): array
}
return [$files, $regularParameters];
}
}
}
7 changes: 4 additions & 3 deletions resources/views/components/field-details.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<b><code>{{ $name }}</code></b>&nbsp;&nbsp;@if($type)<small>{{ $type }}</small>@endif @if(!$required)
<i>optional</i>@endif &nbsp;
@if(($isInput ?? true) && empty($hasChildren))
<b><code>{{ $name }}</code></b>&nbsp;&nbsp;
@if($type)<small>{{ $type }}</small>@endif&nbsp;
@if($isInput && !$required)<i>optional</i>@endif &nbsp;
@if($isInput && empty($hasChildren))
@php
$isList = Str::endsWith($type, '[]');
$fullName =str_replace('[]', '.0', $name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@

@foreach($parameters as $name => $parameter)
@php
$isInput ??= true
@endphp
@foreach($fields as $name => $field)
@if($name === '[]')
@php
$description = "The request body is an array (<code>{$parameter['type']}</code>`)";
$description .= !empty($parameter['description']) ? ", representing ".lcfirst($parameter['description'])."." : '.';
$description = "The request body is an array (<code>{$field['type']}</code>`)";
$description .= !empty($field['description']) ? ", representing ".lcfirst($field['description'])."." : '.';
@endphp
<p>
{!! Parsedown::instance()->text($description) !!}
</p>
@foreach($parameter['__fields'] as $subfieldName => $subfield)
@foreach($field['__fields'] as $subfieldName => $subfield)
@if(!empty($subfield['__fields']))
@component('scribe::components.body-parameters', ['parameters' => [$subfieldName => $subfield], 'endpointId' => $endpointId,])
@endcomponent
<x-scribe::nested-fields
:fields="[$subfieldName => $subfield]" :endpointId="$endpointId" :isInput="$isInput"
/>
@else
<p>
@component('scribe::components.field-details', [
Expand All @@ -23,31 +26,34 @@
'endpointId' => $endpointId,
'hasChildren' => false,
'component' => 'body',
'isInput' => $isInput,
])
@endcomponent
</p>
@endif
@endforeach
@elseif(!empty($parameter['__fields']))
@elseif(!empty($field['__fields']))
<p>
<details>
<summary style="padding-bottom: 10px;">
@component('scribe::components.field-details', [
'name' => $parameter['name'],
'type' => $parameter['type'] ?? 'string',
'required' => $parameter['required'] ?? false,
'description' => $parameter['description'] ?? '',
'example' => $parameter['example'] ?? '',
'name' => $field['name'],
'type' => $field['type'] ?? 'string',
'required' => $field['required'] ?? false,
'description' => $field['description'] ?? '',
'example' => $field['example'] ?? '',
'endpointId' => $endpointId,
'hasChildren' => true,
'component' => 'body',
'isInput' => $isInput,
])
@endcomponent
</summary>
@foreach($parameter['__fields'] as $subfieldName => $subfield)
@foreach($field['__fields'] as $subfieldName => $subfield)
@if(!empty($subfield['__fields']))
@component('scribe::components.body-parameters', ['parameters' => [$subfieldName => $subfield], 'endpointId' => $endpointId,])
@endcomponent
<x-scribe::nested-fields
:fields="[$subfieldName => $subfield]" :endpointId="$endpointId" :isInput="$isInput"
/>
@else
<p>
@component('scribe::components.field-details', [
Expand All @@ -59,6 +65,7 @@
'endpointId' => $endpointId,
'hasChildren' => false,
'component' => 'body',
'isInput' => $isInput,
])
@endcomponent
</p>
Expand All @@ -69,14 +76,15 @@
@else
<p>
@component('scribe::components.field-details', [
'name' => $parameter['name'],
'type' => $parameter['type'] ?? 'string',
'required' => $parameter['required'] ?? false,
'description' => $parameter['description'] ?? '',
'example' => $parameter['example'] ?? '',
'name' => $field['name'],
'type' => $field['type'] ?? 'string',
'required' => $field['required'] ?? false,
'description' => $field['description'] ?? '',
'example' => $field['example'] ?? '',
'endpointId' => $endpointId,
'hasChildren' => false,
'component' => 'body',
'isInput' => $isInput,
])
@endcomponent
</p>
Expand Down
23 changes: 9 additions & 14 deletions resources/views/themes/default/endpoint.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
'example' => $parameter->example ?? '',
'endpointId' => $endpoint->endpointId(),
'component' => 'url',
'isInput' => true,
])
@endcomponent
</p>
Expand All @@ -133,31 +134,25 @@
'example' => $parameter->example ?? '',
'endpointId' => $endpoint->endpointId(),
'component' => 'query',
'isInput' => true,
])
@endcomponent
</p>
@endforeach
@endif
@if(count($endpoint->nestedBodyParameters))
<h4 class="fancy-heading-panel"><b>Body Parameters</b></h4>
@component('scribe::components.body-parameters', ['parameters' => $endpoint->nestedBodyParameters, 'endpointId' => $endpoint->endpointId(),])
@endcomponent
<x-scribe::nested-fields
:fields="$endpoint->nestedBodyParameters" :endpointId="$endpoint->endpointId()"
/>
@endif
</form>

@if(count($endpoint->responseFields))
<h3>Response</h3>
<h4 class="fancy-heading-panel"><b>Response Fields</b></h4>
@foreach($endpoint->responseFields as $name => $field)
<p>
@component('scribe::components.field-details', [
'name' => $field->name,
'type' => $field->type,
'required' => true,
'description' => $field->description,
'isInput' => false,
])
@endcomponent
</p>
@endforeach
<x-scribe::nested-fields
:fields="$endpoint->nestedResponseFields" :endpointId="$endpoint->endpointId()"
:isInput="false"
/>
@endif

0 comments on commit 00b09bb

Please sign in to comment.