fetchPages

The fetchPages function is useful when you want to retrieve all your pages from outside the React context (where you could use the usePages hook instead).

In particular, this comes in handy to retrieve all the pages during the build process of a static website. Indeed, this is the method used in our Gatsby and Next.js starter projects.

Signature (simplified)

const fetchPages = async (
apiKey: string,
options: { type?: string
types?: string[]
tag?: string
usePagination?: boolean
page?: number
pageSize?: number
sort?: string }
): Promise<types.PageFromList[] | types.PagesFromListWithPagination>

Really the TypeScript signature is more complex, because the returned type is an array of PageFromList only when usePagination is true and an object of type PagesFromListWithPagination when usePagination is false.

PropertyDefinition
apiKeyApi Key of your React Bricks app (a string).
optionsOptional object to filter the pages by type or tag (see below).

Options

The options object has the following shape

{
type?: string
types?: string[]
tag?: string
usePagination?: boolean
page?: number
pageSize?: number
sort?: string
}
PropertyDefinition
typeOptional string to return only the pages with the specified page type.
typesOptional array of strings to return only the pages with one of the specified page types.
tagOptional string to return only the pages with the specified tag.
usePaginationIf true, it will consider the page and pageSize parameters and it will return paginated results
pageThe page number, in case of pagination
pageSizeThe page size, in case of pagination
sortSort parameter: currently it accepts only createdAt,-createdAt,publishedAt,-publishedAt

Return value

fetchPages returns a promise which resolves to.

To retrieve the content of each page, you can use the fetchPage function.

Usage example

fetchPages('API_KEY', { type: 'blogPost', tag: 'react' }).then((data) => {
console.log(data)
})