mermaid-server/alternative_api_call.js

42 lines
1.4 KiB
JavaScript
Raw Normal View History

2025-05-24 11:45:48 +02:00
// Alternative API call using GET with URL encoding (like the healthcheck)
export const code = async (inputs) => {
const { diagram, format } = inputs;
if (!diagram || !diagram.trim()) {
throw new Error("No Mermaid input provided");
}
// Normalize line endings
const normalizedDiagram = diagram
.replace(/\r\n/g, '\n') // Convert Windows line endings
.replace(/\r/g, '\n') // Convert old Mac line endings
.trim(); // Remove leading/trailing whitespace
// URL encode the diagram (this preserves newlines as %0A)
const encodedDiagram = encodeURIComponent(normalizedDiagram);
// Log for debugging
console.log("Original diagram:");
console.log(JSON.stringify(normalizedDiagram));
console.log("URL encoded diagram:");
console.log(encodedDiagram);
// Use GET with data parameter (like the healthcheck does)
const url = `https://diagrams.starbit.cloud/generate?type=${encodeURIComponent(format)}&data=${encodedDiagram}`;
const resp = await fetch(url, {
method: "GET",
});
if (!resp.ok) {
const txt = await resp.text();
throw new Error(`Mermaid-server error ${resp.status}: ${txt}`);
}
const buffer = Buffer.from(await resp.arrayBuffer());
const mime = format === "png" ? "image/png" : "image/svg+xml";
const file = `data:${mime};base64,${buffer.toString("base64")}`;
return { file };
};