import { resolvePageRoute , setStoreItem , TCmsConfig , TCromwellPageCoreProps , TDefaultPageName , TGetStaticProps , TPageCmsProps , TPageConfig , TPageInfo , TStaticPageContext , TThemeConfig , } from '@cromwell/core' ; import { getRestApiClient } from '@cromwell/core-frontend' ; import { GetStaticPropsResult } from 'next' ; import { getThemeStaticProps } from '../helpers/getThemeStaticProps' ; import { pluginsDataFetcher , TPluginsSettings } from '../helpers/pluginsDataFetcher' ; import { removeUndefined } from '../helpers/removeUndefined' ; const wrapGetProps = ( pageName , originalGet , getType ) : TGetStaticProps < TCromwellPageCoreProps > => { if ( pageName === '/' ) pageName = 'index' ; if ( pageName . startsWith ( '/' ) ) pageName = pageName . slice ( 1 , pageName . length ) ; const getStaticWrapper = async ( context ) => { const pageConfigName = ( pageName === 'pages/[slug]' && context ?. params ?. slug ) ? `pages/${ context . params . slug }` : pageName ; let rendererData = { } ; try { rendererData = ( await getRestApiClient ( ) . getRendererRage ( pageConfigName ) ) ?? { } ; } catch ( e ) { console . error ( e ) ; } const { pageConfig , themeConfig , cmsSettings , themeCustomConfig , pluginsSettings , userConfig } = rendererData ; if ( themeConfig ?. defaultPages ) setStoreItem ( 'defaultPages' , themeConfig ?. defaultPages ) ; const resolvedPageRoute = resolvePageRoute ( pageConfigName , { slug : context ?. params ?. slug as string } ) ; if ( themeConfig ) { themeConfig . palette = Object . assign ( { } , themeConfig . palette , userConfig ?. palette ) ; } context . pageConfig = rendererData . pageConfig ; context . themeConfig = rendererData . themeConfig ; context . userConfig = rendererData . userConfig ; context . cmsSettings = rendererData . cmsSettings ; context . themeCustomConfig = rendererData . themeCustomConfig ; context . pagesInfo = rendererData . pagesInfo ; const [ childStaticProps , plugins ] = await Promise . all ( [ getThemeStaticProps ( pageName , originalGet , context ) , pluginsDataFetcher ( pageConfigName , context , pluginsSettings ) , ] ) ; const pluginsNextProps = Object . assign ( { } , ... Object . values ( plugins ) . map ( plugin => plugin . nextProps ) . filter ( Boolean ) ) ; if ( childStaticProps && ! childStaticProps . props ) childStaticProps . props = { } ; const cmsProps = { plugins , pageConfig , cmsSettings , themeCustomConfig , pageConfigName , resolvedPageRoute , slug : context ?. params ?. slug , defaultPages : themeConfig ?. defaultPages , themeHeadHtml : themeConfig ?. headHtml , themeFooterHtml : themeConfig ?. footerHtml , palette : themeConfig ?. palette , } ; const pageProps = { ... childStaticProps , ... pluginsNextProps , props : { ... ( childStaticProps . props ?? { } ) , cmsProps : cmsProps } } ; if ( getType === 'getStaticProps' ) { pageProps . revalidate = childStaticProps ?. revalidate ?? 60 ; } return removeUndefined ( pageProps ) ; } ; return getStaticWrapper ; } ; export const wrapGetStaticProps = ( addExport , pageName , pageComponents ) => { if ( pageComponents . getInitialProps ) return ; if ( pageComponents . getServerSideProps ) return ; addExport ( 'getStaticProps' , wrapGetProps ( pageName , pageComponents . getStaticProps , 'getStaticProps' ) ) ; } ; export const wrapGetInitialProps = ( addExport , pageName , pageComponents ) => { if ( pageComponents . getInitialProps ) addExport ( 'getInitialProps' , wrapGetProps ( pageName , pageComponents . getInitialProps , 'getInitialProps' ) ) ; } ; export const wrapGetServerSideProps = ( addExport , pageName , pageComponents ) => { if ( pageComponents . getServerSideProps ) addExport ( 'getServerSideProps' , wrapGetProps ( pageName , pageComponents . getServerSideProps , 'getServerSideProps' ) ) ; } ; export const wrapGetStaticPaths = ( addExport , pageName , pageComponents ) => { if ( pageComponents . getStaticPaths ) addExport ( 'getStaticPaths' , pageComponents . getStaticPaths ) ; } ; export const createGetServerSideProps = ( pageName , comps ) => { return wrapGetProps ( pageName , comps . getServerSideProps , 'getServerSideProps' ) ; } ; export const createGetInitialProps = ( pageName , comps ) => { return wrapGetProps ( pageName , comps . getInitialProps , 'getInitialProps' ) ; } ; export const createGetStaticProps = ( pageName , comps ) => { return wrapGetProps ( pageName , comps . getStaticProps , 'getStaticProps' ) ; } ; export const createGetStaticPaths = ( pageName , comps ) => { return comps . getStaticPaths ; } ;