forge/app/Traits/ApiResponses.php
Refringe 297937a2ea
Scribe API Documentation
Installs Scribe for (mostly) automatic API documentation. Does a pretty good job and it's pretty painless to set-up. I'll have to configure it to work in the CI and to ensure that the static files are being pushed to production as they should be, but this is a good start.
2024-09-17 01:41:00 -04:00

40 lines
908 B
PHP

<?php
namespace App\Traits;
use Illuminate\Http\JsonResponse;
trait ApiResponses
{
/**
* Return a success JSON response.
*/
protected function success(string $message, ?array $data = []): JsonResponse
{
return $this->baseResponse(message: $message, data: $data, code: 200);
}
/**
* The base response.
*/
private function baseResponse(?string $message = '', ?array $data = [], ?int $code = 200): JsonResponse
{
$response = [];
$response['message'] = $message;
if ($data) {
$response['data'] = $data;
}
$response['status'] = $code;
return response()->json($response, $code);
}
/**
* Return an error JSON response.
*/
protected function error(string $message, int $code): JsonResponse
{
return $this->baseResponse(message: $message, code: $code);
}
}