import type { Unchecked, UncheckedError } from '../common.js'; import type { ResponsePluginMarker } from '../response.js'; import type { RouteResponseMap, RouteSchema } from './schema.js'; /** `Response` whose `.json()` method resolves to a known payload type. */ export type RouteResponse = Response & { json(): Promise; }; /** * Helper: given a status-keyed response map, produce the discriminated tuple * union for the client. * * Each entry becomes: * - `$type()` → `[null, T, Status]` * - `$error()` → `[T, null, Status]` */ type InferResponseMapClient = { [K in keyof T & number]: T[K] extends UncheckedError ? [TError, null, K] : T[K] extends Unchecked ? [null, TSuccess, K] : T[K] extends ResponsePluginMarker ? [null, TClient, K] : never; }[keyof T & number]; /** * Infer the generated client action result type from an action schema. * * @remarks Direct JSON markers infer their payload type, plugin markers infer * their client result type, and status-keyed response maps infer a tuple union * of `[null, value, status]` success entries and `[error, null, status]` error * entries. */ export type InferRouteResponse = T extends { response: infer R; } ? R extends ResponsePluginMarker ? TClient : R extends Unchecked ? TResponse : R extends RouteResponseMap ? InferResponseMapClient : void : void; /** * Helper: given a status-keyed response map, produce the union of handler * result types (success values the handler can return directly). */ type InferResponseMapHandlerResult = { [K in keyof T & number]: T[K] extends Unchecked ? TSuccess : T[K] extends ResponsePluginMarker ? TRouter : never; }[keyof T & number]; /** * Infer the non-`Response` handler result type from an action schema. * * @remarks For status-keyed response maps, this includes only success result * values. Declared error responses are returned with `ctx.error(status, body)`. */ export type InferRouteHandlerResult = T extends { response: infer R; } ? R extends ResponsePluginMarker ? TRouter : R extends Unchecked ? TResponse : R extends RouteResponseMap ? InferResponseMapHandlerResult : void : void; /** * Helper: given a status-keyed response map, extract error entries as a union * of `[status, body]` pairs for typing `ctx.error(status, body)`. */ export type InferResponseMapErrors = { [K in keyof T & number]: T[K] extends UncheckedError ? [K, TError] : never; }[keyof T & number]; /** Extract success entries as a union of `[status, body]` pairs. */ export type InferResponseMapSuccesses = { [K in keyof T & number]: T[K] extends Unchecked ? [K, TSuccess] : T[K] extends ResponsePluginMarker ? [K, TRouter] : never; }[keyof T & number]; export {};