Skip to content

Commit

Permalink
fix: added optional responseKey in the createResource method for Make…
Browse files Browse the repository at this point in the history
…sHttpRequests for those cases where the returned response has a different key than the request key.
  • Loading branch information
Razorsheep committed Aug 2, 2023
1 parent 96a3fc3 commit 0638aa9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/REST/Actions/ManagesCustomers.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getCustomerOrders($customerId, array $params = []): Collection

public function createCustomerAddress($customerId, array $data): ApiResource
{
return $this->createResource('addresses', $data, ['customers', $customerId]);
return $this->createResource('addresses', $data, ['customers', $customerId], 'customer_address');
}

public function getCustomerAddresses($customerId, array $params = []): Collection
Expand Down
5 changes: 4 additions & 1 deletion src/Support/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ protected function resourceClassFor(string $resource): string
return class_exists($resourceClass) ? $resourceClass : ApiResource::class;
}

protected function createResource(string $resource, array $data, array $uriPrefix = []): ApiResource
protected function createResource(string $resource, array $data, array $uriPrefix = [], string $responseKey = null): ApiResource
{
$key = Str::singular($resource);
$resourceClass = $this->resourceClassFor($resource);

$response = $this->post(implode('/', [...$uriPrefix, "{$resource}.json"]), [$key => $data]);

if(!empty($responseKey)){
$key = $responseKey;
}
return new $resourceClass($response[$key], $this);
}

Expand Down

0 comments on commit 0638aa9

Please sign in to comment.