Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
5.1.4
=====

* (bug) Fix invalid pagination handling `ManagementApi::sendPaginatedRequest()`.


5.1.3
=====

Expand Down
23 changes: 18 additions & 5 deletions src/Api/ManagementApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,27 @@ public function fetchDatasourceEntries (
/**
* @return AssetData[]
*/
public function fetchAllAssets () : array
public function fetchAllAssets (
?TorrStyle $io = null,
) : array
{
$assets = [];
$page = 1;
$maxPage = null;

do {
$io?->writeln(\sprintf(
"• Fetching all assets metadata page <fg=yellow>%s / %s</> for space <fg=blue>%s</>",
$page,
$maxPage ?? "(unknown)",
$this->config->spaceId,
));

/** @var PaginatedApiResult<array> $result */
$result = $this->sendPaginatedRequest(
"assets",
$page,
1000,
25, // seems to be the max allowed value
"assets",
);

Expand Down Expand Up @@ -463,11 +473,14 @@ private function sendPaginatedRequest (
}

$totalValues = (int) $headers["total"][0];
// use value from headers, if we passed a perPage-value too large. Storyblok will automatically reduce it
// to the max allowed value
$perPage = (int) $headers["per-page"][0];

return new PaginatedApiResult(
(int) $headers["per-page"][0],
(int) ceil($totalValues / $perPage),
"" !== $response->getContent()
perPage: $perPage,
totalPages: (int) ceil($totalValues / $perPage),
entries: "" !== $response->getContent()
? $response->toArray()[$resultKey]
: [],
);
Expand Down