Multiple Source Folders
Organize distinct concerns - public site, customer app, admin dashboard - as independent source folders within a single Vite project.
A Vite template evolved into a fullβstack metaβframework Multiple source folders. Directory-based routing. Cascading middleware. Nested layouts. End-to-end validation. Fetch clients. OpenAPI spec. Koa, Hono, SolidJS, React, Vue and more.
TRefine to specify JSON Schema constraints. import { defineRoute } from "_/front/api";
export default defineRoute<
"users/[id]/{activity}",
[
// validate id as number
number,
// activity, if given, should be one of
TRefine<string, "posts" | "comments" | "likes">,
]
>(({ GET }) => [
GET((ctx) => {
// ctx.validated.params is typed and validated at runtime
const { id, activity } = ctx.validated.params
})
]);json / form / raw. query, headers, cookies. TRefine to specify JSON Schema constraints. import { defineRoute } from "_/admin/api";
type Payload = {
email: TRefine<string, { format: "email" }>;
password: TRefine<string, { pattern: "^(?=.*[a-zA-Z0-9])$" }>;
name: TRefine<string, { minLength: 5; maxLength: 50 }>;
dateOfBirth: TRefine<string, { format: "date" }>;
agreeToTerms: boolean;
marketingOptIn?: boolean;
}
export default defineRoute<"users">(({ POST }) => [
POST<{
json: Payload,
}>((ctx) => {
// ctx.validated.json is typed and validated at runtime
})
]);import { defineRoute } from "_/admin/api";
type Page = {
id: TRefine<string, { format: "uuid" }>;
title: TRefine<string, { minLength: 1; maxLength: 255 }>;
content: string;
tags: string[];
status: "draft" | "published" | "scheduled";
}
export default defineRoute<"pages/[id]">(({ GET }) => [
GET<{
response: [200, "json", Page],
}>(async (ctx) => {
// response should match defined schema
})
]);api/use.ts will run on every route. Routes can use slot option to override global middleware. logger should be added to api/env.d.ts file. import { use } from "_/admin/api";
export default [
use(
(ctx, next) => {
// global logger
return next();
},
{ slot: "logger", },
),
];import { defineRoute } from "_/front/api";
export default defineRoute<"example">(({ use, GET }) => [
use(
async (ctx, next) => {
// custom logger
return next();
},
{ slot: "logger", },
),
GET(async (ctx) => {
// ...
}),
]);export declare module "@kosmojs/api" {
interface UseSlots {
logger: string;
}
}use.ts file in any folder, and its default exported middleware automatically wraps all routes in that folder and its subfolders - no imports or manual wiring required. import { use } from "_/front/api";
export default [
use(async (ctx, next) => {
// any route inside users/ folder and its subfolders
// will wire this middleware automatically
return next();
})
];KosmoJS is a meta-framework that integrates TypeScript, Vite, Koa/Hono, 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. Just thoughtful structure around tools you already know.
π Learn more
Applications often include multiple distinct concerns - each with different routing, auth, and config:
πΉ Public marketing site at /
πΉ Customer application at /app
πΉ Admin dashboard at /admin
Each lives in its own source folder with independent api/ and pages/ directories, sharing types and validation logic across a single project. The directory structure enforces boundaries that code review can't.
π Getting started Β· Directory-based routing
KosmoJS converts your TypeScript types into runtime validators, typed fetch clients, client-side validation, and OpenAPI schemas - all from the same definitions. No duplication, no drift.
π Type safety Β· Validation Β· Fetch clients
Build APIs inside Vite's dev server with hot-reload. Slot-based middleware gives you fine-grained control - override globals per-route or per-subtree, compose request handling precisely. What you build locally is what deploys.
π Dev workflow Β· Middleware
pnpm build produces a bundled API server, optimized frontend assets, and an optional SSR bundle. Deploy to Node.js, Deno, Bun, containers, serverless, or edge.
π Production build
Structure without constraints.
Opinionated about organization, unopinionated about implementation. You choose your frontend framework, state management, styling, database - everything else. The structure scales; your choices remain free.
π About KosmoJS Β· Features