Add sandbox project using Contentful CMS

I used the boilerplate/typescript project as a starting point. This project
fetches and renders books that I'm defining in a Contentful CMS that I created.
This commit is contained in:
William Carroll 2020-03-24 13:27:30 +00:00
parent 57b58e9b2f
commit 527aeeeced
16 changed files with 5965 additions and 0 deletions

View file

@ -0,0 +1,25 @@
import { createClient } from "contentful";
import type { ContentfulClientApi } from "contentful";
const space = process.env.CONTENTFUL_SPACE_ID;
const accessToken = process.env.CONTENTFUL_ACCESS_TOKEN;
let client: ContentfulClientApi;
// Idempotent way to get a reference to the Contentful client.
export const getClient = (): ContentfulClientApi => {
if (typeof client !== 'undefined') {
return client;
} else {
if (typeof space === 'string' && typeof accessToken === 'string') {
let client = createClient({
space,
accessToken,
});
return client;
} else {
throw new Error('Please set CONTENTFUL_SPACE_ID and CONTENTFUL_ACCESS_TOKEN');
}
}
}