106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import puppeteer from "puppeteer";
|
|
|
|
export async function GET(request: Request) {
|
|
// Check if running in Electron/static export mode
|
|
// This route requires server-side Puppeteer execution which is not available in static exports
|
|
// - In Electron builds (BUILD_TARGET=electron): Return stub response
|
|
// - In development mode: Full functionality with Puppeteer
|
|
// - In Docker/server deployments: Full functionality with Puppeteer
|
|
const isStaticMode = process.env.BUILD_TARGET === 'electron' ||
|
|
process.env.IS_STATIC_EXPORT === 'true' ||
|
|
process.env.NEXT_RUNTIME === 'edge';
|
|
|
|
if (isStaticMode) {
|
|
return NextResponse.json(
|
|
{
|
|
error: "This API route requires a server runtime and is not available in static export mode",
|
|
message: "This functionality is only available in development mode or Docker deployment with Puppeteer support"
|
|
},
|
|
{ status: 501 }
|
|
);
|
|
}
|
|
|
|
// Full functionality for development/Docker
|
|
const { searchParams } = new URL(request.url);
|
|
const groupName = searchParams.get("group");
|
|
|
|
if (!groupName) {
|
|
return NextResponse.json({ error: "Missing group name" }, { status: 400 });
|
|
}
|
|
|
|
const schemaPageUrl = `http://localhost/schema?group=${encodeURIComponent(
|
|
groupName
|
|
)}`;
|
|
|
|
let browser;
|
|
try {
|
|
browser = await puppeteer.launch({
|
|
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH,
|
|
headless: true,
|
|
args: [
|
|
"--no-sandbox",
|
|
"--disable-setuid-sandbox",
|
|
"--disable-dev-shm-usage",
|
|
"--disable-gpu",
|
|
"--disable-web-security",
|
|
"--disable-background-timer-throttling",
|
|
"--disable-backgrounding-occluded-windows",
|
|
"--disable-renderer-backgrounding",
|
|
"--disable-features=TranslateUI",
|
|
"--disable-ipc-flooding-protection",
|
|
],
|
|
});
|
|
const page = await browser.newPage();
|
|
await page.setViewport({ width: 1280, height: 720 });
|
|
page.setDefaultNavigationTimeout(300000);
|
|
page.setDefaultTimeout(300000);
|
|
await page.goto(schemaPageUrl, {
|
|
waitUntil: "networkidle0",
|
|
timeout: 300000,
|
|
});
|
|
|
|
await page.waitForSelector("[data-layouts]", { timeout: 300000 });
|
|
await page.waitForSelector("[data-settings]", { timeout: 300000 });
|
|
|
|
const { dataLayouts, dataGroupSettings } = await page.$eval(
|
|
"[data-layouts]",
|
|
(el) => ({
|
|
dataLayouts: el.getAttribute("data-layouts"),
|
|
dataGroupSettings: el.getAttribute("data-settings"),
|
|
})
|
|
);
|
|
|
|
let slides, groupSettings;
|
|
try {
|
|
slides = JSON.parse(dataLayouts || "[]");
|
|
} catch (e) {
|
|
slides = [];
|
|
}
|
|
try {
|
|
groupSettings = JSON.parse(dataGroupSettings || "null");
|
|
} catch (e) {
|
|
groupSettings = null;
|
|
}
|
|
|
|
const response = {
|
|
name: groupName,
|
|
ordered: groupSettings?.ordered ?? false,
|
|
slides: slides.map((slide: any) => ({
|
|
id: slide.id,
|
|
name: slide.name,
|
|
description: slide.description,
|
|
json_schema: slide.json_schema,
|
|
})),
|
|
};
|
|
|
|
return NextResponse.json(response);
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch or parse client page" },
|
|
{ status: 500 }
|
|
);
|
|
} finally {
|
|
if (browser) await browser.close();
|
|
}
|
|
}
|