KosmoJS generates an OpenAPI 3.1 specification directly from your route definitions. Route structure, TypeScript types, TRefine constraints, parameters, responses - all reflected in the spec automatically. No manual schema authoring, no annotation layers.
π§ Enable the generator β
Simply add it to your source folder's vite.config.ts:
import { join } from "node:path";
import devPlugin from "@kosmojs/dev";
import {
koaGenerator,
fetchGenerator,
typeboxGenerator,
openapiGenerator,
} from "@kosmojs/generators";
import defineConfig from "../../vite.base";
import { apiurl, baseurl } from "./config";
const openapiConfig = {
// ...
};
export default defineConfig(import.meta.dirname, {
base: join(baseurl, "/"),
server: { port: 4000 },
plugins: [
devPlugin(apiurl, {
generators: [
koaGenerator(),
fetchGenerator(),
typeboxGenerator(),
openapiGenerator(openapiConfig),
],
}),
],
});βοΈ Configuration β
Required Options β
outfile - Path where the spec is written, relative to your vite.config.ts.
openapi - OpenAPI version. Use "3.1.0" or any 3.1.x version.
info - API metadata:
title(required) - Name of your APIversion(required) - API version, use semantic versioning
servers - Array of server objects:
url(required) - Base URL where the API is serveddescription(optional) - Human-readable label
Optional Info Properties β
summary - One-line summary
description - Detailed description, supports markdown
termsOfService - URL to terms of service
contact - name, url, email
license - name (required), identifier (SPDX), url
Complete Example β
const openapiConfig = {
outfile: "openapi.json",
openapi: "3.1.0",
info: {
title: "My SaaS API",
version: "2.1.0",
summary: "RESTful API for My SaaS Platform",
description: `
# API Documentation
This API provides access to all platform features including
user management, billing, and analytics.`,
termsOfService: "https://myapp.com/terms",
contact: {
name: "API Support",
url: "https://myapp.com/support",
email: "api@myapp.com",
},
license: {
name: "Apache 2.0",
url: "https://www.apache.org/licenses/LICENSE-2.0.html",
},
},
servers: [
{ url: "http://localhost:4000", description: "Development server" },
{ url: "https://staging-api.myapp.com", description: "Staging environment" },
{ url: "https://api.myapp.com", description: "Production server" },
],
};π Generated Specification β
The output is a complete OpenAPI 3.1 document covering:
- Paths - all routes with HTTP methods, parameters, request bodies, and responses
- Schemas - type definitions extracted from your
TypeScripttypes and validation schemas - Parameters - path, query, and header parameters with types and constraints
- Request Bodies - payload schemas for POST, PUT, and PATCH endpoints
- Responses - response schemas with status codes and content types
- Validation Rules -
TRefineconstraints appear as JSON Schema keywords
Path Variations for Optional Parameters β
OpenAPI requires all path parameters to be mandatory, so routes with optional parameters generate multiple paths.
For a route at users/[id]/posts/{postId}/index.ts, the generator produces:
/users/{id}/posts/{postId}- full path with optional parameter present/users/{id}/posts- path without optional parameter
Both reference the same handlers and schemas.
Live Regeneration β
The spec regenerates automatically whenever you modify route definitions, types, or validation schemas. The generator runs in the background alongside the validation and fetch generators - no manual rebuild step required.
Serve the generated spec with any standard tooling: Swagger UI, Redoc, or Stoplight Elements.