server-act 1.5.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -70,14 +70,15 @@ import { z } from "zod";
70
70
 
71
71
  export const sayHelloAction = serverAct
72
72
  .middleware(() => {
73
- const userId = "...";
74
- return { userId };
73
+ const t = i18n();
74
+ const userId = "..."
75
+ return { t, userId };
76
+ })
77
+ .input((ctx) => {
78
+ return z.object({
79
+ name: z.string().min(1, { message: ctx.t("form.name.required") }),
80
+ });
75
81
  })
76
- .input(
77
- z.object({
78
- name: z.string(),
79
- }),
80
- )
81
82
  .action(async ({ ctx, input }) => {
82
83
  console.log("User ID", ctx.userId);
83
84
  return `Hello, ${input.name}`;
@@ -112,7 +113,7 @@ export const sayHelloAction = serverAct
112
113
  )
113
114
  .formAction(async ({ formData, input, formErrors, ctx }) => {
114
115
  if (formErrors) {
115
- return { formData, formErrors: formErrors.formErrors.fieldErrors };
116
+ return { formData, formErrors: formErrors.fieldErrors };
116
117
  }
117
118
  return { message: `Hello, ${input.name}!` };
118
119
  });
@@ -126,7 +127,7 @@ import { useActionState } from "react";
126
127
  import { sayHelloAction } from "./action";
127
128
 
128
129
  export const ClientComponent = () => {
129
- const [state, dispatch] = useFormState(sayHelloAction, undefined);
130
+ const [state, dispatch] = useActionState(sayHelloAction, undefined);
130
131
 
131
132
  return (
132
133
  <form action={dispatch}>
package/dist/index.d.mts CHANGED
@@ -7,6 +7,7 @@ declare function getFormErrors(issues: ReadonlyArray<StandardSchemaV1.Issue>): {
7
7
 
8
8
  declare const unsetMarker: unique symbol;
9
9
  type UnsetMarker = typeof unsetMarker;
10
+ type RemoveUnsetMarker<T> = T extends UnsetMarker ? undefined : T;
10
11
  type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
11
12
  type Prettify<T> = {
12
13
  [P in keyof T]: T[P];
@@ -14,7 +15,7 @@ type Prettify<T> = {
14
15
  type SanitizeFunctionParam<T extends (param: any) => any> = T extends (param: infer P) => infer R ? Equals<P, undefined> extends true ? () => R : Equals<P, P | undefined> extends true ? (param?: P) => R : (param: P) => R : never;
15
16
  type InferParserType<T, TType extends "in" | "out"> = T extends StandardSchemaV1 ? TType extends "in" ? StandardSchemaV1.InferInput<T> : StandardSchemaV1.InferOutput<T> : never;
16
17
  type InferInputType<T, TType extends "in" | "out"> = T extends UnsetMarker ? undefined : InferParserType<T, TType>;
17
- type InferContextType<T> = T extends UnsetMarker ? undefined : T;
18
+ type InferContextType<T> = RemoveUnsetMarker<T>;
18
19
  interface ActionParams<TInput = unknown, TContext = unknown> {
19
20
  _input: TInput;
20
21
  _context: TContext;
@@ -46,9 +47,9 @@ interface ActionBuilder<TParams extends ActionParams> {
46
47
  /**
47
48
  * Create an action for React `useActionState`
48
49
  */
49
- formAction: <TState, TPrevState = undefined>(action: (params: Prettify<{
50
+ formAction: <TState, TPrevState = UnsetMarker>(action: (params: Prettify<{
50
51
  ctx: InferContextType<TParams["_context"]>;
51
- prevState: any;
52
+ prevState: RemoveUnsetMarker<TPrevState>;
52
53
  formData: FormData;
53
54
  } & ({
54
55
  input: InferInputType<TParams["_input"], "out">;
@@ -56,7 +57,7 @@ interface ActionBuilder<TParams extends ActionParams> {
56
57
  } | {
57
58
  input?: undefined;
58
59
  formErrors: ReturnType<typeof getFormErrors>;
59
- })>) => Promise<TState>) => (prevState: TState | TPrevState, formData: InferInputType<TParams["_input"], "in">) => Promise<TState | TPrevState>;
60
+ })>) => Promise<TState>) => (prevState: TState | RemoveUnsetMarker<TPrevState>, formData: InferInputType<TParams["_input"], "in">) => Promise<TState | RemoveUnsetMarker<TPrevState>>;
60
61
  }
61
62
  /**
62
63
  * Server action builder
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ declare function getFormErrors(issues: ReadonlyArray<StandardSchemaV1.Issue>): {
7
7
 
8
8
  declare const unsetMarker: unique symbol;
9
9
  type UnsetMarker = typeof unsetMarker;
10
+ type RemoveUnsetMarker<T> = T extends UnsetMarker ? undefined : T;
10
11
  type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
11
12
  type Prettify<T> = {
12
13
  [P in keyof T]: T[P];
@@ -14,7 +15,7 @@ type Prettify<T> = {
14
15
  type SanitizeFunctionParam<T extends (param: any) => any> = T extends (param: infer P) => infer R ? Equals<P, undefined> extends true ? () => R : Equals<P, P | undefined> extends true ? (param?: P) => R : (param: P) => R : never;
15
16
  type InferParserType<T, TType extends "in" | "out"> = T extends StandardSchemaV1 ? TType extends "in" ? StandardSchemaV1.InferInput<T> : StandardSchemaV1.InferOutput<T> : never;
16
17
  type InferInputType<T, TType extends "in" | "out"> = T extends UnsetMarker ? undefined : InferParserType<T, TType>;
17
- type InferContextType<T> = T extends UnsetMarker ? undefined : T;
18
+ type InferContextType<T> = RemoveUnsetMarker<T>;
18
19
  interface ActionParams<TInput = unknown, TContext = unknown> {
19
20
  _input: TInput;
20
21
  _context: TContext;
@@ -46,9 +47,9 @@ interface ActionBuilder<TParams extends ActionParams> {
46
47
  /**
47
48
  * Create an action for React `useActionState`
48
49
  */
49
- formAction: <TState, TPrevState = undefined>(action: (params: Prettify<{
50
+ formAction: <TState, TPrevState = UnsetMarker>(action: (params: Prettify<{
50
51
  ctx: InferContextType<TParams["_context"]>;
51
- prevState: any;
52
+ prevState: RemoveUnsetMarker<TPrevState>;
52
53
  formData: FormData;
53
54
  } & ({
54
55
  input: InferInputType<TParams["_input"], "out">;
@@ -56,7 +57,7 @@ interface ActionBuilder<TParams extends ActionParams> {
56
57
  } | {
57
58
  input?: undefined;
58
59
  formErrors: ReturnType<typeof getFormErrors>;
59
- })>) => Promise<TState>) => (prevState: TState | TPrevState, formData: InferInputType<TParams["_input"], "in">) => Promise<TState | TPrevState>;
60
+ })>) => Promise<TState>) => (prevState: TState | RemoveUnsetMarker<TPrevState>, formData: InferInputType<TParams["_input"], "in">) => Promise<TState | RemoveUnsetMarker<TPrevState>>;
60
61
  }
61
62
  /**
62
63
  * Server action builder
package/dist/index.js CHANGED
@@ -159,14 +159,16 @@ function createServerActionBuilder(initDef = {}) {
159
159
  if (result.issues) {
160
160
  return yield action({
161
161
  ctx,
162
- prevState,
162
+ // biome-ignore lint/suspicious/noExplicitAny: It's fine
163
+ prevState: prevState,
163
164
  formData,
164
165
  formErrors: getFormErrors(result.issues)
165
166
  });
166
167
  }
167
168
  return yield action({
168
169
  ctx,
169
- prevState,
170
+ // biome-ignore lint/suspicious/noExplicitAny: It's fine
171
+ prevState: prevState,
170
172
  formData,
171
173
  // biome-ignore lint/suspicious/noExplicitAny: It's fine
172
174
  input: result.value
@@ -174,7 +176,8 @@ function createServerActionBuilder(initDef = {}) {
174
176
  }
175
177
  return yield action({
176
178
  ctx,
177
- prevState,
179
+ // biome-ignore lint/suspicious/noExplicitAny: It's fine
180
+ prevState: prevState,
178
181
  formData,
179
182
  input: undefined
180
183
  });
package/dist/index.mjs CHANGED
@@ -157,14 +157,16 @@ function createServerActionBuilder(initDef = {}) {
157
157
  if (result.issues) {
158
158
  return yield action({
159
159
  ctx,
160
- prevState,
160
+ // biome-ignore lint/suspicious/noExplicitAny: It's fine
161
+ prevState: prevState,
161
162
  formData,
162
163
  formErrors: getFormErrors(result.issues)
163
164
  });
164
165
  }
165
166
  return yield action({
166
167
  ctx,
167
- prevState,
168
+ // biome-ignore lint/suspicious/noExplicitAny: It's fine
169
+ prevState: prevState,
168
170
  formData,
169
171
  // biome-ignore lint/suspicious/noExplicitAny: It's fine
170
172
  input: result.value
@@ -172,7 +174,8 @@ function createServerActionBuilder(initDef = {}) {
172
174
  }
173
175
  return yield action({
174
176
  ctx,
175
- prevState,
177
+ // biome-ignore lint/suspicious/noExplicitAny: It's fine
178
+ prevState: prevState,
176
179
  formData,
177
180
  input: undefined
178
181
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "server-act",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "homepage": "https://github.com/chungweileong94/server-act#readme",
5
5
  "author": "chungweileong94",
6
6
  "license": "MIT",
@@ -50,7 +50,7 @@
50
50
  "peerDependencies": {
51
51
  "typescript": ">=5.0.0",
52
52
  "valibot": "^1.0.0",
53
- "zod": "^3.24.0"
53
+ "zod": "^3.24.0 || ^4.0.0-beta.0"
54
54
  },
55
55
  "peerDependenciesMeta": {
56
56
  "typescript": {
@@ -68,7 +68,7 @@
68
68
  "typescript": "^5.6.3",
69
69
  "valibot": "1.0.0-rc.0",
70
70
  "zod": "^3.24.2",
71
- "zod-form-data": "^2.0.2"
71
+ "zod-form-data": "^2.0.6"
72
72
  },
73
73
  "scripts": {
74
74
  "build": "bunchee",