-
Get preview api token and spaceId from Contentful.
-
Install this repository into your vite project
npm install github:DerDeathraven/vite-plugin-contentful-ts-type-generator
- add the plugin to your vite plugin array
- watch as a file is created containing all types for your cms additionally an enum with all Contentful id is created.
use this function to ensure a typesafe contentful experience note that this requires to be called with the ContentfulType enum
export async function fetchByContentType<T extends ContentfulType>(
entry: T,
options?: Object
): Promise<Entry<ContentfulEntries<T>>[]> {
try {
const client = await getContentfulClient();
const response = await client.getEntries<ContentfulEntries<T>>({
content_type: entry,
...(options || {}),
});
return response.items;
} catch (e) {
console.error("Could not fetch contentful entries of the content type.", e);
return [];
}
}