5* Returns null if authentication is successful, or a Response if it fails
6*/
7export async function basicAuthMiddleware(req: Request): Promise<Response | null> {
8const realm = "Usage Dashboard";
9const unauthorizedResponse = new Response("Unauthorized", {
17});
1819export function App() {
20const [audio, setAudio] = useLocalStorage("AUDIO", false);
21const user = useUser();
2import type { ReactNode } from "npm:react@18.2.0";
34export function Layout({ children }: { children: ReactNode }) {
5return (
6<html lang="en">
markdownBlogStarterindex.tsx3 matches
5import { Layout } from "./Layout.tsx";
67function PostComponent({ markdown, link }: { markdown: string; link?: string }) {
8return (
9<div style={{ border: "1px solid gray", padding: "10px", marginBottom: "20px", borderRadius: "5px" }}>
14}
1516export default async function(req: Request): Promise<Response> {
17const url = new URL(req.url);
18if (url.pathname === "/") {
44}
4546function html(children: React.ReactNode) {
47return new Response(
48renderToString(
JsonViewerindex.ts11 matches
6*/
78export default async function (req: Request) {
9const html = `<!DOCTYPE html>
10<html lang="en">
172let hasValidJSON = false;
173
174document.addEventListener('DOMContentLoaded', function() {
175// Initialize preview editor
176const previewContainer = document.getElementById('json-preview');
178mode: 'tree',
179modes: ['tree', 'view', 'form', 'code', 'text'],
180onError: function(err) {
181showError(err.toString());
182}
191mode: 'tree',
192modes: ['tree', 'view', 'form', 'code', 'text'],
193onError: function(err) {
194showError(err.toString());
195}
219});
220
221function switchTab(tabId) {
222// Update tab active states
223document.getElementById('input-tab').classList.toggle('active', tabId === 'input');
236}
237
238function formatJSON() {
239const jsonInput = document.getElementById('json-input').value.trim();
240if (!jsonInput) {
261}
262
263function clearJSON() {
264document.getElementById('json-input').value = '';
265previewEditor.set({});
270}
271
272function copyJSON() {
273try {
274const jsonData = viewerEditor.get();
290}
291
292function showError(message) {
293const errorContainer = document.getElementById('error-container');
294errorContainer.textContent = message;
296}
297
298function hideError() {
299const errorContainer = document.getElementById('error-container');
300errorContainer.textContent = '';
302}
303
304function loadSampleData(index) {
305let sampleJSON = {};
306
readlocalfilenew-file-2413.tsx2 matches
24const output = document.getElementById('output');
2526function log(msg) {
27output.textContent += msg + '\\n';
28}
3132try {
33const test = new Function("return require('fs')");
34test();
35log("๐จ Sandbox likely disabled: 'require' is accessible");
Towniesystem_prompt.txt12 matches
45- Ask clarifying questions when requirements are ambiguous
6- Provide complete, functional solutions rather than skeleton implementations
7- Test your logic against edge cases before presenting the final solution
8- Ensure all code follows Val Town's specific platform requirements
17- **Never bake in secrets into the code** - always use environment variables
18- Include comments explaining complex logic (avoid commenting obvious operations)
19- Follow modern ES6+ conventions and functional programming practices if possible
2021## Types of triggers
2829```ts
30export default async function (req: Request) {
31return new Response("Hello World");
32}
4041```ts
42export default async function () {
43// Scheduled task code
44}
5253```ts
54export default async function (email: Email) {
55// Process email
56}
60## Val Town Standard Libraries
6162Val Town provides several hosted services and utility functions.
6364### Blob Storage
114```
115116## Val Town Utility Functions
117118Val Town provides several utility functions to help with common project tasks.
119120### Importing Utilities
176โ โโโ database/
177โ โ โโโ migrations.ts # Schema definitions
178โ โ โโโ queries.ts # DB query functions
179โ โ โโโ README.md
180โ โโโ routes/ # Route modules
195โโโ shared/
196โโโ README.md
197โโโ utils.ts # Shared types and functions
198```
199203- Main entry point should be `backend/index.ts`
204- Do NOT use Hono serveStatic middleware
205- **Static asset serving:** Use the utility functions to read and serve project files:
206```ts
207import { readFile, serveFile } from "https://esm.town/v/std/utils/index.ts";
237- Run migrations on startup or comment out for performance
238- Change table names when modifying schemas rather than altering
239- Export clear query functions with proper TypeScript typing
240241## Common Gotchas and Solutions
TownieValTownLogo.tsx1 match
1/** @jsxImportSource https://esm.sh/react@18.2.0?dev */
23export function ValTownLogo () {
4return (
5<svg
TownieuseUser.tsx1 match
3const USER_ENDPOINT = "/api/user";
45export function useUser() {
6const [data, setData] = useState<any>(null);
7const [loading, setLoading] = useState(true);
TownieuseUsageStats.ts1 match
1import { useEffect } from "react";
23export function useUsageStats(messages: any[], usages: any[]) {
4useEffect(() => {
5if (!messages?.length) return;