Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add API response classes for success and error #44

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions app/Http/Responses/ApiErrorResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Responses;

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Response;
use Throwable;

class ApiErrorResponse implements Responsable
{
/**
* Class representing an API error response.
*
* This class is responsible for constructing an API error response object
* with the provided message, optional exception, status code, and headers.
*/
public function __construct(
private $message,
private ?Throwable $exception = null,
private int $status = Response::HTTP_INTERNAL_SERVER_ERROR,
private array $headers = []
) {}

/**
* Convert the exception to a JSON response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function toResponse($request)
{
$response = ['message' => $this->message];

if (!is_null($this->exception) && config('app.debug')) {
$response['debug'] = [
'message' => $this->exception->getMessage(),
'file' => $this->exception->getFile(),
'line' => $this->exception->getLine(),
'trace' => $this->exception->getTraceAsString(),
];
}

return response()->json([
$response,
'status' => $this->status,
'timestamp' => now()->toDateTimeString(),
], $this->status, $this->headers);
}

}
41 changes: 41 additions & 0 deletions app/Http/Responses/ApiSuccessResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Responses;

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Response;

class ApiSuccessResponse implements Responsable
{
/**
* Class representing a successful API response.
*
* This class is responsible for constructing a successful API response
* with the provided data, status code, and headers.
*/
public function __construct(
private $data,
private int $status = Response::HTTP_OK,
private array $headers = []
) {}

/**
* Convert the response instance to a JSON response.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response|void
*/
public function toResponse($request)
{
return response()->json(
[
'status' => $this->status,
'data' => $this->data,
'timestamp' => now()->toDateTimeString(),
],
$this->status,
$this->headers
);
}

}