Add support for submit in proxy

This commit is contained in:
philipp lang 2024-02-08 02:23:11 +01:00
parent 05a6fca5cc
commit f4629e7902
2 changed files with 17 additions and 8 deletions

View File

@ -2,8 +2,7 @@
use Silva\Adrema\Support\Proxy; use Silva\Adrema\Support\Proxy;
Route::get( Route::get('/adrema-api/{route}', fn (string $route) => Response::json(app(Proxy::class)->run($route, 'get')))
'/adrema-api/{route}', ->where('route', '[a-zA-Z0-9\-/]+')->middleware('api');
fn (string $route) => Response::json(app(Proxy::class)->run($route)) Route::post('/adrema-api/{route}', fn (string $route) => app(Proxy::class)->submit($route))
)
->where('route', '[a-zA-Z0-9\-/]+')->middleware('api'); ->where('route', '[a-zA-Z0-9\-/]+')->middleware('api');

View File

@ -4,15 +4,13 @@ namespace Silva\Adrema\Support;
use Silva\Adrema\Models\Settings; use Silva\Adrema\Models\Settings;
use Http; use Http;
use Illuminate\Http\JsonResponse;
class Proxy class Proxy
{ {
public function run(string $url): ?array public function run(string $url): ?array
{ {
$url = str($url)->start('/'); $response = Http::acceptJson()->get($this->url($url));
$baseUrl = Settings::get('base_url');
$response = Http::get($baseUrl . $url);
if (!$response->ok()) { if (!$response->ok()) {
return null; return null;
@ -20,4 +18,16 @@ class Proxy
return $response->json(); 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('/');
}
} }