Skip to content
4 changes: 4 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Binaryk\LaravelRestify\Http\Controllers\RepositoryStoreController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateBulkController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateController;
use Binaryk\LaravelRestify\Http\Controllers\RestifyJsSetupController;
use Illuminate\Support\Facades\Route;

// Global Search...
Expand All @@ -29,6 +30,9 @@
Route::post('/profile', '\\'.ProfileUpdateController::class);
Route::post('/profile/avatar', '\\'.ProfileAvatarController::class);

// RestifyJS
Route::get('/restifyjs/setup', '\\'.RestifyJsSetupController::class);

// Filters
Route::get('/{repository}/filters', '\\'.RepositoryFilterController::class);

Expand Down
3 changes: 2 additions & 1 deletion src/Commands/stubs/user-repository.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace App\Restify;

use Binaryk\LaravelRestify\Fields\Field;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserRepository extends Repository
Expand All @@ -15,7 +16,7 @@ class UserRepository extends Repository
return [
Field::make('name')->rules('required'),

Field::make('email')->storingRules('required', ''unique:users')->messages([
Field::make('email')->storingRules('required', 'unique:users')->messages([
'required' => 'This field is required.',
]),

Expand Down
13 changes: 12 additions & 1 deletion src/Eager/Related.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Binaryk\LaravelRestify\Fields\EagerField;
use Binaryk\LaravelRestify\Repositories\Repository;
use Binaryk\LaravelRestify\Traits\Make;
use JsonSerializable;

class Related
class Related implements JsonSerializable
{
use Make;

Expand Down Expand Up @@ -34,4 +35,14 @@ public function resolveField(Repository $repository): EagerField
{
return $this->field->resolve($repository);
}

public function jsonSerialize()
{
return [
'relation' => $this->getRelation(),
'field' => isset($this->field)
? $this->field->jsonSerialize()
: null,
];
}
}
49 changes: 49 additions & 0 deletions src/Http/Controllers/RestifyJsSetupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Binaryk\LaravelRestify\Http\Controllers;

use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Repositories\Repository;
use Binaryk\LaravelRestify\Restify;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;

class RestifyJsSetupController extends Controller
{
public function __invoke(RestifyRequest $request)
{
return response()->json([
'config' => $this->config(),
'repositories' => $this->repositories($request),
]);
}

private function repositories(RestifyRequest $request): array
{
return collect(Restify::$repositories)
->map(fn (string $repository) => app($repository))
->map(fn (Repository $repository) => $repository->restifyjsSerialize($request))
->all();
}

private function config(): array
{
return [
'domain' => $this->deleteFirstAndLastSlash(config('app.url')),
'base' => $this->deleteFirstAndLastSlash(Restify::path()),
];
}

private function deleteFirstAndLastSlash(string $domain): string
{
if (Str::startsWith($domain, '/')) {
$domain = Str::replaceFirst('/', '', $domain);
}

if (Str::endsWith($domain, '/')) {
$domain = Str::replaceLast('/', '', $domain);
}

return $domain;
}
}
15 changes: 15 additions & 0 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Binaryk\LaravelRestify\Repositories;

use Binaryk\LaravelRestify\Actions\Action;
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Controllers\RestResponse;
use Binaryk\LaravelRestify\Eager\Related;
Expand Down Expand Up @@ -1074,4 +1075,18 @@ public function isEagerState(): bool
{
return $this->eagerState === true;
}

public function restifyjsSerialize(RestifyRequest $request): array
{
return [
'uriKey' => static::uriKey(),
'related' => static::collectFilters('matches'),
'sort' => static::collectFilters('sortables'),
'match' => static::collectFilters('matches'),
'searchables' => static::collectFilters('searchables'),
'actions' => $this->resolveActions($request)->filter(fn (Action $action) => $action->isShownOnIndex(
$request, $this
))->values(),
];
}
}
30 changes: 30 additions & 0 deletions tests/Controllers/RestifyJsSetupControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Binaryk\LaravelRestify\Tests\Controllers;

use Binaryk\LaravelRestify\Tests\IntegrationTest;

class RestifyJsSetupControllerTest extends IntegrationTest
{
public function test_returns_configurations()
{
$this->getJson('restifyjs/setup')
->assertJsonStructure([
'config' => [
'domain',
'base',
],
'repositories' => [
[
'uriKey',
'related',
'sort',
'match',
'searchables',
'actions',
],
],
])
->assertOk();
}
}