From 5c948da9f6ba8977832557dc175532dd036760f9 Mon Sep 17 00:00:00 2001 From: Jack Shpartko <3300101@gmail.com> Date: Sun, 16 Aug 2020 11:36:44 +0200 Subject: [PATCH 1/2] Return json with proper content-type --- src/Guide.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Guide.php b/src/Guide.php index 10436b7..afc9921 100644 --- a/src/Guide.php +++ b/src/Guide.php @@ -4,6 +4,7 @@ namespace Sajya\Server; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; use Illuminate\Support\Str; use ReflectionClass; @@ -40,9 +41,9 @@ public function __construct(array $procedures = []) /** * @param string $content * - * @return string + * @return JsonResponse */ - public function handle(string $content = ''): string + public function handle(string $content = ''): JsonResponse { $parser = new Parser($content); @@ -55,7 +56,7 @@ public function handle(string $content = ''): string $response = $parser->isBatch() ? $result->all() : $result->first(); - return json_encode($response, JSON_THROW_ON_ERROR, 512); + return response()->json($response); } /** From c4b5328859f321c78391f5bf48c9b07308ff816c Mon Sep 17 00:00:00 2001 From: Jack Shpartko <3300101@gmail.com> Date: Sun, 16 Aug 2020 11:37:33 +0200 Subject: [PATCH 2/2] Allow use Procedures with default /handle/ method to simplify configuration --- src/Guide.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Guide.php b/src/Guide.php index afc9921..fa97583 100644 --- a/src/Guide.php +++ b/src/Guide.php @@ -110,6 +110,10 @@ public function findProcedure(Request $request): ?string $class = Str::beforeLast($request->getMethod(), '@'); $method = Str::afterLast($request->getMethod(), '@'); + if (Str::contains($request->getMethod(), '@') === false) { + $method = 'handle'; + } + return $this->map ->filter(fn (string $procedure) => $this->getProcedureName($procedure) === $class) ->filter(fn (string $procedure) => $this->checkExistPublicMethod($procedure, $method))