import { PbContext } from "@webiny/api-page-builder/types" ; import { loadDataSources } from "./loadDataSources" ; import { DynamicPage } from "~/types" ; export const loadDynamicPage = async ( args , context ) => { const [ pages ] = await context . pageBuilder . listPublishedPages < DynamicPage > ( { where : { dynamic : true } , limit : 100 } ) ; try { for ( let i = 0 ; i < pages . length ; i ++ ) { const page = pages [ i ] ; let pattern = page . path as any ; const placeholders = Array . from ( pattern . matchAll ( /\{([a-zA-Z]+)\}/g ) ) ; for ( let j = 0 ; j < placeholders . length ; j ++ ) { const [ find , replace ] = placeholders [ j ] ; pattern = pattern . replace ( find , `(?<${ replace }>(.+))` ) ; } const match = args . path . match ( new RegExp ( pattern ) ) ; if ( match ) { const fullPage = await context . pageBuilder . getPublishedPageById < DynamicPage > ( { id : page . id } ) ; const dataSources = await loadDataSources ( fullPage . settings . dataSources , { path : match . groups } , context ) ; fullPage . id = JSON . stringify ( match . groups ) ; fullPage . dataSources = dataSources ; return fullPage ; } } } catch ( err ) { console . log ( "Error loading dynamic page" , err . message ) ; throw err ; } } ;