Skip to content

Commit 5bbfc16

Browse files
committed
docs: clarify resource response mapping
1 parent 891e519 commit 5bbfc16

2 files changed

Lines changed: 34 additions & 41 deletions

File tree

docs/03-api.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,7 @@ resource(string $class): Resource
9999

100100
Protected helper for creating resource instances from an API class.
101101

102-
```php
103-
final class ExampleApi extends Api
104-
{
105-
public function users(): UserResource
106-
{
107-
return $this->resource(UserResource::class);
108-
}
109-
}
110-
```
102+
See [Resource Authoring](04-resource-authoring.md) for the recommended API-to-resource pattern.
111103

112104
## Request Defaults
113105

docs/04-resource-authoring.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,39 @@ return $this
212212

213213
`collection()` returns a plain array of entities.
214214

215+
Use `envelope()` when the response carries metadata, pagination, or any API-specific envelope:
216+
217+
```php
218+
return $this
219+
->endpoint()
220+
->get('/users/{id}', ['id' => $id])
221+
->envelope(UserResponse::class);
222+
```
223+
224+
Envelope classes must implement `ResponseEnvelopeInterface`:
225+
226+
```php
227+
use ProgrammatorDev\Api\Context\Context;
228+
use ProgrammatorDev\Api\Response\Response;
229+
use ProgrammatorDev\Api\Contract\ResponseEnvelopeInterface;
230+
231+
final class UserResponse implements ResponseEnvelopeInterface
232+
{
233+
public function __construct(
234+
private readonly User $user,
235+
private readonly int $statusCode,
236+
) {}
237+
238+
public static function fromResponse(Response $response, ?Context $context = null): static
239+
{
240+
return new self(
241+
user: $response->entity(User::class, key: 'data'),
242+
statusCode: $response->raw()->getStatusCode(),
243+
);
244+
}
245+
}
246+
```
247+
215248
## Context
216249

217250
`Context` carries SDK options into response mapping without passing the full `Api` instance around.
@@ -298,38 +331,6 @@ final class UserResponse implements ResponseEnvelopeInterface
298331

299332
Keep context usage focused on hydration decisions. Entities should still be data/value objects by default and should not perform hidden network calls.
300333

301-
Use `envelope()` when the response carries metadata, pagination, or any API-specific envelope:
302-
303-
```php
304-
return $this
305-
->get('/users/{id}', ['id' => $id])
306-
->envelope(UserResponse::class);
307-
```
308-
309-
Envelope classes must implement `ResponseEnvelopeInterface`:
310-
311-
```php
312-
use ProgrammatorDev\Api\Context\Context;
313-
use ProgrammatorDev\Api\Response\Response;
314-
use ProgrammatorDev\Api\Contract\ResponseEnvelopeInterface;
315-
316-
final class UserResponse implements ResponseEnvelopeInterface
317-
{
318-
public function __construct(
319-
private readonly User $user,
320-
private readonly int $statusCode,
321-
) {}
322-
323-
public static function fromResponse(Response $response, ?Context $context = null): static
324-
{
325-
return new self(
326-
user: $response->entity(User::class, key: 'data'),
327-
statusCode: $response->raw()->getStatusCode(),
328-
);
329-
}
330-
}
331-
```
332-
333334
## API-Specific Resource Chains
334335

335336
Keep API-specific vocabulary out of the base package. Add it in SDK resources with small fluent methods that use the generic endpoint helpers underneath.

0 commit comments

Comments
 (0)