Pahse 5
This commit is contained in:
92
sites/src/loaders/loadSiteDefinition.ts
Normal file
92
sites/src/loaders/loadSiteDefinition.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Site Definition Loader
|
||||
* Phase 5: Sites Renderer & Publishing
|
||||
*
|
||||
* Loads site definitions from the filesystem or API.
|
||||
*/
|
||||
import axios from 'axios';
|
||||
import type { SiteDefinition } from '../types';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || 'https://api.igny8.com/api';
|
||||
const SITES_DATA_PATH = import.meta.env.SITES_DATA_PATH || '/sites';
|
||||
|
||||
/**
|
||||
* Load site definition by site ID.
|
||||
* First tries to load from filesystem (deployed sites),
|
||||
* then falls back to API.
|
||||
*/
|
||||
export async function loadSiteDefinition(siteId: string): Promise<SiteDefinition> {
|
||||
// Try filesystem first (for deployed sites)
|
||||
try {
|
||||
const fsPath = `${SITES_DATA_PATH}/clients/${siteId}/latest/site.json`;
|
||||
const response = await fetch(fsPath);
|
||||
if (response.ok) {
|
||||
const definition = await response.json();
|
||||
return definition;
|
||||
}
|
||||
} catch (error) {
|
||||
// Filesystem load failed, try API
|
||||
console.warn('Failed to load from filesystem, trying API:', error);
|
||||
}
|
||||
|
||||
// Fallback to API
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/v1/site-builder/blueprints/${siteId}/`);
|
||||
const blueprint = response.data;
|
||||
|
||||
// Transform blueprint to site definition format
|
||||
return transformBlueprintToSiteDefinition(blueprint);
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
throw new Error(`Failed to load site: ${error.message}`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform SiteBlueprint to SiteDefinition format.
|
||||
*/
|
||||
function transformBlueprintToSiteDefinition(blueprint: any): SiteDefinition {
|
||||
return {
|
||||
id: blueprint.id,
|
||||
name: blueprint.name,
|
||||
description: blueprint.description,
|
||||
version: blueprint.version,
|
||||
layout: blueprint.structure_json?.layout || 'default',
|
||||
theme: blueprint.structure_json?.theme || {},
|
||||
navigation: blueprint.structure_json?.navigation || [],
|
||||
pages: blueprint.pages?.map((page: any) => ({
|
||||
id: page.id,
|
||||
slug: page.slug,
|
||||
title: page.title,
|
||||
type: page.type,
|
||||
blocks: page.blocks_json || [],
|
||||
status: page.status,
|
||||
})) || [],
|
||||
config: blueprint.config_json || {},
|
||||
created_at: blueprint.created_at,
|
||||
updated_at: blueprint.updated_at,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Load site definition from a specific version.
|
||||
*/
|
||||
export async function loadSiteDefinitionByVersion(
|
||||
siteId: string,
|
||||
version: number
|
||||
): Promise<SiteDefinition> {
|
||||
try {
|
||||
const fsPath = `${SITES_DATA_PATH}/clients/${siteId}/v${version}/site.json`;
|
||||
const response = await fetch(fsPath);
|
||||
if (response.ok) {
|
||||
const definition = await response.json();
|
||||
return definition;
|
||||
}
|
||||
throw new Error(`Version ${version} not found`);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to load site version ${version}: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user