1import { basicAuthMiddleware } from "./auth.ts";
2import { handleApiRequest } from "./api/index.ts";
3import { getRequests } from "./api/requests.ts";
4import { getUserSummary } from "./api/user-summary.ts";
5import { getInferenceCalls } from "./api/inference-calls.ts";
6import { renderDashboard } from "./views/dashboard.ts";
7import { renderRequests } from "./views/requests.ts";
22const path = url.pathname;
2324// Handle API requests
25if (path.startsWith("/api/")) {
26return handleApiRequest(req);
27}
28
45/**
6* Handle API requests
7*/
8export async function handleApiRequest(req: Request): Promise<Response> {
9const url = new URL(req.url);
10const path = url.pathname.replace("/api/", "");
11
12try {
13// Route to the appropriate API handler
14if (path === "requests") {
15const usageId = url.searchParams.get("usage_id");
59}
60} catch (error) {
61console.error("API error:", error);
62return new Response(JSON.stringify({ error: error.message }), {
63status: 500,
38<ol>
39<li>
40Login with your Val Town API token (with projects:read,
41projects:write, user:read permissions)
42</li>
77</div>
78<h3>Cost Tracking</h3>
79<p>See estimated API usage costs for each interaction</p>
80</div>
81</section>
86<ul>
87<li>React frontend with TypeScript</li>
88<li>Hono API server backend</li>
89<li>Web Audio API for sound notifications</li>
90<li>AI SDK for Claude integration</li>
91</ul>
92<p>
93The application proxies requests to the Anthropic API and Val Town
94API, allowing Claude to view and edit your project files directly.
95</p>
96<div>
Townie-06.cursorrules10 matches
13- Generate code in TypeScript or TSX
14- Add appropriate TypeScript types and interfaces for all data structures
15- Prefer official SDKs or libraries than writing API calls directly
16- Ask the user to supply API or library documentation if you are at all unsure about it
17- **Never bake in secrets into the code** - always use environment variables
18- Include comments explaining complex logic (avoid commenting obvious operations)
23### 1. HTTP Trigger
2425- Create web APIs and endpoints
26- Handle HTTP requests and responses
27- Example structure:
173However, it's *extremely importing* to note that `parseProject` and other Standard Library utilities ONLY RUN ON THE SERVER.
174If you need access to this data on the client, run it in the server and pass it to the client by splicing it into the HTML page
175or by making an API request for it.
176177## Val Town Platform Specifics
181- **AI Image:** To inline generate an AI image use: `<img src="https://maxm-imggenurl.web.val.run/the-description-of-your-image" />`
182- **Storage:** DO NOT use the Deno KV module for storage
183- **Browser APIs:** DO NOT use the `alert()`, `prompt()`, or `confirm()` methods
184- **Weather Data:** Use open-meteo for weather data (doesn't require API keys) unless otherwise specified
185- **View Source:** Add a view source link by importing & using `import.meta.url.replace("ems.sh", "val.town)"` (or passing this data to the client) and include `target="_top"` attribute
186- **Error Debugging:** Add `<script src="https://esm.town/v/std/catch"></script>` to HTML to capture client-side errors
187- **Error Handling:** Only use try...catch when there's a clear local resolution; Avoid catches that merely log or return 500s. Let errors bubble up with full context
188- **Environment Variables:** Use `Deno.env.get('keyname')` when you need to, but generally prefer APIs that don't require keys
189- **Imports:** Use `https://esm.sh` for npm and Deno dependencies to ensure compatibility on server and browser
190- **Storage Strategy:** Only use backend storage if explicitly required; prefer simple static client-side sites
224### Backend (Hono) Best Practices
225226- Hono is the recommended API framework
227- Main entry point should be `backend/index.ts`
228- **Static asset serving:** Use the utility functions to read and serve project files:
248});
249```
250- Create RESTful API routes for CRUD operations
251- Always include this snippet at the top-level Hono app to re-throwing errors to see full stack traces:
252```ts
285- For files in the project, use `readFile` helpers
2862875. **API Design:**
288- `fetch` handler is the entry point for HTTP vals
289- Run the Hono app with `export default app.fetch // This is the entry point for HTTP vals`
untitled-3074README.md1 match
5## Structure
67- `types.ts` - TypeScript interfaces for data models and API responses
89## Usage
untitled-3074README.md7 matches
5## Structure
67- `index.ts` - Main entry point for the HTTP API
8- `database/` - Database schema and queries
9- `migrations.ts` - Database table definitions
10- `queries.ts` - Functions for interacting with the database
1112## API Endpoints
1314### Jobs
1516- `GET /api/jobs` - Get all job postings
17- `POST /api/jobs` - Create a new job posting
1819### Chat
2021- `GET /api/chat` - Get chat messages
22- `POST /api/chat` - Create a new chat message
2324## Technologies
2526- Hono - API framework
27- SQLite - Database for storing job postings and chat messages
untitled-3074index.ts8 matches
33});
3435// API Routes
36const api = new Hono();
3738// Job posting endpoints
39api.get("/jobs", async (c) => {
40try {
41const jobs = await getAllJobs();
47});
4849api.post("/jobs", async (c) => {
50try {
51const body = await c.req.json();
6869// Chat message endpoints
70api.get("/chat", async (c) => {
71try {
72const messages = await getChatMessages();
78});
7980api.post("/chat", async (c) => {
81try {
82const body = await c.req.json();
95});
9697// Mount API routes
98app.route("/api", api);
99100// Serve static files
untitled-3074index.js4 matches
113
114try {
115const response = await fetch('/api/jobs', {
116method: 'POST',
117headers: {
148if (message && currentUsername) {
149try {
150const response = await fetch('/api/chat', {
151method: 'POST',
152headers: {
264if (chatSidebar.classList.contains('translate-x-0')) {
265try {
266const response = await fetch('/api/chat');
267const result = await response.json();
268
291async function fetchJobs() {
292try {
293const response = await fetch('/api/jobs');
294const result = await response.json();
295
untitled-3074types.ts2 matches
18}
1920// API response types
21export interface ApiResponse<T> {
22success: boolean;
23data?: T;
untitled-3074README.md1 match
15## Project Structure
1617- `/backend/` - Hono API server and database logic
18- `/frontend/` - HTML, CSS, and client-side JavaScript
19- `/shared/` - Types and utilities shared between frontend and backend