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.
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 likes tracking API using unique IDs with the following structure:
- Val Town Platform: Built for Val Town's serverless environment using Deno runtime
- Backend: Hono web framework with SQLite database for ID-based likes tracking
- 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 name:
likes_by_id_v1(increment version when changing schema) - Core functions:
initDatabase(),recordLike(),getAllLikes(),getLikeCount(),getTotalLikes() - Database tracks likes by unique ID (string identifier)
- Schema:
id(TEXT PRIMARY KEY),likes(INTEGER),created_at,updated_at - Database initializes automatically on startup
- Self-contained HTML documentation and demo page
- Interactive testing interface for all API endpoints
- Code examples for integration
- Styled with custom CSS for professional appearance
- Hono app with error unwrapping for debugging and CORS support
- Routes:
/(documentation page),POST /api/like(record like),GET /api/likes/:id(get count for specific ID),GET /api/likes(all data) - Accepts JSON payloads with
idfield (string) - 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.,
likes_by_id_v1 → likes_by_id_v2) rather than using ALTER TABLE
operations.
The API is designed for cross-origin requests. Example usage from any website:
// Record a like for a specific content ID
fetch("https://your-val-url.val.run/api/like", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: "post-123", // Any unique identifier
}),
})
.then((res) => res.json())
.then((data) => console.log("New like count:", data.likeCount));
// Get like count for a specific ID
fetch("https://your-val-url.val.run/api/likes/post-123")
.then((res) => res.json())
.then((data) => console.log("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