Skip to content

Commit

Permalink
Feat(hub): Rename the Last-Event-ID query parameter to lastEventID (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
Co-authored-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
bpolaszek and azjezz authored Mar 20, 2024
1 parent 7104712 commit 4219fff
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"react/promise-timer": "^1.10",
"rize/uri-template": "^0.3.4",
"symfony/console": "^5.4.0|^6.0.0|^7.0.0",
"symfony/deprecation-contracts": "^3.1",
"symfony/dotenv": "^5.4.0|^6.0.0|^7.0.0",
"symfony/flex": "^1.17|^2",
"symfony/framework-bundle": "^5.4.0|^6.0.0|^7.0.0",
Expand Down
22 changes: 16 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ function nullify(mixed $value, ?string $cast = null): mixed
function extract_last_event_id(ServerRequestInterface $request): ?string
{
$qs = query_string($request->getUri(), new FlatQueryParser());
$lastEventId = nullify($request->getHeaderLine('Last-Event-ID')) ?? $qs->getParam('lastEventID');
if (null === $lastEventId) {
$lastEventId = $qs->getParam('Last-Event-ID') ??
$qs->getParam('Last-Event-Id') ??
$qs->getParam('last-event-id') ??
$qs->getParam('LAST-EVENT-ID');

return nullify($request->getHeaderLine('Last-Event-ID'))
?? $qs->getParam('Last-Event-ID')
?? $qs->getParam('Last-Event-Id')
?? $qs->getParam('last-event-id')
?? $qs->getParam('LAST-EVENT-ID')
?? null;
if ($lastEventId !== null) {
trigger_deprecation(
'freddie/mercure-x',
'1.0',
'Using "Last-Event-ID" query parameter is deprecated, use "lastEventID" instead.',
);
}
}

return $lastEventId;
}

/**
Expand Down
18 changes: 14 additions & 4 deletions tests/Unit/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@
'request' => new ServerRequest('GET', '/.well-known/mercure', ['LAST-EVENT-ID' => 'foo']),
'expected' => 'foo',
];
yield [
'request' => new ServerRequest('GET', '/.well-known/mercure?lastEventID=foo'),
'expected' => 'foo',
];
yield [
'request' => new ServerRequest('GET', '/.well-known/mercure'),
'expected' => null,
];
});

it(
'extracts Last-Event-ID from request using deprecated query parameter.',
fn (ServerRequestInterface $request, ?string $expected) => expect(extract_last_event_id($request))->toBe($expected)
)->with(function () {
yield [
'request' => new ServerRequest('GET', '/.well-known/mercure?Last-Event-ID=foo'),
'expected' => 'foo',
Expand All @@ -91,8 +105,4 @@
'request' => new ServerRequest('GET', '/.well-known/mercure?LAST-EVENT-ID=foo'),
'expected' => 'foo',
];
yield [
'request' => new ServerRequest('GET', '/.well-known/mercure'),
'expected' => null,
];
});

0 comments on commit 4219fff

Please sign in to comment.