Skip to content

Commit 8e19d94

Browse files
authoredJun 4, 2024··
refactor: improve internal code (#150)
Commits in this PR improve internal safe action client class code.
1 parent 54b0b52 commit 8e19d94

14 files changed

+424
-398
lines changed
 

‎packages/next-safe-action/src/action-builder.ts

+140-148
Large diffs are not rendered by default.

‎packages/next-safe-action/src/hooks.ts

+24-24
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const getActionStatus = <
2929
ServerError,
3030
S extends Schema | undefined,
3131
const BAS extends readonly Schema[],
32-
FVE,
33-
FBAVE,
32+
CVE,
33+
CBAVE,
3434
Data,
3535
>({
3636
isIdle,
@@ -39,7 +39,7 @@ const getActionStatus = <
3939
}: {
4040
isIdle: boolean;
4141
isExecuting: boolean;
42-
result: HookResult<ServerError, S, BAS, FVE, FBAVE, Data>;
42+
result: HookResult<ServerError, S, BAS, CVE, CBAVE, Data>;
4343
}): HookActionStatus => {
4444
if (isIdle) {
4545
return "idle";
@@ -70,19 +70,19 @@ const useActionCallbacks = <
7070
ServerError,
7171
S extends Schema | undefined,
7272
const BAS extends readonly Schema[],
73-
FVE,
74-
FBAVE,
73+
CVE,
74+
CBAVE,
7575
Data,
7676
>({
7777
result,
7878
input,
7979
status,
8080
cb,
8181
}: {
82-
result: HookResult<ServerError, S, BAS, FVE, FBAVE, Data>;
82+
result: HookResult<ServerError, S, BAS, CVE, CBAVE, Data>;
8383
input: S extends Schema ? InferIn<S> : undefined;
8484
status: HookActionStatus;
85-
cb?: HookCallbacks<ServerError, S, BAS, FVE, FBAVE, Data>;
85+
cb?: HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>;
8686
}) => {
8787
const onExecuteRef = React.useRef(cb?.onExecute);
8888
const onSuccessRef = React.useRef(cb?.onSuccess);
@@ -129,20 +129,20 @@ export const useAction = <
129129
ServerError,
130130
S extends Schema | undefined,
131131
const BAS extends readonly Schema[],
132-
FVE,
133-
FBAVE,
132+
CVE,
133+
CBAVE,
134134
Data,
135135
>(
136-
safeActionFn: HookSafeActionFn<ServerError, S, BAS, FVE, FBAVE, Data>,
137-
utils?: HookCallbacks<ServerError, S, BAS, FVE, FBAVE, Data>
136+
safeActionFn: HookSafeActionFn<ServerError, S, BAS, CVE, CBAVE, Data>,
137+
utils?: HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
138138
) => {
139139
const [, startTransition] = React.useTransition();
140-
const [result, setResult] = React.useState<HookResult<ServerError, S, BAS, FVE, FBAVE, Data>>(EMPTY_HOOK_RESULT);
140+
const [result, setResult] = React.useState<HookResult<ServerError, S, BAS, CVE, CBAVE, Data>>(EMPTY_HOOK_RESULT);
141141
const [clientInput, setClientInput] = React.useState<S extends Schema ? InferIn<S> : void>();
142142
const [isExecuting, setIsExecuting] = React.useState(false);
143143
const [isIdle, setIsIdle] = React.useState(true);
144144

145-
const status = getActionStatus<ServerError, S, BAS, FVE, FBAVE, Data>({ isExecuting, result, isIdle });
145+
const status = getActionStatus<ServerError, S, BAS, CVE, CBAVE, Data>({ isExecuting, result, isIdle });
146146

147147
const execute = React.useCallback(
148148
(input: S extends Schema ? InferIn<S> : void) => {
@@ -233,19 +233,19 @@ export const useOptimisticAction = <
233233
ServerError,
234234
S extends Schema | undefined,
235235
const BAS extends readonly Schema[],
236-
FVE,
237-
FBAVE,
236+
CVE,
237+
CBAVE,
238238
Data,
239239
State,
240240
>(
241-
safeActionFn: HookSafeActionFn<ServerError, S, BAS, FVE, FBAVE, Data>,
241+
safeActionFn: HookSafeActionFn<ServerError, S, BAS, CVE, CBAVE, Data>,
242242
utils: {
243243
currentState: State;
244244
updateFn: (state: State, input: S extends Schema ? InferIn<S> : undefined) => State;
245-
} & HookCallbacks<ServerError, S, BAS, FVE, FBAVE, Data>
245+
} & HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
246246
) => {
247247
const [, startTransition] = React.useTransition();
248-
const [result, setResult] = React.useState<HookResult<ServerError, S, BAS, FVE, FBAVE, Data>>(EMPTY_HOOK_RESULT);
248+
const [result, setResult] = React.useState<HookResult<ServerError, S, BAS, CVE, CBAVE, Data>>(EMPTY_HOOK_RESULT);
249249
const [clientInput, setClientInput] = React.useState<S extends Schema ? InferIn<S> : void>();
250250
const [isExecuting, setIsExecuting] = React.useState(false);
251251
const [isIdle, setIsIdle] = React.useState(true);
@@ -254,7 +254,7 @@ export const useOptimisticAction = <
254254
utils.updateFn
255255
);
256256

257-
const status = getActionStatus<ServerError, S, BAS, FVE, FBAVE, Data>({ isExecuting, result, isIdle });
257+
const status = getActionStatus<ServerError, S, BAS, CVE, CBAVE, Data>({ isExecuting, result, isIdle });
258258

259259
const execute = React.useCallback(
260260
(input: S extends Schema ? InferIn<S> : void) => {
@@ -353,15 +353,15 @@ export const useStateAction = <
353353
ServerError,
354354
S extends Schema | undefined,
355355
const BAS extends readonly Schema[],
356-
FVE,
357-
FBAVE,
356+
CVE,
357+
CBAVE,
358358
Data,
359359
>(
360-
safeActionFn: HookSafeStateActionFn<ServerError, S, BAS, FVE, FBAVE, Data>,
360+
safeActionFn: HookSafeStateActionFn<ServerError, S, BAS, CVE, CBAVE, Data>,
361361
utils?: {
362362
initResult?: Awaited<ReturnType<typeof safeActionFn>>;
363363
permalink?: string;
364-
} & HookCallbacks<ServerError, S, BAS, FVE, FBAVE, Data>
364+
} & HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
365365
) => {
366366
const [result, dispatcher, isExecuting] = React.useActionState(
367367
safeActionFn,
@@ -370,7 +370,7 @@ export const useStateAction = <
370370
);
371371
const [isIdle, setIsIdle] = React.useState(true);
372372
const [clientInput, setClientInput] = React.useState<S extends Schema ? InferIn<S> : void>();
373-
const status = getActionStatus<ServerError, S, BAS, FVE, FBAVE, Data>({ isExecuting, result, isIdle });
373+
const status = getActionStatus<ServerError, S, BAS, CVE, CBAVE, Data>({ isExecuting, result, isIdle });
374374

375375
const execute = React.useCallback(
376376
(input: S extends Schema ? InferIn<S> : void) => {

‎packages/next-safe-action/src/hooks.types.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export type HookResult<
1010
ServerError,
1111
S extends Schema | undefined,
1212
BAS extends readonly Schema[],
13-
FVE,
14-
FBAVE,
13+
CVE,
14+
CBAVE,
1515
Data,
16-
> = SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data> & {
16+
> = SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> & {
1717
fetchError?: string;
1818
};
1919

@@ -24,18 +24,18 @@ export type HookCallbacks<
2424
ServerError,
2525
S extends Schema | undefined,
2626
BAS extends readonly Schema[],
27-
FVE,
28-
FBAVE,
27+
CVE,
28+
CBAVE,
2929
Data,
3030
> = {
3131
onExecute?: (args: { input: S extends Schema ? InferIn<S> : undefined }) => MaybePromise<void>;
3232
onSuccess?: (args: { data: Data; input: S extends Schema ? InferIn<S> : undefined }) => MaybePromise<void>;
3333
onError?: (args: {
34-
error: Omit<HookResult<ServerError, S, BAS, FVE, FBAVE, Data>, "data">;
34+
error: Omit<HookResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">;
3535
input: S extends Schema ? InferIn<S> : undefined;
3636
}) => MaybePromise<void>;
3737
onSettled?: (args: {
38-
result: HookResult<ServerError, S, BAS, FVE, FBAVE, Data>;
38+
result: HookResult<ServerError, S, BAS, CVE, CBAVE, Data>;
3939
input: S extends Schema ? InferIn<S> : undefined;
4040
}) => MaybePromise<void>;
4141
};
@@ -48,12 +48,12 @@ export type HookSafeActionFn<
4848
ServerError,
4949
S extends Schema | undefined,
5050
BAS extends readonly Schema[],
51-
FVE,
52-
FBAVE,
51+
CVE,
52+
CBAVE,
5353
Data,
5454
> = (
5555
input: S extends Schema ? InferIn<S> : undefined
56-
) => Promise<SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data>>;
56+
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
5757

5858
/**
5959
* Type of the stateful safe action function passed to hooks. Same as `SafeStateActionFn` except it accepts
@@ -63,13 +63,13 @@ export type HookSafeStateActionFn<
6363
ServerError,
6464
S extends Schema | undefined,
6565
BAS extends readonly Schema[],
66-
FVE,
67-
FBAVE,
66+
CVE,
67+
CBAVE,
6868
Data,
6969
> = (
70-
prevResult: SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data>,
70+
prevResult: SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>,
7171
input: S extends Schema ? InferIn<S> : undefined
72-
) => Promise<SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data>>;
72+
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
7373

7474
/**
7575
* Type of the action status returned by `useAction` and `useOptimisticAction` hooks.

‎packages/next-safe-action/src/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type { Infer, Schema } from "@typeschema/main";
22
import type { SafeActionClientOpts } from "./index.types";
33
import { SafeActionClient } from "./safe-action-client";
44
import { DEFAULT_SERVER_ERROR_MESSAGE } from "./utils";
5+
import { formatBindArgsValidationErrors, formatValidationErrors } from "./validation-errors";
56

6-
export { DEFAULT_SERVER_ERROR_MESSAGE } from "./utils";
7+
export { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE } from "./utils";
78
export { flattenBindArgsValidationErrors, flattenValidationErrors, returnValidationErrors } from "./validation-errors";
89

910
export type * from "./index.types";
@@ -36,14 +37,17 @@ export const createSafeActionClient = <ServerError = string, MetadataSchema exte
3637
SafeActionClientOpts<ServerError, MetadataSchema>["handleReturnedServerError"]
3738
>;
3839

39-
return new SafeActionClient<
40-
ServerError,
41-
undefined,
42-
MetadataSchema extends Schema ? Infer<MetadataSchema> : undefined
43-
>({
40+
return new SafeActionClient({
4441
middlewareFns: [async ({ next }) => next({ ctx: undefined })],
4542
handleServerErrorLog,
4643
handleReturnedServerError,
4744
validationStrategy: "zod",
45+
schema: undefined,
46+
bindArgsSchemas: [],
47+
ctxType: undefined,
48+
metadataSchema: createOpts?.defineMetadataSchema?.(),
49+
metadata: undefined as MetadataSchema extends Schema ? Infer<MetadataSchema> : undefined,
50+
formatValidationErrorsFn: formatValidationErrors,
51+
formatBindArgsValidationErrorsFn: formatBindArgsValidationErrors,
4852
});
4953
};

‎packages/next-safe-action/src/index.types.ts

+19-27
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ export type SafeActionResult<
1818
ServerError,
1919
S extends Schema | undefined,
2020
BAS extends readonly Schema[],
21-
FVE = ValidationErrors<S>,
22-
FBAVE = BindArgsValidationErrors<BAS>,
21+
CVE = ValidationErrors<S>,
22+
CBAVE = BindArgsValidationErrors<BAS>,
2323
Data = unknown,
2424
// eslint-disable-next-line
2525
NextCtx = unknown,
2626
> = {
2727
data?: Data;
2828
serverError?: ServerError;
29-
validationErrors?: FVE;
30-
bindArgsValidationErrors?: FBAVE;
29+
validationErrors?: CVE;
30+
bindArgsValidationErrors?: CBAVE;
3131
};
3232

3333
/**
3434
* Type of the function called from components with type safe input data.
3535
*/
36-
export type SafeActionFn<ServerError, S extends Schema | undefined, BAS extends readonly Schema[], FVE, FBAVE, Data> = (
36+
export type SafeActionFn<ServerError, S extends Schema | undefined, BAS extends readonly Schema[], CVE, CBAVE, Data> = (
3737
...clientInputs: [...bindArgsInputs: InferInArray<BAS>, input: S extends Schema ? InferIn<S> : void]
38-
) => Promise<SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data>>;
38+
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
3939

4040
/**
4141
* Type of the stateful function called from components with type safe input data.
@@ -44,30 +44,22 @@ export type SafeStateActionFn<
4444
ServerError,
4545
S extends Schema | undefined,
4646
BAS extends readonly Schema[],
47-
FVE,
48-
FBAVE,
47+
CVE,
48+
CBAVE,
4949
Data,
5050
> = (
5151
...clientInputs: [
5252
...bindArgsInputs: InferInArray<BAS>,
53-
prevResult: Prettify<SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data>>,
53+
prevResult: Prettify<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>,
5454
input: S extends Schema ? InferIn<S> : void,
5555
]
56-
) => Promise<SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data>>;
56+
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
5757

5858
/**
5959
* Type of the result of a middleware function. It extends the result of a safe action with
6060
* information about the action execution.
6161
*/
62-
export type MiddlewareResult<ServerError, NextCtx> = SafeActionResult<
63-
ServerError,
64-
any,
65-
any,
66-
unknown,
67-
unknown,
68-
unknown,
69-
NextCtx
70-
> & {
62+
export type MiddlewareResult<ServerError, NextCtx> = SafeActionResult<ServerError, any, any, any, any, any, NextCtx> & {
7163
parsedInput?: unknown;
7264
bindArgsParsedInputs?: unknown[];
7365
ctx?: unknown;
@@ -77,12 +69,12 @@ export type MiddlewareResult<ServerError, NextCtx> = SafeActionResult<
7769
/**
7870
* Type of the middleware function passed to a safe action client.
7971
*/
80-
export type MiddlewareFn<ServerError, Ctx, NextCtx, MD> = {
72+
export type MiddlewareFn<ServerError, MD, Ctx, NextCtx> = {
8173
(opts: {
8274
clientInput: unknown;
8375
bindArgsClientInputs: unknown[];
8476
ctx: Ctx;
85-
metadata: MD | undefined;
77+
metadata: MD;
8678
next: {
8779
<NC>(opts: { ctx: NC }): Promise<MiddlewareResult<ServerError, NC>>;
8880
};
@@ -92,7 +84,7 @@ export type MiddlewareFn<ServerError, Ctx, NextCtx, MD> = {
9284
/**
9385
* Type of the function that executes server code when defining a new safe action.
9486
*/
95-
export type ServerCodeFn<S extends Schema | undefined, BAS extends readonly Schema[], Ctx, MD, Data> = (args: {
87+
export type ServerCodeFn<MD, Ctx, S extends Schema | undefined, BAS extends readonly Schema[], Data> = (args: {
9688
parsedInput: S extends Schema ? Infer<S> : undefined;
9789
bindArgsParsedInputs: InferArray<BAS>;
9890
ctx: Ctx;
@@ -104,12 +96,12 @@ export type ServerCodeFn<S extends Schema | undefined, BAS extends readonly Sche
10496
*/
10597
export type StateServerCodeFn<
10698
ServerError,
99+
MD,
100+
Ctx,
107101
S extends Schema | undefined,
108102
BAS extends readonly Schema[],
109-
FVE,
110-
FBAVE,
111-
Ctx,
112-
MD,
103+
CVE,
104+
CBAVE,
113105
Data,
114106
> = (
115107
args: {
@@ -118,5 +110,5 @@ export type StateServerCodeFn<
118110
ctx: Ctx;
119111
metadata: MD;
120112
},
121-
utils: { prevResult: Prettify<SafeActionResult<ServerError, S, BAS, FVE, FBAVE, Data>> }
113+
utils: { prevResult: Prettify<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>> }
122114
) => Promise<Data>;

0 commit comments

Comments
 (0)
Please sign in to comment.