Skip to content

Conversation

@dipankarmaikap
Copy link
Collaborator

@dipankarmaikap dipankarmaikap commented Nov 3, 2025

This PR enhances the fetchDatasourceEntries function to work recursively, enabling it to fetch all datasource entries across multiple pages until all results are retrieved.


Note

Introduces a generic pagination helper and applies it to fetch all datasources and their entries across pages, updates tests and mocks for pagination, and aligns OpenAPI specs to use shared page/per_page params.

  • CLI (datasources pull):
    • Pagination: Add fetchAllPages helper and use it in fetchDatasources and fetchDatasourceEntries to retrieve all pages via page query and total header.
    • Entries loading: Fetch entries for each datasource in parallel and include them in results.
    • Filesystem: Preserve datasources/{space} structure when saving consolidated or separate files.
  • Tests (packages/cli/src/commands/datasources/pull/actions.test.ts):
    • Replace inline fixtures with datasource.mock.json (51 datasources) and add paginated MSW handlers.
    • Add assertions for multi-page fetching (counts, lengths, and specific entry checks) and correct URL (?page=1).
    • Update file-saving tests to match new dataset and filenames.
  • Fixtures: Add packages/cli/src/commands/datasources/pull/datasource.mock.json with paginated datasources and entries.
  • OpenAPI:
    • Standardize pagination by referencing ../shared/pagination.yaml#/page and /per_page in assets, components, stories, internal_tags, and add to datasources list endpoint.

Written by Cursor Bugbot for commit b31e50b. This will update automatically on new commits. Configure here.

Closes: #224, #247

cursor[bot]

This comment was marked as outdated.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 3, 2025

Open in StackBlitz

@storyblok/astro

npm i https://pkg.pr.new/@storyblok/astro@366

storyblok

npm i https://pkg.pr.new/storyblok@366

@storyblok/eslint-config

npm i https://pkg.pr.new/@storyblok/eslint-config@366

@storyblok/js

npm i https://pkg.pr.new/@storyblok/js@366

storyblok-js-client

npm i https://pkg.pr.new/storyblok-js-client@366

@storyblok/management-api-client

npm i https://pkg.pr.new/@storyblok/management-api-client@366

@storyblok/nuxt

npm i https://pkg.pr.new/@storyblok/nuxt@366

@storyblok/react

npm i https://pkg.pr.new/@storyblok/react@366

@storyblok/region-helper

npm i https://pkg.pr.new/@storyblok/region-helper@366

@storyblok/richtext

npm i https://pkg.pr.new/@storyblok/richtext@366

@storyblok/svelte

npm i https://pkg.pr.new/@storyblok/svelte@366

@storyblok/vue

npm i https://pkg.pr.new/@storyblok/vue@366

commit: b31e50b

const total = Number(response.headers.get('total')) || 0;
const fetchedEntries = data?.datasource_entries || [];
const allEntries = [...collectedEntries, ...fetchedEntries];
if (allEntries.length < total && fetchedEntries.length > 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Silent pagination halt on missing total header

If the API response doesn't include a valid 'total' header, the pagination logic will fail silently. Line 32 sets total to 0 when the header is missing or invalid (Number(response.headers.get('total')) || 0). This causes the condition on line 35 (allEntries.length < total) to always be false (since any array length is >= 0), preventing recursive pagination even when more pages exist. The function will only return the first page of results instead of fetching all pages.

Fix in Cursor Fix in Web

Copy link
Contributor

@alexjoverm alexjoverm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dipankarmaikap! The code looks great to me. As we discussed, it's only missing an automated test to validate that pagination is applied automatically when results are more than what a page allows

maoberlehner
maoberlehner previously approved these changes Nov 6, 2025
Copy link
Contributor

@maoberlehner maoberlehner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (only test missing as @alexjoverm mentioned)

@maoberlehner maoberlehner dismissed their stale review November 6, 2025 08:33

waiting for test

if (!totalHeader || Number.isNaN(total)) {
// No valid 'total' header — assume not paginated, return current page only
return extractDataFunction(data);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Preserve Collected Items on Missing Header Error

When the 'total' header is missing or invalid, the function returns only the current page's data via extractDataFunction(data), completely ignoring the collectedItems parameter. This causes data loss if pagination was working on previous pages but the header is missing on a subsequent page. The function should return [...collectedItems, ...extractDataFunction(data)] to preserve previously collected items.

Fix in Cursor Fix in Web

Copy link
Contributor

@alexjoverm alexjoverm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dipankarmaikap LGTM! I added a suggestion for a small refactor, as I see per_page being unnecessarily reimplemented in a few places.

If you prefer to tackle it in separate PR, is also fine. Up to you if you want to merge it right away, or tackle it first

updated_at: '2021-08-09T12:00:00Z',
},
];
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: nice refactor!

const entries = await fetchDatasourceEntries(spaceId, ds.id);
return { ...ds, entries };
}) || [],
}),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Pagination missing in datasource search function

The fetchDatasource function doesn't use pagination when searching for a datasource by name. If the target datasource exists but isn't in the first page of results, it won't be found. This function should use fetchAllPages like fetchDatasources does to ensure all pages are searched.

Fix in Cursor Fix in Web

@dipankarmaikap dipankarmaikap merged commit e637a83 into main Nov 7, 2025
6 checks passed
@dipankarmaikap dipankarmaikap deleted the fix/pull-paginated-datasources branch November 7, 2025 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: Pulling datasources doesn't pull all of my datasources

3 participants