contentful / contentful-management.js

JavaScript library for Contentful's Management API (node & browser)
https://contentful.github.io/contentful-management.js
MIT License
266 stars 98 forks source link

feat: add `fetchAll` convenience method on top of paginated endpoints #2365

Closed anho closed 1 month ago

anho commented 1 month ago

Summary

We plastered a few versions of this behaviour in Contentful internal packages, so clearly there is a need for it. This is why we want to start having a single source of truth for it.

Description

The introduced fetchAll method can handle offset-based and cursor-based endpoints by inspecting the initial response.

We have one caveat though: As we have inconsistent ways of handling how we pass down optional query parameters (sometimes as distinct query property, sometimes mingled with mandatory properties). I opted for the more explicit version of using a distinct property. In case there is a need downstream the endpoint method that can't be handled automatically simply need to be wrapped.

An example for the raw.get method looks like this:

const items = await fetchAll(({ query }) => cmaClient.raw.get<CollectionProp<any>>(url, { params: query }), {})