a cache for slack profile pictures and emojis
at main 1.5 kB view raw
1/** 2 * Utility to build Bun-compatible routes from typed route definitions 3 * and generate Swagger documentation 4 */ 5 6import type { RouteDefinition } from "../types/routes"; 7import { swaggerGenerator } from "./swagger-generator"; 8 9type BunRoute = Record< 10 string, 11 (request: Request) => Promise<Response> | Response 12>; 13 14/** 15 * Convert typed routes to Bun server format and generate Swagger 16 */ 17export function buildRoutes(typedRoutes: Record<string, RouteDefinition>) { 18 // Generate Swagger from typed routes 19 swaggerGenerator.addRoutes(typedRoutes); 20 21 // Convert to Bun server format 22 const bunRoutes: Record<string, BunRoute> = {}; 23 24 Object.entries(typedRoutes).forEach(([path, routeConfig]) => { 25 const bunRoute: Record< 26 string, 27 (request: Request) => Promise<Response> | Response 28 > = {}; 29 30 // Convert each HTTP method 31 Object.entries(routeConfig).forEach(([method, typedRoute]) => { 32 if (typedRoute && "handler" in typedRoute) { 33 bunRoute[method] = typedRoute.handler; 34 } 35 }); 36 37 bunRoutes[path] = bunRoute; 38 }); 39 40 return bunRoutes; 41} 42 43/** 44 * Get the generated Swagger specification 45 */ 46export function getSwaggerSpec() { 47 return swaggerGenerator.getSpec(); 48} 49 50/** 51 * Merge typed routes with existing legacy routes 52 * This allows gradual migration 53 */ 54export function mergeRoutes( 55 typedRoutes: Record<string, RouteDefinition>, 56 legacyRoutes: Record<string, BunRoute>, 57) { 58 const builtRoutes = buildRoutes(typedRoutes); 59 60 return { 61 ...legacyRoutes, 62 ...builtRoutes, 63 }; 64}