Public
Like
like-anything
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v17View latest version
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Val Town project that implements a cross-origin likes tracking API with the following structure:
- Val Town Platform: Built specifically for Val Town's serverless environment using Deno runtime
- Backend: Hono web framework with SQLite database for likes tracking across multiple websites
- API Service: RESTful endpoints with CORS support for cross-origin requests
- Entry Point:
index.tsexportsapp.fetchas the HTTP handler
- Uses Val Town's hosted SQLite service (
stevekrouse/sqlite) - Table versioning pattern:
website_likes_v1(increment version when changing schema) - Core functions:
initDatabase(),recordLike(),getAllLikes(),getLikeCount(),getTotalLikes() - Database tracks likes by website and optional page URL
- Database initializes automatically on startup
- Self-contained HTML documentation page
- Shows API endpoints and usage examples
- No longer a tracking frontend, just documentation
- Hono app with error unwrapping for debugging and CORS support
- Routes:
/(API docs),POST /api/like(record like),GET /api/likes/:website(get count),GET /api/likes(all data) - Accepts JSON payloads with website and optional pageUrl
- Returns updated like counts for real-time UI updates
# Run/test the application locally deno run --allow-net --allow-read index.ts # Lint code deno lint # Format code deno fmt # Type checking deno check index.ts
When modifying the database schema, increment the table name (e.g.,
website_likes_v1 → website_likes_v2) rather than using ALTER TABLE
operations.
The API is designed for cross-origin requests. Example usage from any website:
fetch("https://your-val-url.val.run/api/like", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
website: "mysite.com",
pageUrl: window.location.pathname,
}),
})
.then((res) => res.json())
.then((data) => console.log("New like count:", data.likeCount));
Uses Val Town utility functions for proper file serving:
import {
readFile,
serveFile,
} from "https://esm.town/v/std/utils@85-main/index.ts";
Include this pattern in Hono apps for better error visibility:
app.onError((err, c) => {
throw err;
});
The API includes CORS headers to allow requests from any origin:
app.use(
"*",
cors({
origin: "*",
allowMethods: ["GET", "POST", "OPTIONS"],
allowHeaders: ["Content-Type"],
}),
);
- TypeScript: Configured with relaxed settings (
noImplicitAny: false,strict: false) - Imports: Uses
https://esm.shfor npm packages andhttps://esm.town/v/for Val Town utilities - Styling: TailwindCSS via CDN script tag
- Error Debugging: Includes
<script src="https://esm.town/v/std/catch"></script>for client-side error capture