mirror of
https://gh.wpcy.net/https://github.com/aspirepress/AspireCloud.git
synced 2026-06-01 00:19:09 +08:00
* 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
44 lines
1.1 KiB
PHP
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);
|
|
}
|
|
}
|