zagora
Version:
A minimalist, type-safe, error-safe typed functions in 214 lines of TypeScript based on Zod v4. No batteries, just functions. Simple alternative to oRPC and tRPC.
21 lines (20 loc) • 2.04 kB
TypeScript
import { z } from "zod";
//#region src/index.d.ts
type TupleResult<TData, TErr> = [TData | null, TErr | Error | null];
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
type ValuePrefixes<T extends any[]> = T extends [infer H, ...infer R] ? [] | [H, ...ValuePrefixes<R>] : [];
type InferArgsFromTuple<T extends z.ZodTuple<any, any>> = z.infer<T>;
declare function zagora(): FluentBuilder<null, null, null>;
declare const za: FluentBuilder<null, null, null>;
declare class FluentBuilder<InputTuple extends z.ZodTuple<any, any> | null = null, Output extends z.ZodTypeAny | null = null, ErrSchema extends z.ZodTypeAny | Record<string, z.ZodTypeAny> | null = null> {
private _inputTuple;
private _output;
private _err;
input<Tuple extends z.ZodTuple<any, any>>(tuple: Tuple): FluentBuilder<Tuple, Output, ErrSchema>;
output<NewOut extends z.ZodTypeAny>(schema: NewOut): FluentBuilder<InputTuple, NewOut, ErrSchema>;
errors<NewErr extends z.ZodTypeAny>(schema: NewErr): FluentBuilder<InputTuple, Output, NewErr>;
errorsMap<NewMap extends Record<string, z.ZodTypeAny>>(map: NewMap): FluentBuilder<InputTuple, Output, NewMap>;
handler<IT extends z.ZodTuple<any, any> = (InputTuple extends z.ZodTuple<any, any> ? InputTuple : never), Args extends any[] = InferArgsFromTuple<IT>, H extends (...args: Args) => any = (...args: Args) => any>(impl: H): UnionToIntersection<ValuePrefixes<Args> extends infer P ? P extends any ? (...args: P) => Promise<TupleResult<Output extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>> ? z.core.output<Output> : unknown, ErrSchema extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>> ? z.core.output<ErrSchema> : ErrSchema extends Record<string, z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>> ? { [K in keyof ErrSchema]: z.core.output<ErrSchema[K]> }[keyof ErrSchema] : unknown>> : never : never>;
}
//#endregion
export { FluentBuilder, z, za, zagora };