feat: Aggiunta JSON API avanzata
La vecchia JSON API custom verrà rimossa successivamente, quando quella nuova avrà un'interfaccia stabile per i moduli
This commit is contained in:
parent
bafe53efac
commit
a350e4b068
|
@ -3,6 +3,7 @@
|
|||
namespace App\Exceptions;
|
||||
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use LaravelJsonApi\Exceptions\ExceptionParser;
|
||||
use Throwable;
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
|
@ -35,5 +36,9 @@ class Handler extends ExceptionHandler
|
|||
$this->reportable(static function (Throwable $e) {
|
||||
//
|
||||
});
|
||||
|
||||
$this->renderable(
|
||||
ExceptionParser::make()->renderable()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\JsonApi\V1;
|
||||
|
||||
use App\JsonApi\V1\Users\UserSchema;
|
||||
use LaravelJsonApi\Core\Server\Server as BaseServer;
|
||||
|
||||
class Server extends BaseServer
|
||||
{
|
||||
/**
|
||||
* The base URI namespace for this server.
|
||||
*/
|
||||
protected string $baseUri = '/api/v1';
|
||||
|
||||
/**
|
||||
* Bootstrap the server when it is handling an HTTP request.
|
||||
*/
|
||||
public function serving(): void
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the server's list of schemas.
|
||||
*/
|
||||
protected function allSchemas(): array
|
||||
{
|
||||
return [
|
||||
UserSchema::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* TODO: Temporary: it must be added authentication to API routes
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorizable(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace App\JsonApi\V1\Users;
|
||||
|
||||
use App\Models\User;
|
||||
use LaravelJsonApi\Eloquent\Contracts\Paginator;
|
||||
use LaravelJsonApi\Eloquent\Fields\DateTime;
|
||||
use LaravelJsonApi\Eloquent\Fields\ID;
|
||||
use LaravelJsonApi\Eloquent\Fields\Str;
|
||||
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
|
||||
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
|
||||
use LaravelJsonApi\Eloquent\Schema;
|
||||
|
||||
class UserSchema extends Schema
|
||||
{
|
||||
|
||||
/**
|
||||
* The model the schema corresponds to.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static string $model = User::class;
|
||||
|
||||
/**
|
||||
* Get the resource fields.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
ID::make(),
|
||||
Str::make('username'),
|
||||
Str::make('email'),
|
||||
DateTime::make('createdAt')->sortable()->readOnly(),
|
||||
DateTime::make('updatedAt')->sortable()->readOnly(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource filters.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function filters(): array
|
||||
{
|
||||
return [
|
||||
WhereIdIn::make($this),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource paginator.
|
||||
*
|
||||
* @return Paginator|null
|
||||
*/
|
||||
public function pagination(): ?Paginator
|
||||
{
|
||||
return PagePagination::make();
|
||||
}
|
||||
|
||||
}
|
|
@ -39,6 +39,7 @@
|
|||
"innocenzi/laravel-vite": "^0",
|
||||
"laravel/framework": "^8",
|
||||
"laravel/tinker": "^2",
|
||||
"laravel-json-api/laravel": "^1",
|
||||
"maicol07/laravel-json-api-resource": "dev-master",
|
||||
"nette/utils": "^3",
|
||||
"riverskies/laravel-mobile-detect": "^1",
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Root Namespace
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The root JSON:API namespace, within your application's namespace.
|
||||
| This is used when generating any class that does not sit *within*
|
||||
| a server's namespace. For example, new servers and filters.
|
||||
|
|
||||
| By default this is set to `JsonApi` which means the root namespace
|
||||
| will be `\App\JsonApi`, if your application's namespace is `App`.
|
||||
*/
|
||||
'namespace' => 'JsonApi',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Servers
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| A list of the JSON:API compliant APIs in your application, referred to
|
||||
| as "servers". They must be listed below, with the array key being the
|
||||
| unique name for each server, and the value being the fully-qualified
|
||||
| class name of the server class.
|
||||
*/
|
||||
'servers' => [
|
||||
'v1' => \App\JsonApi\V1\Server::class,
|
||||
],
|
||||
];
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
/** @noinspection UnusedFunctionResultInspection */
|
||||
|
||||
use App\Http\Controllers\Api\UserController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
|
||||
use LaravelJsonApi\Laravel\Http\Controllers\JsonApiController;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
@ -16,7 +16,11 @@ use Illuminate\Support\Facades\Route;
|
|||
|
|
||||
*/
|
||||
|
||||
Route::apiResource('users', UserController::class);
|
||||
JsonApiRoute::server('v1')
|
||||
->prefix('v1')
|
||||
->resources(function ($server) {
|
||||
$server->resource('users', JsonApiController::class);
|
||||
});
|
||||
|
||||
/*
|
||||
* Prossimamente, per le chiamate alla API autenticate:
|
||||
|
|
Loading…
Reference in New Issue