AspireCloud/app/Http/JsonResponses.php
Chuck Adams 35b967b124
Repo Admin API (#123)
* fix: switch redis driver to predis because laravel is hopeless

* chore: add spatie/laravel-permission

* build: composer update and bump

* style: death to captain obvious (zap laravel's banner comments)

* feat: initial roles and perms

* chore: assign User role on User created

* refactor: rename 'aspire_press' config key to 'aspirecloud'

* feat: require json content-type on admin api routes

* chore: implement BulkImport endpoint

* chore: give RepoAdmin users BulkImport permissions

* docs: update README
2024-12-28 15:57:51 -07:00

44 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http;
use App\Utils\JSON;
use Illuminate\Http\JsonResponse;
trait JsonResponses
{
/**
* @param array<string, mixed> $data
* @param array<string, string> $headers
*/
public function jsonResponse(array $data, int $status = 200, array $headers = []): JsonResponse
{
return new JsonResponse(JSON::fromAssoc($data), $status, $headers, json: true);
}
/**
* @param string|array<string, mixed> $data
* @param array<string, string> $headers
*/
public function success(string|array $data = 'success', int $status = 200, array $headers = []): JsonResponse
{
if (is_string($data)) {
$data = ['message' => $data];
}
return $this->jsonResponse($data, $status, $headers);
}
/**
* @param array<string, mixed> $data
* @param array<string, string> $headers
*/
public function error(string|array $data, int $status = 400, array $headers = []): JsonResponse
{
if (is_string($data)) {
$data = ['error' => $data];
}
return $this->jsonResponse($data, $status, $headers);
}
}