11import "./load-env.js"
22import { createDenTypeId } from "@openwork-ee/utils/typeid"
3+ import { swaggerUI } from "@hono/swagger-ui"
34import { cors } from "hono/cors"
45import { Hono } from "hono"
56import { logger } from "hono/logger"
67import type { RequestIdVariables } from "hono/request-id"
78import { requestId } from "hono/request-id"
9+ import { describeRoute , openAPIRouteHandler , resolver } from "hono-openapi"
10+ import { z } from "zod"
811import { env } from "./env.js"
912import type { MemberTeamsContext , OrganizationContextVariables , UserOrganizationsContext } from "./middleware/index.js"
13+ import { buildOperationId , emptyResponse , htmlResponse , jsonResponse } from "./openapi.js"
1014import { registerAdminRoutes } from "./routes/admin/index.js"
1115import { registerAuthRoutes } from "./routes/auth/index.js"
1216import { registerMeRoutes } from "./routes/me/index.js"
@@ -17,6 +21,21 @@ import { sessionMiddleware } from "./session.js"
1721
1822type AppVariables = RequestIdVariables & AuthContextVariables & Partial < UserOrganizationsContext > & Partial < OrganizationContextVariables > & Partial < MemberTeamsContext >
1923
24+ const healthResponseSchema = z . object ( {
25+ ok : z . literal ( true ) ,
26+ service : z . literal ( "den-api" ) ,
27+ } ) . meta ( { ref : "DenApiHealthResponse" } )
28+
29+ const openApiDocumentSchema = z . object ( {
30+ openapi : z . string ( ) ,
31+ info : z . object ( {
32+ title : z . string ( ) ,
33+ version : z . string ( ) ,
34+ } ) . passthrough ( ) ,
35+ paths : z . record ( z . string ( ) , z . unknown ( ) ) ,
36+ components : z . object ( { } ) . passthrough ( ) . optional ( ) ,
37+ } ) . passthrough ( ) . meta ( { ref : "OpenApiDocument" } )
38+
2039const app = new Hono < { Variables : AppVariables } > ( )
2140
2241app . use ( "*" , logger ( ) )
@@ -45,20 +64,132 @@ if (env.corsOrigins.length > 0) {
4564
4665app . use ( "*" , sessionMiddleware )
4766
48- app . get ( "/" , ( c ) => {
49- return c . redirect ( "https://openworklabs.com" , 302 )
50- } )
67+ app . get (
68+ "/" ,
69+ describeRoute ( {
70+ tags : [ "System" ] ,
71+ summary : "Redirect API root" ,
72+ description : "Redirects the API root to the OpenWork marketing site instead of serving API content." ,
73+ responses : {
74+ 302 : emptyResponse ( "Redirect to the OpenWork marketing site." ) ,
75+ } ,
76+ } ) ,
77+ ( c ) => {
78+ return c . redirect ( "https://openworklabs.com" , 302 )
79+ } ,
80+ )
5181
52- app . get ( "/health" , ( c ) => {
53- return c . json ( { ok : true , service : "den-api" } )
54- } )
82+ app . get (
83+ "/health" ,
84+ describeRoute ( {
85+ tags : [ "System" ] ,
86+ summary : "Check den-api health" ,
87+ description : "Returns a lightweight health payload for den-api." ,
88+ responses : {
89+ 200 : {
90+ description : "den-api is reachable" ,
91+ content : {
92+ "application/json" : {
93+ schema : resolver ( healthResponseSchema ) ,
94+ } ,
95+ } ,
96+ } ,
97+ } ,
98+ } ) ,
99+ ( c ) => {
100+ return c . json ( { ok : true , service : "den-api" } )
101+ } ,
102+ )
55103
56104registerAdminRoutes ( app )
57105registerAuthRoutes ( app )
58106registerMeRoutes ( app )
59107registerOrgRoutes ( app )
60108registerWorkerRoutes ( app )
61109
110+ app . get (
111+ "/openapi.json" ,
112+ describeRoute ( {
113+ tags : [ "Documentation" ] ,
114+ summary : "Get OpenAPI document" ,
115+ description : "Returns the machine-readable OpenAPI 3.1 document for the Den API so humans and tools can inspect the API surface." ,
116+ responses : {
117+ 200 : jsonResponse ( "OpenAPI document returned successfully." , openApiDocumentSchema ) ,
118+ } ,
119+ } ) ,
120+ openAPIRouteHandler ( app , {
121+ documentation : {
122+ openapi : "3.1.0" ,
123+ info : {
124+ title : "Den API" ,
125+ version : "dev" ,
126+ description : "OpenAPI spec for the Den control plane API." ,
127+ } ,
128+ tags : [
129+ { name : "System" , description : "Service health and operational routes." } ,
130+ { name : "Documentation" , description : "OpenAPI document and Swagger UI routes." } ,
131+ { name : "Organizations" , description : "Organization-scoped Den API routes." } ,
132+ { name : "Organization Invitations" , description : "Organization invitation creation, preview, acceptance, and cancellation routes." } ,
133+ { name : "Organization API Keys" , description : "Organization API key management routes." } ,
134+ { name : "Organization Members" , description : "Organization member management routes." } ,
135+ { name : "Organization Roles" , description : "Organization custom role management routes." } ,
136+ { name : "Organization Teams" , description : "Organization team management routes." } ,
137+ { name : "Organization Templates" , description : "Organization shared template routes." } ,
138+ { name : "Organization LLM Providers" , description : "Organization LLM provider catalog, configuration, and access routes." } ,
139+ { name : "Organization Skills" , description : "Organization skill authoring and sharing routes." } ,
140+ { name : "Organization Skill Hubs" , description : "Organization skill hub management and access routes." } ,
141+ { name : "Workers" , description : "Worker lifecycle, billing, and runtime routes." } ,
142+ { name : "Worker Billing" , description : "Worker subscription and billing status routes." } ,
143+ { name : "Worker Runtime" , description : "Worker runtime inspection and upgrade routes." } ,
144+ { name : "Worker Activity" , description : "Worker heartbeat and activity reporting routes." } ,
145+ { name : "Authentication" , description : "Authentication and desktop sign-in handoff routes." } ,
146+ { name : "Admin" , description : "Administrative reporting routes." } ,
147+ { name : "Users" , description : "Current user and membership routes." } ,
148+ ] ,
149+ components : {
150+ securitySchemes : {
151+ bearerAuth : {
152+ type : "http" ,
153+ scheme : "bearer" ,
154+ bearerFormat : "session-token" ,
155+ } ,
156+ denApiKey : {
157+ type : "apiKey" ,
158+ in : "header" ,
159+ name : "x-api-key" ,
160+ } ,
161+ } ,
162+ } ,
163+ } ,
164+ includeEmptyPaths : true ,
165+ exclude : [ "/docs" , "/openapi.json" ] ,
166+ excludeMethods : [ "OPTIONS" ] ,
167+ defaultOptions : {
168+ ALL : {
169+ operationId : ( route ) => buildOperationId ( route . method , route . path ) ,
170+ } ,
171+ } ,
172+ } ) ,
173+ )
174+
175+ app . get (
176+ "/docs" ,
177+ describeRoute ( {
178+ tags : [ "Documentation" ] ,
179+ summary : "Serve Swagger UI" ,
180+ description : "Serves Swagger UI so developers can browse and try the Den API from a browser." ,
181+ responses : {
182+ 200 : htmlResponse ( "Swagger UI page returned successfully." ) ,
183+ } ,
184+ } ) ,
185+ swaggerUI ( {
186+ url : "/openapi.json" ,
187+ persistAuthorization : true ,
188+ displayOperationId : true ,
189+ defaultModelsExpandDepth : 1 ,
190+ } ) ,
191+ )
192+
62193app . notFound ( ( c ) => {
63194 return c . json ( { error : "not_found" } , 404 )
64195} )
0 commit comments