Added api.response.headers to be included in all api responses.

This commit is contained in:
Abdulmhsen B. A. A.
2024-06-16 14:17:29 +03:00
parent ca32278a52
commit 53d89713bd
4 changed files with 58 additions and 10 deletions

View File

@@ -38,6 +38,13 @@ return (function () {
'backend' => '[a-zA-Z0-9_-]+',
],
'logInternal' => (bool)env('WS_API_LOG_INTERNAL', false),
'response' => [
'headers' => [
'Content-Type' => 'application/json',
'X-Application-Version' => fn() => getAppVersion(),
'Access-Control-Allow-Origin' => '*',
],
],
],
'webui' => [
'enabled' => (bool)env('WEBUI_ENABLED', env('WS_WEBUI_ENABLED', true)),

View File

@@ -4,16 +4,25 @@ declare(strict_types=1);
namespace App\Libs\Middlewares;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface as iResponse;
use Psr\Http\Message\ServerRequestInterface as iRequest;
use Psr\Http\Server\MiddlewareInterface as iMiddleware;
use Psr\Http\Server\RequestHandlerInterface as iHandler;
final class AddCorsMiddleware implements MiddlewareInterface
final class AddCorsMiddleware implements iMiddleware
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
public function process(iRequest $request, iHandler $handler): iResponse
{
return $handler->handle($request)->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Credentials', 'true');
$response = $handler->handle($request);
if (!$response->hasHeader('Access-Control-Allow-Origin')) {
$response = $response->withHeader('Access-Control-Allow-Origin', '*');
}
if (!$response->hasHeader('Access-Control-Allow-Credentials')) {
$response = $response->withHeader('Access-Control-Allow-Credentials', 'true');
}
return $response;
}
}

View File

@@ -404,7 +404,7 @@ if (!function_exists('api_response')) {
array $headers = [],
string|null $reason = null
): iResponse {
return (new Response(
$response = (new Response(
status: $status->value,
headers: $headers,
body: null !== $body ? json_encode(
@@ -412,7 +412,16 @@ if (!function_exists('api_response')) {
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
) : null,
reason: $reason,
))->withHeader('Content-Type', 'application/json')->withHeader('X-Application-Version', getAppVersion());
));
foreach (Config::get('api.response.headers', []) as $key => $val) {
if ($response->hasHeader($key)) {
continue;
}
$response = $response->withHeader($key, getValue($val));
}
return $response;
}
}

View File

@@ -319,6 +319,17 @@ class HelpersTest extends TestCase
public function test_api_response(): void
{
Config::append([
'api' => [
'response' => [
'headers' => [
'Content-Type' => 'application/json',
'X-Application-Version' => fn() => getAppVersion(),
'Access-Control-Allow-Origin' => '*',
],
],
]
]);
$data = ['foo' => 'bar'];
$response = api_response(HTTP_STATUS::HTTP_OK, $data);
$this->assertSame(HTTP_STATUS::HTTP_OK->value, $response->getStatusCode());
@@ -329,6 +340,18 @@ class HelpersTest extends TestCase
public function test_error_response(): void
{
Config::append([
'api' => [
'response' => [
'headers' => [
'Content-Type' => 'application/json',
'X-Application-Version' => fn() => getAppVersion(),
'Access-Control-Allow-Origin' => '*',
],
],
]
]);
$data = ['error' => ['code' => HTTP_STATUS::HTTP_BAD_REQUEST->value, 'message' => 'error message']];
$response = api_error('error message', HTTP_STATUS::HTTP_BAD_REQUEST);
$this->assertSame(HTTP_STATUS::HTTP_BAD_REQUEST->value, $response->getStatusCode());