Backend

This directory contains the server-side code for the Flashcard Learning App.

Structure

  • index.ts - Main Hono app setup, route mounting, and static file serving
  • storage.ts - Blob storage utilities for user data persistence
  • routes/ - API endpoint implementations

Routes

auth.ts

  • Handles user login and initialization
  • Creates new user data if doesn't exist

decks.ts

  • CRUD operations for flashcard decks
  • Cascade deletion of cards when deck is deleted

cards.ts

  • CRUD operations for flashcards
  • Validates deck existence before card creation

study.ts

  • Fetches due cards based on spaced repetition
  • Processes card reviews and updates SM-2 parameters

stats.ts

  • Retrieves user statistics
  • Saves completed study sessions

backup.ts

  • Exports user data as JSON
  • Imports user data from JSON backup

Key Patterns

Authentication

All routes (except login) require X-Username header for user identification.

Error Handling

Hono's onError is configured to throw errors for full stack traces.

Data Flow

  1. Load user data from blob storage
  2. Perform in-memory operations
  3. Save back to blob storage

Adding New Routes

  1. Create new file in routes/
  2. Export a Hono instance
  3. Mount in index.ts with app.route()
  4. Add X-Username header check if needed