import * as Express from 'express' ; import * as Types from '@meetalva/types' ; import { PatternLibrary } from '@meetalva/model' ; import * as ModelTree from '@meetalva/model-tree' ; import { ServerContext } from './context' ; import * as PreviewDocument from '../../preview-document' ; export function previewRouteFactory ( server ) { return async function previewRoute ( req , res ) : Promise < void > { res . type ( 'html' ) ; if ( typeof req . params . id !== 'string' ) { res . sendStatus ( 404 ) ; return ; } const project = await server . dataHost . getProject ( req . params . id ) ; if ( ! project ) { res . sendStatus ( 404 ) ; return ; } project . sync ( server . sender , ModelTree ) ; const userLibraries = project . getPatternLibraries ( ) ; const script = ( lib ) => `<script src="/project/${ project . getId ( ) }/library/${ lib . getId ( ) }" data-bundle="${ lib . getBundleId ( ) }"></script>` ; res . send ( PreviewDocument . previewDocument ( { data : project . toJSON ( ) , transferType : Types . PreviewTransferType . Inline , scripts : userLibraries . map ( script ) } ) ) ; } ; }