Skip to content

Commit

Permalink
Improve internal Json response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Nov 8, 2023
1 parent cd4a23a commit 706922a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/realtime-compiler/src/Http/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class DashboardController
'The dashboard update your project files. You can disable this by setting `server.dashboard.interactive` to `false` in `config/hyde.php`.',
];

protected JsonResponse $response;

public function __construct()
{
$this->title = config('hyde.name').' - Dashboard';
Expand All @@ -73,7 +75,7 @@ public function handle(): Response
}

try {
$this->handlePostRequest();
return $this->handlePostRequest();
} catch (HttpException $exception) {
if (! $this->isAsync) {
throw $exception;
Expand All @@ -95,7 +97,7 @@ protected function show(): string
));
}

protected function handlePostRequest(): void
protected function handlePostRequest(): JsonResponse
{
$actions = array_combine($actions = [
'openInExplorer',
Expand Down Expand Up @@ -126,6 +128,10 @@ protected function handlePostRequest(): void
if ($action === 'createPage') {
$this->createPage();
}

return $this->response ?? new JsonResponse(200, 'OK', [
'message' => 'Action completed successfully',
]);
}

public function getVersion(): string
Expand Down Expand Up @@ -370,7 +376,7 @@ protected function createPage(): void
}

$this->flash('justCreatedPage', RouteKey::fromPage($pageClass, $pageClass::pathToIdentifier($path))->get());
$this->sendJsonResponse(201, "Created file '$path'!");
$this->setJsonResponse(201, "Created file '$path'!");
}
}

Expand Down Expand Up @@ -474,13 +480,11 @@ protected function shouldUnsafeRequestBeBlocked(): bool
return ! in_array($requestIp, $allowedIps, true);
}

protected function sendJsonResponse(int $statusCode, string $body): never
protected function setJsonResponse(int $statusCode, string $body): void
{
(new JsonResponse($statusCode, $this->matchStatusCode($statusCode), [
$this->response = new JsonResponse($statusCode, $this->matchStatusCode($statusCode), [
'body' => $body,
]))->send();

exit;
]);
}

protected function sendJsonErrorResponse(int $statusCode, string $message): JsonResponse
Expand Down

0 comments on commit 706922a

Please sign in to comment.