/** * @fileoverview CON-03 Type Derivation - Route Schema Linking & Envelope Automation * @description Auto-generated TypeScript types for route mapping and type-safe routing * @version 0.18.4-alpha */ import { z } from 'zod'; import { RouteRegistry } from '../registry/RouteRegistry'; /** * All route paths from the RouteRegistry */ export type RoutePaths = keyof typeof RouteRegistry; /** * Route method type helper - gets the HTTP method for a route path */ export type RouteMethod = typeof RouteRegistry[K]["method"]; /** * Route request type helper - gets the request schema type for a route path */ export type RouteRequest = typeof RouteRegistry[K]["request"] extends z.ZodSchema ? T : never; /** * Route response type helper - gets the response schema type for a route path */ export type RouteResponse = typeof RouteRegistry[K]["response"] extends z.ZodSchema ? T : never; /** * Complete route map - maps route paths to their full type information */ export type RouteMap = { [K in RoutePaths]: { path: string; method: RouteMethod; request: RouteRequest; response: RouteResponse; description?: string; deprecated?: boolean; version?: string; }; }; /** * Type-safe route definition from registry */ export type RouteDefinition = typeof RouteRegistry[K]; /** * Get route definition by path - compile-time route path validation */ export function getRouteDefinition(path: K): RouteDefinition { return RouteRegistry[path]; } /** * Type guard to check if a string is a valid route path */ export function isValidRoutePath(path: string): path is RoutePaths { return path in RouteRegistry; } /** * Get all non-deprecated route paths */ export type ActiveRoutePaths = { [K in RoutePaths]: typeof RouteRegistry[K]["deprecated"] extends true ? never : K; }[RoutePaths]; /** * Get all deprecated route paths */ export type DeprecatedRoutePaths = { [K in RoutePaths]: typeof RouteRegistry[K]["deprecated"] extends true ? K : never; }[RoutePaths]; /** * Route metadata helpers */ export const RouteMetadata = { /** * Total number of routes in registry */ totalRoutes: Object.keys(RouteRegistry).length, /** * Number of active (non-deprecated) routes */ activeRoutes: Object.values(RouteRegistry).filter(route => !route.deprecated).length, /** * Number of deprecated routes */ deprecatedRoutes: Object.values(RouteRegistry).filter(route => route.deprecated).length, /** * Routes by HTTP method */ routesByMethod: Object.values(RouteRegistry).reduce((acc, route) => { acc[route.method] = (acc[route.method] || 0) + 1; return acc; }, {} as Record), /** * Routes by version */ routesByVersion: Object.values(RouteRegistry).reduce((acc, route) => { const version = route.version || 'unknown'; acc[version] = (acc[version] || 0) + 1; return acc; }, {} as Record) } as const; /** * Type-safe route response type - for use in frontend/infra code * Provides full type inference for route responses * * @example * ```typescript * // In frontend code: * type PortfolioSummary = RouteResponseType<'/api/infra/portfolio/summary'>; * // PortfolioSummary is now fully typed based on the schema * ``` */ export type RouteResponseType = RouteResponse; /** * Type-safe route request type - for use in frontend/infra code * * @example * ```typescript * // In frontend code: * type LoginRequest = RouteRequestType<'/api/auth/login'>; * // LoginRequest is now fully typed based on the schema * ``` */ export type RouteRequestType = RouteRequest; /** * Utility type for creating type-safe API clients */ export type ApiClient = { [K in RoutePaths]: { request: (data: RouteRequest) => Promise>; }; }; /** * Helper to create a typed API client from the route map */ export function createTypedApiClient() { return {} as ApiClient; } /** * Registry version and generation metadata */ export const ROUTE_MAP_METADATA = { version: "0.18.4-alpha", generated: new Date().toISOString(), source: "manual-registry", // Will change to "auto-generated" in Phase 3 totalRoutes: RouteMetadata.totalRoutes, typeSafety: "full" as const } as const;