import { _tuple } from "./api.js"; import { parse, parseAsync } from "./parse.js"; import * as schemas from "./schemas.js"; import { $ZodTuple } from "./schemas.js"; import type * as util from "./util.js"; ////////////////////////////////////////// ////////////////////////////////////////// ////////// ////////// ////////// $ZodFunction ////////// ////////// ////////// ////////////////////////////////////////// ////////////////////////////////////////// export interface $ZodFunctionDef /* extends schemas.$ZodTypeDef */ { type: "function"; input: $ZodFunctionArgs | null; output: schemas.$ZodType | null; } export type $ZodFunctionArgs = schemas.$ZodType; export type $InferInnerFunctionType = ( ...args: Args["_zod"]["output"] ) => Returns["_zod"]["input"]; export type $InferInnerFunctionTypeAsync = ( ...args: Args["_zod"]["output"] ) => util.MaybeAsync; export type $InferOuterFunctionType = ( ...args: Args["_zod"]["input"] ) => Returns["_zod"]["output"]; export type $InferOuterFunctionTypeAsync = ( ...args: Args["_zod"]["input"] ) => util.MaybeAsync; export class $ZodFunction< Args extends $ZodFunctionArgs = $ZodFunctionArgs, Returns extends schemas.$ZodType = schemas.$ZodType, > { _def: $ZodFunctionDef; _input!: $InferInnerFunctionType; _output!: $InferOuterFunctionType; constructor(def: $ZodFunctionDef) { this._def = def; } implement>( func: F ): F extends this["_output"] ? F : this["_output"] { if (typeof func !== "function") { throw new Error("implement() must be called with a function"); } const impl = ((...args: any[]) => { const parsedArgs = this._def.input ? parse(this._def.input, args, undefined, { callee: impl }) : args; if (!Array.isArray(parsedArgs)) { throw new Error("Invalid arguments schema: not an array or tuple schema."); } const output = func(...parsedArgs); return this._def.output ? parse(this._def.output, output, undefined, { callee: impl }) : output; }) as any; return impl; } implementAsync>( func: F ): F extends $InferOuterFunctionTypeAsync ? F : $InferOuterFunctionTypeAsync { if (typeof func !== "function") { throw new Error("implement() must be called with a function"); } const impl = (async (...args: any[]) => { const parsedArgs = this._def.input ? await parseAsync(this._def.input, args, undefined, { callee: impl }) : args; if (!Array.isArray(parsedArgs)) { throw new Error("Invalid arguments schema: not an array or tuple schema."); } const output = await func(...parsedArgs); return this._def.output ? parseAsync(this._def.output, output, undefined, { callee: impl }) : output; }) as any; return impl; } input( args: Items, rest?: Rest ): $ZodFunction, Returns>; input(args: NewArgs): $ZodFunction; input(...args: any[]): $ZodFunction { if (Array.isArray(args[0])) { return new $ZodFunction({ type: "function", input: new $ZodTuple({ type: "tuple", items: args[0], rest: args[1], }), output: this._def.output, }); } return new $ZodFunction({ type: "function", input: args[0], output: this._def.output, }); } output(output: NewReturns): $ZodFunction { return new $ZodFunction({ type: "function", input: this._def.input, output, }); } } export interface $ZodFunctionParams { input?: I; output?: O; } function _function(): $ZodFunction; function _function< const In extends Array = Array, Out extends schemas.$ZodType = schemas.$ZodType, >(params?: { input?: In; output?: Out }): $ZodFunction<$ZodTuple, Out>; function _function( params?: $ZodFunctionParams ): $ZodFunction; function _function(params?: { output?: schemas.$ZodType; input?: $ZodFunctionArgs | Array; }): any { return new $ZodFunction({ type: "function", input: Array.isArray(params?.input) ? _tuple(schemas.$ZodTuple, params?.input as any) : (params?.input ?? null), output: params?.output ?? null, }); } export { _function as function };