Skip to content

Commit

Permalink
feat: Export and download collection direct from panel
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-code-labx committed Jun 15, 2024
1 parent 0c743a0 commit 43e155d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/Concerns/InteractsWithCollectionExports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace XtendPackages\RESTPresenter\Concerns;

use Illuminate\Support\Facades\Artisan;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

trait InteractsWithCollectionExports
{
protected function downloadExportedCollection(string $type): BinaryFileResponse
{
$filenameSuffix = match ($type) {
'insomnia' => 'insomnia_collection.json',
'postman' => 'postman_collection.json',
default => throw new InvalidArgumentException('Invalid type'),
};

config(['rest-presenter.exporters.provider' => $type]);
Artisan::call('rest-presenter:generate-api-collection');

$latestExportedCollection = $this->getLatestExportedCollection($type, $filenameSuffix);

return response()->download($latestExportedCollection);
}

protected function getLatestExportedCollection(string $type, string $filenameSuffix): string
{
$collections = glob(resource_path("rest-presenter/$type/*_$filenameSuffix"));
if (empty($collections)) {
throw new InvalidArgumentException('No collections found');
}

return end($collections);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;
use Filament\Support\Enums\ActionSize;
use XtendPackages\RESTPresenter\Concerns\InteractsWithCollectionExports;
use XtendPackages\RESTPresenter\StarterKits\Filament\Resources\EndpointResource;
use XtendPackages\RESTPresenter\StarterKits\Filament\Resources\EndpointResource\Widgets\EndpointStatsWidget;

class ManageEndpoints extends ManageRecords
{
use InteractsWithCollectionExports;

protected static string $resource = EndpointResource::class;

protected function getActions(): array
Expand All @@ -20,10 +23,12 @@ protected function getActions(): array
Actions\ActionGroup::make([
Actions\Action::make('insomnia')
->label('Insomnia')
->icon('heroicon-o-arrow-down-tray'),
->icon('heroicon-o-arrow-down-tray')
->action(fn () => $this->downloadExportedCollection('insomnia')),
Actions\Action::make('postman')
->label('Postman')
->icon('heroicon-o-arrow-down-tray'),
->icon('heroicon-o-arrow-down-tray')
->action(fn () => $this->downloadExportedCollection('postman')),
])
->label('Export Collection')
->icon('heroicon-o-circle-stack')
Expand Down

0 comments on commit 43e155d

Please sign in to comment.