{
  "format": "CLARA-CATALOG-1",
  "templates": [
    {
      "id": "hello",
      "name": "Hello API",
      "description": "Start with a typed JSON endpoint.",
      "source": "route GET \"/api/hello\" -> hello;\nfn hello(req: Request): Response {\n  let name: String = query(req, \"name\", \"world\");\n  return json(200, {message: \"Hello, \" + name});\n}",
      "tests": [
        {
          "name": "Greeting",
          "method": "GET",
          "path": "/api/hello?name=Clara",
          "status": 200,
          "contains": "Hello, Clara"
        },
        {
          "name": "Missing route",
          "method": "GET",
          "path": "/api/missing",
          "status": 404
        }
      ]
    },
    {
      "id": "storage",
      "name": "Private data API",
      "description": "Authenticated writes and reads with persistent storage.",
      "source": "route POST \"/api/item\" -> save;\nroute GET \"/api/item\" auth -> read;\nfn save(req: Request): Response {\n  let value: String = bodyString(req, \"value\", \"\");\n  require(len(value) > 0 && len(value) <= 1000, \"Enter a value\");\n  storePut(\"item\", value);\n  return json(201, {ok: true});\n}\nfn read(req: Request): Response { return json(200, {value: storeGet(\"item\")}); }",
      "tests": [
        {
          "name": "Reject blank",
          "method": "POST",
          "path": "/api/item",
          "status": 422,
          "body": {
            "value": ""
          }
        },
        {
          "name": "Save value",
          "method": "POST",
          "path": "/api/item",
          "status": 201,
          "body": {
            "value": "sample"
          }
        },
        {
          "name": "Read saved value",
          "method": "GET",
          "path": "/api/item",
          "status": 200,
          "contains": "sample"
        }
      ]
    },
    {
      "id": "page",
      "name": "HTML page",
      "description": "Safe server-rendered templates with user input.",
      "source": "route GET \"/api/page\" -> page;\ntemplate \"page\" = \"<!doctype html><html><title>Welcome</title><h1>{{name}}</h1><p>Built with CLARA.</p></html>\";\nfn page(req: Request): Response { return render(200, \"page\", {name: query(req, \"name\", \"Visitor\")}); }",
      "tests": [
        {
          "name": "Render page",
          "method": "GET",
          "path": "/api/page",
          "status": 200,
          "contains": "Visitor"
        },
        {
          "name": "Escape input",
          "method": "GET",
          "path": "/api/page?name=%3Cscript%3E",
          "status": 200,
          "contains": "&lt;script&gt;"
        }
      ]
    },
    {
      "id": "compute",
      "name": "Compute API",
      "description": "Typed arithmetic with a WASM-compatible function.",
      "source": "route GET \"/api/square/:n\" -> squareApi;\nfn square(n: Int): Int { return n * n; }\nfn squareApi(req: Request): Response {\n  let n: Int = int(path(req, \"n\", \"0\"));\n  require(n >= 0 && n <= 1000, \"n must be 0..1000\");\n  return json(200, {result: square(n)});\n}",
      "tests": [
        {
          "name": "Square",
          "method": "GET",
          "path": "/api/square/12",
          "status": 200,
          "contains": "144"
        },
        {
          "name": "Limit input",
          "method": "GET",
          "path": "/api/square/1001",
          "status": 422
        }
      ]
    },
    {
      "id": "projects",
      "name": "CLARA Projects",
      "description": "A complete task-management API with revision-safe writes, typed validation, portable priority computation and safe HTML briefs.",
      "source": "// CLARA Projects: authenticated, revision-safe application routes.\nroute GET \"/api/tasks\" auth -> tasks;\nroute GET \"/api/tasks/:id\" auth -> task;\nroute PUT \"/api/tasks/:id\" auth -> saveTask;\nroute GET \"/api/brief/:id\" auth -> brief;\nroute GET \"/api/score/:input\" -> score;\ntemplate \"brief\" = \"<!doctype html><html lang='en'><meta charset='utf-8'><title>Project brief</title><main><h1>{{title}}</h1><p>{{description}}</p><p>Owner: {{owner}}</p><p>Status: {{status}}</p><p>Due: {{due}}</p></main></html>\";\nfn digit(c: String): Bool { return c == \"0\" || c == \"1\" || c == \"2\" || c == \"3\" || c == \"4\" || c == \"5\" || c == \"6\" || c == \"7\" || c == \"8\" || c == \"9\"; }\nfn validId(id: String): Bool {\n if len(id) != 32 { return false; }\n mut i: Int = 0;\n while i < 32 {\n  let c: String = id[i];\n  if !(digit(c) || (c == \"a\" || c == \"b\" || c == \"c\" || c == \"d\" || c == \"e\" || c == \"f\")) { return false; }\n  i = i + 1;\n }\n return true;\n}\nfn validDate(d: String): Bool {\n if d == \"\" { return true; }\n if len(d) != 10 || d[4] != \"-\" || d[7] != \"-\" { return false; }\n mut i: Int = 0;\n while i < 10 {\n  if i != 4 && i != 7 { if !digit(d[i]) { return false; } }\n  i = i + 1;\n }\n let year: Int = int(d[0]+d[1]+d[2]+d[3]);\n let month: Int = int(d[5]+d[6]);\n let day: Int = int(d[8]+d[9]);\n if month < 1 || month > 12 || day < 1 || year < 1 { return false; }\n mut maxDay: Int = 31;\n if month == 4 || month == 6 || month == 9 || month == 11 { maxDay = 30; }\n if month == 2 { maxDay = 28; if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) { maxDay = 29; } }\n return day <= maxDay;\n}\nfn priorityScore(x: Int): Int { return x * x + 2 * x; }\nfn score(req: Request): Response {\n let x: Int = int(path(req,\"input\",\"0\"));\n require(x >= 0 && x <= 50, \"Input must be 0..50\");\n return json(200,{input:x,score:priorityScore(x)});\n}\nfn tasks(req: Request): Response {\n let items: Array<Any> = storeList(\"desk/\");\n return json(200,{items:items,count:len(items)});\n}\nfn task(req: Request): Response {\n let id: String = path(req,\"id\",\"\");\n require(validId(id),\"Invalid identifier\");\n let item: Object = storeGetVersion(\"desk/\"+id);\n return json(200,item);\n}\nfn saveTask(req: Request): Response {\n let id: String = path(req,\"id\",\"\");\n let revision: Int = bodyInt(req,\"revision\",-1);\n let title: String = bodyString(req,\"title\",\"\");\n let description: String = bodyString(req,\"description\",\"\");\n let owner: String = bodyString(req,\"owner\",\"\");\n let due: String = bodyString(req,\"due\",\"\");\n let status: String = bodyString(req,\"status\",\"todo\");\n let priority: Int = bodyInt(req,\"priority\",2);\n let effort: Int = bodyInt(req,\"effort\",1);\n require(validId(id) && revision >= 0,\"Invalid identifier or revision\");\n require(len(title) > 0 && len(title) <= 100 && len(description) <= 1500 && len(owner) <= 60 && validDate(due),\"Invalid task fields\");\n require(status == \"todo\" || status == \"doing\" || status == \"done\" || status == \"archived\",\"Invalid status\");\n require(priority >= 1 && priority <= 3 && effort >= 1 && effort <= 10,\"Invalid priority or effort\");\n if revision == 0 { require(len(storeList(\"desk/\")) < 100,\"Workspace limit: 100 records including archived\"); }\n let item: Object = {title:title,description:description,owner:owner,due:due,status:status,priority:priority,effort:effort,updatedAt:now()};\n if !storeComparePut(\"desk/\"+id,revision,item) { return json(409,{error:\"Revision conflict; reload before editing\"}); }\n return json(200,{id:id,revision:revision+1,item:item});\n}\nfn brief(req: Request): Response {\n let id: String = path(req,\"id\",\"\");\n require(validId(id),\"Invalid identifier\");\n let item: Any = storeGet(\"desk/\"+id);\n if item == null { return json(404,{error:\"Task not found\"}); }\n return render(200,\"brief\",{title:item[\"title\"],description:item[\"description\"],owner:item[\"owner\"],due:item[\"due\"],status:item[\"status\"]});\n}\n",
      "tests": [
        {
          "name": "Empty workspace",
          "method": "GET",
          "path": "/api/tasks",
          "status": 200,
          "contains": "\"count\":0"
        },
        {
          "name": "Create task",
          "method": "PUT",
          "path": "/api/tasks/11111111111111111111111111111111",
          "status": 200,
          "body": {
            "revision": 0,
            "title": "<Review> launch",
            "description": "Prepare the release",
            "owner": "Team",
            "due": "2027-01-01",
            "status": "todo",
            "priority": 3,
            "effort": 4
          },
          "contains": "\"revision\":1"
        },
        {
          "name": "Read stored task",
          "method": "GET",
          "path": "/api/tasks/11111111111111111111111111111111",
          "status": 200,
          "contains": "<Review> launch"
        },
        {
          "name": "Reject stale write",
          "method": "PUT",
          "path": "/api/tasks/11111111111111111111111111111111",
          "status": 409,
          "body": {
            "revision": 0,
            "title": "<Review> launch",
            "description": "Prepare the release",
            "owner": "Team",
            "due": "2027-01-01",
            "status": "todo",
            "priority": 3,
            "effort": 4
          }
        },
        {
          "name": "Reject wrong type",
          "method": "PUT",
          "path": "/api/tasks/11111111111111111111111111111111",
          "status": 422,
          "body": {
            "revision": 1,
            "title": "<Review> launch",
            "description": "Prepare the release",
            "owner": "Team",
            "due": "2027-01-01",
            "status": "todo",
            "priority": "3",
            "effort": 4
          }
        },
        {
          "name": "Escape report",
          "method": "GET",
          "path": "/api/brief/11111111111111111111111111111111",
          "status": 200,
          "contains": "&lt;Review&gt; launch"
        },
        {
          "name": "Move to doing",
          "method": "PUT",
          "path": "/api/tasks/11111111111111111111111111111111",
          "status": 200,
          "body": {
            "revision": 1,
            "title": "<Review> launch",
            "description": "Prepare the release",
            "owner": "Team",
            "due": "2027-01-01",
            "status": "doing",
            "priority": 3,
            "effort": 4
          },
          "contains": "\"revision\":2"
        },
        {
          "name": "Score function",
          "method": "GET",
          "path": "/api/score/20",
          "status": 200,
          "contains": "\"score\":440"
        },
        {
          "name": "Bounded input",
          "method": "GET",
          "path": "/api/score/51",
          "status": 422
        },
        {
          "name": "Reject invalid identifier",
          "method": "GET",
          "path": "/api/tasks/zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
          "status": 422
        },
        {
          "name": "Reject invalid calendar date",
          "method": "PUT",
          "path": "/api/tasks/11111111111111111111111111111111",
          "status": 422,
          "body": {
            "revision": 2,
            "title": "<Review> launch",
            "description": "Prepare the release",
            "owner": "Team",
            "due": "2027-02-30",
            "status": "todo",
            "priority": 3,
            "effort": 4
          }
        }
      ]
    }
  ]
}
