Skip to content

Commit

Permalink
Update Types
Browse files Browse the repository at this point in the history
  • Loading branch information
timoclsn committed Oct 7, 2023
1 parent 536c1d7 commit 44ef2ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
4 changes: 2 additions & 2 deletions apps/resources/lib/serverActions/client.ts
Original file line number Diff line number Diff line change
@@ -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<TResponse extends any> {
isIdle: boolean;
Expand Down Expand Up @@ -58,7 +58,7 @@ export const useAction = <
TInput extends TInputSchema | undefined,
TResponse extends any,
>(
inputAction: BrandedServerAction<TInputSchema, TInput, TResponse>,
inputAction: ServerAction<TInputSchema, TInput, TResponse>,
options: {
onRunAction?: (input: InferInputType<TInputSchema, TInput>) => void;
onSuccess?: (data: TResponse | null) => void;
Expand Down
46 changes: 13 additions & 33 deletions apps/resources/lib/serverActions/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,27 @@ interface Result<TResponse> {
error: string | null;
}

type ServerAction<
export type ServerAction<
TInputSchema extends z.ZodTypeAny,
TInput extends TInputSchema | undefined,
TResponse,
> = (input: InferInputType<TInputSchema, TInput>) => Promise<Result<TResponse>>;

declare const brand: unique symbol;
type Brand<T, TBrand extends string> = T & { [brand]: TBrand };

export type BrandedServerAction<
TInputSchema extends z.ZodTypeAny,
TInput extends TInputSchema | undefined,
TResponse extends any,
> = Brand<ServerAction<TInputSchema, TInput, TResponse>, 'ServerAction'>;

interface CreateActionClientOptions<Context> {
export const createActionClient = <Context>(createClientOpts?: {
middleware?: () => MaybePromise<Context>;
}

export interface ActionBuilderOptions<
TInputSchema extends z.ZodTypeAny,
TResponse extends any,
Context,
> {
input?: TInputSchema;
action: (args: {
input: z.input<TInputSchema>;
ctx: Context;
}) => MaybePromise<void> | MaybePromise<TResponse>;
}

export const createActionClient = <Context>(
createClientOpts?: CreateActionClientOptions<Context>,
) => {
}) => {
const actionBuilder = <
TInputSchema extends z.ZodTypeAny,
TInput extends TInputSchema | undefined,
TResponse extends any,
>(
actionBuilderOpts: ActionBuilderOptions<TInputSchema, TResponse, Context>,
) => {
const serverAction: ServerAction<TInputSchema, TInput, TResponse> = async (
>(actionBuilderOpts: {
input?: TInputSchema;
action: (args: {
input: z.input<TInputSchema>;
ctx: Context;
}) => MaybePromise<void> | MaybePromise<TResponse>;
}) => {
const createAction: ServerAction<TInputSchema, TInput, TResponse> = async (
input,
) => {
try {
Expand All @@ -76,6 +55,7 @@ export const createActionClient = <Context>(
input: parsedInput,
ctx,
});

return {
data: response ?? null,
error: null,
Expand All @@ -99,7 +79,7 @@ export const createActionClient = <Context>(
}
};

return serverAction as BrandedServerAction<TInputSchema, TInput, TResponse>;
return createAction;
};

return actionBuilder;
Expand Down

0 comments on commit 44ef2ee

Please sign in to comment.