From f4629e790222a8a20f7e4d3a53e6568d068600aa Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 8 Feb 2024 02:23:11 +0100 Subject: [PATCH] Add support for submit in proxy --- routes.php | 7 +++---- support/Proxy.php | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/routes.php b/routes.php index 2f3d2a9..62c7f9b 100644 --- a/routes.php +++ b/routes.php @@ -2,8 +2,7 @@ use Silva\Adrema\Support\Proxy; -Route::get( - '/adrema-api/{route}', - fn (string $route) => Response::json(app(Proxy::class)->run($route)) -) +Route::get('/adrema-api/{route}', fn (string $route) => Response::json(app(Proxy::class)->run($route, 'get'))) + ->where('route', '[a-zA-Z0-9\-/]+')->middleware('api'); +Route::post('/adrema-api/{route}', fn (string $route) => app(Proxy::class)->submit($route)) ->where('route', '[a-zA-Z0-9\-/]+')->middleware('api'); diff --git a/support/Proxy.php b/support/Proxy.php index 06f3c06..4549cd6 100644 --- a/support/Proxy.php +++ b/support/Proxy.php @@ -4,15 +4,13 @@ namespace Silva\Adrema\Support; use Silva\Adrema\Models\Settings; use Http; +use Illuminate\Http\JsonResponse; class Proxy { public function run(string $url): ?array { - $url = str($url)->start('/'); - - $baseUrl = Settings::get('base_url'); - $response = Http::get($baseUrl . $url); + $response = Http::acceptJson()->get($this->url($url)); if (!$response->ok()) { return null; @@ -20,4 +18,16 @@ class Proxy return $response->json(); } + + public function submit(string $url): JsonResponse + { + $response = Http::acceptJson()->post($this->url($url), request()->input()); + + return response()->json($response->json(), $response->status()); + } + + private function url(string $internal): string + { + return Settings::get('base_url') . str($internal)->start('/'); + } }