Skip to content

KosmoJS

Structured Vite Template. Build type-safe full-stack apps. Multiple source folders. Directory-based routing. Runtime validation. Generated fetch clients. Any framework.

Simple Params Validation

Require id to be a number
Details β†’
Simple params validation
πŸ” Click to zoom
Refined params validation
πŸ” Click to zoom

Refined Params Validation

Require id to be a positive integer.
Use TRefine to specify JSON Schema constraints.
Details β†’

Payload Validation

Use the first type argument to define the payload schema. TRefine can be used to specify JSON Schema constraints.
Details β†’
Payload validation
πŸ” Click to zoom
Response validation
πŸ” Click to zoom

Response Validation

Use the second type argument to define the response schema. ctx.body should match defined schema for validation to pass.
If no payload validation is needed, use never for the first argument.
Details β†’

Global Middleware

Define middleware to run on every route. Use the slot option to allow routes to override this middleware later. Use the on option to run middleware only for specific HTTP methods.
Details β†’
Global Middleware
πŸ” Click to zoom
Override Middleware
πŸ” Click to zoom

Override Middleware

Use the slot option in routes to override global middleware with the same slot name. Eg., for file upload routes, use a form body parser instead of the default JSON parser.
Feel free to use any Koa-compatible middleware β€” not just those provided by KosmoJS.
Details β†’

🎯 The What ​

KosmoJS is a structured Vite template that keeps your full-stack concerns aligned.

Rather than inventing yet another framework, KosmoJS integrates proven tools - TypeScript, Vite, Koa, and your frontend framework - into a clear organizational pattern. Separation of concerns isn't something you have to remember - it's built into the structure.

No proprietary abstractions. No new paradigms to learn. Just thoughtful structure around tools you already know.

πŸ“˜ Learn more


πŸ’‘ The Why ​

Multiple source folders for distinct concerns - each with its own API and pages directories, eg.:

πŸ”Ή Public marketing site at /
πŸ”Ή Customer application at /app
πŸ”Ή Admin dashboard at /admin

All in one monorepo-like project, each with independent routing and configuration, yet sharing types and validation logic.
API / Pages separation keeps server and client code from mixing. Your directory structure enforces boundaries that code review can't.

πŸ“˜ Getting started Β· Directory-based routing


πŸ“¦ The How ​

At its core, KosmoJS structures full-stack Vite development around a Koa application.

πŸ”Ή Vite handles your frontend builds and organizational structure.
πŸ”Ή Koa powers your API runtime with runtype validation and middleware composition.
πŸ”Ή KosmoJS is the structured template that brings them together.


πŸ›‘οΈ Type Safety & Validation ​

KosmoJS extends TypeScript's compile-time safety to runtime. Your type definitions become validation schemas automatically - no duplication, no drift.

Define parameter types, payload structures, and response shapes once. KosmoJS generates:

  • Runtime validators for your API
  • Typed fetch clients for your frontend
  • Client-side validation that catches errors before requests

Everything stays aligned because everything derives from the same source of truth.

πŸ“˜ Type safety overview Β· Validation Β· Payload validation


⚑ Generated Fetch Clients ​

Every API route gets a fully-typed fetch client with built-in validation. Your frontend knows exactly what parameters each endpoint expects, what payload structure it accepts, and what response shape it returns.

Invalid data is caught client-side, before network requests. Your API never processes malformed requests.

πŸ“˜ Fetch clients intro Β· Getting started Β· Client-side validation


βš™οΈ API Development ​

Build APIs directly inside Vite's dev server with hot-reload support.

Slot-based middleware gives you fine-grained control - override global middleware per endpoint, compose request handling precisely, maintain consistent patterns across routes.

Development and production use the same structure - what you build locally is what deploys.

πŸ“˜ Dev workflow Β· Middleware patterns


🧰 Generators ​

Enable features by adding generators to your Vite config:

  • Validation schemas from TypeScript types
  • Typed fetch clients for every API route
  • OpenAPI 3.1 specs generated automatically
  • Framework integration (SolidJS, React, 🚧 with Vue / Svelte coming soon)

Each generator watches your code and updates artifacts as you develop.

πŸ“˜ SolidJS generator Β· OpenAPI generator


πŸš€ Production Ready ​

pnpm build produces deployment-ready output - bundled API server and optimized frontend assets.

Deploy to any Node.js environment: traditional servers, containers, serverless platforms, or edge runtimes.

πŸ“˜ Production build guide


🧠 Philosophy ​

Structure without constraints.

KosmoJS is opinionated about organization but unopinionated about implementation. Clear boundaries between API and pages. Obvious locations for shared types and utilities. Separation of concerns built into the filesystem.

You choose your frontend framework, state management, styling approach, database, and everything else.
The structure scales; your choices remain free.

πŸ“˜ About KosmoJS Β· Features

Released under the MIT License.