import type { ApiSchemas } from './apiSchemas' /** * Template literal type utilities for converting parameterized paths to concrete paths * This enables type-safe API calls with actual paths like '/test/items/123' instead of '/test/items/:id' */ /** * Convert parameterized path templates to concrete path types * @example '/test/items/:id' -> '/test/items/${string}' * @example '/topics/:id/messages' -> '/topics/${string}/messages' */ export type ResolvedPath = T extends `${infer Prefix}/:${string}/${infer Suffix}` ? `${Prefix}/${string}/${ResolvedPath}` : T extends `${infer Prefix}/:${string}` ? `${Prefix}/${string}` : T /** * Generate all possible concrete paths from ApiSchemas * This creates a union type of all valid API paths */ export type ConcreteApiPaths = { [K in keyof ApiSchemas]: ResolvedPath }[keyof ApiSchemas] /** * Reverse lookup: from concrete path back to original template path * Used to determine which ApiSchema entry matches a concrete path */ export type MatchApiPath = { [K in keyof ApiSchemas]: Path extends ResolvedPath ? K : never }[keyof ApiSchemas] /** * Extract query parameters type for a given concrete path */ export type QueryParamsForPath = MatchApiPath extends keyof ApiSchemas ? ApiSchemas[MatchApiPath] extends { GET: { query?: infer Q } } ? Q : Record : Record /** * Extract request body type for a given concrete path and HTTP method */ export type BodyForPath = MatchApiPath extends keyof ApiSchemas ? ApiSchemas[MatchApiPath] extends { [M in Method]: { body: infer B } } ? B : any : any /** * Extract response type for a given concrete path and HTTP method */ export type ResponseForPath = MatchApiPath extends keyof ApiSchemas ? ApiSchemas[MatchApiPath] extends { [M in Method]: { response: infer R } } ? R : any : any