@@ -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
299332Keep 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
335336Keep 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