Refactor event forwarding and improve test coverage
- Updated event forwarding to include request headers - Refactored validation to improve error handling - Fixed issue with missing headers in HTTP request - Improved test cases for event forwarding and caching - Ensured proper handling of JSON payloads - Fixed Plausible API URL configuration in tests
This commit is contained in:
1 parent
c3033e1093
commit
26454a9501
2 files changed
+43
-16
No files matched your search
+12
-3
@@ -1,13 +1,18 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Concurrency;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
Route::post('api/event', function (Request $request) {
|
||||
$validator = Validator::make($request->all(), [
|
||||
$data = json_decode($request->getContent(), true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
return response()->json(['error' => 'Invalid JSON format'], 422);
|
||||
}
|
||||
|
||||
$validator = Validator::make($data, [
|
||||
'n' => 'required|string',
|
||||
'u' => 'required|url',
|
||||
'd' => 'required|string',
|
||||
@@ -20,7 +25,11 @@ Route::post('api/event', function (Request $request) {
|
||||
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
|
||||
}
|
||||
|
||||
Concurrency::defer(fn () => Http::post(config('plausible-proxy.domain').'/api/event', $request->all()));
|
||||
$headers = collect($request->headers->all())
|
||||
->mapWithKeys(fn($value, $key) => [$key => is_array($value) ? implode(', ', $value) : $value])
|
||||
->all();
|
||||
|
||||
Http::withHeaders($headers)->post(config('plausible-proxy.domain').'/api/event', $data);
|
||||
|
||||
return response()->json(['status' => 'queued']);
|
||||
});
|
||||
Reference in new issue
Block a user