A modern, production-ready full-stack template combining SvelteKit, Hono, and Cloudflare Workers with enterprise-grade architecture patterns including dependency injection, comprehensive testing, and global error handling.
This project implements a comprehensive InversifyJS dependency injection system for both server-side and client-side code, following SOLID principles for maintainable and testable code.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Svelte Components (Browser) โ
โ Use service hooks: useUserApi(), useApi() โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ resolves from
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Client-Side DI Container โ
โ (API services, HTTP client) โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ calls
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Hono API Routes (Edge) โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ resolves from
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Server-Side DI Container โ
โ (Business logic, repositories, logging) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Use dependency injection in your Svelte components for clean, testable code:
<script lang="ts">
import { useUserApi } from '$lib/di/context.svelte';
import { onMount } from 'svelte';
// Inject the User API service
const userApi = useUserApi();
let users = $state([]);
onMount(async () => {
users = await userApi.getAllUsers();
});
const createUser = async (name: string, email: string) => {
const newUser = await userApi.createUser({ name, email });
users = [...users, newUser];
};
</script>
Available Service Hooks:
useUserApi() - User CRUD operationsuseHealthApi() - Health check operationsuseHelloApi() - Hello endpoint operationsuseApi() - Combined facade for all services๐ Learn More:
| Service | Purpose | Scope |
|---|---|---|
UserService |
User business logic and orchestration | Transient |
UserRepository |
Data access and persistence | Singleton |
Logger |
Structured logging with context | Singleton |
ConfigService |
Environment and app configuration | Singleton |
// In your API routes
import { getUserService, getLogger } from '../container/resolvers';
app.get('/users', async (c) => {
const userService = getUserService(c);
const logger = getLogger(c);
logger.info('Fetching users');
const users = await userService.getAllUsers();
return c.json({ success: true, data: users });
});
# Clone and install
git clone <your-repo-url>
cd sveltekit-hono
pnpm install
# Start development
pnpm dev
That's it! Visit http://localhost:5173
# Install Wrangler CLI (one-time setup)
npm install -g wrangler
# Run with Cloudflare Workers simulation
pnpm dev:cf
pnpm dev # SvelteKit dev server (recommended)
pnpm dev:cf # Cloudflare Workers simulation
pnpm build # Production build
pnpm preview # Preview built app
# Unit & Component Tests (Vitest)
pnpm test # Tests in watch mode
pnpm test:run # Run all tests once
pnpm test:ui # Interactive test UI
pnpm test:coverage # Coverage reports
# E2E Tests (Playwright)
pnpm test:e2e # Run E2E tests
pnpm test:e2e:ui # Interactive E2E test UI
pnpm test:e2e:headed # Run with visible browser
pnpm test:e2e:debug # Debug E2E tests
# Code Quality
pnpm check # TypeScript validation
pnpm lint # Code linting
pnpm format # Code formatting
Comprehensive test suite with Vitest + Testing Library:
๐ For complete testing guide, see TESTING.md
Includes: testing strategies, patterns, DI testing, mocking, coverage, and CI/CD setup
# Run tests
pnpm test:run # All tests once
pnpm test # Watch mode
pnpm test:ui # Interactive UI
pnpm test:coverage # With coverage
pnpm deploy # Deploy to dev
pnpm deploy:cf # Deploy to production
Built with Hono and comprehensive error handling:
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/health |
System health check |
GET |
/api/hello |
API info and request details |
| Method | Endpoint | Description | Status Codes |
|---|---|---|---|
GET |
/api/users |
List all users | 200, 500 |
GET |
/api/users/:id |
Get user by ID | 200, 400, 404, 500 |
POST |
/api/users |
Create new user | 201, 400, 409, 500 |
PUT |
/api/users/:id |
Update user | 200, 400, 404, 500 |
DELETE |
/api/users/:id |
Delete user | 200, 400, 404, 500 |
Consistent JSON responses across all endpoints:
// Success
{ "success": true, "data": any, "message?": string, "timestamp": string }
// Error
{ "success": false, "error": string, "timestamp": string }
# Health check
curl http://localhost:5173/api/health
# List users (demo data)
curl http://localhost:5173/api/users
# Create user
curl -X POST http://localhost:5173/api/users \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"[email protected]"}'
Custom Error Classes with proper HTTP status codes:
ValidationError (400) - Invalid input dataNotFoundError (404) - Resource not foundConflictError (409) - Duplicate resourcesBadRequestError, UnauthorizedError, ForbiddenError, InternalServerErrorGlobal Error Handler catches all exceptions and returns consistent JSON responses with structured logging.
Comprehensive test suite with Vitest (unit/component) + Playwright (E2E):
๐ For complete testing guides:
- TESTING.md - Unit & component testing with Vitest
- E2E_TESTING.md - End-to-end testing with Playwright
pnpm test # Watch mode
pnpm test:run # Run once
pnpm test:ui # Interactive UI
pnpm test:coverage # With coverage
pnpm test:e2e # Run E2E tests (headless)
pnpm test:e2e:ui # Interactive UI mode (recommended)
pnpm test:e2e:headed # Run with visible browser
pnpm test:e2e:debug # Debug with DevTools
pnpm test:e2e:chromium # Test in Chrome only
pnpm test:e2e:firefox # Test in Firefox only
pnpm test:e2e:webkit # Test in Safari only
Unit & Component Tests:
E2E Tests (117 tests across 3 browsers):
Input validation is implemented with Zod v4. Schemas live in src/models/user.model.ts and types are inferred directly from the schemas for end-to-end type safety.
// src/models/user.model.ts
import { z } from 'zod';
export const createUserSchema = z.object({
name: z
.string()
.trim()
.min(1, { message: 'Name is required' })
.refine((v) => v.length === 0 || v.length >= 2, {
message: 'Name must be at least 2 characters long'
}),
email: z
.string()
.trim()
.min(1, { message: 'Email is required' })
.refine((v) => v.length === 0 || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v), {
message: 'Email format is invalid'
})
});
export type CreateUserRequest = z.infer<typeof createUserSchema>;
Services validate using safeParse and throw a ValidationError on failure:
// src/services/user.service.ts
const parsed = createUserSchema.safeParse(userData);
if (!parsed.success) {
const messages = parsed.error.issues.map((i) => i.message);
throw new ValidationError(`Validation failed: ${messages.join(', ')}`);
}
src/
โโโ routes/ # SvelteKit routes
โ โโโ +page.svelte # Demo page
โ โโโ api/[...paths]/+server.ts # Hono API server
โโโ container/ # Dependency Injection
โ โโโ inversify.config.ts # IoC container
โ โโโ types.ts # Service types
โ โโโ resolvers.ts # Service resolvers
โโโ models/ # Domain models and schemas (Zod)
โ โโโ user.model.ts # User model + Zod schemas
โ โโโ error.model.ts # Custom error classes and mapping
โโโ interfaces/ # TypeScript interfaces
โโโ services/ # Business logic layer
โโโ types/ # Type definitions
โโโ tests/ # Test suites
โโโ api/ # API tests
โโโ components/ # Component tests
Key Files:
wrangler.toml - Cloudflare Workers configurationsvelte.config.js - SvelteKit + adapter setupvite.config.ts - Vite + testing configuration# One-time setup
wrangler login
# Deploy to development
pnpm deploy
# Deploy to production
pnpm deploy:cf
.env.local (SvelteKit)wrangler.toml (Cloudflare Workers)Using InversifyJS for clean architecture:
pnpm installgit checkout -b feature/your-featurepnpm test:run && pnpm lint && pnpm checkEnsure all tests pass and follow the existing code patterns.
API routes not working?
wrangler.toml configuration+server.tsEnvironment variables?
PUBLIC_wrangler.tomlBuild errors?
pnpm check for TypeScript validationTest failures?
pnpm test:ui for interactive debuggingโก๏ธ See DEVELOPMENT.md for detailed troubleshooting
MIT License - see LICENSE file.
Give a โญ if this helped you!
Built with โค๏ธ using SvelteKit + Hono + Cloudflare Workers