Skip to content

Commit 797af21

Browse files
committed
feat(endpoint): add withCache modifier
1 parent bf3bc31 commit 797af21

6 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎UPGRADE-3.0.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ use ProgrammatorDev\Api\Builder\CacheBuilder;
121121

122122
return $this
123123
->endpoint()
124-
->cache(fn (CacheBuilder $cache) => $cache->defaultTtl(60))
124+
->withCache(fn (CacheBuilder $cache) => $cache->defaultTtl(60))
125125
->get('/live')
126126
->collection(Event::class, key: 'data');
127127
```

‎docs/05-resources.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,15 @@ SDK authors can configure endpoint-specific cache defaults on the endpoint build
238238
```php
239239
return $this
240240
->endpoint()
241-
->cache(fn (CacheBuilder $cache) => $cache->defaultTtl(60))
241+
->withCache(fn (CacheBuilder $cache) => $cache->defaultTtl(60))
242242
->get('/users')
243243
->collection(User::class, key: 'data');
244244
```
245245

246246
Endpoint cache defaults are immutable and apply only to that request. They require API-level cache configuration because the global cache setup provides the PSR-6 pool.
247247

248+
`Endpoint::cache()` is deprecated since version 3.2.0. Use `Endpoint::withCache()` instead.
249+
248250
## Resource Cache Overrides
249251

250252
`withCache()` lets SDK users override cache behavior for one resource chain while keeping query, headers, body, and verbs inside `Endpoint`.

‎docs/09-cache.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function live(): FixtureCollection
7070
{
7171
return $this
7272
->endpoint()
73-
->cache(fn (CacheBuilder $cache) => $cache->defaultTtl(60))
73+
->withCache(fn (CacheBuilder $cache) => $cache->defaultTtl(60))
7474
->get('/fixtures/live')
7575
->envelope(FixtureCollection::class);
7676
}
@@ -80,6 +80,8 @@ This is useful when the SDK author knows that one endpoint should behave differe
8080

8181
Endpoint defaults do not mutate the API cache builder and do not affect later requests.
8282

83+
`Endpoint::cache()` is deprecated since version 3.2.0. Use `Endpoint::withCache()` instead.
84+
8385
## Resource Overrides
8486

8587
SDK users can override cache behavior for one resource chain with `withCache()`. The override wins over endpoint defaults, does not mutate the API cache builder, and does not affect later resource instances.

‎src/Endpoint.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,23 @@ public function __construct(
2323
/**
2424
* @param callable(\ProgrammatorDev\Api\Builder\CacheBuilder): mixed $configure
2525
*/
26-
public function cache(callable $configure): static
26+
public function withCache(callable $configure): static
2727
{
2828
return $this->withPipelineOptions(
2929
$this->pipelineOptions->withDefault(PipelineOption::CACHE, $configure)
3030
);
3131
}
3232

33+
/**
34+
* @deprecated since 3.2.0. Use withCache().
35+
*
36+
* @param callable(\ProgrammatorDev\Api\Builder\CacheBuilder): mixed $configure
37+
*/
38+
public function cache(callable $configure): static
39+
{
40+
return $this->withCache($configure);
41+
}
42+
3343
/**
3444
* @throws \JsonException
3545
*/

‎tests/Fixture/UserResource.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,26 @@ public function createWithEndpointCache(array $data): Response
5757
{
5858
return $this
5959
->endpoint()
60-
->cache(fn($cache) => $cache->methods(['POST']))
60+
->withCache(fn($cache) => $cache->methods(['POST']))
6161
->json($data)
6262
->post('/users');
6363
}
6464

6565
public function createWithChainedEndpointCache(array $data): Response
66+
{
67+
return $this
68+
->endpoint()
69+
->withCache(fn($cache) => $cache->methods(['POST']))
70+
->withCache(fn($cache) => $cache->methods(['GET']))
71+
->json($data)
72+
->post('/users');
73+
}
74+
75+
public function createWithDeprecatedEndpointCache(array $data): Response
6676
{
6777
return $this
6878
->endpoint()
6979
->cache(fn($cache) => $cache->methods(['POST']))
70-
->cache(fn($cache) => $cache->methods(['GET']))
7180
->json($data)
7281
->post('/users');
7382
}

‎tests/Integration/CacheTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ public function testEndpointCanOverrideCacheConfiguration(): void
4444
$this->assertCount(1, $client->getRequests());
4545
}
4646

47+
public function testDeprecatedEndpointCacheAliasStillConfiguresCache(): void
48+
{
49+
$client = $this->mockClient(new Response(body: '{"id":1,"name":"John"}'));
50+
$api = new FakeApi($client);
51+
$api->setup()->cache(new ArrayAdapter())->methods(['GET']);
52+
53+
$first = $api->users()->createWithDeprecatedEndpointCache(['name' => 'John']);
54+
$second = $api->users()->createWithDeprecatedEndpointCache(['name' => 'John']);
55+
56+
$this->assertSame(['id' => 1, 'name' => 'John'], $first->data());
57+
$this->assertSame(['id' => 1, 'name' => 'John'], $second->data());
58+
$this->assertCount(1, $client->getRequests());
59+
}
60+
4761
public function testResourceCacheOverrideWinsOverEndpointCacheDefault(): void
4862
{
4963
$client = $this->mockClient(

0 commit comments

Comments
 (0)