diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100644 index 751c682..0000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1,19 +0,0 @@ -id(); - - // add fields - - $table->timestamps(); - }); - } -}; diff --git a/phpstan.neon.dist b/phpstan.neon.dist index ab1b4c3..b678f5e 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,7 +6,6 @@ parameters: paths: - src - config - - database tmpDir: build/phpstan checkOctaneCompatibility: true - checkModelProperties: true + checkModelProperties: true \ No newline at end of file diff --git a/resources/views/.gitkeep b/resources/views/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/routes/api.php b/routes/api.php index 6799a28..399887d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,35 +1,6 @@ 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', - 'r' => 'nullable|url', - 'm' => 'nullable|string', - 'p' => 'nullable|array', - ]); - - if ($validator->fails()) { - return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422); - } - - $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']); -}); +Route::post('api/event', PlausibleEventApiController::class); diff --git a/routes/web.php b/routes/web.php index 07f79ce..30fef29 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,34 +1,6 @@ header('Content-Type', 'application/javascript') - ->header('Cache-Control', 'public, max-age=86400'); - } - - $response = Http::timeout(5)->get("{$plausibleDomain}/js/script.js"); - - if ($response->successful()) { - $script = $response->body(); - Cache::put('plausible_script_js', $script, now()->addHours(6)); - - return response($script) - ->header('Content-Type', 'application/javascript') - ->header('Cache-Control', 'public, max-age=86400'); - } - - return response('Error fetching Plausible script', 500); -}); - -Route::get('/api/routes', function () { - return response()->json( - collect(Route::getRoutes())->map(fn ($route) => $route->uri()) - ); -}); +Route::get('/js/script.js', PlausibleScriptProxyController::class); diff --git a/src/Commands/LaravelPlausibleProxyCommand.php b/src/Commands/LaravelPlausibleProxyCommand.php deleted file mode 100644 index cc2f549..0000000 --- a/src/Commands/LaravelPlausibleProxyCommand.php +++ /dev/null @@ -1,19 +0,0 @@ -comment('All done'); - - return self::SUCCESS; - } -} diff --git a/src/Facades/LaravelPlausibleProxy.php b/src/Facades/LaravelPlausibleProxy.php deleted file mode 100644 index b72ede7..0000000 --- a/src/Facades/LaravelPlausibleProxy.php +++ /dev/null @@ -1,16 +0,0 @@ -headers->all()) + ->mapWithKeys(fn ($value, $key) => [$key => implode(', ', $value)]) + ->all(); + + Http::withHeaders($headers) + ->post(config('plausible-proxy.domain').'/api/event', $request->validated()); + + return response()->json(['status' => 'queued']); + } +} diff --git a/src/Http/Controllers/PlausibleScriptProxyController.php b/src/Http/Controllers/PlausibleScriptProxyController.php new file mode 100644 index 0000000..580b3f5 --- /dev/null +++ b/src/Http/Controllers/PlausibleScriptProxyController.php @@ -0,0 +1,38 @@ +addHours(6), + callback: function () { + $plausibleDomain = config('plausible-proxy.domain'); + + return Http::timeout(5) + ->get("{$plausibleDomain}/js/script.js") + ->throw() + ->body(); + } + ); + } catch (RequestException $e) { + Log::error('Failed to fetch Plausible script: '.$e->getMessage()); + + return response('Error fetching Plausible script', 500); + } + + return response($script) + ->header('Content-Type', 'application/javascript') + ->header('Cache-Control', 'public, max-age=86400'); + } +} diff --git a/src/Http/Requests/PlausibleEventRequest.php b/src/Http/Requests/PlausibleEventRequest.php new file mode 100644 index 0000000..189a3a0 --- /dev/null +++ b/src/Http/Requests/PlausibleEventRequest.php @@ -0,0 +1,25 @@ + ['required', 'string'], + 'u' => ['required', 'url'], + 'd' => ['required', 'string'], + 'r' => ['nullable', 'url'], + 'm' => ['nullable', 'string'], + 'p' => ['nullable', 'array'], + ]; + } +} diff --git a/src/LaravelPlausibleProxy.php b/src/LaravelPlausibleProxy.php deleted file mode 100755 index a72c463..0000000 --- a/src/LaravelPlausibleProxy.php +++ /dev/null @@ -1,5 +0,0 @@ -assertStatus(422) - ->assertJsonStructure(['status', 'errors']); + ->assertJsonValidationErrors(['u' => 'The u field must be a valid URL.']); }); it('accepts valid event data and forwards headers', function () {