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:
parent
57b58e9b2f
commit
527aeeeced
16 changed files with 5965 additions and 0 deletions
25
website/sandbox/contentful/src/contentful.ts
Normal file
25
website/sandbox/contentful/src/contentful.ts
Normal 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');
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue