Skip to content

Latest commit

 

History

History
80 lines (58 loc) · 1.88 KB

File metadata and controls

80 lines (58 loc) · 1.88 KB

Chanfana Template — Task API

A starter Cloudflare Worker using Chanfana (OpenAPI), Hono, D1, and Zod v4.

Scaffold a new project with the create-cloudflare CLI:

npm create cloudflare@latest -- --template https://github.com/cloudflare/chanfana/tree/main/template

Setup

# Install dependencies
npm install

# Create the D1 database
npx wrangler d1 create tasks-db
# Copy the returned database_id into wrangler.jsonc

# Run the migration locally
npm run db:migrate:local

Development

npm run dev          # Start local dev server (http://localhost:8787)

Once running, open:

Testing

Tests use @cloudflare/vitest-pool-workers with a local D1 binding (no real database needed):

npm test

Deployment

# Apply migration to remote D1
npm run db:migrate:remote

# Deploy to Cloudflare
npm run deploy

Project Structure

src/
  index.ts                 # Hono app + route registration
  models/task.ts           # Zod schema + shared meta
  endpoints/
    taskCreate.ts          # POST   /api/tasks
    taskList.ts            # GET    /api/tasks
    taskRead.ts            # GET    /api/tasks/:id
    taskUpdate.ts          # PUT    /api/tasks/:id
    taskDelete.ts          # DELETE /api/tasks/:id
    hello.ts               # GET    /api/hello  (custom non-D1 example)
test/
  tasks.test.ts            # Integration tests
migrations/
  0001_create_tasks/up.sql # D1 schema

Adding a New Model

  1. Create a Zod schema and meta object in src/models/
  2. Create endpoint classes in src/endpoints/ extending D1CreateEndpoint, D1ReadEndpoint, etc.
  3. Register routes in src/index.ts
  4. Add a D1 migration in migrations/

See the Chanfana docs for the full API reference.