Skip to content

Commit

Permalink
Generate API documentation with scramble (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 authored Mar 7, 2024
1 parent d4628a5 commit 4f9e085
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 56 deletions.
12 changes: 12 additions & 0 deletions app/Filament/Pages/Auth/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Filament\Pages\Auth;

use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\TextInput;
use JeffGreco13\FilamentBreezy\Pages\MyProfile;

Expand Down Expand Up @@ -41,4 +42,15 @@ protected function getUpdateProfileFormSchema(): array
->label(__('user.field.email')),
];
}

protected function getCreateApiTokenFormSchema(): array
{
return [
TextInput::make('token_name')
->label(__('filament-breezy::default.fields.token_name'))
->required(),

Hidden::make('abilities'),
];
}
}
5 changes: 4 additions & 1 deletion app/Http/Controllers/OrganisationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use App\Http\Resources\OrganisationResource;
use App\Models\Organisation;
use Illuminate\Http\Resources\Json\JsonResource;

class OrganisationController extends Controller
{
public function __invoke()
public function __invoke(): JsonResource
{
$this->authorize('accessApi');

return OrganisationResource::collection(
Organisation::query()
->withoutEagerLoads(['city'])
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use App\Http\Resources\ResourceResource;
use App\Models\Resource;
use Illuminate\Http\Resources\Json\JsonResource;

class ResourceController extends Controller
{
public function __invoke()
public function __invoke(): JsonResource
{
$this->authorize('accessApi');

return ResourceResource::collection(
Resource::query()
->with([
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Resources/IdAndNameResource.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
Expand All @@ -15,8 +17,8 @@ class IdAndNameResource extends JsonResource
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name
'id' => (int) $this->id,
'name' => $this->name,
];
}
}
4 changes: 2 additions & 2 deletions app/Http/Resources/OrganisationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OrganisationResource extends JsonResource
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'id' => (int) $this->id,
'name' => $this->name,
'type' => $this->type,
'status' => $this->status,
Expand All @@ -29,7 +29,7 @@ public function toArray(Request $request): array
'activity_counties' => IdAndNameResource::collection($this->activityCounties),
'created_at' => $this->created_at->format('Y-m-d H:i:s'),
'updated_at' => $this->updated_at->format('Y-m-d H:i:s'),
'volunteers_count' => $this->volunteers_count,
'volunteers_count' => (int) $this->volunteers_count,
];
}
}
9 changes: 9 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use App\Filament\Pages\Auth\Settings;
use Carbon\Carbon;
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;
use Filament\Facades\Filament;
use Filament\Navigation\UserMenuItem;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -52,6 +55,12 @@ public function boot()
->icon('heroicon-o-cog'),
]);
});

Scramble::extendOpenApi(function (OpenApi $openApi) {
$openApi->secure(
SecurityScheme::http('bearer', 'JWT')
);
});
}

public function registerReleaseVersion(): void
Expand Down
10 changes: 10 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace App\Providers;

use App\Models\User;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -33,5 +35,13 @@ public function boot()
return (new EloquentUserProvider($app['hash'], $config['model']))
->withQuery(fn (Builder $query) => $query->withoutGlobalScopes());
});

Gate::define('accessApi', function (User $user) {
if (! config('filament-breezy.enable_sanctum')) {
return false;
}

return $user->isPlatformAdmin();
});
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"require": {
"php": "^8.1",
"alcea/cnp": "^3.0",
"dedoc/scramble": "^0.8.5",
"filament/filament": "^2.17",
"filament/spatie-laravel-media-library-plugin": "^2.17",
"guzzlehttp/guzzle": "^7.8",
Expand Down
173 changes: 124 additions & 49 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion config/filament-breezy.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,7 @@
|--------------------------------------------------------------------------
| Sanctum permissions
*/
'sanctum_permissions' => ['create', 'read', 'update', 'delete'],
'sanctum_permissions' => [
// 'create', 'read', 'update', 'delete',
],
];
Loading

0 comments on commit 4f9e085

Please sign in to comment.