diff --git a/apps/resources/lib/serverActions/client.ts b/apps/resources/lib/serverActions/client.ts index db66e1f8..c104664e 100644 --- a/apps/resources/lib/serverActions/client.ts +++ b/apps/resources/lib/serverActions/client.ts @@ -1,7 +1,7 @@ import { useCallback, useReducer, useTransition } from 'react'; import { z } from 'zod'; import { getErrorMessage } from '../utils'; -import { BrandedServerAction, InferInputType } from './server'; +import { InferInputType, ServerAction } from './server'; interface State { isIdle: boolean; @@ -58,7 +58,7 @@ export const useAction = < TInput extends TInputSchema | undefined, TResponse extends any, >( - inputAction: BrandedServerAction, + inputAction: ServerAction, options: { onRunAction?: (input: InferInputType) => void; onSuccess?: (data: TResponse | null) => void; diff --git a/apps/resources/lib/serverActions/server.ts b/apps/resources/lib/serverActions/server.ts index 5db12a92..46b62e62 100644 --- a/apps/resources/lib/serverActions/server.ts +++ b/apps/resources/lib/serverActions/server.ts @@ -13,48 +13,27 @@ interface Result { error: string | null; } -type ServerAction< +export type ServerAction< TInputSchema extends z.ZodTypeAny, TInput extends TInputSchema | undefined, TResponse, > = (input: InferInputType) => Promise>; -declare const brand: unique symbol; -type Brand = T & { [brand]: TBrand }; - -export type BrandedServerAction< - TInputSchema extends z.ZodTypeAny, - TInput extends TInputSchema | undefined, - TResponse extends any, -> = Brand, 'ServerAction'>; - -interface CreateActionClientOptions { +export const createActionClient = (createClientOpts?: { middleware?: () => MaybePromise; -} - -export interface ActionBuilderOptions< - TInputSchema extends z.ZodTypeAny, - TResponse extends any, - Context, -> { - input?: TInputSchema; - action: (args: { - input: z.input; - ctx: Context; - }) => MaybePromise | MaybePromise; -} - -export const createActionClient = ( - createClientOpts?: CreateActionClientOptions, -) => { +}) => { const actionBuilder = < TInputSchema extends z.ZodTypeAny, TInput extends TInputSchema | undefined, TResponse extends any, - >( - actionBuilderOpts: ActionBuilderOptions, - ) => { - const serverAction: ServerAction = async ( + >(actionBuilderOpts: { + input?: TInputSchema; + action: (args: { + input: z.input; + ctx: Context; + }) => MaybePromise | MaybePromise; + }) => { + const createAction: ServerAction = async ( input, ) => { try { @@ -76,6 +55,7 @@ export const createActionClient = ( input: parsedInput, ctx, }); + return { data: response ?? null, error: null, @@ -99,7 +79,7 @@ export const createActionClient = ( } }; - return serverAction as BrandedServerAction; + return createAction; }; return actionBuilder;