| | import agility from "@agility/content-fetch"; |
| | import { CMS_LANG, CMS_CHANNEL } from "./constants"; |
| | import { asyncForEach } from "./utils"; |
| | export { validatePreview } from "./preview"; |
| | import { normalizePosts } from "./normalize"; |
| | import { requireComponentDependencyByName } from "./dependencies"; |
| |
|
| | |
| | const liveClient = agility.getApi({ |
| | guid: process.env.AGILITY_CMS_GUID, |
| | apiKey: process.env.AGILITY_CMS_API_FETCH_KEY, |
| | }); |
| |
|
| | |
| | const previewClient = agility.getApi({ |
| | guid: process.env.AGILITY_CMS_GUID, |
| | apiKey: process.env.AGILITY_CMS_API_PREVIEW_KEY, |
| | isPreview: true, |
| | }); |
| |
|
| | export const getClient = (preview = false) => |
| | preview ? previewClient : liveClient; |
| |
|
| | |
| | export class APIClient { |
| | public preview: any; |
| | public client: any; |
| |
|
| | constructor({ preview = false }) { |
| | this.preview = preview; |
| | this.client = getClient(preview); |
| | } |
| |
|
| | async getAllPosts(take) { |
| | const data = await this.client.getContentList({ |
| | referenceName: `posts`, |
| | languageCode: CMS_LANG, |
| | contentLinkDepth: 1, |
| | take: take, |
| | }); |
| |
|
| | return data.items; |
| | } |
| |
|
| | async getLatestPost() { |
| | const data = await this.getAllPosts(1); |
| | const normalizedPosts = normalizePosts(data); |
| |
|
| | return normalizedPosts[0] || null; |
| | } |
| |
|
| | async getPostDetails({ contentID, preview }) { |
| | const post = await this.client.getContentItem({ |
| | contentID, |
| | languageCode: CMS_LANG, |
| | contentLinkDepth: 1, |
| | }); |
| | const normalizedPost = normalizePosts([post])[0]; |
| |
|
| | return normalizedPost; |
| | } |
| |
|
| | async getPostsForMoreStories({ postToExcludeContentID }) { |
| | let allPosts = await this.getAllPosts(5); |
| |
|
| | |
| | if (postToExcludeContentID < 0) { |
| | allPosts.shift(); |
| | } |
| |
|
| | const postsLessThisPost = allPosts.filter((p) => { |
| | return p.contentID !== postToExcludeContentID; |
| | }); |
| |
|
| | const normalizedMorePosts = normalizePosts(postsLessThisPost); |
| | return normalizedMorePosts; |
| | } |
| | } |
| |
|
| | export async function getAgilityPaths() { |
| | console.log(`Agility CMS => Fetching sitemap for getAgilityPaths...`); |
| |
|
| | const sitemapFlat = await getClient().getSitemapFlat({ |
| | channelName: CMS_CHANNEL, |
| | languageCode: CMS_LANG, |
| | }); |
| |
|
| | |
| | |
| | const paths = Object.keys(sitemapFlat) |
| | .filter((s) => sitemapFlat[s].isFolder !== true) |
| | .map((s) => { |
| | return s; |
| | }); |
| |
|
| | return paths; |
| | } |
| |
|
| | export async function getAgilityPageProps({ params, preview }) { |
| | |
| | const client = getClient(preview); |
| |
|
| | let path = "/"; |
| | if (params) { |
| | |
| | path = ""; |
| | params.slug.forEach((slug) => { |
| | path += "/" + slug; |
| | }); |
| | } |
| |
|
| | console.log(`Agility CMS => Getting page props for '${path}'...`); |
| |
|
| | |
| | const sitemap = await client.getSitemapFlat({ |
| | channelName: CMS_CHANNEL, |
| | languageCode: CMS_LANG, |
| | }); |
| |
|
| | let pageInSitemap = sitemap[path]; |
| | let page = null; |
| |
|
| | if (path === "/") { |
| | let firstPagePathInSitemap = Object.keys(sitemap)[0]; |
| | pageInSitemap = sitemap[firstPagePathInSitemap]; |
| | } |
| |
|
| | if (pageInSitemap) { |
| | |
| | page = await client.getPage({ |
| | pageID: pageInSitemap.pageID, |
| | languageCode: CMS_LANG, |
| | contentLinkDepth: 1, |
| | }); |
| | } else { |
| | |
| | console.error("page [" + path + "] not found in sitemap."); |
| | return; |
| | } |
| |
|
| | if (!page) { |
| | console.error("page [" + path + "] not found in getpage method."); |
| | return; |
| | } |
| |
|
| | |
| | let pageTemplateName = page.templateName.replace(/[^0-9a-zA-Z]/g, ""); |
| |
|
| | |
| | await asyncForEach(Object.keys(page.zones), async (zoneName) => { |
| | let modules = []; |
| |
|
| | |
| | const modulesForThisContentZone = page.zones[zoneName]; |
| |
|
| | |
| | await asyncForEach(modulesForThisContentZone, async (moduleItem) => { |
| | let ModuleComponentToRender = requireComponentDependencyByName( |
| | moduleItem.module, |
| | ); |
| |
|
| | if (ModuleComponentToRender) { |
| | |
| | let moduleData = null; |
| |
|
| | if (ModuleComponentToRender.getCustomInitialProps) { |
| | |
| | console.log( |
| | `Agility CMS => Fetching additional data via getCustomInitialProps for ${moduleItem.module}...`, |
| | ); |
| | moduleData = await ModuleComponentToRender.getCustomInitialProps({ |
| | client: new APIClient({ preview }), |
| | item: moduleItem.item, |
| | languageCode: CMS_LANG, |
| | channelName: CMS_CHANNEL, |
| | pageInSitemap: pageInSitemap, |
| | }); |
| | } |
| |
|
| | |
| | if (moduleData != null) { |
| | moduleItem.item = moduleData; |
| | } |
| |
|
| | modules.push({ |
| | moduleName: moduleItem.module, |
| | item: moduleItem.item, |
| | }); |
| | } else { |
| | console.error( |
| | `No react component found for the module "${moduleItem.module}". Cannot render module.`, |
| | ); |
| | } |
| | }); |
| |
|
| | |
| | page.zones[zoneName] = modules; |
| | }); |
| |
|
| | return { |
| | sitemapNode: pageInSitemap, |
| | page: page, |
| | pageTemplateName: pageTemplateName, |
| | languageCode: CMS_LANG, |
| | channelName: CMS_CHANNEL, |
| | }; |
| | } |
| |
|