Created
October 7, 2021 09:30
-
-
Save marcolink/53e0b286d79b75c9c5979792b310b857 to your computer and use it in GitHub Desktop.
fetch contentful collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/marcolink/contentful-collection