Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b6afc54f9 | ||
|
|
a2c3ad76c0 | ||
|
|
42debe755e | ||
|
|
9788f6f895 | ||
|
|
395b15b80b | ||
|
|
164c3ab5dd | ||
|
|
26454a9501 | ||
|
|
c3033e1093 | ||
|
|
f622f3152b |
No files matched your search
@@ -17,7 +17,7 @@ jobs:
|
|||||||
fail-fast: true
|
fail-fast: true
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-latest, windows-latest]
|
os: [ubuntu-latest, windows-latest]
|
||||||
php: [8.4]
|
php: [8.4, 8.3]
|
||||||
laravel: [11.*]
|
laravel: [11.*]
|
||||||
stability: [prefer-lowest, prefer-stable]
|
stability: [prefer-lowest, prefer-stable]
|
||||||
include:
|
include:
|
||||||
|
|||||||
@@ -30,3 +30,7 @@ phpstan.neon
|
|||||||
testbench.yaml
|
testbench.yaml
|
||||||
/docs
|
/docs
|
||||||
/coverage
|
/coverage
|
||||||
|
|
||||||
|
# Ignore Mac DS_Store files
|
||||||
|
.DS_Store
|
||||||
|
**/.DS_Store
|
||||||
@@ -1,3 +1,19 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
All notable changes to `laravel-plausible-proxy` will be documented in this file.
|
All notable changes to `laravel-plausible-proxy` will be documented in this file.
|
||||||
|
|
||||||
|
## v1.1 - 2025-01-31
|
||||||
|
|
||||||
|
### What's Changed
|
||||||
|
|
||||||
|
* Refactor event forwarding and improve test coverage by @mathiaspalmqvist in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/1
|
||||||
|
|
||||||
|
### New Contributors
|
||||||
|
|
||||||
|
* @mathiaspalmqvist made their first contribution in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/1
|
||||||
|
|
||||||
|
**Full Changelog**: https://github.com/MattitjaAB/laravel-plausible-proxy/compare/v1.0...v1.1
|
||||||
|
|
||||||
|
## First release - 2025-01-30
|
||||||
|
|
||||||
|
**Full Changelog**: https://github.com/MattitjaAB/laravel-plausible-proxy/commits/v1.0
|
||||||
@@ -9,7 +9,7 @@ A Laravel package for proxying Plausible Analytics, improving performance and pr
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
- Laravel 11
|
- Laravel 11
|
||||||
- PHP 8.4+
|
- PHP 8.3+
|
||||||
|
|
||||||
## 🚀 Installation
|
## 🚀 Installation
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.4",
|
"php": "^8.3",
|
||||||
"spatie/laravel-package-tools": "^1.16",
|
"spatie/laravel-package-tools": "^1.16",
|
||||||
"illuminate/contracts": "^10.0||^11.0"
|
"illuminate/contracts": "^10.0||^11.0"
|
||||||
},
|
},
|
||||||
|
|||||||
+12
-3
@@ -1,13 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Concurrency;
|
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
|
||||||
Route::post('api/event', function (Request $request) {
|
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',
|
'n' => 'required|string',
|
||||||
'u' => 'required|url',
|
'u' => 'required|url',
|
||||||
'd' => 'required|string',
|
'd' => 'required|string',
|
||||||
@@ -20,7 +25,11 @@ Route::post('api/event', function (Request $request) {
|
|||||||
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
|
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']);
|
return response()->json(['status' => 'queued']);
|
||||||
});
|
});
|
||||||
@@ -15,7 +15,7 @@ it('returns plausible script from cache', function () {
|
|||||||
|
|
||||||
it('fetches plausible script if not cached', function () {
|
it('fetches plausible script if not cached', function () {
|
||||||
Http::fake([
|
Http::fake([
|
||||||
'https://plausible.io/js/script.js' => Http::response('console.log("Plausible script");', 200),
|
config('plausible-proxy.domain').'/js/script.js' => Http::response('console.log("Plausible script");', 200),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response = $this->get('/js/script.js');
|
$response = $this->get('/js/script.js');
|
||||||
@@ -29,7 +29,7 @@ it('fetches plausible script if not cached', function () {
|
|||||||
|
|
||||||
it('handles failed script fetch', function () {
|
it('handles failed script fetch', function () {
|
||||||
Http::fake([
|
Http::fake([
|
||||||
'https://plausible.io/js/script.js' => Http::response('', 500),
|
config('plausible-proxy.domain').'/js/script.js' => Http::response('', 500),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response = $this->get('/js/script.js');
|
$response = $this->get('/js/script.js');
|
||||||
@@ -49,26 +49,35 @@ it('rejects invalid event data', function () {
|
|||||||
->assertJsonStructure(['status', 'errors']);
|
->assertJsonStructure(['status', 'errors']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('accepts valid event data and defers request', function () {
|
it('accepts valid event data and forwards headers', function () {
|
||||||
Http::fake();
|
config()->set('plausible-proxy.domain', 'https://plausible.io');
|
||||||
|
|
||||||
$concurrencyMock = Mockery::mock('alias:Illuminate\Support\Facades\Concurrency');
|
Http::fake([
|
||||||
$concurrencyMock->shouldReceive('defer')->once()->andReturnUsing(function ($callback) {
|
'https://plausible.io/api/event' => function ($request) {
|
||||||
$callback();
|
return Http::response(['status' => 'queued'], 200);
|
||||||
});
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
$headers = [
|
||||||
|
'User-Agent' => 'TestAgent',
|
||||||
|
'Accept' => 'application/json',
|
||||||
|
];
|
||||||
|
|
||||||
$response = $this->postJson('/api/event', [
|
$response = $this->postJson('/api/event', [
|
||||||
'n' => 'pageview',
|
'n' => 'pageview',
|
||||||
'u' => 'https://example.com/page',
|
'u' => 'https://example.com/page',
|
||||||
'd' => 'example.com',
|
'd' => 'example.com',
|
||||||
]);
|
], $headers);
|
||||||
|
|
||||||
$response->assertStatus(200)
|
$response->assertStatus(200)
|
||||||
->assertJson(['status' => 'queued']);
|
->assertJson(['status' => 'queued']);
|
||||||
|
|
||||||
Http::assertSent(fn ($request) => $request->url() === config('plausible-proxy.domain').'/api/event' &&
|
Http::assertSentCount(1);
|
||||||
$request['n'] === 'pageview'
|
|
||||||
);
|
|
||||||
|
|
||||||
Mockery::close();
|
Http::assertSent(function ($request) {
|
||||||
|
return $request->url() === 'https://plausible.io/api/event'
|
||||||
|
&& isset($request['n']) && $request['n'] === 'pageview'
|
||||||
|
&& isset($request['u']) && $request['u'] === 'https://example.com/page'
|
||||||
|
&& isset($request['d']) && $request['d'] === 'example.com';
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user