swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
57 lines · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

import type { APIRoute } from "astro";
import { readdir, readFile } from "fs/promises";
import { join } from "path";
import yaml from "js-yaml";
import { logger } from "@/lib/logger";

const TEMPLATES_DIR = join(process.cwd(), "src/lib/templates/workflows");

export interface WorkflowTemplate {
    id: string;
    name: string;
    filename: string;
    content: string;
}

export const GET: APIRoute = async () => {
    try {
        const files = await readdir(TEMPLATES_DIR);
        const templates: WorkflowTemplate[] = [];

        for (const file of files) {
            if (!file.endsWith(".yml") && !file.endsWith(".yaml")) continue;

            const content = await readFile(join(TEMPLATES_DIR, file), "utf-8");
            let name = file;

            try {
                const parsed = yaml.load(content) as any;
                if (parsed && parsed.name) {
                    name = parsed.name;
                }
            } catch (e) {
                // Ignore parsing error, use filename
            }

            templates.push({
                id: file.replace(/\.(yml|yaml)$/, ""),
                name,
                filename: file,
                content
            });
        }

        return new Response(JSON.stringify(templates), {
            headers: { "Content-Type": "application/json" }
        });

    } catch (error) {
        logger.error({ err: error }, "Failed to list workflow templates");
        return new Response(JSON.stringify({ error: "Failed to load templates" }), {
            status: 500,
            headers: { "Content-Type": "application/json" }
        });
    }
};