Skip to content

Instantly share code, notes, and snippets.

@marcolink
Created October 7, 2021 09:30
Show Gist options
  • Save marcolink/53e0b286d79b75c9c5979792b310b857 to your computer and use it in GitHub Desktop.
Save marcolink/53e0b286d79b75c9c5979792b310b857 to your computer and use it in GitHub Desktop.
fetch contentful collection
import { CollectionProp } from 'contentful-management/types';
type CollectionFuncResponse<TItem> = Promise<CollectionProp<TItem>>;
type CollectionFunc<TItem> = (skip: number, limit: number) => CollectionFuncResponse<TItem>;
export async function getAllCollectionItems<TItem>(
endpoint: CollectionFunc<TItem>,
skip = 0,
limit = 100
): Promise<Array<TItem>> {
const getResult = async (currentSkip: number) => {
const result = await endpoint(currentSkip, limit);
const length = currentSkip + result.items.length;
if (result.total > length) {
return [...result.items, ...(await getResult(length))];
} else {
return result.items;
}
};
return getResult(skip);
}
@marcolink
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment