11/* eslint-disable no-unused-vars */
2- import {
3- z ,
4- // infer as _infer,
5- ZodObject ,
6- ZodRawShape ,
7- ZodSchema ,
8- ZodTypeAny ,
9- ZodType ,
10- } from 'zod' ;
2+ import { z , ZodObject , ZodRawShape , ZodSchema , ZodTypeAny , ZodType } from 'zod' ;
113import { Simplify , zInfer } from './utils/type-utils' ;
124
135// Generic type for resolver functions
@@ -20,8 +12,6 @@ export type Resolver<
2012ctx :Simplify < TContext > ;
2113} ) => Promise < TOutputSchema extends ZodTypeAny ?zInfer < TOutputSchema > :void > ;
2214
23- // Define the builder interface capturing generic types for input and output
24-
2515export type ZodSchemaOrRawShape = ZodSchema < any > | ZodRawShape ;
2616export type InferZodSchemaOrRawShape < T extends ZodSchemaOrRawShape > =
2717T extends ZodRawShape ?ZodObject < T > :T ;
@@ -117,7 +107,7 @@ export function action<
117107resolver,
118108type :'mutation' ,
119109} ;
120- return createResolver ( newDef ) as unknown as Action <
110+ return createAction ( newDef ) as unknown as Action <
121111TResolver ,
122112TInputSchema ,
123113TOutputSchema ,
@@ -143,7 +133,7 @@ export function action<
143133resolver,
144134type :'query' ,
145135} ;
146- return createResolver ( newDef ) as unknown as Action <
136+ return createAction ( newDef ) as unknown as Action <
147137TResolver ,
148138TInputSchema ,
149139TOutputSchema ,
@@ -158,6 +148,7 @@ export function action<
158148...initialDef ,
159149} ) ;
160150}
151+
161152export interface ActionBuilder <
162153TInputSchema extends ZodTypeAny | undefined ,
163154TOutputSchema extends ZodTypeAny | undefined ,
@@ -194,17 +185,17 @@ export type Action<
194185TType extends 'mutation' | 'query' | undefined ,
195186TContext extends Record < string , any > | unknown = unknown ,
196187> = ActionDef < TResolver , TInputSchema , TOutputSchema , TType , TContext > & {
197- callerWithoutParser :(
188+ raw :(
198189ctx :TContext ,
199190input :TInputSchema extends ZodTypeAny ?zInfer < TInputSchema > :void
200- ) => ReturnType < TResolver > ;
201- } & {
202- (
203- ctx :TContext ,
191+ ) => Simplify < ReturnType < TResolver > > ;
192+ } & ( (
204193input :TInputSchema extends ZodTypeAny ?zInfer < TInputSchema > :void
205- ) :ReturnType < TResolver > ;
206- } ;
207- export function createResolver <
194+ ) => ReturnType < TResolver > ) ;
195+
196+ //
197+
198+ export function createAction <
208199TResolver extends Resolver < any , any , any > ,
209200TInputSchema extends ZodTypeAny | undefined ,
210201TOutputSchema extends ZodTypeAny | undefined ,
@@ -214,7 +205,7 @@ export function createResolver<
214205/**
215206 * Invokes the resolver with middleware logic
216207 */
217- const callerWithMiddleware = async (
208+ const invokeWithMiddleware = async (
218209ctx :TContext ,
219210input :TInputSchema extends ZodTypeAny ?zInfer < TInputSchema > :null ,
220211middlewares :Middleware < TContext , TContext > [ ] = def . middleware
@@ -240,31 +231,34 @@ export function createResolver<
240231} ;
241232
242233/**
243- *Calls the resolver function without parsing input/output
244- * Useful forcalling the resolver when theinput/output is already parsed
234+ *Invokes the resolver without parsing input/output
235+ * Useful forraw calls from thebackend
245236 */
246- const callerWithoutParser = async (
237+ const rawCall = async (
247238ctx :TContext ,
248239input :TInputSchema extends ZodTypeAny ?zInfer < TInputSchema > :null
249240) => {
250- return callerWithMiddleware ( ctx , input ) ;
241+ return invokeWithMiddleware ( ctx , input ) ;
251242} ;
252243
253244/**
254245 * Invokes the resolver with parsing input/output and middleware logic
255- * Useful for safecalling theresolver directly
246+ * Useful for safecalls from thefrontend
256247 */
257- const callerWithParser = async ( ctx : TContext , input :any ) => {
248+ const safeCall = async ( input :any ) => {
258249const maybeParsedInput = def . inputSchema
259250?def . inputSchema . parse ( input )
260251:input ;
261- const result = await callerWithMiddleware ( ctx , maybeParsedInput ) ;
252+ const result = await invokeWithMiddleware (
253+ undefined as any ,
254+ maybeParsedInput
255+ ) ;
262256
263257const maybeParsedOutput = def . outputSchema
264258?def . outputSchema . parse ( result )
265259:result ;
266260return maybeParsedOutput ;
267261} ;
268262
269- return Object . assign ( callerWithParser , def , { callerWithoutParser } ) ;
263+ return Object . assign ( safeCall , def , { raw : rawCall } ) ;
270264}